Skip to content

Commit 443ca73

Browse files
authored
gh-93353: Fix importlib.resources._tempfile() finalizer (#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 3ceb4b8 commit 443ca73

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Lib/importlib/resources/_common.py

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the :func:`importlib.resources.as_file` context manager to remove the
2+
temporary file if destroyed late during Python finalization: keep a local
3+
reference to the :func:`os.remove` function. Patch by Victor Stinner.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy