mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Fix test_mailbox by supporting context manager protocol for get_file() returns.
This commit is contained in:
parent
849e12bfe9
commit
6ce29fa7a8
3 changed files with 24 additions and 3 deletions
|
@ -184,6 +184,10 @@ Maildir, mbox, MH, Babyl, and MMDF.
|
||||||
file-like object behaves as if open in binary mode. This file should be
|
file-like object behaves as if open in binary mode. This file should be
|
||||||
closed once it is no longer needed.
|
closed once it is no longer needed.
|
||||||
|
|
||||||
|
.. versionadded:: 3.2
|
||||||
|
The file-like object supports the context manager protocol, so that
|
||||||
|
you can use a :keyword:`with` statement to automatically close it.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
Unlike other representations of messages, file-like representations are
|
Unlike other representations of messages, file-like representations are
|
||||||
|
|
|
@ -1827,6 +1827,8 @@ class _ProxyFile:
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close the file."""
|
"""Close the file."""
|
||||||
|
if hasattr(self._file, 'close'):
|
||||||
|
self._file.close()
|
||||||
del self._file
|
del self._file
|
||||||
|
|
||||||
def _read(self, size, read_method):
|
def _read(self, size, read_method):
|
||||||
|
@ -1838,6 +1840,13 @@ class _ProxyFile:
|
||||||
self._pos = self._file.tell()
|
self._pos = self._file.tell()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Context manager protocol support."""
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *exc):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
|
||||||
class _PartialFile(_ProxyFile):
|
class _PartialFile(_ProxyFile):
|
||||||
"""A read-only wrapper of part of a file."""
|
"""A read-only wrapper of part of a file."""
|
||||||
|
@ -1871,6 +1880,11 @@ 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
|
||||||
|
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."""
|
||||||
|
|
|
@ -57,6 +57,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Support context manager protocol for file-like objects returned by
|
||||||
|
mailbox ``get_file()`` methods.
|
||||||
|
|
||||||
- Issue #10246: uu.encode didn't close file objects explicitly when filenames
|
- Issue #10246: uu.encode didn't close file objects explicitly when filenames
|
||||||
were given to it. Patch by Brian Brazil.
|
were given to it. Patch by Brian Brazil.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue