Skip to content

Commit 312de66

Browse files
miss-islingtonencukouambvemmatyping
authored
[3.9] gh-135374: Adjust test for setuptools' replacement of distutils (GH-138796) (GH-139304)
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 987af36) Co-authored-by: Petr Viktorin <[email protected]> Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Emma Smith <[email protected]>
1 parent 598165e commit 312de66

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
@@ -61,6 +61,12 @@ def check_all(self, modname):
6161
self.assertEqual(keys, all_set, "in module {}".format(modname))
6262

6363
def walk_modules(self, basedir, modpath):
64+
if modpath == 'distutils.':
65+
# gh-135374: when setuptools is installed, it now replaces
66+
# 'distutils' with its own version.
67+
# In a security-fix only branch of CPython,
68+
# skip the __all__ test rather than deal with the fallout.
69+
return
6470
for fn in sorted(os.listdir(basedir)):
6571
path = os.path.join(basedir, fn)
6672
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
import sys
55
from test import support
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 support.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
@@ -44,13 +71,6 @@ def test_untested_modules_can_be_imported(self):
4471
import distutils.command.sdist
4572
import distutils.command.upload
4673

47-
import html.entities
48-
49-
try:
50-
import tty # Not available on Windows
51-
except ImportError:
52-
if support.verbose:
53-
print("skipping tty")
5474

5575

5676
if __name__ == "__main__":

0 commit comments

Comments
 (0)