summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-11-09 15:40:10 (GMT)
committerGitHub <noreply@github.com>2023-11-09 15:40:10 (GMT)
commit5089faf954d2c6c07e1acfd5d270c331f7c6acdc (patch)
tree7cb5d58ccdda361449dbbffd3b0c74c6a7629714
parente983ca859de279682631dbb13b959f14a7d89a7b (diff)
downloadcpython-5089faf954d2c6c07e1acfd5d270c331f7c6acdc.zip
cpython-5089faf954d2c6c07e1acfd5d270c331f7c6acdc.tar.gz
cpython-5089faf954d2c6c07e1acfd5d270c331f7c6acdc.tar.bz2
[3.12] gh-111881: Import doctest lazily in libregrtest (GH-111884) (#111893)
gh-111881: Import doctest lazily in libregrtest (GH-111884) 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. (cherry picked from commit 6f09f69b7f85962f66d10637c3325bbb2b2d9853) Co-authored-by: Victor Stinner <vstinner@python.org>
-rw-r--r--Lib/test/libregrtest/single.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/Lib/test/libregrtest/single.py b/Lib/test/libregrtest/single.py
index ad75ef5..5c7bc7d 100644
--- a/Lib/test/libregrtest/single.py
+++ b/Lib/test/libregrtest/single.py
@@ -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