Skip to content

Commit

Permalink
Merge pull request #234 from python/bugfix/as_file-refleak
Browse files Browse the repository at this point in the history
Avoid file descriptor refleaks in as_file.
  • Loading branch information
jaraco authored Jul 30, 2021
2 parents 54a61c4 + 778172d commit 64484b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v5.2.2
======

* #234: Fix refleak in ``as_file`` caught by CPython tests.

v5.2.1
======

Expand Down
8 changes: 5 additions & 3 deletions importlib_resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ def _tempfile(reader, suffix=''):
# properly.
fd, raw_path = tempfile.mkstemp(suffix=suffix)
try:
os.write(fd, reader())
os.close(fd)
try:
os.write(fd, reader())
finally:
os.close(fd)
del reader
yield pathlib.Path(raw_path)
finally:
try:
os.remove(raw_path)
except (FileNotFoundError, PermissionError):
except FileNotFoundError:
pass


Expand Down

0 comments on commit 64484b7

Please sign in to comment.