Commits

Benjamin Peterson committed 032c2e5

make file closing more robust

Comments (0)

Files changed (1)

Lib/test/test_import.py

             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]