Skip to content

Commit 4025963

Browse files
vstinnerjaraco
authored andcommitted
pythongh-93353: Fix importlib.resources._tempfile() finalizer (python#93377)
Fix the importlib.resources.as_file() context manager to remove the temporary file if destroyed late during Python finalization: keep a local reference to the os.remove() function. Patch by Victor Stinner.
1 parent acfc5c9 commit 4025963

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

importlib_resources/_common.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ def from_package(package):
6767

6868

6969
@contextlib.contextmanager
70-
def _tempfile(reader, suffix=''):
70+
def _tempfile(reader, suffix='',
71+
# gh-93353: Keep a reference to call os.remove() in late Python
72+
# finalization.
73+
*, _os_remove=os.remove):
7174
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
7275
# blocks due to the need to close the temporary file to work on Windows
7376
# properly.
@@ -81,7 +84,7 @@ def _tempfile(reader, suffix=''):
8184
yield pathlib.Path(raw_path)
8285
finally:
8386
try:
84-
os.remove(raw_path)
87+
_os_remove(raw_path)
8588
except FileNotFoundError:
8689
pass
8790

0 commit comments

Comments
 (0)