summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2020-03-24 06:41:21 -0700
committerGitHub <noreply@github.com>2020-03-24 15:41:21 +0200
commited3ec04a085593dd0d07820f9bce836d2c46d01e (patch)
treeb21b91b2233132377f5604eefa6db4c43a16a49d /src
parent251a0939a0e09352ae930f480cadbabfc103d240 (diff)
downloadwheel-git-ed3ec04a085593dd0d07820f9bce836d2c46d01e.tar.gz
Fix resource leak in WheelFile.open() (#338)
In WheelFile.open(), if the hash does not exist, avoid opening the file before raising the exception. Previously would leak the open file resource which could result in a ResourceWarning when Python warnings are enabled: ResourceWarning: unclosed file <_io.FileIO name='…/test_testzip_missing_hash0/test-1.0-py2.py3-none-any.whl' mode='rb' closefd=True> Python warnings are now enabled during tests to help catch these earlier.
Diffstat (limited to 'src')
-rw-r--r--src/wheel/wheelfile.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/wheel/wheelfile.py b/src/wheel/wheelfile.py
index acc5dab..3ee97dd 100644
--- a/src/wheel/wheelfile.py
+++ b/src/wheel/wheelfile.py
@@ -90,13 +90,13 @@ class WheelFile(ZipFile):
if eof and running_hash.digest() != expected_hash:
raise WheelError("Hash mismatch for file '{}'".format(native(ef_name)))
- ef = ZipFile.open(self, name_or_info, mode, pwd)
ef_name = as_unicode(name_or_info.filename if isinstance(name_or_info, ZipInfo)
else name_or_info)
- if mode == 'r' and not ef_name.endswith('/'):
- if ef_name not in self._file_hashes:
- raise WheelError("No hash found for file '{}'".format(native(ef_name)))
+ if mode == 'r' and not ef_name.endswith('/') and ef_name not in self._file_hashes:
+ raise WheelError("No hash found for file '{}'".format(native(ef_name)))
+ ef = ZipFile.open(self, name_or_info, mode, pwd)
+ if mode == 'r' and not ef_name.endswith('/'):
algorithm, expected_hash = self._file_hashes[ef_name]
if expected_hash is not None:
# Monkey patch the _update_crc method to also check for the hash from RECORD