[Python-checkins] r85449 - python/branches/release27-maint/Lib/test/test_old_mailbox.py

brian.curtin python-checkins at python.org
Thu Oct 14 04:06:55 CEST 2010


Author: brian.curtin
Date: Thu Oct 14 04:06:55 2010
New Revision: 85449

Log:
Implement #7944. Use `with` throughout the test suite.


Modified:
   python/branches/release27-maint/Lib/test/test_old_mailbox.py

Modified: python/branches/release27-maint/Lib/test/test_old_mailbox.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_old_mailbox.py	(original)
+++ python/branches/release27-maint/Lib/test/test_old_mailbox.py	Thu Oct 14 04:06:55 2010
@@ -48,18 +48,16 @@
         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 @@
         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 @@
 
     def test_from_regex (self):
         # Testing new regex from bug #1633678
-        f = open(self._path, 'w')
-        f.write("""From fred at example.com Mon May 31 13:24:50 2004 +0200
+        with open(self._path, 'w') as f:
+            f.write("""From fred at example.com Mon May 31 13:24:50 2004 +0200
 Subject: message 1
 
 body1
@@ -137,9 +136,9 @@
 
 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!


More information about the Python-checkins mailing list