From 4ad84cb8294cacb6c430d61bdd40137ffcd0c6f7 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Fri, 21 Aug 2026 19:30:38 +0100 Subject: [PATCH] Refuse to run `make doctest` under a mismatched Python --- Doc/Makefile | 8 ++++++++ Doc/make.bat | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Doc/Makefile b/Doc/Makefile index d77ece1e681bcf..6674534f194035 100644 --- a/Doc/Makefile +++ b/Doc/Makefile @@ -132,6 +132,14 @@ coverage: build .PHONY: doctest doctest: BUILDER = doctest doctest: + @venv_version=$$($(VENVDIR)/bin/python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])'); \ + tree_version=$$($(PYTHON) tools/extensions/patchlevel.py --short); \ + if [ "$$venv_version" != "$$tree_version" ]; then \ + echo "The venv runs Python $$venv_version, but this source tree is Python $$tree_version."; \ + echo "The doctests are executed by the venv interpreter, so they would test"; \ + echo "Python $$venv_version rather than the code documented here."; \ + exit 1; \ + fi @$(MAKE) build BUILDER=$(BUILDER) || { \ echo "Testing of doctests in the sources finished, look at the" \ "results in build/doctest/output.txt"; \ diff --git a/Doc/make.bat b/Doc/make.bat index 99f0d5c44f0098..132b21c5b2fce8 100644 --- a/Doc/make.bat +++ b/Doc/make.bat @@ -9,6 +9,19 @@ call ..\PCbuild\find_python.bat %PYTHON% if not defined PYTHON set PYTHON=py +if "%1" NEQ "doctest" goto :skipversioncheck +for /f "usebackq" %%v in (`%PYTHON% -c "import sys; print(sys.version_info[0], sys.version_info[1], sep='.')"`) do set PYVERSION=%%v +for /f "usebackq" %%v in (`%PYTHON% tools/extensions/patchlevel.py --short`) do set TREEVERSION=%%v +if "%PYVERSION%" NEQ "%TREEVERSION%" ( + echo. + echo.%PYTHON% is Python %PYVERSION%, but this source tree is Python %TREEVERSION%. + echo.The doctests are executed by that interpreter, so they would test + echo.Python %PYVERSION% rather than the code documented here. + popd + exit /B 1 +) +:skipversioncheck + if not defined SPHINXBUILD ( %PYTHON% -c "import sphinx" > nul 2> nul if errorlevel 1 (