Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
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 Sep 21, 2022
c4cf39f
Based on the review from @eryksun the code and the documentation is u…
Ev2geny Sep 22, 2022
ac0b241
documentation is slightly corrected
Ev2geny Sep 22, 2022
384614f
minor documentation update
Ev2geny Sep 22, 2022
0e0b6ba
Update Doc/whatsnew/3.12.rst
Ev2geny Sep 22, 2022
cc81ab5
Deletion of file upon finalization is moved from _TemporaryFileWrappe…
Ev2geny Sep 23, 2022
5af6150
Some additions to Lib/test/test_tempfile.py
Ev2geny Sep 24, 2022
239354e
clearing of the files in the situation when delete and not delete_on_…
Ev2geny Sep 24, 2022
5de68c0
if statement is added to avoid errors, when testing. Not sure 100% is…
Ev2geny Sep 24, 2022
eef3439
Apply suggestions from code review
Ev2geny Sep 25, 2022
c9801ee
__del__ was defined twice in _TemporaryFileWrapper. Fixed now
Ev2geny Sep 25, 2022
6944ab2
_TemporaryFileCloser is returned to original state and the logic, whi…
Ev2geny Sep 25, 2022
44f0664
All delete operations are moved to the class _TemporaryFileCloser
Ev2geny Sep 25, 2022
1dfaf72
Minor update to comments
Ev2geny Sep 25, 2022
e54feb5
Whitespec fixes by patchcheck.py
Ev2geny Sep 25, 2022
d2d1119
Update Lib/tempfile.py
Ev2geny Sep 26, 2022
bd4b2dd
Update Lib/tempfile.py
Ev2geny Sep 26, 2022
29eeaff
Modified exception handling in NamedTemporaryFile
Ev2geny Sep 26, 2022
5306b6d
Wrapping at 80 characters is fixed
Ev2geny Sep 26, 2022
1c99fb3
Update Lib/tempfile.py
Ev2geny Oct 1, 2022
29b89d3
Documentation update, based on the feedback from @zooba
Ev2geny Oct 1, 2022
e586028
Minor PEP8 update based on the feedback from @zooba
Ev2geny Oct 1, 2022
3eceb45
Improving comments in the init test file, based on the feedback from …
Ev2geny Oct 1, 2022
892e87d
Updating of documentation
Ev2geny Oct 2, 2022
67996b2
Merge branch 'fix-issue-14243' of https://github.com/Ev2geny/cpython …
Ev2geny Oct 2, 2022
07b963e
Apply suggestions from code review
zooba Oct 4, 2022
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
Prev Previous commit
Next Next commit
clearing of the files in the situation when delete and not delete_on_…
…close is moved to _TemporaryFileWrapper

based on the advice from @eryksun

There are errors in the test (at least on Linux)
  • Loading branch information
Ev2geny committed Sep 24, 2022
commit 239354e2e4a09914cd9fac0847e9d883c84c0e08
51 changes: 35 additions & 16 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,16 @@ def close(self, unlink=_os.unlink):
if self.delete and self.delete_on_close:
unlink(self.name)

# 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
self.file.close()

def __del__(self):
self.close()
# Need to ensure the file is deleted on __del__ if delete = True and
# file still exists
if self.delete and _os.path.exists(self.name):
_os.unlink(self.name)

class _TemporaryFileWrapper:
"""Temporary file wrapper
Expand All @@ -467,6 +464,8 @@ class _TemporaryFileWrapper:
temporary use. In particular, it seeks to automatically
remove the file when it is no longer needed.
"""
name = None
_cleanup_called = False

def __init__(self, file, name, delete=True, delete_on_close=True):
self.file = file
Expand Down Expand Up @@ -501,22 +500,31 @@ def __enter__(self):
self.file.__enter__()
return self

def _cleanup(self):
if self._cleanup_called or self.name is None:
return
self._cleanup_called = True
try:
self.close()
finally:
# If the file is to be deleted, but not on close, delete it now.
if self.delete and not self.delete_on_close:
try:
_os.unlink(self.name)
except FileNotFoundError:
# The file may have been deleted already.
pass

# Need to trap __exit__ as well to ensure the file gets
# deleted when used in a with statement
def __exit__(self, exc, value, tb):
result = self.file.__exit__(exc, value, tb)
self.close()
# If the file is to be deleted, but not on close, delete it now.
if self.delete and not self.delete_on_close:
try:
_os.unlink(self.name)
# It is okay to ignore FileNotFoundError. The file may have
# been deleted already.
except FileNotFoundError:
pass

self._cleanup()
return result

def __del__(self):
self._cleanup()

def close(self):
"""
Close the temporary file, possibly deleting it.
Expand All @@ -533,6 +541,17 @@ def __iter__(self):
for line in self.file:
yield line

def __del__(self):
# This is to delete the temporary file in case delete = True,
# delete_on_close = False and no context manager was used
if self.delete:
try:
_os.unlink(self.name)
# It is okay to ignore FileNotFoundError. The file may have
# been deleted already.
except FileNotFoundError:
pass


def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
newline=None, suffix=None, prefix=None,
Expand Down