bpo-41718: Reduce libregrtest runtest imports (GH-24980)
Move clear_caches() from libregrtest.refleak to libregrtest.utils to
avoid importing libregrtest.refleak when it's not needed.
clear_caches() now only calls re.purge() if 're' is in sys.modules.
diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py
index b4ef9fb..470d7dd 100644
--- a/Lib/test/libregrtest/runtest.py
+++ b/Lib/test/libregrtest/runtest.py
@@ -13,7 +13,7 @@
from test import support
from test.support import import_helper
from test.support import os_helper
-from test.libregrtest.refleak import dash_R, clear_caches
+from test.libregrtest.utils import clear_caches
from test.libregrtest.save_env import saved_test_environment
from test.libregrtest.utils import format_duration, print_warning
@@ -226,6 +226,9 @@ def _runtest_inner2(ns, test_name):
the_module = importlib.import_module(abstest)
+ if ns.huntrleaks:
+ from test.libregrtest.refleak import dash_R
+
# If the test has a test_main, that will run the appropriate
# tests. If not, use normal unittest test loading.
test_runner = getattr(the_module, "test_main", None)