Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-93353: Fix importlib.resources._tempfile() finalizer #93377

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/importlib/resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.