Skip to content

Commit 987af36

Browse files
encukouambvemmatyping
authored
[3.11] pythongh-135374: Adjust test for setuptools' replacement of distutils (pythonGH-138796)
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 Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Emma Smith <[email protected]>
1 parent 262aa73 commit 987af36

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

Lib/test/test___all__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def check_all(self, modname):
8080
self.assertEqual(keys, all_set, "in module {}".format(modname))
8181

8282
def walk_modules(self, basedir, modpath):
83+
if modpath == 'distutils.':
84+
# gh-135374: when setuptools is installed, it now replaces
85+
# 'distutils' with its own version.
86+
# In a security-fix only branch of CPython,
87+
# skip the __all__ test rather than deal with the fallout.
88+
return
8389
for fn in sorted(os.listdir(basedir)):
8490
path = os.path.join(basedir, fn)
8591
if os.path.isdir(path):

Lib/test/test_sundry.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from test.support import import_helper
55
from test.support import warnings_helper
66
import unittest
7+
import sys
78

89
class TestUntestedModules(unittest.TestCase):
910
def test_untested_modules_can_be_imported(self):
@@ -18,6 +19,32 @@ def test_untested_modules_can_be_imported(self):
1819
self.fail('{} has tests even though test_sundry claims '
1920
'otherwise'.format(name))
2021

22+
import html.entities
23+
24+
try:
25+
import tty # Not available on Windows
26+
except ImportError:
27+
if support.verbose:
28+
print("skipping tty")
29+
30+
def test_distutils_modules(self):
31+
with warnings_helper.check_warnings(quiet=True):
32+
33+
path_copy = sys.path[:]
34+
import distutils
35+
if '_distutils_hack' in sys.modules:
36+
# gh-135374: when 'setuptools' is installed, it now replaces
37+
# 'distutils' with its own version.
38+
# This imports '_distutils_hack' and modifies sys.path.
39+
# The setuptols version of distutils also does not include some
40+
# of the modules tested here.
41+
42+
# Undo the path modifications and skip the test.
43+
44+
sys.path[:] = path_copy
45+
raise unittest.SkipTest(
46+
'setuptools has replaced distutils with its own version')
47+
2148
import distutils.bcppcompiler
2249
import distutils.ccompiler
2350
import distutils.cygwinccompiler
@@ -41,13 +68,6 @@ def test_untested_modules_can_be_imported(self):
4168
import distutils.command.sdist
4269
import distutils.command.upload
4370

44-
import html.entities
45-
46-
try:
47-
import tty # Not available on Windows
48-
except ImportError:
49-
if support.verbose:
50-
print("skipping tty")
5171

5272

5373
if __name__ == "__main__":

0 commit comments

Comments
 (0)