]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] gh-135374: Adjust test for setuptools' replacement of distutils (GH-138796...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Oct 2025 12:03:43 +0000 (14:03 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 12:03:43 +0000 (14:03 +0200)
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
(cherry picked from commit 987af36a717793e97aad57f7da36a0677edfbdbd)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Co-authored-by: Emma Smith <emma@emmatyping.dev>
Lib/test/test___all__.py
Lib/test/test_sundry.py

index 7e1f80f25125656e75f67c941e2f70ad5e46c410..756f53444830b63288118281881863dfce3509ae 100644 (file)
@@ -61,6 +61,12 @@ class AllTest(unittest.TestCase):
                 self.assertEqual(keys, all_set, "in module {}".format(modname))
 
     def walk_modules(self, basedir, modpath):
+        if modpath == 'distutils.':
+            # 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.
+            return
         for fn in sorted(os.listdir(basedir)):
             path = os.path.join(basedir, fn)
             if os.path.isdir(path):
index 2accad1aeebd4f9734835c02c7ee1d3faaf0bf1e..ed0ec6cd1c664a84de06c9f3b2036729cc21078a 100644 (file)
@@ -4,6 +4,7 @@ import platform
 import sys
 from test import support
 import unittest
+import sys
 
 class TestUntestedModules(unittest.TestCase):
     def test_untested_modules_can_be_imported(self):
@@ -18,6 +19,32 @@ class TestUntestedModules(unittest.TestCase):
                     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 support.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
@@ -44,13 +71,6 @@ class TestUntestedModules(unittest.TestCase):
             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__":