@@ -93,7 +93,7 @@ def __init__(self, path):
93
93
except KeyError :
94
94
files = _read_directory (path )
95
95
_zip_directory_cache [path ] = files
96
- self ._files = files
96
+ self ._cache_is_valid = True
97
97
self .archive = path
98
98
# a prefix directory following the ZIP file path.
99
99
self .prefix = _bootstrap_external ._path_join (* prefix [::- 1 ])
@@ -152,7 +152,7 @@ def get_data(self, pathname):
152
152
key = pathname [len (self .archive + path_sep ):]
153
153
154
154
try :
155
- toc_entry = self ._files [key ]
155
+ toc_entry = self ._get_files () [key ]
156
156
except KeyError :
157
157
raise OSError (0 , '' , key )
158
158
return _get_data (self .archive , toc_entry )
@@ -189,7 +189,7 @@ def get_source(self, fullname):
189
189
fullpath = f'{ path } .py'
190
190
191
191
try :
192
- toc_entry = self ._files [fullpath ]
192
+ toc_entry = self ._get_files () [fullpath ]
193
193
except KeyError :
194
194
# we have the module, but no source
195
195
return None
@@ -268,14 +268,26 @@ def get_resource_reader(self, fullname):
268
268
return ZipReader (self , fullname )
269
269
270
270
271
- def invalidate_caches (self ):
272
- """Reload the file data of the archive path."""
271
+ def _get_files (self ):
272
+ """Return the files within the archive path."""
273
+ if not self ._cache_is_valid :
274
+ try :
275
+ _zip_directory_cache [self .archive ] = _read_directory (self .archive )
276
+ except ZipImportError :
277
+ _zip_directory_cache .pop (self .archive , None )
278
+ self ._cache_is_valid = True
279
+
273
280
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 = {}
281
+ files = _zip_directory_cache [self .archive ]
282
+ except KeyError :
283
+ files = {}
284
+
285
+ return files
286
+
287
+
288
+ def invalidate_caches (self ):
289
+ """Invalidates the cache of file data of the archive path."""
290
+ self ._cache_is_valid = False
279
291
280
292
281
293
def __repr__ (self ):
@@ -305,15 +317,15 @@ def _is_dir(self, path):
305
317
# of a namespace package. We test by seeing if the name, with an
306
318
# appended path separator, exists.
307
319
dirpath = path + path_sep
308
- # If dirpath is present in self._files , we have a directory.
309
- return dirpath in self ._files
320
+ # If dirpath is present in self._get_files() , we have a directory.
321
+ return dirpath in self ._get_files ()
310
322
311
323
# Return some information about a module.
312
324
def _get_module_info (self , fullname ):
313
325
path = _get_module_path (self , fullname )
314
326
for suffix , isbytecode , ispackage in _zip_searchorder :
315
327
fullpath = path + suffix
316
- if fullpath in self ._files :
328
+ if fullpath in self ._get_files () :
317
329
return ispackage
318
330
return None
319
331
@@ -656,7 +668,7 @@ def _get_mtime_and_size_of_source(self, path):
656
668
# strip 'c' or 'o' from *.py[co]
657
669
assert path [- 1 :] in ('c' , 'o' )
658
670
path = path [:- 1 ]
659
- toc_entry = self ._files [path ]
671
+ toc_entry = self ._get_files () [path ]
660
672
# fetch the time stamp of the .py file for comparison
661
673
# with an embedded pyc time stamp
662
674
time = toc_entry [5 ]
@@ -676,7 +688,7 @@ def _get_pyc_source(self, path):
676
688
path = path [:- 1 ]
677
689
678
690
try :
679
- toc_entry = self ._files [path ]
691
+ toc_entry = self ._get_files () [path ]
680
692
except KeyError :
681
693
return None
682
694
else :
@@ -692,7 +704,7 @@ def _get_module_code(self, fullname):
692
704
fullpath = path + suffix
693
705
_bootstrap ._verbose_message ('trying {}{}{}' , self .archive , path_sep , fullpath , verbosity = 2 )
694
706
try :
695
- toc_entry = self ._files [fullpath ]
707
+ toc_entry = self ._get_files () [fullpath ]
696
708
except KeyError :
697
709
pass
698
710
else :
0 commit comments