8000 gh-103200: Fix performance issues with `zipimport.invalidate_caches()` by desmondcheongzx · Pull Request #103208 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103200: Fix performance issues with zipimport.invalidate_caches() #103208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Prev Previous commit
Next Next commit
Remove separate key for invalidating cache
  • Loading branch information
desmondcheongzx committed Jul 4, 2023
commit 27edee8a2997a8c9d5935e5a68a36712774f4656
18 changes: 8 additions & 10 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def __init__(self, path):

if path not in _zip_directory_cache:
_zip_directory_cache[path] = _read_directory(path)
self._cache_is_valid = True
self.archive = path
# a prefix directory following the ZIP file path.
self.prefix = _bootstrap_external._path_join(*prefix[::-1])
Expand Down Expand Up @@ -267,24 +266,23 @@ def get_resource_reader(self, fullname):

def _get_files(self):
"""Return the files within the archive path."""
if not self._cache_is_valid:
try:
_zip_directory_cache[self.archive] = _read_directory(self.archive)
except ZipImportError:
_zip_directory_cache.pop(self.archive, None)
self._cache_is_valid = True

try:
files = _zip_directory_cache[self.archive]
except KeyError:
files = {}
try:
files = _zip_directory_cache[self.archive] = _read_directory(self.archive)
except ZipImportError:
files = _zip_directory_cache.pop(self.archive, {})

return files


def invalidate_caches(self):
"""Invalidates the cache of file data of the archive path."""
self._cache_is_valid = False
try:
del _zip_directory_cache[self.archive]
except KeyError:
pass


def __repr__(self):
Expand Down
0