From 86a50bf5f9d0f2863013ee7dab434fbacbd56e63 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 11 Sep 2025 16:37:49 +0200 Subject: [PATCH 1/2] gh-135374: Adjust test for setuptools' replacement of distutils ensurepip installs a bundled copy of distutils, which overrides the stdlib module. This affects several tests. This commit: - skips distutils in test___all__, as we're unlikely to break `__all__` in a security-fix-only branch (and if we do it's not much of a a big deal) - skips importability tests of distutils submodules if the setuptools hack is detected --- Lib/test/test___all__.py | 6 ++++++ Lib/test/test_sundry.py | 34 +++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py index 1ec83cb0b14401..1757c934c78604 100644 --- a/Lib/test/test___all__.py +++ b/Lib/test/test___all__.py @@ -80,6 +80,12 @@ def check_all(self, modname): self.assertEqual(keys, all_set, "in module {}".format(modname)) def walk_modules(self, basedir, modpath): + if modpath == 'distutils.': + # gh-135374: when setuptools ins installed, it now replaces + # 'distutils' with its own version. + # In a security-fix only branch of CPython, + # skip the __all__ test rather than deal with the fallout. + return for fn in sorted(os.listdir(basedir)): path = os.path.join(basedir, fn) if os.path.isdir(path): diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py index de2e7305ccce24..b1782947c07ecb 100644 --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -4,6 +4,7 @@ from test.support import import_helper from test.support import warnings_helper import unittest +import sys class TestUntestedModules(unittest.TestCase): def test_untested_modules_can_be_imported(self): @@ -18,6 +19,32 @@ def test_untested_modules_can_be_imported(self): self.fail('{} has tests even though test_sundry claims ' 'otherwise'.format(name)) + import html.entities + + try: + import tty # Not available on Windows + except ImportError: + if support.verbose: + print("skipping tty") + + def test_distutils_modules(self): + with warnings_helper.check_warnings(quiet=True): + + path_copy = sys.path[:] + import distutils + if '_distutils_hack' in sys.modules: + # gh-135374: when 'setuptools' is installed, it now replaces + # 'distutils' with its own version. + # This imports '_distutils_hack' and modifies sys.path. + # The setuptols version of distutils also does not include some + # of the modules tested here. + + # Undo the path modifications and skip the test. + + sys.path[:] = path_copy + raise unittest.SkipTest( + 'setuptools has replaced distutils with its own version') + import distutils.bcppcompiler import distutils.ccompiler import distutils.cygwinccompiler @@ -41,13 +68,6 @@ def test_untested_modules_can_be_imported(self): import distutils.command.sdist import distutils.command.upload - import html.entities - - try: - import tty # Not available on Windows - except ImportError: - if support.verbose: - print("skipping tty") if __name__ == "__main__": From abe584b178ecd01c5400a3d70886a772b60c0149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 15 Sep 2025 15:47:08 +0200 Subject: [PATCH 2/2] Update Lib/test/test___all__.py Co-authored-by: Emma Smith --- Lib/test/test___all__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test___all__.py b/Lib/test/test___all__.py index 1757c934c78604..c3657b43b8312b 100644 --- a/Lib/test/test___all__.py +++ b/Lib/test/test___all__.py @@ -81,7 +81,7 @@ def check_all(self, modname): def walk_modules(self, basedir, modpath): if modpath == 'distutils.': - # gh-135374: when setuptools ins installed, it now replaces + # gh-135374: when setuptools is installed, it now replaces # 'distutils' with its own version. # In a security-fix only branch of CPython, # skip the __all__ test rather than deal with the fallout.