]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45183: don't raise an exception when calling zipimport.zipimporter.find_spec...
authorBrett Cannon <brett@python.org>
Fri, 17 Sep 2021 23:48:17 +0000 (16:48 -0700)
committerGitHub <noreply@github.com>
Fri, 17 Sep 2021 23:48:17 +0000 (16:48 -0700)
This can occur when the zip file gets deleted, you call zipimport.zipimporter.invalidate_cache(), and then try to use zipimport.zipimporter.find_spec() (i.e. you left the zip file path on sys.path).

Lib/test/test_zipimport.py
Lib/zipimport.py
Misc/NEWS.d/next/Library/2021-09-17-15-58-53.bpo-45183.Vv_vch.rst [new file with mode: 0644]

index 938674fcbd385b407343ed0d08a139b7e39e5530..486200af70391801d756f529af9c87c337345f12 100644 (file)
@@ -548,8 +548,9 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
         # Check that the cached data is removed if the file is deleted
         os.remove(TEMP_ZIP)
         zi.invalidate_caches()
-        self.assertIsNone(zi._files)
+        self.assertFalse(zi._files)
         self.assertIsNone(zipimport._zip_directory_cache.get(zi.archive))
+        self.assertIsNone(zi.find_spec("name_does_not_matter"))
 
     def testZipImporterMethodsInSubDirectory(self):
         packdir = TESTPACK + os.sep
index c55fec6aa1c47da4d4ae65b251e95f049a7d578c..25eaee9c0f291b6d7ff65b511295f2020d5f38cc 100644 (file)
@@ -334,7 +334,7 @@ class zipimporter(_bootstrap_external._LoaderBasics):
             _zip_directory_cache[self.archive] = self._files
         except ZipImportError:
             _zip_directory_cache.pop(self.archive, None)
-            self._files = None
+            self._files = {}
 
 
     def __repr__(self):
diff --git a/Misc/NEWS.d/next/Library/2021-09-17-15-58-53.bpo-45183.Vv_vch.rst b/Misc/NEWS.d/next/Library/2021-09-17-15-58-53.bpo-45183.Vv_vch.rst
new file mode 100644 (file)
index 0000000..f3194b3
--- /dev/null
@@ -0,0 +1,3 @@
+Have zipimport.zipimporter.find_spec() not raise an exception when the underlying zip
+file has been deleted and the internal cache has been reset via
+invalidate_cache().