]> granicus.if.org Git - python/commitdiff
Make test_import a little bit more robust for cleaning up after itself in the
authorBrett Cannon <bcannon@gmail.com>
Sat, 3 Jul 2010 01:32:48 +0000 (01:32 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 3 Jul 2010 01:32:48 +0000 (01:32 +0000)
face of a failure.

Lib/test/test_import.py

index 512759d318d3811a0ecd44205e0264b0fb5a0e5c..929caa66e68910b8d180b65a6c38ee73ebd5fd20 100644 (file)
@@ -147,22 +147,24 @@ class ImportTests(unittest.TestCase):
         filename = module + '.py'
 
         # Create a file with a list of 65000 elements.
-        with open(filename, 'w+') as f:
+        with open(filename, 'w') as f:
             f.write('d = [\n')
             for i in range(65000):
                 f.write('"",\n')
             f.write(']')
 
-        # Compile & remove .py file; we only need .pyc (or .pyo).
-        # Bytecode must be relocated from the PEP 3147 bytecode-only location.
-        py_compile.compile(filename)
-        unlink(filename)
-        make_legacy_pyc(filename)
+        try:
+            # Compile & remove .py file; we only need .pyc (or .pyo).
+            # Bytecode must be relocated from the PEP 3147 bytecode-only location.
+            py_compile.compile(filename)
+        finally:
+            unlink(filename)
 
         # Need to be able to load from current dir.
         sys.path.append('')
 
         try:
+            make_legacy_pyc(filename)
             # This used to crash.
             exec('import ' + module)
         finally: