]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#11700: proxy object close methods can now be called multiple times
authorR David Murray <rdmurray@bitdance.com>
Sat, 18 Jun 2011 02:24:05 +0000 (22:24 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 18 Jun 2011 02:24:05 +0000 (22:24 -0400)
This makes them work like the close provided by regular file objects.

Lib/mailbox.py
Lib/test/test_mailbox.py
Misc/NEWS

index b96b270d68730dd9db2ab148d63a9e6857d2726c..e23ea8c6329c6a54e460c43b458694c761dedc8c 100644 (file)
@@ -1923,9 +1923,10 @@ class _ProxyFile:
 
     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 @@ class _ProxyFile:
 
     @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 @@ class _PartialFile(_ProxyFile):
     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):
index 10317c3bb913f8c4efd2a7300958c437f7ccbbdc..fb4812dd9947359ceae6134e7e5d57e2a3148af7 100644 (file)
@@ -297,6 +297,13 @@ class TestMailbox(TestBase):
         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 @@ class TestProxyFileBase(TestBase):
 
     def _test_close(self, proxy):
         # Close a file
+        self.assertFalse(proxy.closed)
+        proxy.close()
+        self.assertTrue(proxy.closed)
+        # Issue 11700 subsequent closes should be a no-op.
         proxy.close()
-        self.assertRaises(AttributeError, lambda: proxy.close())
+        self.assertTrue(proxy.closed)
 
 
 class TestProxyFile(TestProxyFileBase):
index ec8b72ec32b6703a439448558a7cc7b7e3b7b2d3..170d5224d96b8b66112fc893074db60b8e484bb5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,6 +25,9 @@ Core and Builtins
 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