]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Restore original sys.path when running TTK tests
authorNick Coghlan <ncoghlan@gmail.com>
Sat, 17 Oct 2009 14:40:54 +0000 (14:40 +0000)
committerNick Coghlan <ncoghlan@gmail.com>
Sat, 17 Oct 2009 14:40:54 +0000 (14:40 +0000)
Lib/test/test_support.py
Lib/test/test_ttk_guionly.py
Lib/test/test_ttk_textonly.py

index 432fa5293479749d03c1da4e963d885c78dc57a9..45b1b0187f8beaf0149cbeb34156c02c73ca6679 100644 (file)
@@ -578,6 +578,31 @@ class EnvironmentVarGuard(UserDict.DictMixin):
                 self._environ[k] = v
 
 
+class DirsOnSysPath(object):
+    """Context manager to temporarily add directories to sys.path.
+
+    This makes a copy of sys.path, appends any directories given
+    as positional arguments, then reverts sys.path to the copied
+    settings when the context ends.
+
+    Note that *all* sys.path modifications in the body of the
+    context manager, including replacement of the object,
+    will be reverted at the end of the block.
+    """
+
+    def __init__(self, *paths):
+        self.original_value = sys.path[:]
+        self.original_object = sys.path
+        sys.path.extend(paths)
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *ignore_exc):
+        sys.path = self.original_object
+        sys.path[:] = self.original_value
+
+
 class TransientResource(object):
 
     """Raise ResourceDenied if an exception is raised while the context manager
index 379ebaefa3ffceb72904a6b61e778286691c63ae..421f01421c6ac650600b17de53c13cbc82b842a4 100644 (file)
@@ -18,10 +18,9 @@ except TclError, msg:
 this_dir = os.path.dirname(os.path.abspath(__file__))
 lib_tk_test = os.path.abspath(os.path.join(this_dir, os.path.pardir,
     'lib-tk', 'test'))
-if lib_tk_test not in sys.path:
-    sys.path.append(lib_tk_test)
 
-import runtktests
+with test_support.DirsOnSysPath(lib_tk_test):
+    import runtktests
 
 def test_main(enable_gui=False):
     if enable_gui:
@@ -30,7 +29,8 @@ def test_main(enable_gui=False):
         elif 'gui' not in test_support.use_resources:
             test_support.use_resources.append('gui')
 
-    test_support.run_unittest(
+    with test_support.DirsOnSysPath(lib_tk_test):
+        test_support.run_unittest(
             *runtktests.get_tests(text=False, packages=['test_ttk']))
 
 if __name__ == '__main__':
index dde1b3551dce4d9baf27e7fb0ff5242ef14de90d..e0cb2f5fe761dcd72c04f78f5131d90cf06d85cb 100644 (file)
@@ -7,13 +7,13 @@ test_support.import_module('_tkinter')
 
 this_dir = os.path.dirname(os.path.abspath(__file__))
 lib_tk_test = os.path.abspath(os.path.join(this_dir, '..', 'lib-tk', 'test'))
-if lib_tk_test not in sys.path:
-    sys.path.append(lib_tk_test)
 
-import runtktests
+with test_support.DirsOnSysPath(lib_tk_test):
+    import runtktests
 
 def test_main():
-    test_support.run_unittest(
+    with test_support.DirsOnSysPath(lib_tk_test):
+        test_support.run_unittest(
             *runtktests.get_tests(gui=False, packages=['test_ttk']))
 
 if __name__ == '__main__':