From b5fd3461a900abd51c4912ca86e81a31318286c2 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 30 Jul 2021 16:49:13 -0400 Subject: [PATCH 1/2] Avoid file descriptor refleaks in as_file. --- importlib_resources/_common.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 From 778172d18cdb9a971b7d0418f0d668045588fd60 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 30 Jul 2021 16:55:07 -0400 Subject: [PATCH 2/2] Update changelog. --- CHANGES.rst | 5 +++++ 1 file changed, 5 insertions(+) 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 ======