Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
extension.py153 linesDownload Raw Back to setuptools
1import re2import functools3import distutils.core4import distutils.errors5import distutils.extension6from typing import TYPE_CHECKING7 8from .monkey import get_unpatched9 10 11def _have_cython():12    """13    Return True if Cython can be imported.14    """15    cython_impl = 'Cython.Distutils.build_ext'16    try:17        # from (cython_impl) import build_ext18        __import__(cython_impl, fromlist=['build_ext']).build_ext19        return True20    except Exception:21        pass22    return False23 24 25# for compatibility26have_pyrex = _have_cython27if TYPE_CHECKING:28    # Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/1096229    _Extension = distutils.core.Extension30else:31    _Extension = get_unpatched(distutils.core.Extension)32 33 34class Extension(_Extension):35    """36    Describes a single extension module.37 38    This means that all source files will be compiled into a single binary file39    ``<module path>.<suffix>`` (with ``<module path>`` derived from ``name`` and40    ``<suffix>`` defined by one of the values in41    ``importlib.machinery.EXTENSION_SUFFIXES``).42 43    In the case ``.pyx`` files are passed as ``sources and`` ``Cython`` is **not**44    installed in the build environment, ``setuptools`` may also try to look for the45    equivalent ``.cpp`` or ``.c`` files.46 47    :arg str name:48      the full name of the extension, including any packages -- ie.49      *not* a filename or pathname, but Python dotted name50 51    :arg list[str] sources:52      list of source filenames, relative to the distribution root53      (where the setup script lives), in Unix form (slash-separated)54      for portability.  Source files may be C, C++, SWIG (.i),55      platform-specific resource files, or whatever else is recognized56      by the "build_ext" command as source for a Python extension.57 58    :keyword list[str] include_dirs:59      list of directories to search for C/C++ header files (in Unix60      form for portability)61 62    :keyword list[tuple[str, str|None]] define_macros:63      list of macros to define; each macro is defined using a 2-tuple:64      the first item corresponding to the name of the macro and the second65      item either a string with its value or None to66      define it without a particular value (equivalent of "#define67      FOO" in source or -DFOO on Unix C compiler command line)68 69    :keyword list[str] undef_macros:70      list of macros to undefine explicitly71 72    :keyword list[str] library_dirs:73      list of directories to search for C/C++ libraries at link time74 75    :keyword list[str] libraries:76      list of library names (not filenames or paths) to link against77 78    :keyword list[str] runtime_library_dirs:79      list of directories to search for C/C++ libraries at run time80      (for shared extensions, this is when the extension is loaded).81      Setting this will cause an exception during build on Windows82      platforms.83 84    :keyword list[str] extra_objects:85      list of extra files to link with (eg. object files not implied86      by 'sources', static library that must be explicitly specified,87      binary resource files, etc.)88 89    :keyword list[str] extra_compile_args:90      any extra platform- and compiler-specific information to use91      when compiling the source files in 'sources'.  For platforms and92      compilers where "command line" makes sense, this is typically a93      list of command-line arguments, but for other platforms it could94      be anything.95 96    :keyword list[str] extra_link_args:97      any extra platform- and compiler-specific information to use98      when linking object files together to create the extension (or99      to create a new static Python interpreter).  Similar100      interpretation as for 'extra_compile_args'.101 102    :keyword list[str] export_symbols:103      list of symbols to be exported from a shared extension.  Not104      used on all platforms, and not generally necessary for Python105      extensions, which typically export exactly one symbol: "init" +106      extension_name.107 108    :keyword list[str] swig_opts:109      any extra options to pass to SWIG if a source file has the .i110      extension.111 112    :keyword list[str] depends:113      list of files that the extension depends on114 115    :keyword str language:116      extension language (i.e. "c", "c++", "objc"). Will be detected117      from the source extensions if not provided.118 119    :keyword bool optional:120      specifies that a build failure in the extension should not abort the121      build process, but simply not install the failing extension.122 123    :keyword bool py_limited_api:124      opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.125 126    :raises setuptools.errors.PlatformError: if 'runtime_library_dirs' is127      specified on Windows. (since v63)128    """129 130    def __init__(self, name, sources, *args, **kw):131        # The *args is needed for compatibility as calls may use positional132        # arguments. py_limited_api may be set only via keyword.133        self.py_limited_api = kw.pop("py_limited_api", False)134        super().__init__(name, sources, *args, **kw)135 136    def _convert_pyx_sources_to_lang(self):137        """138        Replace sources with .pyx extensions to sources with the target139        language extension. This mechanism allows language authors to supply140        pre-converted sources but to prefer the .pyx sources.141        """142        if _have_cython():143            # the build has Cython, so allow it to compile the .pyx files144            return145        lang = self.language or ''146        target_ext = '.cpp' if lang.lower() == 'c++' else '.c'147        sub = functools.partial(re.sub, '.pyx$', target_ext)148        self.sources = list(map(sub, self.sources))149 150 151class Library(Extension):152    """Just like a regular Extension, but built as a library instead"""153 
codekingpro/portable-devtools · Team Ai