Skip to content

Commit

Permalink
Rename classes and add compatibility shims.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 2, 2024
1 parent fea3706 commit 01807e1
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 56 deletions.
3 changes: 3 additions & 0 deletions distutils/_msvccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .compilers.C import msvc

MSVCCompiler = msvc.Compiler
21 changes: 21 additions & 0 deletions distutils/ccompiler.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 13 additions & 13 deletions distutils/compilers/C/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -755,7 +755,7 @@ def link_shared_object(
target_lang=None,
):
self.link(
CCompiler.SHARED_OBJECT,
Compiler.SHARED_OBJECT,
objects,
output_filename,
output_dir,
Expand Down Expand Up @@ -784,7 +784,7 @@ def link_executable(
target_lang=None,
):
self.link(
CCompiler.EXECUTABLE,
Compiler.EXECUTABLE,
objects,
self.executable_filename(output_progname),
output_dir,
Expand Down
15 changes: 8 additions & 7 deletions distutils/compilers/C/borland.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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[:]
Expand Down
14 changes: 7 additions & 7 deletions distutils/compilers/C/cygwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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'
Expand Down Expand Up @@ -192,7 +192,7 @@ def link(
if not debug:
extra_preargs.append("-s")

UnixCCompiler.link(
super().link(
self,
target_desc,
objects,
Expand Down Expand Up @@ -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'
Expand Down
29 changes: 15 additions & 14 deletions distutils/compilers/C/msvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions distutils/compilers/C/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions distutils/compilers/C/zos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down Expand Up @@ -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']
Expand Down
31 changes: 31 additions & 0 deletions distutils/cygwinccompiler.py
Original file line number Diff line number Diff line change
@@ -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.
"""
5 changes: 3 additions & 2 deletions distutils/tests/test_msvccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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',
)

Expand Down
3 changes: 3 additions & 0 deletions distutils/unixccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .compilers.C import unix

UnixCCompiler = unix.Compiler
3 changes: 3 additions & 0 deletions distutils/zosccompiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .compilers.C import zos

zOSCCompiler = zos.Compiler

0 comments on commit 01807e1

Please sign in to comment.