diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-06-13 17:52:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 17:52:50 (GMT) |
commit | f9585e2adc36fb2886f96ef86a5aa14215fc1151 (patch) | |
tree | c9cf13b54d8a4f44b03015893c911d6382225024 | |
parent | 58277de8e651df287ceae053eeb321c0f8406a1b (diff) | |
download | cpython-f9585e2adc36fb2886f96ef86a5aa14215fc1151.zip cpython-f9585e2adc36fb2886f96ef86a5aa14215fc1151.tar.gz cpython-f9585e2adc36fb2886f96ef86a5aa14215fc1151.tar.bz2 |
gh-93353: Fix importlib.resources._tempfile() finalizer (GH-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.
(cherry picked from commit 443ca731d6b1267fe2f92985e0490460c95e44a8)
Co-authored-by: Victor Stinner <vstinner@python.org>
-rw-r--r-- | Lib/importlib/_common.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-05-31-14-58-40.gh-issue-93353.9Hvm6o.rst | 3 |
2 files changed, 8 insertions, 2 deletions
diff --git a/Lib/importlib/_common.py b/Lib/importlib/_common.py index 549fee3..84144c0 100644 --- a/Lib/importlib/_common.py +++ b/Lib/importlib/_common.py @@ -80,7 +80,10 @@ def from_package(package): @contextlib.contextmanager -def _tempfile(reader, suffix=''): +def _tempfile(reader, suffix='', + # gh-93353: Keep a reference to call os.remove() in late Python + # finalization. + *, _os_remove=os.remove): # Not using tempfile.NamedTemporaryFile as it leads to deeper 'try' # blocks due to the need to close the temporary file to work on Windows # properly. @@ -92,7 +95,7 @@ def _tempfile(reader, suffix=''): yield pathlib.Path(raw_path) finally: try: - os.remove(raw_path) + _os_remove(raw_path) except FileNotFoundError: pass diff --git a/Misc/NEWS.d/next/Library/2022-05-31-14-58-40.gh-issue-93353.9Hvm6o.rst b/Misc/NEWS.d/next/Library/2022-05-31-14-58-40.gh-issue-93353.9Hvm6o.rst new file mode 100644 index 0000000..67be3c6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-31-14-58-40.gh-issue-93353.9Hvm6o.rst @@ -0,0 +1,3 @@ +Fix the :func:`importlib.resources.as_file` context manager to remove the +temporary file if destroyed late during Python finalization: keep a local +reference to the :func:`os.remove` function. Patch by Victor Stinner. |