mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
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 443ca731d6
)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
58277de8e6
commit
f9585e2adc
2 changed files with 8 additions and 2 deletions
|
@ -80,7 +80,10 @@ def from_package(package):
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@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'
|
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
|
||||||
# blocks due to the need to close the temporary file to work on Windows
|
# blocks due to the need to close the temporary file to work on Windows
|
||||||
# properly.
|
# properly.
|
||||||
|
@ -92,7 +95,7 @@ def _tempfile(reader, suffix=''):
|
||||||
yield pathlib.Path(raw_path)
|
yield pathlib.Path(raw_path)
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
os.remove(raw_path)
|
_os_remove(raw_path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue