This commit is contained in:
lz_db
2026-10-16 22:48:30 +08:00
parent 190de72571
commit 0b36fd3383
3161 changed files with 12787 additions and 77274 deletions
@@ -3,11 +3,8 @@
Provides the Extension class, used to describe C/C++ extension
modules in setup scripts."""
from __future__ import annotations
import os
import warnings
from collections.abc import Iterable
# This class is really only used by the "build_ext" command, so it might
# make sense to put it in distutils.command.build_ext. However, that
@@ -29,14 +26,12 @@ class Extension:
name : string
the full name of the extension, including any packages -- ie.
*not* a filename or pathname, but Python dotted name
sources : Iterable[string | os.PathLike]
iterable of source filenames (except strings, which could be misinterpreted
as a single filename), relative to the distribution root (where the setup
script lives), in Unix form (slash-separated) for portability. Can be any
non-string iterable (list, tuple, set, etc.) containing strings or
PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific
resource files, or whatever else is recognized by the "build_ext" command
as source for a Python extension.
sources : [string]
list of source filenames, relative to the distribution root
(where the setup script lives), in Unix form (slash-separated)
for portability. Source files may be C, C++, SWIG (.i),
platform-specific resource files, or whatever else is recognized
by the "build_ext" command as source for a Python extension.
include_dirs : [string]
list of directories to search for C/C++ header files (in Unix
form for portability)
@@ -91,42 +86,31 @@ class Extension:
# setup_keywords in core.py.
def __init__(
self,
name: str,
sources: Iterable[str | os.PathLike[str]],
include_dirs: list[str] | None = None,
define_macros: list[tuple[str, str | None]] | None = None,
undef_macros: list[str] | None = None,
library_dirs: list[str] | None = None,
libraries: list[str] | None = None,
runtime_library_dirs: list[str] | None = None,
extra_objects: list[str] | None = None,
extra_compile_args: list[str] | None = None,
extra_link_args: list[str] | None = None,
export_symbols: list[str] | None = None,
swig_opts: list[str] | None = None,
depends: list[str] | None = None,
language: str | None = None,
optional: bool | None = None,
**kw, # To catch unknown keywords
name,
sources,
include_dirs=None,
define_macros=None,
undef_macros=None,
library_dirs=None,
libraries=None,
runtime_library_dirs=None,
extra_objects=None,
extra_compile_args=None,
extra_link_args=None,
export_symbols=None,
swig_opts=None,
depends=None,
language=None,
optional=None,
**kw # To catch unknown keywords
):
if not isinstance(name, str):
raise TypeError("'name' must be a string")
# handle the string case first; since strings are iterable, disallow them
if isinstance(sources, str):
raise TypeError(
"'sources' must be an iterable of strings or PathLike objects, not a string"
)
# now we check if it's iterable and contains valid types
try:
self.sources = list(map(os.fspath, sources))
except TypeError:
raise TypeError(
"'sources' must be an iterable of strings or PathLike objects"
)
raise AssertionError("'name' must be a string")
if not (isinstance(sources, list) and all(isinstance(v, str) for v in sources)):
raise AssertionError("'sources' must be a list of strings")
self.name = name
self.sources = sources
self.include_dirs = include_dirs or []
self.define_macros = define_macros or []
self.undef_macros = undef_macros or []
@@ -146,16 +130,22 @@ class Extension:
if len(kw) > 0:
options = [repr(option) for option in kw]
options = ', '.join(sorted(options))
msg = f"Unknown Extension options: {options}"
msg = "Unknown Extension options: %s" % options
warnings.warn(msg)
def __repr__(self):
return f'<{self.__class__.__module__}.{self.__class__.__qualname__}({self.name!r}) at {id(self):#x}>'
return '<{}.{}({!r}) at {:#x}>'.format(
self.__class__.__module__,
self.__class__.__qualname__,
self.name,
id(self),
)
def read_setup_file(filename): # noqa: C901
"""Reads a Setup file and returns Extension instances."""
from distutils.sysconfig import _variable_rx, expand_makefile_vars, parse_makefile
from distutils.sysconfig import parse_makefile, expand_makefile_vars, _variable_rx
from distutils.text_file import TextFile
from distutils.util import split_quoted
@@ -166,11 +156,11 @@ def read_setup_file(filename): # noqa: C901
# <module> ... [<sourcefile> ...] [<cpparg> ...] [<library> ...]
file = TextFile(
filename,
strip_comments=True,
skip_blanks=True,
join_lines=True,
lstrip_ws=True,
rstrip_ws=True,
strip_comments=1,
skip_blanks=1,
join_lines=1,
lstrip_ws=1,
rstrip_ws=1,
)
try:
extensions = []
@@ -183,7 +173,7 @@ def read_setup_file(filename): # noqa: C901
continue
if line[0] == line[-1] == "*":
file.warn(f"'{line}' lines not handled yet")
file.warn("'%s' lines not handled yet" % line)
continue
line = expand_makefile_vars(line, vars)
@@ -249,7 +239,7 @@ def read_setup_file(filename): # noqa: C901
# and append it to sources. Hmmmm.
ext.extra_objects.append(word)
else:
file.warn(f"unrecognized argument '{word}'")
file.warn("unrecognized argument '%s'" % word)
extensions.append(ext)
finally: