Skip to content
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

gh-59110: zipimport: support namespace packages when no directory entry exists #121233

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 6 additions & 6 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,10 @@ def testInvalidateCaches(self):
z.writestr(zinfo, data)

zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi._get_files()), sorted([*files, *extra_files]))
# Check that the file information remains accurate after reloading
zi.invalidate_caches()
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi._get_files()), sorted([*files, *extra_files]))
# Add a new file to the ZIP archive
newfile = {"spam2" + pyc_ext: (NOW, test_pyc)}
files.update(newfile)
Expand All @@ -620,7 +620,7 @@ def testInvalidateCaches(self):
z.writestr(zinfo, data)
# Check that we can detect the new file after invalidating the cache
zi.invalidate_caches()
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi._get_files()), sorted([*files, *extra_files]))
spec = zi.find_spec('spam2')
self.assertIsNotNone(spec)
self.assertIsInstance(spec.loader, zipimport.zipimporter)
Expand Down Expand Up @@ -648,10 +648,10 @@ def testInvalidateCachesWithMultipleZipimports(self):
z.writestr(zinfo, data)

zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi._get_files()), sorted([*files, *extra_files]))
# Zipimporter for the same path.
zi2 = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(list(zi2._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi2._get_files()), sorted([*files, *extra_files]))
# Add a new file to the ZIP archive to make the cache wrong.
newfile = {"spam2" + pyc_ext: (NOW, test_pyc)}
files.update(newfile)
Expand All @@ -664,7 +664,7 @@ def testInvalidateCachesWithMultipleZipimports(self):
# Invalidate the cache of the first zipimporter.
zi.invalidate_caches()
# Check that the second zipimporter detects the new file and isn't using a stale cache.
self.assertEqual(list(zi2._get_files()), [*files, *extra_files])
self.assertEqual(sorted(zi2._get_files()), sorted([*files, *extra_files]))
spec = zi2.find_spec('spam2')
self.assertIsNotNone(spec)
self.assertIsInstance(spec.loader, zipimport.zipimporter)
Expand Down
10 changes: 9 additions & 1 deletion Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,20 @@ def _read_directory(archive):
finally:
fp.seek(start_offset)
_bootstrap._verbose_message('zipimport: found {} names in {!r}', count, archive)

# Add implicit directories.
for name in list(files):
while (i := name.rstrip(path_sep).rfind(path_sep)) >= 0:
while True:
i = name.rstrip(path_sep).rfind(path_sep)
if i < 0:
break
name = name[:i + 1]
if name in files:
break
files[name] = None
count += 1
_bootstrap._verbose_message('zipimport: added {} implicit directories in {!r}',
count, archive)
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
return files

# During bootstrap, we may need to load the encodings
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`zipimport` supports now namespace packages when no directory entry
exists.