René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0eaa5566-9270-3401-955e-67f479388e09
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id86E4:73D9:8BD0E4:CD33C6:6A4E19A1
html-safe-noncea62f2453e1f756597ab957c8c24e4d3d9d3ebf7f052b0ec0a79eab760d3491b8
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkU0OjczRDk6OEJEMEU0OkNEMzNDNjo2QTRFMTlBMSIsInZpc2l0b3JfaWQiOiI4NjgxNjczMzI5OTk0NTA0NjA5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac55ca2880b95cb13c9340d1a0ebeb628aa7e0ed2c3e3994da995ccccd6618cdd4
hovercard-subject-tagissue:1371007048
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/96785/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c6426e0048c98c3543526061eaafcbf9099e4ed796ba31ab9968baa1a7013a60/python/cpython/issues/96785
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c6426e0048c98c3543526061eaafcbf9099e4ed796ba31ab9968baa1a7013a60/python/cpython/issues/96785
og:image:altZipimport 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameHorofic
hostnamegithub.com
expected-hostnamegithub.com
None030096ee0db095447bfe77409d33bfac127ca7128299c58deef27c52eaa1b1f0
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseea9187571e3edc5f2780f750631138669441ca50
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/96785#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F96785
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F96785
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/96785
Reloadhttps://github.com/python/cpython/issues/96785
Reloadhttps://github.com/python/cpython/issues/96785
Please reload this pagehttps://github.com/python/cpython/issues/96785
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/96785
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 34.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.6k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Zipimport fails to consume contents() of an imported zip file containing just .pyc files https://github.com/python/cpython/issues/96785#top
3.7 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.7%20(EOL)%22
3.8 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.8%20(EOL)%22
3.9 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.9%20(EOL)%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/Horofic
Horofichttps://github.com/Horofic
on Sep 13, 2022https://github.com/python/cpython/issues/96785#issue-1371007048
#775https://github.com/python/cpython/blob/ce5e6f098f8a270e50b989baa75765584573706b/Lib/zipimport.py#L775
#775https://github.com/python/cpython/blob/ce5e6f098f8a270e50b989baa75765584573706b/Lib/zipimport.py#L775
3.7 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.7%20(EOL)%22
3.8 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.8%20(EOL)%22
3.9 (EOL)end of lifehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.9%20(EOL)%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.