]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111881: Import doctest lazily in libregrtest (#111884)
authorVictor Stinner <vstinner@python.org>
Thu, 9 Nov 2023 15:00:10 +0000 (16:00 +0100)
committerGitHub <noreply@github.com>
Thu, 9 Nov 2023 15:00:10 +0000 (15:00 +0000)
In most cases, doctest is not needed. So don't always import it at
startup. The change reduces the number of modules already
imported when a test is run.

Lib/test/libregrtest/single.py

index ad75ef54a8c3f8a892b7abb341b278fa4ca35f12..5c7bc7d40fb3945beebcb3d3580a86e4d2bd9547 100644 (file)
@@ -1,4 +1,3 @@
-import doctest
 import faulthandler
 import gc
 import importlib
@@ -99,14 +98,18 @@ def regrtest_runner(result: TestResult, test_func, runtests: RunTests) -> None:
             stats = test_result
         case unittest.TestResult():
             stats = TestStats.from_unittest(test_result)
-        case doctest.TestResults():
-            stats = TestStats.from_doctest(test_result)
         case None:
             print_warning(f"{result.test_name} test runner returned None: {test_func}")
             stats = None
         case _:
-            print_warning(f"Unknown test result type: {type(test_result)}")
-            stats = None
+            # Don't import doctest at top level since only few tests return
+            # a doctest.TestResult instance.
+            import doctest
+            if isinstance(test_result, doctest.TestResults):
+                stats = TestStats.from_doctest(test_result)
+            else:
+                print_warning(f"Unknown test result type: {type(test_result)}")
+                stats = None
 
     result.stats = stats