diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-06-13 17:49:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 17:49:48 (GMT) |
commit | dc6dd8ee873ed96027d30c8a2e96518657d46241 (patch) | |
tree | 8f5be32449584b2de02020bbb82cb9cbdf50c146 /Lib/importlib | |
parent | 3d1c080591910c9c1ee80e5465ce7c91b67c907d (diff) | |
download | cpython-dc6dd8ee873ed96027d30c8a2e96518657d46241.zip cpython-dc6dd8ee873ed96027d30c8a2e96518657d46241.tar.gz cpython-dc6dd8ee873ed96027d30c8a2e96518657d46241.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>
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/resources/_common.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/Lib/importlib/resources/_common.py b/Lib/importlib/resources/_common.py index 147ea19..ca1fa8a 100644 --- a/Lib/importlib/resources/_common.py +++ b/Lib/importlib/resources/_common.py @@ -67,7 +67,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. @@ -81,7 +84,7 @@ def _tempfile(reader, suffix=''): yield pathlib.Path(raw_path) finally: try: - os.remove(raw_path) + _os_remove(raw_path) except FileNotFoundError: pass |