-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from contextlib import contextmanager | ||
from tempfile import NamedTemporaryFile | ||
|
||
|
||
@contextmanager | ||
def closeable_temporary_file(**kwargs): | ||
""" | ||
Context manager wrapper for NamedTemporaryFile, which allows | ||
closing the file handle without deleting the underling file. | ||
Deletion is delegated to the context manager closing. | ||
Note: With Python 3.12, a new parameter 'delete_on_close' is introduced | ||
for NamedTemporaryFile which accomplishes the same thing. Consequently, | ||
this api should be replaced when the code is updated to 3.12. | ||
""" | ||
|
||
kwargs["delete"] = False | ||
with NamedTemporaryFile(**kwargs) as file: | ||
yield file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters