From 01807e171404387bbfba46ba856c58c2b768de8c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 2 Sep 2024 18:07:58 -0400 Subject: [PATCH] Rename classes and add compatibility shims. --- distutils/_msvccompiler.py | 3 +++ distutils/ccompiler.py | 21 +++++++++++++++++++ distutils/compilers/C/base.py | 26 +++++++++++------------ distutils/compilers/C/borland.py | 15 +++++++------- distutils/compilers/C/cygwin.py | 14 ++++++------- distutils/compilers/C/msvc.py | 29 +++++++++++++------------- distutils/compilers/C/unix.py | 19 +++++++++-------- distutils/compilers/C/zos.py | 8 +++---- distutils/cygwinccompiler.py | 31 ++++++++++++++++++++++++++++ distutils/tests/test_msvccompiler.py | 5 +++-- distutils/unixccompiler.py | 3 +++ distutils/zosccompiler.py | 3 +++ 12 files changed, 121 insertions(+), 56 deletions(-) create mode 100644 distutils/_msvccompiler.py create mode 100644 distutils/ccompiler.py create mode 100644 distutils/cygwinccompiler.py create mode 100644 distutils/unixccompiler.py create mode 100644 distutils/zosccompiler.py diff --git a/distutils/_msvccompiler.py b/distutils/_msvccompiler.py new file mode 100644 index 00000000..34d9735b --- /dev/null +++ b/distutils/_msvccompiler.py @@ -0,0 +1,3 @@ +from .compilers.C import msvc + +MSVCCompiler = msvc.Compiler diff --git a/distutils/ccompiler.py b/distutils/ccompiler.py new file mode 100644 index 00000000..1f788c50 --- /dev/null +++ b/distutils/ccompiler.py @@ -0,0 +1,21 @@ +from .compilers.C import base +from .compilers.C.base import ( + CompileError, + gen_lib_options, + gen_preprocess_options, + get_default_compiler, + new_compiler, + show_compilers, +) + +__all__ = [ + 'CompileError', + 'gen_lib_options', + 'gen_preprocess_options', + 'get_default_compiler', + 'new_compiler', + 'show_compilers', +] + + +CCompiler = base.Compiler diff --git a/distutils/compilers/C/base.py b/distutils/compilers/C/base.py index bc4743bc..0173649f 100644 --- a/distutils/compilers/C/base.py +++ b/distutils/compilers/C/base.py @@ -1,6 +1,6 @@ """distutils.ccompiler -Contains CCompiler, an abstract base class that defines the interface +Contains Compiler, an abstract base class that defines the interface for the Distutils compiler abstraction model.""" import os @@ -9,23 +9,23 @@ import types import warnings -from ._itertools import always_iterable -from ._log import log -from ._modified import newer_group -from .dir_util import mkpath -from .errors import ( +from ..._itertools import always_iterable +from ..._log import log +from ..._modified import newer_group +from ...dir_util import mkpath +from ...errors import ( CompileError, DistutilsModuleError, DistutilsPlatformError, LinkError, UnknownFileError, ) -from .file_util import move_file -from .spawn import spawn -from .util import execute, is_mingw, split_quoted +from ...file_util import move_file +from ...spawn import spawn +from ...util import execute, is_mingw, split_quoted -class CCompiler: +class Compiler: """Abstract base class to define the interface that must be implemented by real compiler classes. Also has some utility methods used by several compiler classes. @@ -724,7 +724,7 @@ def link_shared_lib( target_lang=None, ): self.link( - CCompiler.SHARED_LIBRARY, + Compiler.SHARED_LIBRARY, objects, self.library_filename(output_libname, lib_type='shared'), output_dir, @@ -755,7 +755,7 @@ def link_shared_object( target_lang=None, ): self.link( - CCompiler.SHARED_OBJECT, + Compiler.SHARED_OBJECT, objects, output_filename, output_dir, @@ -784,7 +784,7 @@ def link_executable( target_lang=None, ): self.link( - CCompiler.EXECUTABLE, + Compiler.EXECUTABLE, objects, self.executable_filename(output_progname), output_dir, diff --git a/distutils/compilers/C/borland.py b/distutils/compilers/C/borland.py index 9157b433..ab29e14d 100644 --- a/distutils/compilers/C/borland.py +++ b/distutils/compilers/C/borland.py @@ -14,17 +14,18 @@ import os import warnings -from ._log import log -from ._modified import newer -from .ccompiler import CCompiler, gen_preprocess_options -from .errors import ( +from ..._log import log +from ..._modified import newer +from ...errors import ( CompileError, DistutilsExecError, LibError, LinkError, UnknownFileError, ) -from .file_util import write_file +from ...file_util import write_file +from . import base +from .base import gen_preprocess_options warnings.warn( "bcppcompiler is deprecated and slated to be removed " @@ -34,7 +35,7 @@ ) -class BCPPCompiler(CCompiler): +class BCPPCompiler(base.Compiler): """Concrete class that implements an interface to the Borland C/C++ compiler, as defined by the CCompiler abstract class. """ @@ -214,7 +215,7 @@ def link( # noqa: C901 if self._need_link(objects, output_filename): # Figure out linker args based on type of target. - if target_desc == CCompiler.EXECUTABLE: + if target_desc == globals()['base'].Compiler.EXECUTABLE: startup_obj = 'c0w32' if debug: ld_args = self.ldflags_exe_debug[:] diff --git a/distutils/compilers/C/cygwin.py b/distutils/compilers/C/cygwin.py index 18b1b355..a23171ab 100644 --- a/distutils/compilers/C/cygwin.py +++ b/distutils/compilers/C/cygwin.py @@ -14,15 +14,15 @@ import warnings from subprocess import check_output -from .errors import ( +from ...errors import ( CCompilerError, CompileError, DistutilsExecError, DistutilsPlatformError, ) -from .file_util import write_file -from .unixccompiler import UnixCCompiler -from .version import LooseVersion, suppress_known_deprecation +from ...file_util import write_file +from ...version import LooseVersion, suppress_known_deprecation +from . import unix def get_msvcr(): @@ -36,7 +36,7 @@ def get_msvcr(): ) -class CygwinCCompiler(UnixCCompiler): +class Compiler(unix.Compiler): """Handles the Cygwin port of the GNU C compiler to Windows.""" compiler_type = 'cygwin' @@ -192,7 +192,7 @@ def link( if not debug: extra_preargs.append("-s") - UnixCCompiler.link( + super().link( self, target_desc, objects, @@ -235,7 +235,7 @@ def out_extensions(self): # the same as cygwin plus some additional parameters -class Mingw32CCompiler(CygwinCCompiler): +class MinGW32Compiler(Compiler): """Handles the Mingw32 port of the GNU C compiler to Windows.""" compiler_type = 'mingw32' diff --git a/distutils/compilers/C/msvc.py b/distutils/compilers/C/msvc.py index 03653929..f0c73a26 100644 --- a/distutils/compilers/C/msvc.py +++ b/distutils/compilers/C/msvc.py @@ -23,16 +23,17 @@ from itertools import count -from ._log import log -from .ccompiler import CCompiler, gen_lib_options -from .errors import ( +from ..._log import log +from ...errors import ( CompileError, DistutilsExecError, DistutilsPlatformError, LibError, LinkError, ) -from .util import get_host_platform, get_platform +from ...util import get_host_platform, get_platform +from . import base +from .base import gen_lib_options def _find_vc2015(): @@ -226,7 +227,7 @@ def _get_vcvars_spec(host_platform, platform): return vc_hp if vc_hp == vc_plat else f'{vc_hp}_{vc_plat}' -class MSVCCompiler(CCompiler): +class Compiler(base.Compiler): """Concrete class that implements an interface to Microsoft Visual C++, as defined by the CCompiler abstract class.""" @@ -339,15 +340,15 @@ def initialize(self, plat_name=None): self.ldflags_static_debug = [*ldflags_debug] self._ldflags = { - (CCompiler.EXECUTABLE, None): self.ldflags_exe, - (CCompiler.EXECUTABLE, False): self.ldflags_exe, - (CCompiler.EXECUTABLE, True): self.ldflags_exe_debug, - (CCompiler.SHARED_OBJECT, None): self.ldflags_shared, - (CCompiler.SHARED_OBJECT, False): self.ldflags_shared, - (CCompiler.SHARED_OBJECT, True): self.ldflags_shared_debug, - (CCompiler.SHARED_LIBRARY, None): self.ldflags_static, - (CCompiler.SHARED_LIBRARY, False): self.ldflags_static, - (CCompiler.SHARED_LIBRARY, True): self.ldflags_static_debug, + (base.Compiler.EXECUTABLE, None): self.ldflags_exe, + (base.Compiler.EXECUTABLE, False): self.ldflags_exe, + (base.Compiler.EXECUTABLE, True): self.ldflags_exe_debug, + (base.Compiler.SHARED_OBJECT, None): self.ldflags_shared, + (base.Compiler.SHARED_OBJECT, False): self.ldflags_shared, + (base.Compiler.SHARED_OBJECT, True): self.ldflags_shared_debug, + (base.Compiler.SHARED_LIBRARY, None): self.ldflags_static, + (base.Compiler.SHARED_LIBRARY, False): self.ldflags_static, + (base.Compiler.SHARED_LIBRARY, True): self.ldflags_static_debug, } self.initialized = True diff --git a/distutils/compilers/C/unix.py b/distutils/compilers/C/unix.py index 6c1116ae..fc97241f 100644 --- a/distutils/compilers/C/unix.py +++ b/distutils/compilers/C/unix.py @@ -21,13 +21,14 @@ import shlex import sys -from . import sysconfig -from ._log import log -from ._macos_compat import compiler_fixup -from ._modified import newer -from .ccompiler import CCompiler, gen_lib_options, gen_preprocess_options -from .compat import consolidate_linker_args -from .errors import CompileError, DistutilsExecError, LibError, LinkError +from ... import sysconfig +from ..._log import log +from ..._macos_compat import compiler_fixup +from ..._modified import newer +from ...compat import consolidate_linker_args +from ...errors import CompileError, DistutilsExecError, LibError, LinkError +from . import base +from .base import gen_lib_options, gen_preprocess_options # XXX Things not currently handled: # * optimization/debug/warning flags; we just use whatever's in Python's @@ -105,7 +106,7 @@ def _linker_params(linker_cmd, compiler_cmd): return linker_cmd[pivot:] -class UnixCCompiler(CCompiler): +class Compiler(base.Compiler): compiler_type = 'unix' # These are used by CCompiler in two places: the constructor sets @@ -264,7 +265,7 @@ def link( # Select a linker based on context: linker_exe when # building an executable or linker_so (with shared options) # when building a shared library. - building_exe = target_desc == CCompiler.EXECUTABLE + building_exe = target_desc == base.Compiler.EXECUTABLE linker = ( self.linker_exe if building_exe diff --git a/distutils/compilers/C/zos.py b/distutils/compilers/C/zos.py index af1e7fa5..5de91e49 100644 --- a/distutils/compilers/C/zos.py +++ b/distutils/compilers/C/zos.py @@ -13,9 +13,9 @@ import os -from . import sysconfig -from .errors import CompileError, DistutilsExecError -from .unixccompiler import UnixCCompiler +from ... import sysconfig +from ...errors import CompileError, DistutilsExecError +from . import unix _cc_args = { 'ibm-openxl': [ @@ -101,7 +101,7 @@ # Python on z/OS is built with no compiler specific options in it's CFLAGS. # But each compiler requires it's own specific options to build successfully, # though some of the options are common between them -class zOSCCompiler(UnixCCompiler): +class Compiler(unix.Compiler): src_extensions = ['.c', '.C', '.cc', '.cxx', '.cpp', '.m', '.s'] _cpp_extensions = ['.cc', '.cpp', '.cxx', '.C'] _asm_extensions = ['.s'] diff --git a/distutils/cygwinccompiler.py b/distutils/cygwinccompiler.py new file mode 100644 index 00000000..de89e3cd --- /dev/null +++ b/distutils/cygwinccompiler.py @@ -0,0 +1,31 @@ +from .compilers.C import cygwin +from .compilers.C.cygwin import ( + CONFIG_H_NOTOK, + CONFIG_H_OK, + CONFIG_H_UNCERTAIN, + check_config_h, + get_msvcr, + is_cygwincc, +) + +__all__ = [ + 'CONFIG_H_NOTOK', + 'CONFIG_H_OK', + 'CONFIG_H_UNCERTAIN', + 'CygwinCCompiler', + 'Mingw32CCompiler', + 'check_config_h', + 'get_msvcr', + 'is_cygwincc', +] + + +CygwinCCompiler = cygwin.Compiler +Mingw32CCompiler = cygwin.MinGW32Compiler + + +get_versions = None +""" +A stand-in for the previous get_versions() function to prevent failures +when monkeypatched. See pypa/setuptools#2969. +""" diff --git a/distutils/tests/test_msvccompiler.py b/distutils/tests/test_msvccompiler.py index 71129cae..4e0a5596 100644 --- a/distutils/tests/test_msvccompiler.py +++ b/distutils/tests/test_msvccompiler.py @@ -5,6 +5,7 @@ import threading import unittest.mock as mock from distutils import _msvccompiler +from distutils.compilers.C import msvc from distutils.errors import DistutilsPlatformError from distutils.tests import support @@ -21,10 +22,10 @@ def test_no_compiler(self, monkeypatch): def _find_vcvarsall(plat_spec): return None, None - monkeypatch.setattr(_msvccompiler, '_find_vcvarsall', _find_vcvarsall) + monkeypatch.setattr(msvc, '_find_vcvarsall', _find_vcvarsall) with pytest.raises(DistutilsPlatformError): - _msvccompiler._get_vc_env( + msvc._get_vc_env( 'wont find this version', ) diff --git a/distutils/unixccompiler.py b/distutils/unixccompiler.py new file mode 100644 index 00000000..9cd30ad9 --- /dev/null +++ b/distutils/unixccompiler.py @@ -0,0 +1,3 @@ +from .compilers.C import unix + +UnixCCompiler = unix.Compiler diff --git a/distutils/zosccompiler.py b/distutils/zosccompiler.py new file mode 100644 index 00000000..e49630ac --- /dev/null +++ b/distutils/zosccompiler.py @@ -0,0 +1,3 @@ +from .compilers.C import zos + +zOSCCompiler = zos.Compiler