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
38 changes: 32 additions & 6 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,30 @@ def testNamespacePackage(self):
mod = importlib.import_module('.'.join((subpkg, TESTMOD + '3')))
self.assertEqual('path1.zip', mod.__file__.split(os.sep)[-4])

def testImportSubmodulesInZip(self):
with ZipFile(TEMP_ZIP, "w") as z:
z.writestr("a/__init__.py", b'')
z.writestr("a/b/c/__init__.py", b'def foo(): return "foo"')

importer = zipimport.zipimporter(TEMP_ZIP + "/a/")
spec = importer.find_spec("a.b")
self.assertEqual(spec.loader, None)
self.assertEqual(spec.submodule_search_locations,
[(TEMP_ZIP + "/a/b").replace("/", zipimport.path_sep)])
self.assertRaises(zipimport.ZipImportError, importer.get_code, "a.b")
self.assertEqual(importer.get_data("a/b/"), b"")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is a necessary test case for get_data, maybe directory path will be never passed to it.


sys.path.insert(0, TEMP_ZIP)
try:
import a.b.c
self.assertIn('namespace', str(a.b).lower())
self.assertEqual(a.b.c.foo(), "foo")
finally:
del sys.path[0]
sys.modules.pop('a.b.c', None)
sys.modules.pop('a.b', None)
sys.modules.pop('a', None)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the ImportHooksBaseTestCase has setUp and tearDown to restore the environ, so the finally section could be removed.

def testZipImporterMethods(self):
packdir = TESTPACK + os.sep
packdir2 = packdir + TESTPACK2 + os.sep
Expand Down Expand Up @@ -520,6 +544,7 @@ def testInvalidateCaches(self):
packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc),
"spam" + pyc_ext: (NOW, test_pyc)}
extra_files = [packdir, packdir2]
self.addCleanup(os_helper.unlink, TEMP_ZIP)
with ZipFile(TEMP_ZIP, "w") as z:
for name, (mtime, data) in files.items():
Expand All @@ -529,10 +554,10 @@ def testInvalidateCaches(self):
z.writestr(zinfo, data)

zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(zi._get_files().keys(), files.keys())
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
# Check that the file information remains accurate after reloading
zi.invalidate_caches()
self.assertEqual(zi._get_files().keys(), files.keys())
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
# Add a new file to the ZIP archive
newfile = {"spam2" + pyc_ext: (NOW, test_pyc)}
files.update(newfile)
Expand All @@ -544,7 +569,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(zi._get_files().keys(), files.keys())
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
spec = zi.find_spec('spam2')
self.assertIsNotNone(spec)
self.assertIsInstance(spec.loader, zipimport.zipimporter)
Expand All @@ -562,6 +587,7 @@ def testInvalidateCachesWithMultipleZipimports(self):
packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc),
"spam" + pyc_ext: (NOW, test_pyc)}
extra_files = [packdir, packdir2]
self.addCleanup(os_helper.unlink, TEMP_ZIP)
with ZipFile(TEMP_ZIP, "w") as z:
for name, (mtime, data) in files.items():
Expand All @@ -571,10 +597,10 @@ def testInvalidateCachesWithMultipleZipimports(self):
z.writestr(zinfo, data)

zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(zi._get_files().keys(), files.keys())
self.assertEqual(list(zi._get_files()), [*files, *extra_files])
# Zipimporter for the same path.
zi2 = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(zi2._get_files().keys(), files.keys())
self.assertEqual(list(zi2._get_files()), [*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 @@ -587,7 +613,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(zi2._get_files().keys(), files.keys())
self.assertEqual(list(zi2._get_files()), [*files, *extra_files])
spec = zi2.find_spec('spam2')
self.assertIsNotNone(spec)
self.assertIsInstance(spec.loader, zipimport.zipimporter)
Expand Down
8 changes: 8 additions & 0 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def get_data(self, pathname):
toc_entry = self._get_files()[key]
except KeyError:
raise OSError(0, '', key)
if toc_entry is None:
return b''
return _get_data(self.archive, toc_entry)


Expand Down Expand Up @@ -554,6 +556,12 @@ def _read_directory(archive):
finally:
fp.seek(start_offset)
_bootstrap._verbose_message('zipimport: found {} names in {!r}', count, archive)
for name in list(files):
while (i := name.rstrip(path_sep).rfind(path_sep)) >= 0:
name = name[:i + 1]
if name in files:
break
files[name] = None
return files

# During bootstrap, we may need to load the encodings
Expand Down
Loading