Skip to content

Commit 87aa83f

Browse files
Add a test case with multiple zip imports
1 parent dd160ab commit 87aa83f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Lib/test/test_zipimport.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,43 @@ def testInvalidateCaches(self):
546546
self.assertIsNone(zipimport._zip_directory_cache.get(zi.archive))
547547
self.assertIsNone(zi.find_spec("name_does_not_matter"))
548548

549+
def testInvalidateCachesWithMultipleZipimports(self):
550+
packdir = TESTPACK + os.sep
551+
packdir2 = packdir + TESTPACK2 + os.sep
552+
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
553+
packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
554+
packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc),
555+
"spam" + pyc_ext: (NOW, test_pyc)}
556+
self.addCleanup(os_helper.unlink, TEMP_ZIP)
557+
with ZipFile(TEMP_ZIP, "w") as z:
558+
for name, (mtime, data) in files.items():
559+
zinfo = ZipInfo(name, time.localtime(mtime))
560+
zinfo.compress_type = self.compression
561+
zinfo.comment = b"spam"
562+
z.writestr(zinfo, data)
563+
564+
zi = zipimport.zipimporter(TEMP_ZIP)
565+
self.assertEqual(zi._get_files().keys(), files.keys())
566+
# Zipimport the same path
567+
zi2 = zipimport.zipimporter(TEMP_ZIP)
568+
self.assertEqual(zi2._get_files().keys(), files.keys())
569+
# Add a new file to the ZIP archive
570+
newfile = {"spam2" + pyc_ext: (NOW, test_pyc)}
571+
files.update(newfile)
572+
with ZipFile(TEMP_ZIP, "a") as z:
573+
for name, (mtime, data) in newfile.items():
574+
zinfo = ZipInfo(name, time.localtime(mtime))
575+
zinfo.compress_type = self.compression
576+
zinfo.comment = b"spam"
577+
z.writestr(zinfo, data)
578+
# Invalidate the cache of the first zipimporter
579+
zi.invalidate_caches()
580+
# Check that the second zipimporter detects the new file
581+
self.assertEqual(zi2._get_files().keys(), files.keys())
582+
spec = zi2.find_spec('spam2')
583+
self.assertIsNotNone(spec)
584+
self.assertIsInstance(spec.loader, zipimport.zipimporter)
585+
549586
def testZipImporterMethodsInSubDirectory(self):
550587
packdir = TESTPACK + os.sep
551588
packdir2 = packdir + TESTPACK2 + os.sep

0 commit comments

Comments
 (0)