From 16a1f6343f0c13b4a4366ba2cf5b344ff0e72b38 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 13 Jun 2009 13:01:19 +0000 Subject: [PATCH] make file closing more robust --- Lib/test/test_import.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index d30f905dd9b..dbfadfb7089 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -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]