Skip to content

Commit dfc13e0

Browse files
author
Xavier de Gaye
committed
Issue 26931: Skip the test_distutils tests using a compiler executable
that is not found
1 parent 00305ad commit dfc13e0

6 files changed

Lines changed: 46 additions & 25 deletions

File tree

‎Lib/distutils/tests/test_build_clib.py‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import sys
55

6-
from test.support import run_unittest
6+
from test.support import run_unittest, missing_compiler_executable
77

88
from distutils.command.build_clib import build_clib
99
from distutils.errors import DistutilsSetupError
@@ -116,19 +116,11 @@ def test_run(self):
116116
cmd.build_temp = build_temp
117117
cmd.build_clib = build_temp
118118

119-
# before we run the command, we want to make sure
120-
# all commands are present on the system
121-
# by creating a compiler and checking its executables
122-
from distutils.ccompiler import new_compiler
123-
from distutils.sysconfig import customize_compiler
124-
125-
compiler = new_compiler()
126-
customize_compiler(compiler)
127-
for ccmd in compiler.executables.values():
128-
if ccmd is None:
129-
continue
130-
if find_executable(ccmd[0]) is None:
131-
self.skipTest('The %r command is not found' % ccmd[0])
119+
# Before we run the command, we want to make sure
120+
# all commands are present on the system.
121+
ccmd = missing_compiler_executable()
122+
if ccmd is not None:
123+
self.skipTest('The %r command is not found' % ccmd)
132124

133125
# this should work
134126
cmd.run()

‎Lib/distutils/tests/test_build_ext.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def build_ext(self, *args, **kwargs):
4141
return build_ext(*args, **kwargs)
4242

4343
def test_build_ext(self):
44+
cmd = support.missing_compiler_executable()
45+
if cmd is not None:
46+
self.skipTest('The %r command is not found' % cmd)
4447
global ALREADY_TESTED
4548
copy_xxmodule_c(self.tmp_dir)
4649
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
@@ -295,6 +298,9 @@ def test_compiler_option(self):
295298
self.assertEqual(cmd.compiler, 'unix')
296299

297300
def test_get_outputs(self):
301+
cmd = support.missing_compiler_executable()
302+
if cmd is not None:
303+
self.skipTest('The %r command is not found' % cmd)
298304
tmp_dir = self.mkdtemp()
299305
c_file = os.path.join(tmp_dir, 'foo.c')
300306
self.write_file(c_file, 'void PyInit_foo(void) {}\n')

‎Lib/distutils/tests/test_config_cmd.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
import os
44
import sys
5-
from test.support import run_unittest
5+
from test.support import run_unittest, missing_compiler_executable
66

77
from distutils.command.config import dump_file, config
88
from distutils.tests import support
@@ -39,6 +39,9 @@ def test_dump_file(self):
3939

4040
@unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
4141
def test_search_cpp(self):
42+
cmd = missing_compiler_executable(['preprocessor'])
43+
if cmd is not None:
44+
self.skipTest('The %r command is not found' % cmd)
4245
pkg_dir, dist = self.create_dist()
4346
cmd = config(dist)
4447

‎Lib/distutils/tests/test_install.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from distutils.extension import Extension
1818

1919
from distutils.tests import support
20+
from test import support as test_support
2021

2122

2223
def _make_ext_name(modname):
@@ -196,6 +197,9 @@ def test_record(self):
196197
self.assertEqual(found, expected)
197198

198199
def test_record_extensions(self):
200+
cmd = test_support.missing_compiler_executable()
201+
if cmd is not None:
202+
self.skipTest('The %r command is not found' % cmd)
199203
install_dir = self.mkdtemp()
200204
project_dir, dist = self.create_dist(ext_modules=[
201205
Extension('xx', ['xxmodule.c'])])

‎Lib/distutils/tests/test_sysconfig.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ def test_get_python_lib(self):
3939
self.assertNotEqual(sysconfig.get_python_lib(),
4040
sysconfig.get_python_lib(prefix=TESTFN))
4141

42-
def test_get_python_inc(self):
43-
inc_dir = sysconfig.get_python_inc()
44-
# This is not much of a test. We make sure Python.h exists
45-
# in the directory returned by get_python_inc() but we don't know
46-
# it is the correct file.
47-
self.assertTrue(os.path.isdir(inc_dir), inc_dir)
48-
python_h = os.path.join(inc_dir, "Python.h")
49-
self.assertTrue(os.path.isfile(python_h), python_h)
50-
5142
def test_get_config_vars(self):
5243
cvars = sysconfig.get_config_vars()
5344
self.assertIsInstance(cvars, dict)

‎Lib/test/support/__init__.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"check_warnings", "check_no_resource_warning", "EnvironmentVarGuard",
106106
"run_with_locale", "swap_item",
107107
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
108-
"run_with_tz", "PGO",
108+
"run_with_tz", "PGO", "missing_compiler_executable",
109109
]
110110

111111
class Error(Exception):
@@ -2491,3 +2491,28 @@ def __del__(self):
24912491
# The sequence should be deallocated just after the end of iterating
24922492
gc_collect()
24932493
test.assertTrue(done)
2494+
2495+
2496+
def missing_compiler_executable(cmd_names=[]):
2497+
"""Check if the compiler components used to build the interpreter exist.
2498+
2499+
Check for the existence of the compiler executables whose names are listed
2500+
in 'cmd_names' or all the compiler executables when 'cmd_names' is empty
2501+
and return the first missing executable or None when none is found
2502+
missing.
2503+
2504+
"""
2505+
from distutils import ccompiler, sysconfig, spawn
2506+
compiler = ccompiler.new_compiler()
2507+
sysconfig.customize_compiler(compiler)
2508+
for name in compiler.executables:
2509+
if cmd_names and name not in cmd_names:
2510+
continue
2511+
cmd = getattr(compiler, name)
2512+
if cmd_names:
2513+
assert cmd is not None, \
2514+
"the '%s' executable is not configured" % name
2515+
elif cmd is None:
2516+
continue
2517+
if spawn.find_executable(cmd[0]) is None:
2518+
return cmd[0]

0 commit comments

Comments
 (0)