]> granicus.if.org Git - python/commitdiff
make file closing more robust
authorBenjamin Peterson <benjamin@python.org>
Sat, 13 Jun 2009 13:01:19 +0000 (13:01 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 13 Jun 2009 13:01:19 +0000 (13:01 +0000)
Lib/test/test_import.py

index d30f905dd9b7c581d80f5fca304725c895fdbb61..dbfadfb708992084a4f5361553e046cfc87e2585 100644 (file)
@@ -46,13 +46,12 @@ class ImportTest(unittest.TestCase):
             else:
                 pyc = TESTFN + ".pyc"
 
-            f = open(source, "w")
-            print("# This tests Python's ability to import a", ext, "file.", file=f)
-            a = random.randrange(1000)
-            b = random.randrange(1000)
-            print("a =", a, file=f)
-            print("b =", b, file=f)
-            f.close()
+            with open(source, "w") as f:
+                print("# This tests Python's ability to import a", ext, "file.", file=f)
+                a = random.randrange(1000)
+                b = random.randrange(1000)
+                print("a =", a, file=f)
+                print("b =", b, file=f)
 
             if TESTFN in sys.modules:
                 del sys.modules[TESTFN]