]> granicus.if.org Git - python/commitdiff
Implement #7944. Use `with` throughout the test suite.
authorBrian Curtin <brian.curtin@gmail.com>
Thu, 14 Oct 2010 02:06:55 +0000 (02:06 +0000)
committerBrian Curtin <brian.curtin@gmail.com>
Thu, 14 Oct 2010 02:06:55 +0000 (02:06 +0000)
Lib/test/test_old_mailbox.py

index c1bebaf2a11bae7386de8855fbf193e48696ff03..e8dff5079b52e799e223e605acb879669e7f6774 100644 (file)
@@ -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!