[Python-checkins] [3.10] bpo-45183: don't raise an exception when calling zipimport.zipimporter.find_spec() when the zip file is missing and the internal cache has been reset (GH-28435) (#28438)

brettcannon webhook-mailer at python.org
Fri Sep 17 20:46:30 EDT 2021


https://github.com/python/cpython/commit/e1bdecb6dc7ac33256d5fa875d45634512d2a90e
commit: e1bdecb6dc7ac33256d5fa875d45634512d2a90e
branch: 3.10
author: Brett Cannon <brett at python.org>
committer: brettcannon <brett at python.org>
date: 2021-09-17T17:46:22-07:00
summary:

[3.10] bpo-45183: don't raise an exception when calling zipimport.zipimporter.find_spec() when the zip file is missing and the internal cache has been reset (GH-28435) (#28438)

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).
(cherry picked from commit 209b7035f714dcc41df054b0b023e0b955d7e1a2)

Co-authored-by: Brett Cannon <brett at python.org>

files:
A Misc/NEWS.d/next/Library/2021-09-17-15-58-53.bpo-45183.Vv_vch.rst
M Lib/test/test_zipimport.py
M Lib/zipimport.py
M Python/importlib_zipimport.h

diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index f6f5ca4adce45..8acfeef2745fc 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -547,8 +547,9 @@ def testInvalidateCaches(self):
         # 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
diff --git a/Lib/zipimport.py b/Lib/zipimport.py
index c55fec6aa1c47..25eaee9c0f291 100644
--- a/Lib/zipimport.py
+++ b/Lib/zipimport.py
@@ -334,7 +334,7 @@ def invalidate_caches(self):
             _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
index 0000000000000..f3194b34318f4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-09-17-15-58-53.bpo-45183.Vv_vch.rst
@@ -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().
diff --git a/Python/importlib_zipimport.h b/Python/importlib_zipimport.h
index af0171133e721..bb870c3a0353a 100644
--- a/Python/importlib_zipimport.h
+++ b/Python/importlib_zipimport.h
@@ -548,7 +548,7 @@ const unsigned char _Py_M__zipimport[] = {
     0,106,1,131,1,124,0,95,2,124,0,106,2,116,3,124,
     0,106,1,60,0,87,0,100,1,83,0,4,0,116,4,121,
     35,1,0,1,0,1,0,116,3,160,5,124,0,106,1,100,
-    1,161,2,1,0,100,1,124,0,95,2,89,0,100,1,83,
+    1,161,2,1,0,105,0,124,0,95,2,89,0,100,1,83,
     0,119,0,41,2,122,41,82,101,108,111,97,100,32,116,104,
     101,32,102,105,108,101,32,100,97,116,97,32,111,102,32,116,
     104,101,32,97,114,99,104,105,118,101,32,112,97,116,104,46,



More information about the Python-checkins mailing list