From 4e77645986fd27beaf25779426ae8580009fae33 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 12 Sep 2023 05:01:33 +0200 Subject: [PATCH] gh-109276: libregrtest only checks saved_test_environment() once (#109278) There is no need to check for environment changes twice. --- Lib/test/libregrtest/save_env.py | 2 +- Lib/test/libregrtest/single.py | 25 +++++++++++-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Lib/test/libregrtest/save_env.py b/Lib/test/libregrtest/save_env.py index 55c1f780148..b2cc381344b 100644 --- a/Lib/test/libregrtest/save_env.py +++ b/Lib/test/libregrtest/save_env.py @@ -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 diff --git a/Lib/test/libregrtest/single.py b/Lib/test/libregrtest/single.py index c26e542cf15..de605662873 100644 --- a/Lib/test/libregrtest/single.py +++ b/Lib/test/libregrtest/single.py @@ -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: