make file closing more robust

This commit is contained in:
Benjamin Peterson 2009-06-13 13:01:19 +00:00
parent 94f5583777
commit 16a1f6343f

View 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]