Title: Zipimport fails to consume contents() of an imported zip file containing just .pyc files · Issue #96785 · python/cpython · GitHub
Open Graph Title: Zipimport fails to consume contents() of an imported zip file containing just .pyc files · Issue #96785 · python/cpython
X Title: Zipimport fails to consume contents() of an imported zip file containing just .pyc files · Issue #96785 · python/cpython
Description: Zipimport fails to consume contents() of an imported zip file containing just .pyc files Bug report This code: from zipimport import zipimporter def main(): importer = zipimporter("module.zip") # zip file containing only __init__.pyc and...
Open Graph Description: Zipimport fails to consume contents() of an imported zip file containing just .pyc files Bug report This code: from zipimport import zipimporter def main(): importer = zipimporter("module.zip") # z...
X Description: Zipimport fails to consume contents() of an imported zip file containing just .pyc files Bug report This code: from zipimport import zipimporter def main(): importer = zipimporter("module.zip&...
Opengraph URL: https://github.com/python/cpython/issues/96785
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Zipimport fails to consume contents() of an imported zip file containing just .pyc files ","articleBody":"# Zipimport fails to consume contents() of an imported zip file containing just .pyc files \r\n**Bug report**\r\n\r\nThis code:\r\n\r\n```python\r\nfrom zipimport import zipimporter\r\n\r\ndef main():\r\n importer = zipimporter(\"module.zip\") # zip file containing only __init__.pyc and oops.pyc\r\n reader = importer.get_resource_reader(\"oops.oops\") # \u003czipimport._ZipImportResourceReader\u003e\r\n\r\n modules = reader.contents() # generator containing the names of the imported pyc files\r\n for module in modules:\r\n # some other logic to do with the module\r\n print(module) # raises the AssertionError when consumed\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\nRaises an ``AssertionError`` exception, when the generator of the contents from zipped modules containing just ``.pyc`` files is consumed.\r\n\r\nThe exception is raised here in the ``_ZipImportResourceReader`` class on line [#775](https://github.com/python/cpython/blob/ce5e6f098f8a270e50b989baa75765584573706b/Lib/zipimport.py#L775) in ``zipimport.py`` due to an assert on ``__init__.py``, forgetting that ``.pyc`` or ``.pyo`` files can also be a valid candidates to import.\r\n\r\nSnippet in question:\r\n\r\n```python\r\n775: assert relative_path.name == '__init__.py'\r\n776: package_path = relative_path.parent\r\n778: subdirs_seen = set()\r\n```\r\n\r\nThis is the exact traceback raised using python 3.9:\r\n\r\n```python\r\n$ python zimport.py\r\nTraceback (most recent call last):\r\nFile \"/Users/user/zimport.py\", line 15, in \u003cmodule\u003e\r\n main()\r\nFile \"/Users/user/zimport.py\", line 10, in main\r\n for module in modules:\r\nFile \"\u003cfrozen zipimport\u003e\", line 775, in contents\r\nAssertionError\r\n```\r\n\r\nChanging the assert statement on line [#775](https://github.com/python/cpython/blob/ce5e6f098f8a270e50b989baa75765584573706b/Lib/zipimport.py#L775) to the following resolves the issue.\r\n\r\n```python\r\n775: assert relative_path.name in ('__init__.py', '__init__.pyc', '__init__.pyo')\r\n```\r\n\r\n**Your environment**\r\n\r\nTested this on on the following Python docker images:\r\n* python:3.10 (fixed here)\r\n* python:3.9\r\n* python:3.8\r\n* python:3.7\r\n\r\nAnd used the following zip file to reproduce the issue at hand:\r\n\r\n Path = module.zip\r\n Type = zip\r\n Physical Size = 781\r\n\r\n Date Time Attr Size Compressed Name\r\n ------------------- ----- ------------ ------------ ------------------------\r\n 2022-09-07 13:59:23 D.... 0 0 oops\r\n 2022-09-07 13:58:26 ..... 102 92 oops/__init__.pyc\r\n 2022-09-07 13:58:21 ..... 478 261 oops/oops.pyc\r\n ------------------- ----- ------------ ------------ ------------------------\r\n 2022-09-07 13:59:23 580 353 2 files, 1 folders\r\n\r\nI also tested the same code on the Python 3.8 and 3.7 Docker images. Though, the issue seems to be different and depending on the version used. I did not have time to pin-point the exact issue *(yet)*. But have included the tracebacks for good measure.\r\n\r\n**Traceback using 3.7:**\r\n\r\n```python\r\n$ python zimport.py\r\nTraceback (most recent call last):\r\nFile \"zimport.py\", line 19, in \u003cmodule\u003e\r\n main()\r\nFile \"zimport.py\", line 10, in main\r\n reader = importer.get_resource_reader(\"oops.oops\")\r\nFile \"/usr/local/lib/python3.7/importlib/resources.py\", line 312, in contents\r\n fullname_path = Path(self.zipimporter.get_filename(self.fullname))\r\nzipimport.ZipImportError: can't find module 'oops.oops'\r\n```\r\n\r\n**Traceback using 3.8:**\r\n\r\n```python\r\n$ python zimport.py\r\nTraceback (most recent call last):\r\nFile \"zimport.py\", line 19, in \u003cmodule\u003e\r\n main()\r\nFile \"zimport.py\", line 10, in main\r\n reader = importer.get_resource_reader(\"oops.oops\")\r\nFile \"\u003cfrozen zipimport\u003e\", line 771, in contents\r\nFile \"\u003cfrozen zipimport\u003e\", line 191, in get_filename\r\nFile \"\u003cfrozen zipimport\u003e\", line 721, in _get_module_code\r\nzipimport.ZipImportError: can't find module 'oops.oops'\r\n```\r\n","author":{"url":"https://github.com/Horofic","@type":"Person","name":"Horofic"},"datePublished":"2022-09-13T07:44:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/96785/cpython/issues/96785"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0eaa5566-9270-3401-955e-67f479388e09 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86E4:73D9:8BD0E4:CD33C6:6A4E19A1 |
| html-safe-nonce | a62f2453e1f756597ab957c8c24e4d3d9d3ebf7f052b0ec0a79eab760d3491b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkU0OjczRDk6OEJEMEU0OkNEMzNDNjo2QTRFMTlBMSIsInZpc2l0b3JfaWQiOiI4NjgxNjczMzI5OTk0NTA0NjA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 55ca2880b95cb13c9340d1a0ebeb628aa7e0ed2c3e3994da995ccccd6618cdd4 |
| hovercard-subject-tag | issue:1371007048 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/96785/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c6426e0048c98c3543526061eaafcbf9099e4ed796ba31ab9968baa1a7013a60/python/cpython/issues/96785 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c6426e0048c98c3543526061eaafcbf9099e4ed796ba31ab9968baa1a7013a60/python/cpython/issues/96785 |
| og:image:alt | Zipimport fails to consume contents() of an imported zip file containing just .pyc files Bug report This code: from zipimport import zipimporter def main(): importer = zipimporter("module.zip") # z... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Horofic |
| hostname | github.com |
| expected-hostname | github.com |
| None | 030096ee0db095447bfe77409d33bfac127ca7128299c58deef27c52eaa1b1f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ea9187571e3edc5f2780f750631138669441ca50 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width