diff options
author | Éric Araujo <merwok@netwok.org> | 2011-06-17 17:39:18 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-06-17 17:39:18 (GMT) |
commit | 980d344ff3d99b8893619b00a4ef3752c8ff0a0e (patch) | |
tree | 28b9ddf672384b418edd26e3ea987d3ddb83c13c /Lib | |
parent | 74d68135adffce55cf96e62c0c71ab72945a7753 (diff) | |
parent | 4c7d21e2b3207d2123949c10d139520c9d19adb1 (diff) | |
download | cpython-980d344ff3d99b8893619b00a4ef3752c8ff0a0e.zip cpython-980d344ff3d99b8893619b00a4ef3752c8ff0a0e.tar.gz cpython-980d344ff3d99b8893619b00a4ef3752c8ff0a0e.tar.bz2 |
Brange merge
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/mailbox.py | 4 | ||||
-rw-r--r-- | Lib/test/test_mailbox.py | 33 |
2 files changed, 35 insertions, 2 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 0e4f99b..b96b270 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -20,6 +20,7 @@ import email import email.message import email.generator import io +import contextlib try: if sys.platform == 'os2emx': # OS/2 EMX fcntl() not adequate @@ -76,7 +77,8 @@ class Mailbox: if not self._factory: return self.get_message(key) else: - return self._factory(self.get_file(key)) + with contextlib.closing(self.get_file(key)) as file: + return self._factory(file) def get_message(self, key): """Return a Message representation or raise a KeyError.""" diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py index e462fe3..f5abb41 100644 --- a/Lib/test/test_mailbox.py +++ b/Lib/test/test_mailbox.py @@ -1201,6 +1201,37 @@ class TestBabyl(TestMailbox): self.assertEqual(set(self._box.get_labels()), set(['blah'])) +class FakeFileLikeObject: + + def __init__(self): + self.closed = False + + def close(self): + self.closed = True + + +class FakeMailBox(mailbox.Mailbox): + + def __init__(self): + mailbox.Mailbox.__init__(self, '', lambda file: None) + self.files = [FakeFileLikeObject() for i in range(10)] + + def get_file(self, key): + return self.files[key] + + +class TestFakeMailBox(unittest.TestCase): + + def test_closing_fd(self): + box = FakeMailBox() + for i in range(10): + self.assertFalse(box.files[i].closed) + for i in range(10): + box[i] + for i in range(10): + self.assertTrue(box.files[i].closed) + + class TestMessage(TestBase): _factory = mailbox.Message # Overridden by subclasses to reuse tests @@ -2113,7 +2144,7 @@ def test_main(): TestBabyl, TestMessage, TestMaildirMessage, TestMboxMessage, TestMHMessage, TestBabylMessage, TestMMDFMessage, TestMessageConversion, TestProxyFile, TestPartialFile, - MaildirTestCase) + MaildirTestCase, TestFakeMailBox) support.run_unittest(*tests) support.reap_children() |