]> granicus.if.org Git - python/commitdiff
simplify cleanup of test_replace_parent_in_sys_modules (closes #28862)
authorBenjamin Peterson <benjamin@python.org>
Sun, 4 Dec 2016 07:17:04 +0000 (23:17 -0800)
committerBenjamin Peterson <benjamin@python.org>
Sun, 4 Dec 2016 07:17:04 +0000 (23:17 -0800)
Lib/test/test_import.py

index 432500505e07b13772577d9011f6c7409d9478e5..9a1fb678c6124cffa1426981c7496bd98ede3f40 100644 (file)
@@ -400,20 +400,19 @@ class ImportTests(unittest.TestCase):
     def test_replace_parent_in_sys_modules(self):
         dir_name = os.path.abspath(TESTFN)
         os.mkdir(dir_name)
-        try:
-            pkg_dir = os.path.join(dir_name, 'sa')
-            os.mkdir(pkg_dir)
-            with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file:
-                init_file.write("import v1")
-            with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file:
-                v1_file.write("import sys;"
-                              "sys.modules['sa'] = sys.modules[__name__];"
-                              "import sa")
-            sys.path.insert(0, dir_name)
-            # a segfault means the test failed!
-            import sa
-        finally:
-            rmtree(dir_name)
+        self.addCleanup(rmtree, dir_name)
+        pkg_dir = os.path.join(dir_name, 'sa')
+        os.mkdir(pkg_dir)
+        with open(os.path.join(pkg_dir, '__init__.py'), 'w') as init_file:
+            init_file.write("import v1")
+        with open(os.path.join(pkg_dir, 'v1.py'), 'w') as v1_file:
+            v1_file.write("import sys;"
+                          "sys.modules['sa'] = sys.modules[__name__];"
+                          "import sa")
+        sys.path.insert(0, dir_name)
+        self.addCleanup(sys.path.pop, 0)
+        # a segfault means the test failed!
+        import sa
 
     def test_fromlist_type(self):
         with self.assertRaises(TypeError) as cm: