Skip to content

Commit 209b703

Browse files
authored
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)
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).
1 parent f481338 commit 209b703

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Lib/test/test_zipimport.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,9 @@ def testInvalidateCaches(self):
548548
# Check that the cached data is removed if the file is deleted
549549
os.remove(TEMP_ZIP)
550550
zi.invalidate_caches()
551-
self.assertIsNone(zi._files)
551+
self.assertFalse(zi._files)
552552
self.assertIsNone(zipimport._zip_directory_cache.get(zi.archive))
553+
self.assertIsNone(zi.find_spec("name_does_not_matter"))
553554

554555
def testZipImporterMethodsInSubDirectory(self):
555556
packdir = TESTPACK + os.sep

Lib/zipimport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def invalidate_caches(self):
334334
_zip_directory_cache[self.archive] = self._files
335335
except ZipImportError:
336336
_zip_directory_cache.pop(self.archive, None)
337-
self._files = None
337+
self._files = {}
338338

339339

340340
def __repr__(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Have zipimport.zipimporter.find_spec() not raise an exception when the underlying zip
2+
file has been deleted and the internal cache has been reset via
3+
invalidate_cache().

0 commit comments

Comments
 (0)