]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109276: libregrtest only checks saved_test_environment() once (#109278)
authorVictor Stinner <vstinner@python.org>
Tue, 12 Sep 2023 03:01:33 +0000 (05:01 +0200)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2023 03:01:33 +0000 (05:01 +0200)
There is no need to check for environment changes twice.

Lib/test/libregrtest/save_env.py
Lib/test/libregrtest/single.py

index 55c1f7801489b01a8fa72f3f71f1ad139c7045e2..b2cc381344b2efe2120126e259197ff0f149045f 100644 (file)
@@ -36,7 +36,7 @@ class saved_test_environment:
     items is also printed.
     """
 
-    def __init__(self, test_name, verbose=0, quiet=False, *, pgo=False):
+    def __init__(self, test_name, verbose, quiet, *, pgo):
         self.test_name = test_name
         self.verbose = verbose
         self.quiet = quiet
index c26e542cf15ef510a8ac29551c07d35e610675bd..de6056628738bcca874536a1497e8cc0e3a2c80c 100644 (file)
@@ -68,18 +68,14 @@ def regrtest_runner(result: TestResult, test_func, runtests: RunTests) -> None:
     result.stats = stats
 
 
-def save_env(test_name: TestName, runtests: RunTests):
-    return saved_test_environment(test_name, runtests.verbose, runtests.quiet,
-                                  pgo=runtests.pgo)
-
-
 # Storage of uncollectable GC objects (gc.garbage)
 GC_GARBAGE = []
 
 
 def _load_run_test(result: TestResult, runtests: RunTests) -> None:
-    # Load the test function, run the test function.
-    module_name = abs_module_name(result.test_name, runtests.test_dir)
+    # Load the test module and run the tests.
+    test_name = result.test_name
+    module_name = abs_module_name(test_name, runtests.test_dir)
 
     # Remove the module from sys.module to reload it if it was already imported
     sys.modules.pop(module_name, None)
@@ -88,13 +84,13 @@ def _load_run_test(result: TestResult, runtests: RunTests) -> None:
 
     if hasattr(test_mod, "test_main"):
         # https://github.com/python/cpython/issues/89392
-        raise Exception(f"Module {result.test_name} defines test_main() which is no longer supported by regrtest")
+        raise Exception(f"Module {test_name} defines test_main() which "
+                        f"is no longer supported by regrtest")
     def test_func():
         return run_unittest(test_mod)
 
     try:
-        with save_env(result.test_name, runtests):
-            regrtest_runner(result, test_func, runtests)
+        regrtest_runner(result, test_func, runtests)
     finally:
         # First kill any dangling references to open files etc.
         # This can also issue some ResourceWarnings which would otherwise get
@@ -102,11 +98,11 @@ def _load_run_test(result: TestResult, runtests: RunTests) -> None:
         # failures.
         support.gc_collect()
 
-        remove_testfn(result.test_name, runtests.verbose)
+        remove_testfn(test_name, runtests.verbose)
 
     if gc.garbage:
         support.environment_altered = True
-        print_warning(f"{result.test_name} created {len(gc.garbage)} "
+        print_warning(f"{test_name} created {len(gc.garbage)} "
                       f"uncollectable object(s)")
 
         # move the uncollectable objects somewhere,
@@ -119,7 +115,7 @@ def _load_run_test(result: TestResult, runtests: RunTests) -> None:
 
 def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
                              display_failure: bool = True) -> None:
-    # Detect environment changes, handle exceptions.
+    # Handle exceptions, detect environment changes.
 
     # Reset the environment_altered flag to detect if a test altered
     # the environment
@@ -135,7 +131,8 @@ def _runtest_env_changed_exc(result: TestResult, runtests: RunTests,
         clear_caches()
         support.gc_collect()
 
-        with save_env(test_name, runtests):
+        with saved_test_environment(test_name,
+                                    runtests.verbose, quiet, pgo=pgo):
             _load_run_test(result, runtests)
     except support.ResourceDenied as msg:
         if not quiet and not pgo: