From: Brian Curtin Date: Thu, 14 Oct 2010 02:06:55 +0000 (+0000) Subject: Implement #7944. Use `with` throughout the test suite. X-Git-Tag: v2.7.1rc1~165 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4b09b04b4d0c77acb6eeca2fa1bd4e3ab48d3a8a;p=python Implement #7944. Use `with` throughout the test suite. --- diff --git a/Lib/test/test_old_mailbox.py b/Lib/test/test_old_mailbox.py index c1bebaf2a1..e8dff5079b 100644 --- a/Lib/test/test_old_mailbox.py +++ b/Lib/test/test_old_mailbox.py @@ -48,18 +48,16 @@ class MaildirTestCase(unittest.TestCase): filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain")) tmpname = os.path.join(self._dir, "tmp", filename) newname = os.path.join(self._dir, dir, filename) - fp = open(tmpname, "w") - self._msgfiles.append(tmpname) - if mbox: - fp.write(FROM_) - fp.write(DUMMY_MESSAGE) - fp.close() + with open(tmpname, "w") as fp: + self._msgfiles.append(tmpname) + if mbox: + fp.write(FROM_) + fp.write(DUMMY_MESSAGE) if hasattr(os, "link"): os.link(tmpname, newname) else: - fp = open(newname, "w") - fp.write(DUMMY_MESSAGE) - fp.close() + with open(newname, "w") as fp: + fp.write(DUMMY_MESSAGE) self._msgfiles.append(newname) return tmpname @@ -102,11 +100,12 @@ class MaildirTestCase(unittest.TestCase): import email.parser fname = self.createMessage("cur", True) n = 0 - for msg in mailbox.PortableUnixMailbox(open(fname), + with open(fname) as f: + for msg in mailbox.PortableUnixMailbox(f, email.parser.Parser().parse): - n += 1 - self.assertEqual(msg["subject"], "Simple Test") - self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE)) + n += 1 + self.assertEqual(msg["subject"], "Simple Test") + self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE)) self.assertEqual(n, 1) class MboxTestCase(unittest.TestCase): @@ -119,8 +118,8 @@ class MboxTestCase(unittest.TestCase): def test_from_regex (self): # Testing new regex from bug #1633678 - f = open(self._path, 'w') - f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200 + with open(self._path, 'w') as f: + f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200 Subject: message 1 body1 @@ -137,9 +136,9 @@ Subject: message 4 body4 """) - f.close() - box = mailbox.UnixMailbox(open(self._path, 'r')) - self.assertTrue(len(list(iter(box))) == 4) + with open(self._path, 'r') as f: + box = mailbox.UnixMailbox(f) + self.assertTrue(len(list(iter(box))) == 4) # XXX We still need more tests!