summaryrefslogtreecommitdiff
path: root/Lib/test/test_old_mailbox.py
diff options
context:
space:
mode:
authorBrian Curtin <brian.curtin@gmail.com>2010-10-14 02:06:55 +0000
committerBrian Curtin <brian.curtin@gmail.com>2010-10-14 02:06:55 +0000
commit4b09b04b4d0c77acb6eeca2fa1bd4e3ab48d3a8a (patch)
tree4e17284d53cf1f5d53b8b58120895a7916559c2e /Lib/test/test_old_mailbox.py
parent6ec1eb8e7357fb3ecd70aa2b054e75ee6f9b248d (diff)
downloadcpython-git-4b09b04b4d0c77acb6eeca2fa1bd4e3ab48d3a8a.tar.gz
Implement #7944. Use `with` throughout the test suite.
Diffstat (limited to 'Lib/test/test_old_mailbox.py')
-rw-r--r--Lib/test/test_old_mailbox.py35
1 files changed, 17 insertions, 18 deletions
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!