Path: blob/main/test/lib/python3.9/site-packages/setuptools/extension.py
4798 views
import re1import functools2import distutils.core3import distutils.errors4import distutils.extension56from .monkey import get_unpatched789def _have_cython():10"""11Return True if Cython can be imported.12"""13cython_impl = 'Cython.Distutils.build_ext'14try:15# from (cython_impl) import build_ext16__import__(cython_impl, fromlist=['build_ext']).build_ext17return True18except Exception:19pass20return False212223# for compatibility24have_pyrex = _have_cython2526_Extension = get_unpatched(distutils.core.Extension)272829class Extension(_Extension):30"""Extension that uses '.c' files in place of '.pyx' files"""3132def __init__(self, name, sources, *args, **kw):33# The *args is needed for compatibility as calls may use positional34# arguments. py_limited_api may be set only via keyword.35self.py_limited_api = kw.pop("py_limited_api", False)36super().__init__(name, sources, *args, **kw)3738def _convert_pyx_sources_to_lang(self):39"""40Replace sources with .pyx extensions to sources with the target41language extension. This mechanism allows language authors to supply42pre-converted sources but to prefer the .pyx sources.43"""44if _have_cython():45# the build has Cython, so allow it to compile the .pyx files46return47lang = self.language or ''48target_ext = '.cpp' if lang.lower() == 'c++' else '.c'49sub = functools.partial(re.sub, '.pyx$', target_ext)50self.sources = list(map(sub, self.sources))515253class Library(Extension):54"""Just like a regular Extension, but built as a library instead"""555657