8000 gh-121342: Fixed `pkgutil.iter_zipimport_modules` on an invalidated cache (#121342) by beachmachine · Pull Request #121705 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121342: Fixed pkgutil.iter_zipimport_modules on an invalidated cache (#121342) #121705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
8000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Lib/pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,15 @@ def _iter_file_finder_modules(importer, prefix=''):
from zipimport import zipimporter

def iter_zipimport_modules(importer, prefix=''):
dirlist = sorted(zipimport._zip_directory_cache[importer.archive])
"""Iterate through the modules available within a zipfile

For each module found in the zipfile, a tuple containing the module's
name and boolean indicating whether the module is a package
gets yielded.

It is assumed that importer is an instance of zipimport.zipimporter.
"""
dirlist = sorted(zipimporter(importer.archive)._get_files())
_prefix = importer.prefix
plen = len(_prefix)
yielded = {}
Expand Down
45 changes: 45 additions & 0 deletions Lib/test/test_pkgutil.py
< 8000 td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,51 @@ def test_mixed_namespace(self):
del sys.modules['foo.bar']
del sys.modules['foo.baz']

def test_iter_zipimport_modules(self):
with zipfile.ZipFile(
tempfile.NamedTemporaryFile(suffix='.zip', delete=False),
mode='w'
) as tmp_file_zip:
tmp_file_zip.writestr(
'foo.py',
'print("foot")'
)
tmp_file_zip.writestr(
'bar/__init__.py',
'print("bar")'
)

module_zip = pkgutil.zipimporter(tmp_file_zip.filename)

self.assertIn(('foo', False), pkgutil.iter_zipimport_modules(module_zip, prefix=''))
self.assertIn(('bar', True), pkgutil.iter_zipimport_modules(module_zip, prefix=''))

# Cleanup
os.remove(tmp_file_zip.filename)

def test_iter_zipimport_modules_invalidate_caches(self):
with zipfile.ZipFile(
tempfile.NamedTemporaryFile(suffix='.zip', delete=False),
mode='w'
) as tmp_file_zip:
tmp_file_zip.writestr(
'foo.py',
'print("foo")'
)
tmp_file_zip.writestr(
'bar/__init__.py',
'print("bar")'
)

module_zip = pkgutil.zipimporter(tmp_file_zip.filename)
module_zip.invalidate_caches()

self.assertIn(('foo', False), pkgutil.iter_zipimport_modules(module_zip, prefix=''))
self.assertIn(('bar', True), pkgutil.iter_zipimport_modules(module_zip, prefix=''))

# Cleanup
os.remove(tmp_file_zip.filename)

# XXX: test .pkg files


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``pkgutil.iter_zipimport_modules`` when called on an invalidated cache.
Patch by Andreas Stocker.
Loading
0