Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-58451: Add optional delete_on_close parameter to NamedTemporaryFile#97015
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3df957c
Brought the same changes as in now closed PR 22431, but to the latest…
Ev2geny c4cf39f
Based on the review from @eryksun the code and the documentation is u…
Ev2geny ac0b241
documentation is slightly corrected
Ev2geny 384614f
minor documentation update
Ev2geny 0e0b6ba
Update Doc/whatsnew/3.12.rst
Ev2geny cc81ab5
Deletion of file upon finalization is moved from _TemporaryFileWrappe…
Ev2geny 5af6150
Some additions to Lib/test/test_tempfile.py
Ev2geny 239354e
clearing of the files in the situation when delete and not delete_on_…
Ev2geny 5de68c0
if statement is added to avoid errors, when testing. Not sure 100% is…
Ev2geny eef3439
Apply suggestions from code review
Ev2geny c9801ee
__del__ was defined twice in _TemporaryFileWrapper. Fixed now
Ev2geny 6944ab2
_TemporaryFileCloser is returned to original state and the logic, whi…
Ev2geny 44f0664
All delete operations are moved to the class _TemporaryFileCloser
Ev2geny 1dfaf72
Minor update to comments
Ev2geny e54feb5
Whitespec fixes by patchcheck.py
Ev2geny d2d1119
Update Lib/tempfile.py
Ev2geny bd4b2dd
Update Lib/tempfile.py
Ev2geny 29eeaff
Modified exception handling in NamedTemporaryFile
Ev2geny 5306b6d
Wrapping at 80 characters is fixed
Ev2geny 1c99fb3
Update Lib/tempfile.py
Ev2geny 29b89d3
Documentation update, based on the feedback from @zooba
Ev2geny e586028
Minor PEP8 update based on the feedback from @zooba
Ev2geny 3eceb45
Improving comments in the init test file, based on the feedback from …
Ev2geny 892e87d
Updating of documentation
Ev2geny 67996b2
Merge branch 'fix-issue-14243' of https://github.com/Ev2geny/cpython …
Ev2geny 07b963e
Apply suggestions from code review
zooba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -418,42 +418,42 @@ class _TemporaryFileCloser: | ||
| underlying file object, without adding a __del__ method to the | ||
| temporary file.""" | ||
| file = None # Set here since __del__ checks it | ||
| cleanup_called = False | ||
| close_called = False | ||
| def __init__(self, file, name, delete=True): | ||
| def __init__(self, file, name, delete=True, delete_on_close=True): | ||
Ev2geny marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.file = file | ||
| self.name = name | ||
| self.delete = delete | ||
| self.delete_on_close = delete_on_close | ||
| # NT provides delete-on-close as a primitive, so we don't need | ||
| # the wrapper to do anything special. We still use it so that | ||
| # file.name is useful (i.e. not "(fdopen)") with NamedTemporaryFile. | ||
| if _os.name != 'nt': | ||
| # Cache the unlinker so we don't get spurious errors at | ||
| # shutdown when the module-level "os" is None'd out. Note | ||
| # that this must be referenced as self.unlink, because the | ||
| # name TemporaryFileWrapper may also get None'd out before | ||
| # __del__ is called. | ||
| def close(self, unlink=_os.unlink): | ||
| if not self.close_called and self.file is not None: | ||
| self.close_called = True | ||
| try: | ||
| def cleanup(self, windows=(_os.name == 'nt'), unlink=_os.unlink): | ||
| if not self.cleanup_called: | ||
| self.cleanup_called = True | ||
| try: | ||
| if not self.close_called: | ||
| self.close_called = True | ||
| self.file.close() | ||
| finally: | ||
| if self.delete: | ||
| finally: | ||
| # Windows provides delete-on-close as a primitive, in which | ||
| # case the file was deleted by self.file.close(). | ||
| if self.delete and not (windows and self.delete_on_close): | ||
| try: | ||
| unlink(self.name) | ||
| except FileNotFoundError: | ||
| pass | ||
| # Need to ensure the file is deleted on __del__ | ||
| def __del__(self): | ||
| self.close() | ||
| else: | ||
| def close(self): | ||
| if not self.close_called: | ||
| self.close_called = True | ||
| def close(self): | ||
| if not self.close_called: | ||
| self.close_called = True | ||
| try: | ||
| self.file.close() | ||
| finally: | ||
| if self.delete and self.delete_on_close: | ||
| self.cleanup() | ||
| def __del__(self): | ||
| self.cleanup() | ||
Ev2geny marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| class _TemporaryFileWrapper: | ||
| @@ -464,11 +464,11 @@ class _TemporaryFileWrapper: | ||
| remove the file when it is no longer needed. | ||
| """ | ||
| def __init__(self, file, name, delete=True): | ||
| def __init__(self, file, name, delete=True, delete_on_close=True): | ||
| self.file = file | ||
| self.name = name | ||
| self.delete = delete | ||
| self._closer = _TemporaryFileCloser(file, name, delete) | ||
| self._closer = _TemporaryFileCloser(file, name, delete, | ||
| delete_on_close) | ||
| def __getattr__(self, name): | ||
| # Attribute lookups are delegated to the underlying file | ||
| @@ -499,7 +499,7 @@ def __enter__(self): | ||
| # deleted when used in a with statement | ||
| def __exit__(self, exc, value, tb): | ||
| result = self.file.__exit__(exc, value, tb) | ||
| self.close() | ||
| self._closer.cleanup() | ||
| return result | ||
| def close(self): | ||
| @@ -518,18 +518,21 @@ def __iter__(self): | ||
| for line in self.file: | ||
| yield line | ||
| def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, | ||
| newline=None, suffix=None, prefix=None, | ||
| dir=None, delete=True, *, errors=None): | ||
| dir=None, delete=True, *, errors=None, | ||
| delete_on_close=True): | ||
| """Create and return a temporary file. | ||
| Arguments: | ||
| 'prefix', 'suffix', 'dir' -- as for mkstemp. | ||
| 'mode' -- the mode argument to io.open (default "w+b"). | ||
| 'buffering' -- the buffer size argument to io.open (default -1). | ||
| 'encoding' -- the encoding argument to io.open (default None) | ||
| 'newline' -- the newline argument to io.open (default None) | ||
| 'delete' -- whether the file is deleted on close (default True). | ||
| 'delete' -- whether the file is automatically deleted (default True). | ||
| 'delete_on_close' -- if 'delete', whether the file is deleted on close | ||
| (default True) or otherwise either on context manager exit | ||
| (if context manager was used) or on object finalization. . | ||
| 'errors' -- the errors argument to io.open (default None) | ||
| The file is created as mkstemp() would do it. | ||
| @@ -548,7 +551,7 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, | ||
| # Setting O_TEMPORARY in the flags causes the OS to delete | ||
| # the file when it is closed. This is only supported by Windows. | ||
| if _os.name == 'nt' and delete: | ||
| if _os.name == 'nt' and delete and delete_on_close: | ||
| flags |= _os.O_TEMPORARY | ||
| if "b" not in mode: | ||
| @@ -567,12 +570,13 @@ def opener(*args): | ||
| raw = getattr(file, 'buffer', file) | ||
| raw = getattr(raw, 'raw', raw) | ||
| raw.name = name | ||
| return _TemporaryFileWrapper(file, name, delete) | ||
| return _TemporaryFileWrapper(file, name, delete, delete_on_close) | ||
| except: | ||
| file.close() | ||
| raise | ||
| except: | ||
| if name is not None and not (_os.name == 'nt' and delete): | ||
| if name is not None and not ( | ||
| _os.name == 'nt' and delete and delete_on_close): | ||
| _os.unlink(name) | ||
| raise | ||
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1 Misc/NEWS.d/next/Library/2020-09-28-04-56-04.bpo-14243.YECnxv.rst
This file contains hidden or 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 @@ | ||
| The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter *delete_on_close* |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.