[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -2,15 +2,21 @@
Contains CCompiler, an abstract base class that defines the interface
for the Distutils compiler abstraction model."""
from __future__ import annotations
import os
import re
import sys
import sys, os, re
from distutils.errors import *
from distutils.spawn import spawn
from distutils.file_util import move_file
from distutils.dir_util import mkpath
from distutils.dep_util import newer_group
from distutils.util import split_quoted, execute
from distutils import log
from distutils.dep_util import newer_group
from distutils.dir_util import mkpath
from distutils.errors import *
from distutils.file_util import move_file
from distutils.spawn import spawn
from distutils.util import execute
from distutils.util import split_quoted
class CCompiler:
"""Abstract base class to define the interface that must be implemented
@ -56,7 +62,6 @@ class CCompiler:
# think this is useless without the ability to null out the
# library search path anyways.
# Subclasses that rely on the standard filename generation methods
# implemented below should override these; see the comment near
# those methods ('object_filenames()' et. al.) for details:
@ -74,13 +79,14 @@ class CCompiler:
# what language to use when mixing source types. For example, if some
# extension has two files with ".c" extension, and one with ".cpp", it
# is still linked as c++.
language_map = {".c" : "c",
".cc" : "c++",
".cpp" : "c++",
".cxx" : "c++",
".m" : "objc",
}
language_order = ["c++", "objc", "c"]
language_map = {
'.c': 'c',
'.cc': 'c++',
'.cpp': 'c++',
'.cxx': 'c++',
'.m': 'objc',
}
language_order = ['c++', 'objc', 'c']
def __init__(self, verbose=0, dry_run=0, force=0):
self.dry_run = dry_run
@ -146,8 +152,10 @@ class CCompiler:
for key in kwargs:
if key not in self.executables:
raise ValueError("unknown executable '%s' for class %s" %
(key, self.__class__.__name__))
raise ValueError(
"unknown executable '%s' for class %s" %
(key, self.__class__.__name__),
)
self.set_executable(key, kwargs[key])
def set_executable(self, key, value):
@ -170,14 +178,19 @@ class CCompiler:
nothing if all definitions are OK, raise TypeError otherwise.
"""
for defn in definitions:
if not (isinstance(defn, tuple) and
(len(defn) in (1, 2) and
(isinstance (defn[1], str) or defn[1] is None)) and
isinstance (defn[0], str)):
raise TypeError(("invalid macro definition '%s': " % defn) + \
"must be tuple (string,), (string, string), or " + \
"(string, None)")
if not (
isinstance(defn, tuple) and
(
len(defn) in (1, 2) and
(isinstance(defn[1], str) or defn[1] is None)
) and
isinstance(defn[0], str)
):
raise TypeError(
("invalid macro definition '%s': " % defn) +
'must be tuple (string,), (string, string), or ' +
'(string, None)',
)
# -- Bookkeeping methods -------------------------------------------
@ -190,7 +203,7 @@ class CCompiler:
"""
# Delete from the list of macro definitions/undefinitions if
# already there (so that this one will take precedence).
i = self._find_macro (name)
i = self._find_macro(name)
if i is not None:
del self.macros[i]
@ -207,7 +220,7 @@ class CCompiler:
"""
# Delete from the list of macro definitions/undefinitions if
# already there (so that this one will take precedence).
i = self._find_macro (name)
i = self._find_macro(name)
if i is not None:
del self.macros[i]
@ -301,14 +314,15 @@ class CCompiler:
"""
self.objects = objects[:]
# -- Private utility methods --------------------------------------
# (here for the convenience of subclasses)
# Helper method to prep compiler in subclass compile() methods
def _setup_compile(self, outdir, macros, incdirs, sources, depends,
extra):
def _setup_compile(
self, outdir, macros, incdirs, sources, depends,
extra,
):
"""Process arguments and decide which source files to compile."""
if outdir is None:
outdir = self.output_dir
@ -328,14 +342,17 @@ class CCompiler:
incdirs = list(incdirs) + (self.include_dirs or [])
else:
raise TypeError(
"'include_dirs' (if supplied) must be a list of strings")
"'include_dirs' (if supplied) must be a list of strings",
)
if extra is None:
extra = []
# Get the list of expected output (object) files
objects = self.object_filenames(sources, strip_dir=0,
output_dir=outdir)
objects = self.object_filenames(
sources, strip_dir=0,
output_dir=outdir,
)
assert len(objects) == len(sources)
pp_opts = gen_preprocess_options(macros, incdirs)
@ -387,7 +404,8 @@ class CCompiler:
include_dirs = list(include_dirs) + (self.include_dirs or [])
else:
raise TypeError(
"'include_dirs' (if supplied) must be a list of strings")
"'include_dirs' (if supplied) must be a list of strings",
)
return output_dir, macros, include_dirs
@ -434,27 +452,33 @@ class CCompiler:
if libraries is None:
libraries = self.libraries
elif isinstance(libraries, (list, tuple)):
libraries = list (libraries) + (self.libraries or [])
libraries = list(libraries) + (self.libraries or [])
else:
raise TypeError(
"'libraries' (if supplied) must be a list of strings")
"'libraries' (if supplied) must be a list of strings",
)
if library_dirs is None:
library_dirs = self.library_dirs
elif isinstance(library_dirs, (list, tuple)):
library_dirs = list (library_dirs) + (self.library_dirs or [])
library_dirs = list(library_dirs) + (self.library_dirs or [])
else:
raise TypeError(
"'library_dirs' (if supplied) must be a list of strings")
"'library_dirs' (if supplied) must be a list of strings",
)
if runtime_library_dirs is None:
runtime_library_dirs = self.runtime_library_dirs
elif isinstance(runtime_library_dirs, (list, tuple)):
runtime_library_dirs = (list(runtime_library_dirs) +
(self.runtime_library_dirs or []))
runtime_library_dirs = (
list(runtime_library_dirs) +
(self.runtime_library_dirs or [])
)
else:
raise TypeError("'runtime_library_dirs' (if supplied) "
"must be a list of strings")
raise TypeError(
"'runtime_library_dirs' (if supplied) "
'must be a list of strings',
)
return (libraries, library_dirs, runtime_library_dirs)
@ -466,9 +490,9 @@ class CCompiler:
return True
else:
if self.dry_run:
newer = newer_group (objects, output_file, missing='newer')
newer = newer_group(objects, output_file, missing='newer')
else:
newer = newer_group (objects, output_file)
newer = newer_group(objects, output_file)
return newer
def detect_language(self, sources):
@ -491,12 +515,13 @@ class CCompiler:
pass
return lang
# -- Worker methods ------------------------------------------------
# (must be implemented by subclasses)
def preprocess(self, source, output_file=None, macros=None,
include_dirs=None, extra_preargs=None, extra_postargs=None):
def preprocess(
self, source, output_file=None, macros=None,
include_dirs=None, extra_preargs=None, extra_postargs=None,
):
"""Preprocess a single C/C++ source file, named in 'source'.
Output will be written to file named 'output_file', or stdout if
'output_file' not supplied. 'macros' is a list of macro
@ -508,9 +533,11 @@ class CCompiler:
"""
pass
def compile(self, sources, output_dir=None, macros=None,
include_dirs=None, debug=0, extra_preargs=None,
extra_postargs=None, depends=None):
def compile(
self, sources, output_dir=None, macros=None,
include_dirs=None, debug=0, extra_preargs=None,
extra_postargs=None, depends=None,
):
"""Compile one or more source files.
'sources' must be a list of filenames, most likely C/C++
@ -562,8 +589,10 @@ class CCompiler:
# A concrete compiler class can either override this method
# entirely or implement _compile().
macros, objects, extra_postargs, pp_opts, build = \
self._setup_compile(output_dir, macros, include_dirs, sources,
depends, extra_postargs)
self._setup_compile(
output_dir, macros, include_dirs, sources,
depends, extra_postargs,
)
cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
for obj in objects:
@ -582,8 +611,10 @@ class CCompiler:
# should implement _compile().
pass
def create_static_lib(self, objects, output_libname, output_dir=None,
debug=0, target_lang=None):
def create_static_lib(
self, objects, output_libname, output_dir=None,
debug=0, target_lang=None,
):
"""Link a bunch of stuff together to create a static library file.
The "bunch of stuff" consists of the list of object files supplied
as 'objects', the extra object files supplied to
@ -608,26 +639,27 @@ class CCompiler:
"""
pass
# values for target_desc parameter in link()
SHARED_OBJECT = "shared_object"
SHARED_LIBRARY = "shared_library"
EXECUTABLE = "executable"
SHARED_OBJECT = 'shared_object'
SHARED_LIBRARY = 'shared_library'
EXECUTABLE = 'executable'
def link(self,
target_desc,
objects,
output_filename,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None):
def link(
self,
target_desc,
objects,
output_filename,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None,
):
"""Link a bunch of stuff together to create an executable or
shared library file.
@ -673,66 +705,74 @@ class CCompiler:
"""
raise NotImplementedError
# Old 'link_*()' methods, rewritten to use the new 'link()' method.
def link_shared_lib(self,
objects,
output_libname,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None):
self.link(CCompiler.SHARED_LIBRARY, objects,
self.library_filename(output_libname, lib_type='shared'),
output_dir,
libraries, library_dirs, runtime_library_dirs,
export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)
def link_shared_lib(
self,
objects,
output_libname,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None,
):
self.link(
CCompiler.SHARED_LIBRARY, objects,
self.library_filename(output_libname, lib_type='shared'),
output_dir,
libraries, library_dirs, runtime_library_dirs,
export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang,
)
def link_shared_object(
self,
objects,
output_filename,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None,
):
self.link(
CCompiler.SHARED_OBJECT, objects,
output_filename, output_dir,
libraries, library_dirs, runtime_library_dirs,
export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang,
)
def link_shared_object(self,
objects,
output_filename,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
export_symbols=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
build_temp=None,
target_lang=None):
self.link(CCompiler.SHARED_OBJECT, objects,
output_filename, output_dir,
libraries, library_dirs, runtime_library_dirs,
export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)
def link_executable(self,
objects,
output_progname,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
target_lang=None):
self.link(CCompiler.EXECUTABLE, objects,
self.executable_filename(output_progname), output_dir,
libraries, library_dirs, runtime_library_dirs, None,
debug, extra_preargs, extra_postargs, None, target_lang)
def link_executable(
self,
objects,
output_progname,
output_dir=None,
libraries=None,
library_dirs=None,
runtime_library_dirs=None,
debug=0,
extra_preargs=None,
extra_postargs=None,
target_lang=None,
):
self.link(
CCompiler.EXECUTABLE, objects,
self.executable_filename(output_progname), output_dir,
libraries, library_dirs, runtime_library_dirs, None,
debug, extra_preargs, extra_postargs, None, target_lang,
)
# -- Miscellaneous methods -----------------------------------------
# These are all used by the 'gen_lib_options() function; there is
@ -757,8 +797,10 @@ class CCompiler:
"""
raise NotImplementedError
def has_function(self, funcname, includes=None, include_dirs=None,
libraries=None, library_dirs=None):
def has_function(
self, funcname, includes=None, include_dirs=None,
libraries=None, library_dirs=None,
):
"""Return a boolean indicating whether funcname is supported on
the current platform. The optional arguments can be used to
augment the compilation environment.
@ -775,17 +817,19 @@ class CCompiler:
libraries = []
if library_dirs is None:
library_dirs = []
fd, fname = tempfile.mkstemp(".c", funcname, text=True)
f = os.fdopen(fd, "w")
fd, fname = tempfile.mkstemp('.c', funcname, text=True)
f = os.fdopen(fd, 'w')
try:
for incl in includes:
f.write("""#include "%s"\n""" % incl)
f.write("""\
f.write(
"""\
int main (int argc, char **argv) {
%s();
return 0;
}
""" % funcname)
""" % funcname,
)
finally:
f.close()
try:
@ -796,19 +840,21 @@ int main (int argc, char **argv) {
os.remove(fname)
try:
self.link_executable(objects, "a.out",
libraries=libraries,
library_dirs=library_dirs)
self.link_executable(
objects, 'a.out',
libraries=libraries,
library_dirs=library_dirs,
)
except (LinkError, TypeError):
return False
else:
os.remove("a.out")
os.remove('a.out')
finally:
for fn in objects:
os.remove(fn)
return True
def find_library_file (self, dirs, lib, debug=0):
def find_library_file(self, dirs, lib, debug=0):
"""Search the specified list of directories for a static or shared
library file 'lib' and return the full path to that file. If
'debug' true, look for a debugging version (if that makes sense on
@ -857,15 +903,20 @@ int main (int argc, char **argv) {
obj_names = []
for src_name in source_filenames:
base, ext = os.path.splitext(src_name)
base = os.path.splitdrive(base)[1] # Chop off the drive
base = os.path.splitdrive(base)[1] # Chop off the drive
base = base[os.path.isabs(base):] # If abs, chop off leading /
if ext not in self.src_extensions:
raise UnknownFileError(
"unknown file type '%s' (from '%s')" % (ext, src_name))
"unknown file type '{}' (from '{}')".format(ext, src_name),
)
if strip_dir:
base = os.path.basename(base)
obj_names.append(os.path.join(output_dir,
base + self.obj_extension))
obj_names.append(
os.path.join(
output_dir,
base + self.obj_extension,
),
)
return obj_names
def shared_object_filename(self, basename, strip_dir=0, output_dir=''):
@ -880,14 +931,17 @@ int main (int argc, char **argv) {
basename = os.path.basename(basename)
return os.path.join(output_dir, basename + (self.exe_extension or ''))
def library_filename(self, libname, lib_type='static', # or 'shared'
strip_dir=0, output_dir=''):
def library_filename(
self, libname, lib_type='static', # or 'shared'
strip_dir=0, output_dir='',
):
assert output_dir is not None
if lib_type not in ("static", "shared", "dylib", "xcode_stub"):
if lib_type not in ('static', 'shared', 'dylib', 'xcode_stub'):
raise ValueError(
"'lib_type' must be \"static\", \"shared\", \"dylib\", or \"xcode_stub\"")
fmt = getattr(self, lib_type + "_lib_format")
ext = getattr(self, lib_type + "_lib_extension")
"'lib_type' must be \"static\", \"shared\", \"dylib\", or \"xcode_stub\"",
)
fmt = getattr(self, lib_type + '_lib_format')
ext = getattr(self, lib_type + '_lib_extension')
dir, base = os.path.split(libname)
filename = fmt % (base, ext)
@ -896,7 +950,6 @@ int main (int argc, char **argv) {
return os.path.join(output_dir, dir, filename)
# -- Utility methods -----------------------------------------------
def announce(self, msg, level=1):
@ -908,7 +961,7 @@ int main (int argc, char **argv) {
print(msg)
def warn(self, msg):
sys.stderr.write("warning: %s\n" % msg)
sys.stderr.write('warning: %s\n' % msg)
def execute(self, func, args, msg=None, level=1):
execute(func, args, msg, self.dry_run)
@ -919,7 +972,7 @@ int main (int argc, char **argv) {
def move_file(self, src, dst):
return move_file(src, dst, dry_run=self.dry_run)
def mkpath (self, name, mode=0o777):
def mkpath(self, name, mode=0o777):
mkpath(name, mode, dry_run=self.dry_run)
@ -939,7 +992,8 @@ _default_compilers = (
('posix', 'unix'),
('nt', 'msvc'),
)
)
def get_default_compiler(osname=None, platform=None):
"""Determine the default compiler to use for the given platform.
@ -962,20 +1016,32 @@ def get_default_compiler(osname=None, platform=None):
# Default to Unix compiler
return 'unix'
# Map compiler types to (module_name, class_name) pairs -- ie. where to
# find the code that implements an interface to this compiler. (The module
# is assumed to be in the 'distutils' package.)
compiler_class = { 'unix': ('unixccompiler', 'UnixCCompiler',
"standard UNIX-style compiler"),
'msvc': ('_msvccompiler', 'MSVCCompiler',
"Microsoft Visual C++"),
'cygwin': ('cygwinccompiler', 'CygwinCCompiler',
"Cygwin port of GNU C Compiler for Win32"),
'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
"Mingw32 port of GNU C Compiler for Win32"),
'bcpp': ('bcppcompiler', 'BCPPCompiler',
"Borland C++ Compiler"),
}
compiler_class = {'unix': (
'unixccompiler', 'UnixCCompiler',
'standard UNIX-style compiler',
),
'msvc': (
'_msvccompiler', 'MSVCCompiler',
'Microsoft Visual C++',
),
'cygwin': (
'cygwinccompiler', 'CygwinCCompiler',
'Cygwin port of GNU C Compiler for Win32',
),
'mingw32': (
'cygwinccompiler', 'Mingw32CCompiler',
'Mingw32 port of GNU C Compiler for Win32',
),
'bcpp': (
'bcppcompiler', 'BCPPCompiler',
'Borland C++ Compiler',
),
}
def show_compilers():
"""Print list of available compilers (used by the "--help-compiler"
@ -987,11 +1053,13 @@ def show_compilers():
from distutils.fancy_getopt import FancyGetopt
compilers = []
for compiler in compiler_class.keys():
compilers.append(("compiler="+compiler, None,
compiler_class[compiler][2]))
compilers.append((
'compiler=' + compiler, None,
compiler_class[compiler][2],
))
compilers.sort()
pretty_printer = FancyGetopt(compilers)
pretty_printer.print_help("List of available compilers:")
pretty_printer.print_help('List of available compilers:')
def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
@ -1020,18 +1088,20 @@ def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
raise DistutilsPlatformError(msg)
try:
module_name = "distutils." + module_name
__import__ (module_name)
module_name = 'distutils.' + module_name
__import__(module_name)
module = sys.modules[module_name]
klass = vars(module)[class_name]
except ImportError:
raise DistutilsModuleError(
"can't compile C/C++ code: unable to load module '%s'" % \
module_name)
"can't compile C/C++ code: unable to load module '%s'" %
module_name,
)
except KeyError:
raise DistutilsModuleError(
"can't compile C/C++ code: unable to find class '%s' "
"in module '%s'" % (class_name, module_name))
"can't compile C/C++ code: unable to find class '%s' "
"in module '%s'" % (class_name, module_name),
)
# XXX The None is necessary to preserve backwards compatibility
# with classes that expect verbose to be the first positional
@ -1064,27 +1134,28 @@ def gen_preprocess_options(macros, include_dirs):
for macro in macros:
if not (isinstance(macro, tuple) and 1 <= len(macro) <= 2):
raise TypeError(
"bad macro definition '%s': "
"each element of 'macros' list must be a 1- or 2-tuple"
% macro)
"bad macro definition '%s': "
"each element of 'macros' list must be a 1- or 2-tuple"
% macro,
)
if len(macro) == 1: # undefine this macro
pp_opts.append("-U%s" % macro[0])
pp_opts.append('-U%s' % macro[0])
elif len(macro) == 2:
if macro[1] is None: # define with no explicit value
pp_opts.append("-D%s" % macro[0])
pp_opts.append('-D%s' % macro[0])
else:
# XXX *don't* need to be clever about quoting the
# macro value here, because we're going to avoid the
# shell at all costs when we spawn the command!
pp_opts.append("-D%s=%s" % macro)
pp_opts.append('-D%s=%s' % macro)
for dir in include_dirs:
pp_opts.append("-I%s" % dir)
pp_opts.append('-I%s' % dir)
return pp_opts
def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
"""Generate linker options for searching library directories and
linking with specific libraries. 'libraries' and 'library_dirs' are,
respectively, lists of library names (not filenames!) and search
@ -1116,8 +1187,10 @@ def gen_lib_options (compiler, library_dirs, runtime_library_dirs, libraries):
if lib_file:
lib_opts.append(lib_file)
else:
compiler.warn("no library file corresponding to "
"'%s' found (skipping)" % lib)
compiler.warn(
'no library file corresponding to '
"'%s' found (skipping)" % lib,
)
else:
lib_opts.append(compiler.library_option (lib))
lib_opts.append(compiler.library_option(lib))
return lib_opts