Skip to content

Commit

Permalink
Copy 'missing_compiler_executable from Python 3.12 and customize it f…
Browse files Browse the repository at this point in the history
…or compatibility with distutils.
  • Loading branch information
jaraco committed Jan 6, 2024
1 parent 16769f8 commit ff32ae0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
32 changes: 32 additions & 0 deletions distutils/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,35 @@
distutils.command.tests package, since command identification is done
by import rather than matching pre-defined names.
"""

def missing_compiler_executable(cmd_names=[]):
"""Check if the compiler components used to build the interpreter exist.
Check for the existence of the compiler executables whose names are listed
in 'cmd_names' or all the compiler executables when 'cmd_names' is empty
and return the first missing executable or None when none is found
missing.
"""
from distutils import ccompiler, sysconfig, spawn
from distutils import errors

compiler = ccompiler.new_compiler()
sysconfig.customize_compiler(compiler)
if compiler.compiler_type == "msvc":
# MSVC has no executables, so check whether initialization succeeds
try:
compiler.initialize()
except errors.PlatformError:
return "msvc"
for name in compiler.executables:
if cmd_names and name not in cmd_names:
continue
cmd = getattr(compiler, name)
if cmd_names:
assert cmd is not None, \
"the '%s' executable is not configured" % name
elif not cmd:
continue
if spawn.find_executable(cmd[0]) is None:
return cmd[0]
4 changes: 1 addition & 3 deletions distutils/tests/test_build_clib.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Tests for distutils.command.build_clib."""
import os

from test.support import missing_compiler_executable

import pytest

from distutils.command.build_clib import build_clib
from distutils.errors import DistutilsSetupError
from distutils.tests import support
from distutils.tests import support, missing_compiler_executable


class TestBuildCLib(support.TempdirManager):
Expand Down
5 changes: 3 additions & 2 deletions distutils/tests/test_build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from distutils.core import Distribution
from distutils.command.build_ext import build_ext
from distutils import sysconfig
from distutils.tests import missing_compiler_executable
from distutils.tests.support import (
TempdirManager,
copy_xxmodule_c,
Expand Down Expand Up @@ -89,7 +90,7 @@ def build_ext(self, *args, **kwargs):
return build_ext(*args, **kwargs)

def test_build_ext(self):
cmd = support.missing_compiler_executable()
missing_compiler_executable()
copy_xxmodule_c(self.tmp_dir)
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
xx_ext = Extension('xx', [xx_c])
Expand Down Expand Up @@ -359,7 +360,7 @@ def test_compiler_option(self):
assert cmd.compiler == 'unix'

def test_get_outputs(self):
cmd = support.missing_compiler_executable()
missing_compiler_executable()
tmp_dir = self.mkdtemp()
c_file = os.path.join(tmp_dir, 'foo.c')
self.write_file(c_file, 'void PyInit_foo(void) {}\n')
Expand Down
3 changes: 1 addition & 2 deletions distutils/tests/test_config_cmd.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Tests for distutils.command.config."""
import os
import sys
from test.support import missing_compiler_executable

import pytest

from distutils.command.config import dump_file, config
from distutils.tests import support
from distutils.tests import support, missing_compiler_executable
from distutils._log import log


Expand Down
5 changes: 2 additions & 3 deletions distutils/tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from distutils.errors import DistutilsOptionError
from distutils.extension import Extension

from distutils.tests import support
from test import support as test_support
from distutils.tests import support, missing_compiler_executable


def _make_ext_name(modname):
Expand Down Expand Up @@ -213,7 +212,7 @@ def test_record(self):
assert found == expected

def test_record_extensions(self):
cmd = test_support.missing_compiler_executable()
cmd = missing_compiler_executable()
if cmd is not None:
pytest.skip('The %r command is not found' % cmd)
install_dir = self.mkdtemp()
Expand Down

0 comments on commit ff32ae0

Please sign in to comment.