@@ -88,12 +88,8 @@ def __init__(self, path):
88
88
raise ZipImportError ('not a Zip file' , path = path )
89
89
break
90
90
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 )
97
93
self .archive = path
98
94
# a prefix directory following the ZIP file path.
99
95
self .prefix = _bootstrap_external ._path_join (* prefix [::- 1 ])
@@ -152,7 +148,7 @@ def get_data(self, pathname):
152
148
key = pathname [len (self .archive + path_sep ):]
153
149
154
150
try :
155
- toc_entry = self ._files [key ]
151
+ toc_entry = self ._get_files () [key ]
156
152
except KeyError :
157
153
raise OSError (0 , '' , key )
158
154
return _get_data (self .archive , toc_entry )
@@ -189,7 +185,7 @@ def get_source(self, fullname):
189
185
fullpath = f'{ path } .py'
190
186
191
187
try :
192
- toc_entry = self ._files [fullpath ]
188
+ toc_entry = self ._get_files () [fullpath ]
193
189
except KeyError :
194
190
# we have the module, but no source
195
191
return None
@@ -268,14 +264,22 @@ def get_resource_reader(self, fullname):
268
264
return ZipReader (self , fullname )
269
265
270
266
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."""
273
269
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 )
279
283
280
284
281
285
def __repr__ (self ):
@@ -305,15 +309,15 @@ def _is_dir(self, path):
305
309
# of a namespace package. We test by seeing if the name, with an
306
310
# appended path separator, exists.
307
311
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 ()
310
314
311
315
# Return some information about a module.
312
316
def _get_module_info (self , fullname ):
313
317
path = _get_module_path (self , fullname )
314
318
for suffix , isbytecode , ispackage in _zip_searchorder :
315
319
fullpath = path + suffix
316
- if fullpath in self ._files :
320
+ if fullpath in self ._get_files () :
317
321
return ispackage
318
322
return None
319
323
@@ -656,7 +660,7 @@ def _get_mtime_and_size_of_source(self, path):
656
660
# strip 'c' or 'o' from *.py[co]
657
661
assert path [- 1 :] in ('c' , 'o' )
658
662
path = path [:- 1 ]
659
- toc_entry = self ._files [path ]
663
+ toc_entry = self ._get_files () [path ]
660
664
# fetch the time stamp of the .py file for comparison
661
665
# with an embedded pyc time stamp
662
666
time = toc_entry [5 ]
@@ -676,7 +680,7 @@ def _get_pyc_source(self, path):
676
680
path = path [:- 1 ]
677
681
678
682
try :
679
- toc_entry = self ._files [path ]
683
+ toc_entry = self ._get_files () [path ]
680
684
except KeyError :
681
685
return None
682
686
else :
@@ -692,7 +696,7 @@ def _get_module_code(self, fullname):
692
696
fullpath = path + suffix
693
697
_bootstrap ._verbose_message ('trying {}{}{}' , self .archive , path_sep , fullpath , verbosity = 2 )
694
698
try :
695
- toc_entry = self ._files [fullpath ]
699
+ toc_entry = self ._get_files () [fullpath ]
696
700
except KeyError :
697
701
pass
698
702
else :
0 commit comments