Skip to content

Commit

Permalink
Fix resource leak in WheelFile.open() (#338)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jdufresne committed Mar 24, 2020
1 parent 251a093 commit ed3ec04
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeqa-test.yml
Expand Up @@ -50,4 +50,4 @@ jobs:
- name: Install test dependencies
run: pip install .[test]
- name: Test with pytest
run: pytest
run: python -b -m pytest -W always
8 changes: 4 additions & 4 deletions src/wheel/wheelfile.py
Expand Up @@ -90,13 +90,13 @@ def _update_crc(newdata, eof=None):
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
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -9,7 +9,7 @@ minversion = 3.3.0
skip_missing_interpreters = true

[testenv]
commands = pytest {posargs}
commands = {envpython} -b -m pytest -W always {posargs}
extras = test

[testenv:flake8]
Expand Down

0 comments on commit ed3ec04

Please sign in to comment.