Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-26175: Fix SpooledTemporaryFile IOBase abstract#3249

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
GFernie wants to merge13 commits intopython:mainfromGFernie:bpo-26175_fix-SpooledTemporaryFile-IOBase
Closed
Show file tree
Hide file tree
Changes from2 commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
83d6152
Fix SpooledTemporaryFile IOBase abstract
GFernieAug 30, 2017
3b69baa
Add news entry for bpo-26175 patch
GFernieAug 30, 2017
753b4fb
Update 2017-08-30-23-14-17.bpo-26175.GOaj9y.rst
merwokSep 20, 2017
a70113e
Make SpooledTemporaryFile subclass IOBase
GFernieSep 30, 2017
fb28362
Refactor tests to more reliably test abstract
GFernieSep 30, 2017
bc9d0a9
Return new absolute position from underlying file on seek
GFernieSep 9, 2018
b588dec
Do nothing on __del__
GFernieSep 9, 2018
ab49dd1
Return new file size from underlying file on truncate
GFernieSep 9, 2018
4251b21
Merge remote-tracking branch 'origin/master' into bpo-26175_fix-Spool…
GFernieOct 30, 2019
19f4c08
Add versionchanged tag
GFernieOct 30, 2019
4519b9c
Use :class: instead of :py:data:
GFernieOct 30, 2019
554aad6
Revert "Do nothing on __del__"
GFernieNov 23, 2019
be094b3
Add test for rolled file finalisation
GFernieNov 23, 2019
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
15 changes: 15 additions & 0 deletionsLib/tempfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -685,6 +685,9 @@ def __exit__(self, exc, value, tb):
def __iter__(self):
return self._file.__iter__()

def __del__(self):
Copy link

@ppperyppperyJun 6, 2018
edited
Loading

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__

Copy link
Member

@vadmiumvadmiumJun 9, 2018
edited
Loading

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 defaultIOBase.__del__ implementation, it will callclose and defeat any ResourceWarning that might otherwise be emitted. Perhaps it is better to make__del__ do nothing,or set it toobject.__del__. [Seems that last option doesn’t exist.]

Copy link
ContributorAuthor

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

Copy link
Member

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

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 fileshould be deleted when the file object disappears.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The point is thatIOBase 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.

return self._file.__del__()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hmm, calling the__del__ method directly like that is not correct.__del__ is only to be called when all external references are gone. Instead I would suggest something like the following:

def__del__(self):ifnotself.closed:importwarningswarnings.warn('unclosed file %r'% (self,),ResourceWarning,stacklevel=2,source=self)self.close()

(this is adapted fromFileIO.__del__ inLib/pyio.py)


def close(self):
self._file.close()

Expand DownExpand Up@@ -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)

Expand All@@ -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
Expand All@@ -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)
Expand Down
29 changes: 29 additions & 0 deletionsLib/test/test_tempfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -982,6 +982,35 @@ def test_basic(self):
f = self.do_create(max_size=100, pre="a", suf=".txt")
self.assertFalse(f._rolled)

def test_iobase_abstract(self):
# SpooledTemporaryFile should implement the IOBase abstract
iobase_abstract = (
'close',
'closed',
'fileno',
'flush',
'isatty',
'read',
'readable',
'readinto',
'readline',
'readlines',
'seek',
'seekable',
'tell',
'truncate',
'write',
'writable',
'writelines',
'__del__',
)
f = self.do_create()
for attribute in iobase_abstract:
self.assertTrue(
hasattr(f, attribute),
'{} attribute missing'.format(attribute)
)

def test_del_on_close(self):
# A SpooledTemporaryFile is deleted when closed
dir = tempfile.mkdtemp()
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fully implement `io.IOBase` abstract for `tempfile.SpooledTemporaryFile`.
Patch by Gary Fernie

[8]ページ先頭

©2009-2025 Movatter.jp