Commit 4b09b04b authored by Brian Curtin's avatar Brian Curtin

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

parent 6ec1eb8e
...@@ -48,18 +48,16 @@ class MaildirTestCase(unittest.TestCase): ...@@ -48,18 +48,16 @@ class MaildirTestCase(unittest.TestCase):
filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain")) filename = os.extsep.join((str(t), str(pid), "myhostname", "mydomain"))
tmpname = os.path.join(self._dir, "tmp", filename) tmpname = os.path.join(self._dir, "tmp", filename)
newname = os.path.join(self._dir, dir, filename) newname = os.path.join(self._dir, dir, filename)
fp = open(tmpname, "w") with open(tmpname, "w") as fp:
self._msgfiles.append(tmpname) self._msgfiles.append(tmpname)
if mbox: if mbox:
fp.write(FROM_) fp.write(FROM_)
fp.write(DUMMY_MESSAGE) fp.write(DUMMY_MESSAGE)
fp.close()
if hasattr(os, "link"): if hasattr(os, "link"):
os.link(tmpname, newname) os.link(tmpname, newname)
else: else:
fp = open(newname, "w") with open(newname, "w") as fp:
fp.write(DUMMY_MESSAGE) fp.write(DUMMY_MESSAGE)
fp.close()
self._msgfiles.append(newname) self._msgfiles.append(newname)
return tmpname return tmpname
...@@ -102,7 +100,8 @@ class MaildirTestCase(unittest.TestCase): ...@@ -102,7 +100,8 @@ class MaildirTestCase(unittest.TestCase):
import email.parser import email.parser
fname = self.createMessage("cur", True) fname = self.createMessage("cur", True)
n = 0 n = 0
for msg in mailbox.PortableUnixMailbox(open(fname), with open(fname) as f:
for msg in mailbox.PortableUnixMailbox(f,
email.parser.Parser().parse): email.parser.Parser().parse):
n += 1 n += 1
self.assertEqual(msg["subject"], "Simple Test") self.assertEqual(msg["subject"], "Simple Test")
...@@ -119,7 +118,7 @@ class MboxTestCase(unittest.TestCase): ...@@ -119,7 +118,7 @@ class MboxTestCase(unittest.TestCase):
def test_from_regex (self): def test_from_regex (self):
# Testing new regex from bug #1633678 # Testing new regex from bug #1633678
f = open(self._path, 'w') with open(self._path, 'w') as f:
f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200 f.write("""From fred@example.com Mon May 31 13:24:50 2004 +0200
Subject: message 1 Subject: message 1
...@@ -137,8 +136,8 @@ Subject: message 4 ...@@ -137,8 +136,8 @@ Subject: message 4
body4 body4
""") """)
f.close() with open(self._path, 'r') as f:
box = mailbox.UnixMailbox(open(self._path, 'r')) box = mailbox.UnixMailbox(f)
self.assertTrue(len(list(iter(box))) == 4) self.assertTrue(len(list(iter(box))) == 4)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment