[Python-checkins] cpython (2.7): simplify cleanup of test_replace_parent_in_sys_modules (closes #28862)

benjamin.peterson python-checkins at python.org
Sun Dec 4 02:17:10 EST 2016


https://hg.python.org/cpython/rev/6e9939e1f933
changeset:   105447:6e9939e1f933
branch:      2.7
parent:      105444:2df9604bfe01
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Dec 03 23:17:04 2016 -0800
summary:
  simplify cleanup of test_replace_parent_in_sys_modules (closes #28862)

files:
  Lib/test/test_import.py |  27 +++++++++++++--------------
  1 files changed, 13 insertions(+), 14 deletions(-)


diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -400,20 +400,19 @@
     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:

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list