Commit 3e6ecb09 authored by Jurko Gospodnetić's avatar Jurko Gospodnetić

clear cached zip archive directory data when removing it from cache

This is an extra safety measure to avoid someone holding a reference to this
cached data and using its content even after we know that the underlying zip
archive has been removed and possibly even replaced.

Change suggested by PJ Eby (pje on BitBucket) in a setuptools pull request #51
comment:
  https://bitbucket.org/pypa/setuptools/pull-request/51/diff#comment-2018183

--HG--
extra : rebase_source : 6de2309bc7446647749cfe78ab00e0230a07f92f
parent 9101bdd5
...@@ -1646,16 +1646,21 @@ def update_dist_caches(dist_path, fix_zipimporter_caches): ...@@ -1646,16 +1646,21 @@ def update_dist_caches(dist_path, fix_zipimporter_caches):
if fix_zipimporter_caches: if fix_zipimporter_caches:
_replace_zip_directory_cache_data(normalized_path) _replace_zip_directory_cache_data(normalized_path)
else: else:
# Clear the relevant zipimport._zip_directory_cache data. This will not # Here, even though we do not want to fix existing and now stale
# remove related zipimport.zipimporter instances but should at least # zipimporter cache information, we still want to remove it. Related to
# not leave the old zip archive directory data behind to be reused by # Python's zip archive directory information cache, we clear each of
# some newly created zipimport.zipimporter loaders. This whole stale # its stale entries in two phases:
# data removal step does not seem strictly necessary, but has been left # 1. Clear the entry so attempting to access zip archive information
# in because it was done before we started replacing the zip archive # via any existing stale zipimport.zipimporter instances fails.
# directory information cache content if possible, and there are no # 2. Remove the entry from the cache so any newly constructed
# relevant unit tests that we can depend on to tell us if this is # zipimport.zipimporter instances do not end up using old stale
# really needed. # zip archive directory information.
_uncache(normalized_path, zipimport._zip_directory_cache) # This whole stale data removal step does not seem strictly necessary,
# but has been left in because it was done before we started replacing
# the zip archive directory information cache content if possible, and
# there are no relevant unit tests that we can depend on to tell us if
# this is really needed.
_remove_and_clear_zip_directory_cache_data(normalized_path)
def _collect_zipimporter_cache_entries(normalized_path, cache): def _collect_zipimporter_cache_entries(normalized_path, cache):
""" """
...@@ -1723,6 +1728,13 @@ def _replace_zip_directory_cache_data(normalized_path): ...@@ -1723,6 +1728,13 @@ def _replace_zip_directory_cache_data(normalized_path):
zipimport._zip_directory_cache, zipimport._zip_directory_cache,
updater=replace_cached_zip_archive_directory_data) updater=replace_cached_zip_archive_directory_data)
def _remove_and_clear_zip_directory_cache_data(normalized_path):
def clear_and_remove_cached_zip_archive_directory_data(path, old_entry):
old_entry.clear()
_update_zipimporter_cache(normalized_path,
zipimport._zip_directory_cache,
updater=clear_and_remove_cached_zip_archive_directory_data)
def is_python(text, filename='<string>'): def is_python(text, filename='<string>'):
"Is this string a valid Python script?" "Is this string a valid Python script?"
try: try:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment