Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/extension.py @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
1 from __future__ import annotations | |
2 | |
3 import functools | |
4 import re | |
5 from typing import TYPE_CHECKING | |
6 | |
7 from setuptools._path import StrPath | |
8 | |
9 from .monkey import get_unpatched | |
10 | |
11 import distutils.core | |
12 import distutils.errors | |
13 import distutils.extension | |
14 | |
15 | |
16 def _have_cython(): | |
17 """ | |
18 Return True if Cython can be imported. | |
19 """ | |
20 cython_impl = 'Cython.Distutils.build_ext' | |
21 try: | |
22 # from (cython_impl) import build_ext | |
23 __import__(cython_impl, fromlist=['build_ext']).build_ext | |
24 except Exception: | |
25 return False | |
26 return True | |
27 | |
28 | |
29 # for compatibility | |
30 have_pyrex = _have_cython | |
31 if TYPE_CHECKING: | |
32 # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962 | |
33 from distutils.core import Extension as _Extension | |
34 else: | |
35 _Extension = get_unpatched(distutils.core.Extension) | |
36 | |
37 | |
38 class Extension(_Extension): | |
39 """ | |
40 Describes a single extension module. | |
41 | |
42 This means that all source files will be compiled into a single binary file | |
43 ``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and | |
44 ``<suffix>`` defined by one of the values in | |
45 ``importlib.machinery.EXTENSION_SUFFIXES``). | |
46 | |
47 In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not** | |
48 installed in the build environment, ``setuptools`` may also try to look for the | |
49 equivalent ``.cpp`` or ``.c`` files. | |
50 | |
51 :arg str name: | |
52 the full name of the extension, including any packages -- ie. | |
53 *not* a filename or pathname, but Python dotted name | |
54 | |
55 :arg list[str|os.PathLike[str]] sources: | |
56 list of source filenames, relative to the distribution root | |
57 (where the setup script lives), in Unix form (slash-separated) | |
58 for portability. Source files may be C, C++, SWIG (.i), | |
59 platform-specific resource files, or whatever else is recognized | |
60 by the "build_ext" command as source for a Python extension. | |
61 | |
62 :keyword list[str] include_dirs: | |
63 list of directories to search for C/C++ header files (in Unix | |
64 form for portability) | |
65 | |
66 :keyword list[tuple[str, str|None]] define_macros: | |
67 list of macros to define; each macro is defined using a 2-tuple: | |
68 the first item corresponding to the name of the macro and the second | |
69 item either a string with its value or None to | |
70 define it without a particular value (equivalent of "#define | |
71 FOO" in source or -DFOO on Unix C compiler command line) | |
72 | |
73 :keyword list[str] undef_macros: | |
74 list of macros to undefine explicitly | |
75 | |
76 :keyword list[str] library_dirs: | |
77 list of directories to search for C/C++ libraries at link time | |
78 | |
79 :keyword list[str] libraries: | |
80 list of library names (not filenames or paths) to link against | |
81 | |
82 :keyword list[str] runtime_library_dirs: | |
83 list of directories to search for C/C++ libraries at run time | |
84 (for shared extensions, this is when the extension is loaded). | |
85 Setting this will cause an exception during build on Windows | |
86 platforms. | |
87 | |
88 :keyword list[str] extra_objects: | |
89 list of extra files to link with (eg. object files not implied | |
90 by 'sources', static library that must be explicitly specified, | |
91 binary resource files, etc.) | |
92 | |
93 :keyword list[str] extra_compile_args: | |
94 any extra platform- and compiler-specific information to use | |
95 when compiling the source files in 'sources'. For platforms and | |
96 compilers where "command line" makes sense, this is typically a | |
97 list of command-line arguments, but for other platforms it could | |
98 be anything. | |
99 | |
100 :keyword list[str] extra_link_args: | |
101 any extra platform- and compiler-specific information to use | |
102 when linking object files together to create the extension (or | |
103 to create a new static Python interpreter). Similar | |
104 interpretation as for 'extra_compile_args'. | |
105 | |
106 :keyword list[str] export_symbols: | |
107 list of symbols to be exported from a shared extension. Not | |
108 used on all platforms, and not generally necessary for Python | |
109 extensions, which typically export exactly one symbol: "init" + | |
110 extension_name. | |
111 | |
112 :keyword list[str] swig_opts: | |
113 any extra options to pass to SWIG if a source file has the .i | |
114 extension. | |
115 | |
116 :keyword list[str] depends: | |
117 list of files that the extension depends on | |
118 | |
119 :keyword str language: | |
120 extension language (i.e. "c", "c++", "objc"). Will be detected | |
121 from the source extensions if not provided. | |
122 | |
123 :keyword bool optional: | |
124 specifies that a build failure in the extension should not abort the | |
125 build process, but simply not install the failing extension. | |
126 | |
127 :keyword bool py_limited_api: | |
128 opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`. | |
129 | |
130 :raises setuptools.errors.PlatformError: if ``runtime_library_dirs`` is | |
131 specified on Windows. (since v63) | |
132 """ | |
133 | |
134 # These 4 are set and used in setuptools/command/build_ext.py | |
135 # The lack of a default value and risk of `AttributeError` is purposeful | |
136 # to avoid people forgetting to call finalize_options if they modify the extension list. | |
137 # See example/rationale in https://github.com/pypa/setuptools/issues/4529. | |
138 _full_name: str #: Private API, internal use only. | |
139 _links_to_dynamic: bool #: Private API, internal use only. | |
140 _needs_stub: bool #: Private API, internal use only. | |
141 _file_name: str #: Private API, internal use only. | |
142 | |
143 def __init__( | |
144 self, | |
145 name: str, | |
146 sources: list[StrPath], | |
147 *args, | |
148 py_limited_api: bool = False, | |
149 **kw, | |
150 ): | |
151 # The *args is needed for compatibility as calls may use positional | |
152 # arguments. py_limited_api may be set only via keyword. | |
153 self.py_limited_api = py_limited_api | |
154 super().__init__( | |
155 name, | |
156 sources, # type: ignore[arg-type] # Vendored version of setuptools supports PathLike | |
157 *args, | |
158 **kw, | |
159 ) | |
160 | |
161 def _convert_pyx_sources_to_lang(self): | |
162 """ | |
163 Replace sources with .pyx extensions to sources with the target | |
164 language extension. This mechanism allows language authors to supply | |
165 pre-converted sources but to prefer the .pyx sources. | |
166 """ | |
167 if _have_cython(): | |
168 # the build has Cython, so allow it to compile the .pyx files | |
169 return | |
170 lang = self.language or '' | |
171 target_ext = '.cpp' if lang.lower() == 'c++' else '.c' | |
172 sub = functools.partial(re.sub, '.pyx$', target_ext) | |
173 self.sources = list(map(sub, self.sources)) | |
174 | |
175 | |
176 class Library(Extension): | |
177 """Just like a regular Extension, but built as a library instead""" |