]> granicus.if.org Git - python/commitdiff
Use with open() as fo: ... instead of try: fo = open(...) finally: fo.close()
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 16 May 2010 00:34:40 +0000 (00:34 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 16 May 2010 00:34:40 +0000 (00:34 +0000)
fo is not set if the open() fails.

Lib/test/list_tests.py

index e2fa9fe63238554628aa2ee6a58f46ff66664bb5..546416ed56c13328ea238a1811f3149c2e616cf0 100644 (file)
@@ -57,13 +57,11 @@ class CommonTest(seq_tests.CommonTest):
         d.append(d)
         d.append(400)
         try:
-            fo = open(test_support.TESTFN, "wb")
-            print >> fo, d,
-            fo.close()
-            fo = open(test_support.TESTFN, "rb")
-            self.assertEqual(fo.read(), repr(d))
+            with open(test_support.TESTFN, "wb") as fo:
+                print >> fo, d,
+            with open(test_support.TESTFN, "rb") as fo:
+                self.assertEqual(fo.read(), repr(d))
         finally:
-            fo.close()
             os.remove(test_support.TESTFN)
 
     def test_set_subscript(self):