diff --git a/CHANGES.rst b/CHANGES.rst index c2a6e9cc..71f8f403 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +v5.2.2 +====== + +* #234: Fix refleak in ``as_file`` caught by CPython tests. + v5.2.1 ====== diff --git a/importlib_resources/_common.py b/importlib_resources/_common.py index 9bb6bda5..25511672 100644 --- a/importlib_resources/_common.py +++ b/importlib_resources/_common.py @@ -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