[Python-checkins] cpython (merge 3.2 -> default): merge #11700: proxy object close methods can now be called multiple times

r.david.murray python-checkins at python.org
Sat Jun 18 04:26:09 CEST 2011


http://hg.python.org/cpython/rev/0705b9037b20
changeset:   70858:0705b9037b20
parent:      70855:f041d2223ed3
parent:      70857:8319db2dd342
user:        R David Murray <rdmurray at bitdance.com>
date:        Fri Jun 17 22:25:14 2011 -0400
summary:
  merge #11700: proxy object close methods can now be called multiple times

files:
  Lib/mailbox.py           |  14 ++++++++++----
  Lib/test/test_mailbox.py |  13 ++++++++++++-
  Misc/NEWS                |   3 +++
  3 files changed, 25 insertions(+), 5 deletions(-)


diff --git a/Lib/mailbox.py b/Lib/mailbox.py
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -1923,9 +1923,10 @@
 
     def close(self):
         """Close the file."""
-        if hasattr(self._file, 'close'):
-            self._file.close()
-        del self._file
+        if hasattr(self, '_file'):
+            if hasattr(self._file, 'close'):
+                self._file.close()
+            del self._file
 
     def _read(self, size, read_method):
         """Read size bytes using read_method."""
@@ -1957,6 +1958,10 @@
 
     @property
     def closed(self):
+        if not hasattr(self, '_file'):
+            return True
+        if not hasattr(self._file, 'closed'):
+            return False
         return self._file.closed
 
 
@@ -1995,7 +2000,8 @@
     def close(self):
         # do *not* close the underlying file object for partial files,
         # since it's global to the mailbox object
-        del self._file
+        if hasattr(self, '_file'):
+            del self._file
 
 
 def _lock_file(f, dotlock=True):
diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py
--- a/Lib/test/test_mailbox.py
+++ b/Lib/test/test_mailbox.py
@@ -297,6 +297,13 @@
         self.assertEqual(data1.decode('ascii').replace(os.linesep, '\n'),
                          _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):
         # Get keys using iterkeys()
         self._check_iteration(self._box.keys, do_keys=True, do_values=False)
@@ -1862,8 +1869,12 @@
 
     def _test_close(self, proxy):
         # Close a file
+        self.assertFalse(proxy.closed)
         proxy.close()
-        self.assertRaises(AttributeError, lambda: proxy.close())
+        self.assertTrue(proxy.closed)
+        # Issue 11700 subsequent closes should be a no-op.
+        proxy.close()
+        self.assertTrue(proxy.closed)
 
 
 class TestProxyFile(TestProxyFileBase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -193,6 +193,9 @@
 Library
 -------
 
+- Issue #11700: mailbox proxy object close methods can now be called multiple
+  times without error.
+
 - Issue #11767: Correct file descriptor leak in mailbox's __getitem__ method.
 
 - Issue #12133: AbstractHTTPHandler.do_open() of urllib.request closes the HTTP

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list