-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
serhiy-storchaka
merged 5 commits into
python:main
from
serhiy-storchaka:zipimport-namespace
Jul 4, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1522e26
gh-121111: zipimport: fix support of namespaces containing only subpa…
serhiy-storchaka 1b645b7
Fix test_importlib.
serhiy-storchaka 0824fea
Rewrite tests.
serhiy-storchaka 07a3594
Add a NEWS entry and refactor.
serhiy-storchaka a7488b6
Merge branch 'main' into zipimport-namespace
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"") | ||
|
||
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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the |
||
def testZipImporterMethods(self): | ||
packdir = TESTPACK + os.sep | ||
packdir2 = packdir + TESTPACK2 + os.sep | ||
|
@@ -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(): | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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(): | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.