@@ -88,12 +88,8 @@ def __init__(self, path):
8888 raise ZipImportError ('not a Zip file' , path = path )
8989 break
9090
91- try :
92- files = _zip_directory_cache [path ]
93- except KeyError :
94- files = _read_directory (path )
95- _zip_directory_cache [path ] = files
96- self ._files = files
91+ if path not in _zip_directory_cache :
92+ _zip_directory_cache [path ] = _read_directory (path )
9793 self .archive = path
9894 # a prefix directory following the ZIP file path.
9995 self .prefix = _bootstrap_external ._path_join (* prefix [::- 1 ])
@@ -152,7 +148,7 @@ def get_data(self, pathname):
152148 key = pathname [len (self .archive + path_sep ):]
153149
154150 try :
155- toc_entry = self ._files [key ]
151+ toc_entry = self ._get_files () [key ]
156152 except KeyError :
157153 raise OSError (0 , '' , key )
158154 return _get_data (self .archive , toc_entry )
@@ -189,7 +185,7 @@ def get_source(self, fullname):
189185 fullpath = f'{ path } .py'
190186
191187 try :
192- toc_entry = self ._files [fullpath ]
188+ toc_entry = self ._get_files () [fullpath ]
193189 except KeyError :
194190 # we have the module, but no source
195191 return None
@@ -268,14 +264,22 @@ def get_resource_reader(self, fullname):
268264 return ZipReader (self , fullname )
269265
270266
271- def invalidate_caches (self ):
272- """Reload the file data of the archive path."""
267+ def _get_files (self ):
268+ """Return the files within the archive path."""
273269 try :
274- self ._files = _read_directory (self .archive )
275- _zip_directory_cache [self .archive ] = self ._files
276- except ZipImportError :
277- _zip_directory_cache .pop (self .archive , None )
278- self ._files = {}
270+ files = _zip_directory_cache [self .archive ]
271+ except KeyError :
272+ try :
273+ files = _zip_directory_cache [self .archive ] = _read_directory (self .archive )
274+ except ZipImportError :
275+ files = {}
276+
277+ return files
278+
279+
280+ def invalidate_caches (self ):
281+ """Invalidates the cache of file data of the archive path."""
282+ _zip_directory_cache .pop (self .archive , None )
279283
280284
281285 def __repr__ (self ):
@@ -305,15 +309,15 @@ def _is_dir(self, path):
305309 # of a namespace package. We test by seeing if the name, with an
306310 # appended path separator, exists.
307311 dirpath = path + path_sep
308- # If dirpath is present in self._files , we have a directory.
309- return dirpath in self ._files
312+ # If dirpath is present in self._get_files() , we have a directory.
313+ return dirpath in self ._get_files ()
310314
311315# Return some information about a module.
312316def _get_module_info (self , fullname ):
313317 path = _get_module_path (self , fullname )
314318 for suffix , isbytecode , ispackage in _zip_searchorder :
315319 fullpath = path + suffix
316- if fullpath in self ._files :
320+ if fullpath in self ._get_files () :
317321 return ispackage
318322 return None
319323
@@ -656,7 +660,7 @@ def _get_mtime_and_size_of_source(self, path):
656660 # strip 'c' or 'o' from *.py[co]
657661 assert path [- 1 :] in ('c' , 'o' )
658662 path = path [:- 1 ]
659- toc_entry = self ._files [path ]
663+ toc_entry = self ._get_files () [path ]
660664 # fetch the time stamp of the .py file for comparison
661665 # with an embedded pyc time stamp
662666 time = toc_entry [5 ]
@@ -676,7 +680,7 @@ def _get_pyc_source(self, path):
676680 path = path [:- 1 ]
677681
678682 try :
679- toc_entry = self ._files [path ]
683+ toc_entry = self ._get_files () [path ]
680684 except KeyError :
681685 return None
682686 else :
@@ -692,7 +696,7 @@ def _get_module_code(self, fullname):
692696 fullpath = path + suffix
693697 _bootstrap ._verbose_message ('trying {}{}{}' , self .archive , path_sep , fullpath , verbosity = 2 )
694698 try :
695- toc_entry = self ._files [fullpath ]
699+ toc_entry = self ._get_files () [fullpath ]
696700 except KeyError :
697701 pass
698702 else :
0 commit comments