From 319d21aab66b89e748385bbf01e770da7d45b0a9 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 2 Sep 2024 17:19:35 -0400 Subject: [PATCH 1/6] Move compiler implementations into their own package. --- distutils/{ccompiler.py => compilers/C/base.py} | 0 distutils/{cygwinccompiler.py => compilers/C/cygwin.py} | 0 distutils/{_msvccompiler.py => compilers/C/msvc.py} | 0 distutils/{unixccompiler.py => compilers/C/unix.py} | 0 distutils/{zosccompiler.py => compilers/C/zos.py} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename distutils/{ccompiler.py => compilers/C/base.py} (100%) rename distutils/{cygwinccompiler.py => compilers/C/cygwin.py} (100%) rename distutils/{_msvccompiler.py => compilers/C/msvc.py} (100%) rename distutils/{unixccompiler.py => compilers/C/unix.py} (100%) rename distutils/{zosccompiler.py => compilers/C/zos.py} (100%) diff --git a/distutils/ccompiler.py b/distutils/compilers/C/base.py similarity index 100% rename from distutils/ccompiler.py rename to distutils/compilers/C/base.py diff --git a/distutils/cygwinccompiler.py b/distutils/compilers/C/cygwin.py similarity index 100% rename from distutils/cygwinccompiler.py rename to distutils/compilers/C/cygwin.py diff --git a/distutils/_msvccompiler.py b/distutils/compilers/C/msvc.py similarity index 100% rename from distutils/_msvccompiler.py rename to distutils/compilers/C/msvc.py diff --git a/distutils/unixccompiler.py b/distutils/compilers/C/unix.py similarity index 100% rename from distutils/unixccompiler.py rename to distutils/compilers/C/unix.py diff --git a/distutils/zosccompiler.py b/distutils/compilers/C/zos.py similarity index 100% rename from distutils/zosccompiler.py rename to distutils/compilers/C/zos.py From 2a34ad69354c0858024a4961cbb0c8d5fbac74cc Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 2 Sep 2024 18:07:58 -0400 Subject: [PATCH 2/6] Rename classes and add compatibility shims. --- distutils/_msvccompiler.py | 3 +++ distutils/ccompiler.py | 21 +++++++++++++++++++ distutils/compilers/C/base.py | 24 ++++++++++----------- distutils/compilers/C/cygwin.py | 15 +++++++------- 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 +++ 11 files changed, 112 insertions(+), 49 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 5e73e56d..60e929c0 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 @@ -11,22 +11,22 @@ from more_itertools import always_iterable -from ._log import log -from ._modified import newer_group -from .dir_util import mkpath -from .errors import ( +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. @@ -725,7 +725,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, @@ -756,7 +756,7 @@ def link_shared_object( target_lang=None, ): self.link( - CCompiler.SHARED_OBJECT, + Compiler.SHARED_OBJECT, objects, output_filename, output_dir, @@ -785,7 +785,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/cygwin.py b/distutils/compilers/C/cygwin.py index 18b1b355..c6954bfc 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,8 +192,7 @@ def link( if not debug: extra_preargs.append("-s") - UnixCCompiler.link( - self, + super().link( target_desc, objects, output_filename, @@ -235,7 +234,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 e7652218..85faca97 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 From f673e62750f48d941551f4e2d5392870c843b4ed Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 5 Sep 2024 09:38:29 -0400 Subject: [PATCH 3/6] Move compiler execeptions to their package. --- distutils/compilers/C/errors.py | 24 +++++++++++++++++++ distutils/errors.py | 41 +++++++++++---------------------- 2 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 distutils/compilers/C/errors.py diff --git a/distutils/compilers/C/errors.py b/distutils/compilers/C/errors.py new file mode 100644 index 00000000..01328592 --- /dev/null +++ b/distutils/compilers/C/errors.py @@ -0,0 +1,24 @@ +class Error(Exception): + """Some compile/link operation failed.""" + + +class PreprocessError(Error): + """Failure to preprocess one or more C/C++ files.""" + + +class CompileError(Error): + """Failure to compile one or more C/C++ source files.""" + + +class LibError(Error): + """Failure to create a static library from one or more C/C++ object + files.""" + + +class LinkError(Error): + """Failure to link one or more C/C++ object files into an executable + or shared library file.""" + + +class UnknownFileType(Error): + """Attempt to process an unknown file type.""" diff --git a/distutils/errors.py b/distutils/errors.py index 3196a4f0..7c6ee258 100644 --- a/distutils/errors.py +++ b/distutils/errors.py @@ -5,6 +5,20 @@ including :exc:`SystemExit`. """ +# compiler exceptions aliased for compatibility +from .compilers.C.errors import ( + CompileError, # noqa: F401 + LibError, # noqa: F401 + LinkError, # noqa: F401 + PreprocessError, # noqa: F401 +) +from .compilers.C.errors import ( + Error as CCompilerError, # noqa: F401 +) +from .compilers.C.errors import ( + UnknownFileType as UnknownFileError, # noqa: F401 +) + class DistutilsError(Exception): """The root of all Distutils evil.""" @@ -95,30 +109,3 @@ class DistutilsTemplateError(DistutilsError): class DistutilsByteCompileError(DistutilsError): """Byte compile error.""" - - -# Exception classes used by the CCompiler implementation classes -class CCompilerError(Exception): - """Some compile/link operation failed.""" - - -class PreprocessError(CCompilerError): - """Failure to preprocess one or more C/C++ files.""" - - -class CompileError(CCompilerError): - """Failure to compile one or more C/C++ source files.""" - - -class LibError(CCompilerError): - """Failure to create a static library from one or more C/C++ object - files.""" - - -class LinkError(CCompilerError): - """Failure to link one or more C/C++ object files into an executable - or shared library file.""" - - -class UnknownFileError(CCompilerError): - """Attempt to process an unknown file type.""" From cc1b01e85460f58f041a5e2829bd34dd082068ca Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 5 Sep 2024 09:46:33 -0400 Subject: [PATCH 4/6] In compiler package, rely on compiler errors. --- distutils/compilers/C/base.py | 10 ++++++---- distutils/compilers/C/cygwin.py | 8 +++++--- distutils/compilers/C/msvc.py | 8 +++++--- distutils/compilers/C/unix.py | 7 ++++++- distutils/compilers/C/zos.py | 3 ++- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/distutils/compilers/C/base.py b/distutils/compilers/C/base.py index 60e929c0..8bbe3248 100644 --- a/distutils/compilers/C/base.py +++ b/distutils/compilers/C/base.py @@ -15,15 +15,17 @@ 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 .errors import ( + CompileError, + LinkError, + UnknownFileType, +) class Compiler: @@ -974,7 +976,7 @@ def _make_out_path(self, output_dir, strip_dir, src_name): try: new_ext = self.out_extensions[ext] except LookupError: - raise UnknownFileError(f"unknown file type '{ext}' (from '{src_name}')") + raise UnknownFileType(f"unknown file type '{ext}' (from '{src_name}')") if strip_dir: base = os.path.basename(base) return os.path.join(output_dir, base + new_ext) diff --git a/distutils/compilers/C/cygwin.py b/distutils/compilers/C/cygwin.py index c6954bfc..fbaf84cd 100644 --- a/distutils/compilers/C/cygwin.py +++ b/distutils/compilers/C/cygwin.py @@ -15,14 +15,16 @@ from subprocess import check_output from ...errors import ( - CCompilerError, - CompileError, DistutilsExecError, DistutilsPlatformError, ) from ...file_util import write_file from ...version import LooseVersion, suppress_known_deprecation from . import unix +from .errors import ( + CompileError, + Error, +) def get_msvcr(): @@ -245,7 +247,7 @@ def __init__(self, verbose=False, dry_run=False, force=False): shared_option = "-shared" if is_cygwincc(self.cc): - raise CCompilerError('Cygwin gcc cannot be used with --compiler=mingw32') + raise Error('Cygwin gcc cannot be used with --compiler=mingw32') self.set_executables( compiler=f'{self.cc} -O -Wall', diff --git a/distutils/compilers/C/msvc.py b/distutils/compilers/C/msvc.py index 85faca97..f69f233d 100644 --- a/distutils/compilers/C/msvc.py +++ b/distutils/compilers/C/msvc.py @@ -25,15 +25,17 @@ from ..._log import log from ...errors import ( - CompileError, DistutilsExecError, DistutilsPlatformError, - LibError, - LinkError, ) from ...util import get_host_platform, get_platform from . import base from .base import gen_lib_options +from .errors import ( + CompileError, + LibError, + LinkError, +) def _find_vc2015(): diff --git a/distutils/compilers/C/unix.py b/distutils/compilers/C/unix.py index fc97241f..1ba93e6a 100644 --- a/distutils/compilers/C/unix.py +++ b/distutils/compilers/C/unix.py @@ -26,9 +26,14 @@ from ..._macos_compat import compiler_fixup from ..._modified import newer from ...compat import consolidate_linker_args -from ...errors import CompileError, DistutilsExecError, LibError, LinkError +from ...errors import DistutilsExecError from . import base from .base import gen_lib_options, gen_preprocess_options +from .errors import ( + CompileError, + LibError, + LinkError, +) # XXX Things not currently handled: # * optimization/debug/warning flags; we just use whatever's in Python's diff --git a/distutils/compilers/C/zos.py b/distutils/compilers/C/zos.py index 5de91e49..82d017fc 100644 --- a/distutils/compilers/C/zos.py +++ b/distutils/compilers/C/zos.py @@ -14,8 +14,9 @@ import os from ... import sysconfig -from ...errors import CompileError, DistutilsExecError +from ...errors import DistutilsExecError from . import unix +from .errors import CompileError _cc_args = { 'ibm-openxl': [ From ef7c98d5538d367658ff6f8b2eeff99b5a47f635 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 6 Sep 2024 13:06:48 -0400 Subject: [PATCH 5/6] Move compiler tests to the compilers package. --- .../test_ccompiler.py => compilers/C/tests/test_base.py} | 0 .../C/tests/test_cygwin.py} | 0 .../C/tests/test_mingw.py} | 0 .../test_msvccompiler.py => compilers/C/tests/test_msvc.py} | 0 .../test_unixccompiler.py => compilers/C/tests/test_unix.py} | 5 ++--- 5 files changed, 2 insertions(+), 3 deletions(-) rename distutils/{tests/test_ccompiler.py => compilers/C/tests/test_base.py} (100%) rename distutils/{tests/test_cygwinccompiler.py => compilers/C/tests/test_cygwin.py} (100%) rename distutils/{tests/test_mingwccompiler.py => compilers/C/tests/test_mingw.py} (100%) rename distutils/{tests/test_msvccompiler.py => compilers/C/tests/test_msvc.py} (100%) rename distutils/{tests/test_unixccompiler.py => compilers/C/tests/test_unix.py} (99%) diff --git a/distutils/tests/test_ccompiler.py b/distutils/compilers/C/tests/test_base.py similarity index 100% rename from distutils/tests/test_ccompiler.py rename to distutils/compilers/C/tests/test_base.py diff --git a/distutils/tests/test_cygwinccompiler.py b/distutils/compilers/C/tests/test_cygwin.py similarity index 100% rename from distutils/tests/test_cygwinccompiler.py rename to distutils/compilers/C/tests/test_cygwin.py diff --git a/distutils/tests/test_mingwccompiler.py b/distutils/compilers/C/tests/test_mingw.py similarity index 100% rename from distutils/tests/test_mingwccompiler.py rename to distutils/compilers/C/tests/test_mingw.py diff --git a/distutils/tests/test_msvccompiler.py b/distutils/compilers/C/tests/test_msvc.py similarity index 100% rename from distutils/tests/test_msvccompiler.py rename to distutils/compilers/C/tests/test_msvc.py diff --git a/distutils/tests/test_unixccompiler.py b/distutils/compilers/C/tests/test_unix.py similarity index 99% rename from distutils/tests/test_unixccompiler.py rename to distutils/compilers/C/tests/test_unix.py index 50b66544..82384525 100644 --- a/distutils/tests/test_unixccompiler.py +++ b/distutils/compilers/C/tests/test_unix.py @@ -6,14 +6,13 @@ from distutils import sysconfig from distutils.compat import consolidate_linker_args from distutils.errors import DistutilsPlatformError +from distutils.tests import support +from distutils.tests.compat.py38 import EnvironmentVarGuard from distutils.unixccompiler import UnixCCompiler from distutils.util import _clear_cached_macosx_ver import pytest -from . import support -from .compat.py38 import EnvironmentVarGuard - @pytest.fixture(autouse=True) def save_values(monkeypatch): From b430f8262acaa30922ad93c217f1e0ddabeb5381 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 6 Sep 2024 13:24:14 -0400 Subject: [PATCH 6/6] Direct tests to the new names --- distutils/compilers/C/tests/test_base.py | 9 +++--- distutils/compilers/C/tests/test_cygwin.py | 19 +++++-------- distutils/compilers/C/tests/test_mingw.py | 32 ++++++++-------------- distutils/compilers/C/tests/test_msvc.py | 16 +++++------ distutils/compilers/C/tests/test_unix.py | 7 +++-- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/distutils/compilers/C/tests/test_base.py b/distutils/compilers/C/tests/test_base.py index d23b907c..0d6c2a95 100644 --- a/distutils/compilers/C/tests/test_base.py +++ b/distutils/compilers/C/tests/test_base.py @@ -3,10 +3,11 @@ import sys import sysconfig import textwrap -from distutils import ccompiler import pytest +from .. import base + def _make_strs(paths): """ @@ -44,7 +45,7 @@ def test_set_include_dirs(c_file): Extensions should build even if set_include_dirs is invoked. In particular, compiler-specific paths should not be overridden. """ - compiler = ccompiler.new_compiler() + compiler = base.new_compiler() python = sysconfig.get_paths()['include'] compiler.set_include_dirs([python]) compiler.compile(_make_strs([c_file])) @@ -58,7 +59,7 @@ def test_has_function_prototype(): # Issue https://github.com/pypa/setuptools/issues/3648 # Test prototype-generating behavior. - compiler = ccompiler.new_compiler() + compiler = base.new_compiler() # Every C implementation should have these. assert compiler.has_function('abort') @@ -82,7 +83,7 @@ def test_include_dirs_after_multiple_compile_calls(c_file): Calling compile multiple times should not change the include dirs (regression test for setuptools issue #3591). """ - compiler = ccompiler.new_compiler() + compiler = base.new_compiler() python = sysconfig.get_paths()['include'] compiler.set_include_dirs([python]) compiler.compile(_make_strs([c_file])) diff --git a/distutils/compilers/C/tests/test_cygwin.py b/distutils/compilers/C/tests/test_cygwin.py index 677bc0ac..9adf6b8e 100644 --- a/distutils/compilers/C/tests/test_cygwin.py +++ b/distutils/compilers/C/tests/test_cygwin.py @@ -3,17 +3,12 @@ import os import sys from distutils import sysconfig -from distutils.cygwinccompiler import ( - CONFIG_H_NOTOK, - CONFIG_H_OK, - CONFIG_H_UNCERTAIN, - check_config_h, - get_msvcr, -) from distutils.tests import support import pytest +from .. import cygwin + @pytest.fixture(autouse=True) def stuff(request, monkeypatch, distutils_managed_tempdir): @@ -54,24 +49,24 @@ def test_check_config_h(self): '4.0.1 (Apple Computer, Inc. build 5370)]' ) - assert check_config_h()[0] == CONFIG_H_OK + assert cygwin.check_config_h()[0] == cygwin.CONFIG_H_OK # then it tries to see if it can find "__GNUC__" in pyconfig.h sys.version = 'something without the *CC word' # if the file doesn't exist it returns CONFIG_H_UNCERTAIN - assert check_config_h()[0] == CONFIG_H_UNCERTAIN + assert cygwin.check_config_h()[0] == cygwin.CONFIG_H_UNCERTAIN # if it exists but does not contain __GNUC__, it returns CONFIG_H_NOTOK self.write_file(self.python_h, 'xxx') - assert check_config_h()[0] == CONFIG_H_NOTOK + assert cygwin.check_config_h()[0] == cygwin.CONFIG_H_NOTOK # and CONFIG_H_OK if __GNUC__ is found self.write_file(self.python_h, 'xxx __GNUC__ xxx') - assert check_config_h()[0] == CONFIG_H_OK + assert cygwin.check_config_h()[0] == cygwin.CONFIG_H_OK def test_get_msvcr(self): - assert get_msvcr() == [] + assert cygwin.get_msvcr() == [] @pytest.mark.skipif('sys.platform != "cygwin"') def test_dll_libraries_not_none(self): diff --git a/distutils/compilers/C/tests/test_mingw.py b/distutils/compilers/C/tests/test_mingw.py index 3e3ad505..dc45687a 100644 --- a/distutils/compilers/C/tests/test_mingw.py +++ b/distutils/compilers/C/tests/test_mingw.py @@ -1,26 +1,24 @@ from distutils import sysconfig -from distutils.errors import CCompilerError, DistutilsPlatformError +from distutils.errors import DistutilsPlatformError from distutils.util import is_mingw, split_quoted import pytest +from .. import cygwin, errors -class TestMingw32CCompiler: + +class TestMinGW32Compiler: @pytest.mark.skipif(not is_mingw(), reason='not on mingw') def test_compiler_type(self): - from distutils.cygwinccompiler import Mingw32CCompiler - - compiler = Mingw32CCompiler() + compiler = cygwin.MinGW32Compiler() assert compiler.compiler_type == 'mingw32' @pytest.mark.skipif(not is_mingw(), reason='not on mingw') def test_set_executables(self, monkeypatch): - from distutils.cygwinccompiler import Mingw32CCompiler - monkeypatch.setenv('CC', 'cc') monkeypatch.setenv('CXX', 'c++') - compiler = Mingw32CCompiler() + compiler = cygwin.MinGW32Compiler() assert compiler.compiler == split_quoted('cc -O -Wall') assert compiler.compiler_so == split_quoted('cc -shared -O -Wall') @@ -30,27 +28,21 @@ def test_set_executables(self, monkeypatch): @pytest.mark.skipif(not is_mingw(), reason='not on mingw') def test_runtime_library_dir_option(self): - from distutils.cygwinccompiler import Mingw32CCompiler - - compiler = Mingw32CCompiler() + compiler = cygwin.MinGW32Compiler() with pytest.raises(DistutilsPlatformError): compiler.runtime_library_dir_option('/usr/lib') @pytest.mark.skipif(not is_mingw(), reason='not on mingw') def test_cygwincc_error(self, monkeypatch): - import distutils.cygwinccompiler - - monkeypatch.setattr(distutils.cygwinccompiler, 'is_cygwincc', lambda _: True) + monkeypatch.setattr(cygwin, 'is_cygwincc', lambda _: True) - with pytest.raises(CCompilerError): - distutils.cygwinccompiler.Mingw32CCompiler() + with pytest.raises(errors.Error): + cygwin.MinGW32Compiler() @pytest.mark.skipif('sys.platform == "cygwin"') def test_customize_compiler_with_msvc_python(self): - from distutils.cygwinccompiler import Mingw32CCompiler - # In case we have an MSVC Python build, but still want to use - # Mingw32CCompiler, then customize_compiler() shouldn't fail at least. + # MinGW32Compiler, then customize_compiler() shouldn't fail at least. # https://github.com/pypa/setuptools/issues/4456 - compiler = Mingw32CCompiler() + compiler = cygwin.MinGW32Compiler() sysconfig.customize_compiler(compiler) diff --git a/distutils/compilers/C/tests/test_msvc.py b/distutils/compilers/C/tests/test_msvc.py index 4e0a5596..b92d28ee 100644 --- a/distutils/compilers/C/tests/test_msvc.py +++ b/distutils/compilers/C/tests/test_msvc.py @@ -1,17 +1,15 @@ -"""Tests for distutils._msvccompiler.""" - import os import sys 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 import pytest -needs_winreg = pytest.mark.skipif('not hasattr(_msvccompiler, "winreg")') +from .. import msvc + +needs_winreg = pytest.mark.skipif('not hasattr(msvc, "winreg")') class Testmsvccompiler(support.TempdirManager): @@ -38,7 +36,7 @@ def test_get_vc_env_unicode(self): old_distutils_use_sdk = os.environ.pop('DISTUTILS_USE_SDK', None) os.environ[test_var] = test_value try: - env = _msvccompiler._get_vc_env('x86') + env = msvc._get_vc_env('x86') assert test_var.lower() in env assert test_value == env[test_var.lower()] finally: @@ -51,7 +49,7 @@ def test_get_vc_env_unicode(self): def test_get_vc(self, ver): # This function cannot be mocked, so pass if VC is found # and skip otherwise. - lookup = getattr(_msvccompiler, f'_find_vc{ver}') + lookup = getattr(msvc, f'_find_vc{ver}') expected_version = {2015: 14, 2017: 15}[ver] version, path = lookup() if not version: @@ -78,7 +76,7 @@ def test_concurrent_safe(self): """ Concurrent calls to spawn should have consistent results. """ - compiler = _msvccompiler.MSVCCompiler() + compiler = msvc.Compiler() compiler._paths = "expected" inner_cmd = 'import os; assert os.environ["PATH"] == "expected"' command = [sys.executable, '-c', inner_cmd] @@ -99,7 +97,7 @@ def test_concurrent_safe_fallback(self): """ from distutils import ccompiler - compiler = _msvccompiler.MSVCCompiler() + compiler = msvc.Compiler() compiler._paths = "expected" def CCompiler_spawn(self, cmd): diff --git a/distutils/compilers/C/tests/test_unix.py b/distutils/compilers/C/tests/test_unix.py index 82384525..dd070eef 100644 --- a/distutils/compilers/C/tests/test_unix.py +++ b/distutils/compilers/C/tests/test_unix.py @@ -8,11 +8,12 @@ from distutils.errors import DistutilsPlatformError from distutils.tests import support from distutils.tests.compat.py38 import EnvironmentVarGuard -from distutils.unixccompiler import UnixCCompiler from distutils.util import _clear_cached_macosx_ver import pytest +from .. import unix + @pytest.fixture(autouse=True) def save_values(monkeypatch): @@ -23,7 +24,7 @@ def save_values(monkeypatch): @pytest.fixture(autouse=True) def compiler_wrapper(request): - class CompilerWrapper(UnixCCompiler): + class CompilerWrapper(unix.Compiler): def rpath_foo(self): return self.runtime_library_dir_option('/foo') @@ -320,7 +321,7 @@ def test_has_function(self): self.cc.has_function('abort') def test_find_library_file(self, monkeypatch): - compiler = UnixCCompiler() + compiler = unix.Compiler() compiler._library_root = lambda dir: dir monkeypatch.setattr(os.path, 'exists', lambda d: 'existing' in d)