mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
#11700: proxy object close methods can now be called multiple times
This makes them work like the close provided by regular file objects. This patch also backports the close-the-underlying-file code for _ProxyFile objects that was introduced along with context manager support in the 3.x branch.
This commit is contained in:
parent
c74a6ba2d6
commit
f1138bb1b6
3 changed files with 22 additions and 2 deletions
|
@ -1854,7 +1854,10 @@ class _ProxyFile:
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close the file."""
|
"""Close the file."""
|
||||||
del self._file
|
if hasattr(self, '_file'):
|
||||||
|
if hasattr(self._file, 'close'):
|
||||||
|
self._file.close()
|
||||||
|
del self._file
|
||||||
|
|
||||||
def _read(self, size, read_method):
|
def _read(self, size, read_method):
|
||||||
"""Read size bytes using read_method."""
|
"""Read size bytes using read_method."""
|
||||||
|
@ -1898,6 +1901,12 @@ class _PartialFile(_ProxyFile):
|
||||||
size = remaining
|
size = remaining
|
||||||
return _ProxyFile._read(self, size, read_method)
|
return _ProxyFile._read(self, size, read_method)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
# do *not* close the underlying file object for partial files,
|
||||||
|
# since it's global to the mailbox object
|
||||||
|
if hasattr(self, '_file'):
|
||||||
|
del self._file
|
||||||
|
|
||||||
|
|
||||||
def _lock_file(f, dotlock=True):
|
def _lock_file(f, dotlock=True):
|
||||||
"""Lock file f using lockf and dot locking."""
|
"""Lock file f using lockf and dot locking."""
|
||||||
|
|
|
@ -174,6 +174,13 @@ class TestMailbox(TestBase):
|
||||||
self.assertEqual(self._box.get_file(key1).read().replace(os.linesep, '\n'),
|
self.assertEqual(self._box.get_file(key1).read().replace(os.linesep, '\n'),
|
||||||
_sample_message)
|
_sample_message)
|
||||||
|
|
||||||
|
def test_get_file_can_be_closed_twice(self):
|
||||||
|
# Issue 11700
|
||||||
|
key = self._box.add(_sample_message)
|
||||||
|
f = self._box.get_file(key)
|
||||||
|
f.close()
|
||||||
|
f.close()
|
||||||
|
|
||||||
def test_iterkeys(self):
|
def test_iterkeys(self):
|
||||||
# Get keys using iterkeys()
|
# Get keys using iterkeys()
|
||||||
self._check_iteration(self._box.iterkeys, do_keys=True, do_values=False)
|
self._check_iteration(self._box.iterkeys, do_keys=True, do_values=False)
|
||||||
|
@ -1670,7 +1677,8 @@ class TestProxyFileBase(TestBase):
|
||||||
def _test_close(self, proxy):
|
def _test_close(self, proxy):
|
||||||
# Close a file
|
# Close a file
|
||||||
proxy.close()
|
proxy.close()
|
||||||
self.assertRaises(AttributeError, lambda: proxy.close())
|
# Issue 11700 subsequent closes should be a no-op, not an error.
|
||||||
|
proxy.close()
|
||||||
|
|
||||||
|
|
||||||
class TestProxyFile(TestProxyFileBase):
|
class TestProxyFile(TestProxyFileBase):
|
||||||
|
|
|
@ -16,6 +16,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #11700: mailbox proxy object close methods can now be called multiple
|
||||||
|
times without error, and _ProxyFile now closes the wrapped file.
|
||||||
|
|
||||||
- Issue #12133: AbstractHTTPHandler.do_open() of urllib.request closes the HTTP
|
- Issue #12133: AbstractHTTPHandler.do_open() of urllib.request closes the HTTP
|
||||||
connection if its getresponse() method fails with a socket error. Patch
|
connection if its getresponse() method fails with a socket error. Patch
|
||||||
written by Ezio Melotti.
|
written by Ezio Melotti.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue