-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
bpo-26175: Fix SpooledTemporaryFile IOBase abstract #3249
Closed
GFernie
wants to merge
13
commits into
python:main
from
GFernie:bpo-26175_fix-SpooledTemporaryFile-IOBase
Closed
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
83d6152
Fix SpooledTemporaryFile IOBase abstract
GFernie 3b69baa
Add news entry for bpo-26175 patch
GFernie 753b4fb
Update 2017-08-30-23-14-17.bpo-26175.GOaj9y.rst
merwok a70113e
Make SpooledTemporaryFile subclass IOBase
GFernie fb28362
Refactor tests to more reliably test abstract
GFernie bc9d0a9
Return new absolute position from underlying file on seek
GFernie b588dec
Do nothing on __del__
GFernie ab49dd1
Return new file size from underlying file on truncate
GFernie 4251b21
Merge remote-tracking branch 'origin/master' into bpo-26175_fix-Spool…
GFernie 19f4c08
Add versionchanged tag
GFernie 4519b9c
Use :class: instead of :py:data:
GFernie 554aad6
Revert "Do nothing on __del__"
GFernie be094b3
Add test for rolled file finalisation
GFernie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Fix SpooledTemporaryFile IOBase abstract
One would assume that this class implements the IOBase abstract. As the underlying file-like object is either io.BytesIO, io.StringIO, or a true file object, this is a reasonable abstract expect and to implement. Regardless, the behaviour of this class does not change much in the case of the attribute being missing from the underlying file-like; an AttributeError is still raised, albeit from one additional frame on the stack trace.
- Loading branch information
commit 83d6152028ad56d7eb0e4038c9c6d5c643908629
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 |
---|---|---|
|
@@ -685,6 +685,9 @@ def __exit__(self, exc, value, tb): | |
def __iter__(self): | ||
return self._file.__iter__() | ||
|
||
def __del__(self): | ||
return self._file.__del__() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, calling the def __del__(self):
if not self.closed:
import warnings
warnings.warn('unclosed file %r' % (self,), ResourceWarning,
stacklevel=2, source=self)
self.close() (this is adapted from |
||
|
||
def close(self): | ||
self._file.close() | ||
|
||
|
@@ -737,6 +740,12 @@ def newlines(self): | |
def read(self, *args): | ||
return self._file.read(*args) | ||
|
||
def readable(self): | ||
return self._file.readable() | ||
|
||
def readinto(self, b): | ||
return self._file.readinto(b) | ||
|
||
def readline(self, *args): | ||
return self._file.readline(*args) | ||
|
||
|
@@ -746,6 +755,9 @@ def readlines(self, *args): | |
def seek(self, *args): | ||
self._file.seek(*args) | ||
|
||
def seekable(self): | ||
return self._file.seekable() | ||
|
||
@property | ||
def softspace(self): | ||
return self._file.softspace | ||
|
@@ -767,6 +779,9 @@ def write(self, s): | |
self._check(file) | ||
return rv | ||
|
||
def writable(self): | ||
return self._file.writable() | ||
|
||
def writelines(self, iterable): | ||
file = self._file | ||
rv = file.writelines(iterable) | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be added: deleting the SpooledTemporaryFile will null out the reference to the underline file, and therefor call its
__del__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you inherit the default
IOBase.__del__
implementation, it will call close and defeat any ResourceWarning that might otherwise be emitted. Perhaps it is better to make__del__
do nothing,or set it to. [Seems that last option doesn’t exist.]object.__del__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree: the underlying file should not be explicitly deleted as this is not expected behaviour. I can imagine a situation where someone is deliberately holding a reference to the underlying file and they would not expect/want it to be closed until their own reference has fallen out of scope. I've changed the method to do nothing and added a docstring to reflect this.
Thanks for your feedback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc says: """This function operates exactly as TemporaryFile() does, except [irrelevant differences]."""
And then, about TemporaryFile: """On completion of the context or destruction of the file object the temporary file will be removed from the filesystem."""
So it seems the underlying file should be deleted when the file object disappears.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is that
IOBase
implements a__del__
which has some side-effects. Those side effects are not desirable here. Any underlying buffers being wrapped, e.g. theTemporaryFile
or theBytesIO
, will be gc'd and handled as they should.I think this implementation is correct.