summaryrefslogtreecommitdiffstats
path: root/contrib/tools/cython/Cython/Distutils
diff options
context:
space:
mode:
authornik-bes <[email protected]>2025-05-19 07:20:13 +0300
committernik-bes <[email protected]>2025-05-19 07:36:02 +0300
commit317b7368e24941ff76499f500579fd9b10f6656e (patch)
treeabbcbaea595e7d2e9f23cf59a408b3082fe4340d /contrib/tools/cython/Cython/Distutils
parent6b666a52d40308ab9b3532cd8d3008b9f37cfffb (diff)
Update Cython to 3.0.10.
commit_hash:b43c96b868cd36d636192fd2c6024d9f0d2fb6f8
Diffstat (limited to 'contrib/tools/cython/Cython/Distutils')
-rw-r--r--contrib/tools/cython/Cython/Distutils/build_ext.py147
-rw-r--r--contrib/tools/cython/Cython/Distutils/extension.py5
-rw-r--r--contrib/tools/cython/Cython/Distutils/old_build_ext.py78
3 files changed, 170 insertions, 60 deletions
diff --git a/contrib/tools/cython/Cython/Distutils/build_ext.py b/contrib/tools/cython/Cython/Distutils/build_ext.py
index 598bb4a89b1..f04a4e0fa70 100644
--- a/contrib/tools/cython/Cython/Distutils/build_ext.py
+++ b/contrib/tools/cython/Cython/Distutils/build_ext.py
@@ -1,25 +1,138 @@
import sys
+import os
-if 'setuptools' in sys.modules:
+try:
+ from __builtin__ import basestring
+except ImportError:
+ basestring = str
+
+# Always inherit from the "build_ext" in distutils since setuptools already imports
+# it from Cython if available, and does the proper distutils fallback otherwise.
+# https://github.com/pypa/setuptools/blob/9f1822ee910df3df930a98ab99f66d18bb70659b/setuptools/command/build_ext.py#L16
+
+# setuptools imports Cython's "build_ext", so make sure we go first.
+_build_ext_module = sys.modules.get('setuptools.command.build_ext')
+if _build_ext_module is None:
try:
- from setuptools.command.build_ext import build_ext as _build_ext
+ import distutils.command.build_ext as _build_ext_module
except ImportError:
- # We may be in the process of importing setuptools, which tries
- # to import this.
- from distutils.command.build_ext import build_ext as _build_ext
-else:
+ # Python 3.12 no longer has distutils, but setuptools can replace it.
+ try:
+ import setuptools.command.build_ext as _build_ext_module
+ except ImportError:
+ raise ImportError("'distutils' cannot be imported. Please install setuptools.")
+
+
+# setuptools remembers the original distutils "build_ext" as "_du_build_ext"
+_build_ext = getattr(_build_ext_module, '_du_build_ext', None)
+if _build_ext is None:
+ _build_ext = getattr(_build_ext_module, 'build_ext', None)
+if _build_ext is None:
from distutils.command.build_ext import build_ext as _build_ext
-class new_build_ext(_build_ext, object):
+class build_ext(_build_ext, object):
+
+ user_options = _build_ext.user_options + [
+ ('cython-cplus', None,
+ "generate C++ source files"),
+ ('cython-create-listing', None,
+ "write errors to a listing file"),
+ ('cython-line-directives', None,
+ "emit source line directives"),
+ ('cython-include-dirs=', None,
+ "path to the Cython include files" + _build_ext.sep_by),
+ ('cython-c-in-temp', None,
+ "put generated C files in temp directory"),
+ ('cython-gen-pxi', None,
+ "generate .pxi file for public declarations"),
+ ('cython-directives=', None,
+ "compiler directive overrides"),
+ ('cython-gdb', None,
+ "generate debug information for cygdb"),
+ ('cython-compile-time-env', None,
+ "cython compile time environment"),
+ ]
+
+ boolean_options = _build_ext.boolean_options + [
+ 'cython-cplus', 'cython-create-listing', 'cython-line-directives',
+ 'cython-c-in-temp', 'cython-gdb',
+ ]
+
+ def initialize_options(self):
+ super(build_ext, self).initialize_options()
+ self.cython_cplus = 0
+ self.cython_create_listing = 0
+ self.cython_line_directives = 0
+ self.cython_include_dirs = None
+ self.cython_directives = None
+ self.cython_c_in_temp = 0
+ self.cython_gen_pxi = 0
+ self.cython_gdb = False
+ self.cython_compile_time_env = None
+
def finalize_options(self):
- if self.distribution.ext_modules:
- nthreads = getattr(self, 'parallel', None) # -j option in Py3.5+
- nthreads = int(nthreads) if nthreads else None
- from Cython.Build.Dependencies import cythonize
- self.distribution.ext_modules[:] = cythonize(
- self.distribution.ext_modules, nthreads=nthreads, force=self.force)
- super(new_build_ext, self).finalize_options()
-
-# This will become new_build_ext in the future.
-from .old_build_ext import old_build_ext as build_ext
+ super(build_ext, self).finalize_options()
+ if self.cython_include_dirs is None:
+ self.cython_include_dirs = []
+ elif isinstance(self.cython_include_dirs, basestring):
+ self.cython_include_dirs = \
+ self.cython_include_dirs.split(os.pathsep)
+ if self.cython_directives is None:
+ self.cython_directives = {}
+
+ def get_extension_attr(self, extension, option_name, default=False):
+ return getattr(self, option_name) or getattr(extension, option_name, default)
+
+ def build_extension(self, ext):
+ from Cython.Build.Dependencies import cythonize
+
+ # Set up the include_path for the Cython compiler:
+ # 1. Start with the command line option.
+ # 2. Add in any (unique) paths from the extension
+ # cython_include_dirs (if Cython.Distutils.extension is used).
+ # 3. Add in any (unique) paths from the extension include_dirs
+ includes = list(self.cython_include_dirs)
+ for include_dir in getattr(ext, 'cython_include_dirs', []):
+ if include_dir not in includes:
+ includes.append(include_dir)
+
+ # In case extension.include_dirs is a generator, evaluate it and keep
+ # result
+ ext.include_dirs = list(ext.include_dirs)
+ for include_dir in ext.include_dirs + list(self.include_dirs):
+ if include_dir not in includes:
+ includes.append(include_dir)
+
+ # Set up Cython compiler directives:
+ # 1. Start with the command line option.
+ # 2. Add in any (unique) entries from the extension
+ # cython_directives (if Cython.Distutils.extension is used).
+ directives = dict(self.cython_directives)
+ if hasattr(ext, "cython_directives"):
+ directives.update(ext.cython_directives)
+
+ if self.get_extension_attr(ext, 'cython_cplus'):
+ ext.language = 'c++'
+
+ options = {
+ 'use_listing_file': self.get_extension_attr(ext, 'cython_create_listing'),
+ 'emit_linenums': self.get_extension_attr(ext, 'cython_line_directives'),
+ 'include_path': includes,
+ 'compiler_directives': directives,
+ 'build_dir': self.build_temp if self.get_extension_attr(ext, 'cython_c_in_temp') else None,
+ 'generate_pxi': self.get_extension_attr(ext, 'cython_gen_pxi'),
+ 'gdb_debug': self.get_extension_attr(ext, 'cython_gdb'),
+ 'c_line_in_traceback': not getattr(ext, 'no_c_in_traceback', 0),
+ 'compile_time_env': self.get_extension_attr(ext, 'cython_compile_time_env', default=None),
+ }
+
+ new_ext = cythonize(
+ ext,force=self.force, quiet=self.verbose == 0, **options
+ )[0]
+
+ ext.sources = new_ext.sources
+ super(build_ext, self).build_extension(ext)
+
+# backward compatibility
+new_build_ext = build_ext
diff --git a/contrib/tools/cython/Cython/Distutils/extension.py b/contrib/tools/cython/Cython/Distutils/extension.py
index d8bdbf0f5bc..93744ec45e3 100644
--- a/contrib/tools/cython/Cython/Distutils/extension.py
+++ b/contrib/tools/cython/Cython/Distutils/extension.py
@@ -8,11 +8,6 @@ __revision__ = "$Id:$"
import sys
import distutils.extension as _Extension
-try:
- import warnings
-except ImportError:
- warnings = None
-
class Extension(_Extension.Extension):
# When adding arguments to this constructor, be sure to update
diff --git a/contrib/tools/cython/Cython/Distutils/old_build_ext.py b/contrib/tools/cython/Cython/Distutils/old_build_ext.py
index aa2a1cf2292..cec54d93d0a 100644
--- a/contrib/tools/cython/Cython/Distutils/old_build_ext.py
+++ b/contrib/tools/cython/Cython/Distutils/old_build_ext.py
@@ -8,7 +8,6 @@ Note that this module is deprecated. Use cythonize() instead.
__revision__ = "$Id:$"
-import inspect
import sys
import os
from distutils.errors import DistutilsPlatformError
@@ -16,7 +15,6 @@ from distutils.dep_util import newer, newer_group
from distutils import log
from distutils.command import build_ext as _build_ext
from distutils import sysconfig
-import warnings
try:
@@ -25,22 +23,30 @@ except ImportError:
basestring = str
+# FIXME: the below does not work as intended since importing 'Cython.Distutils' already
+# imports this module through 'Cython/Distutils/build_ext.py', so the condition is
+# always false and never prints the warning.
+"""
+import inspect
+import warnings
+
def _check_stack(path):
- try:
- for frame in inspect.getouterframes(inspect.currentframe(), 0):
- if path in frame[1].replace(os.sep, '/'):
- return True
- except Exception:
- pass
- return False
+ try:
+ for frame in inspect.getouterframes(inspect.currentframe(), 0):
+ if path in frame[1].replace(os.sep, '/'):
+ return True
+ except Exception:
+ pass
+ return False
+
if (not _check_stack('setuptools/extensions.py')
- and not _check_stack('pyximport/pyxbuild.py')
- and not _check_stack('Cython/Distutils/build_ext.py')):
+ and not _check_stack('pyximport/pyxbuild.py')
+ and not _check_stack('Cython/Distutils/build_ext.py')):
warnings.warn(
"Cython.Distutils.old_build_ext does not properly handle dependencies "
"and is deprecated.")
-
+"""
extension_name_re = _build_ext.extension_name_re
@@ -163,7 +169,7 @@ class old_build_ext(_build_ext.build_ext):
# _build_ext.build_ext.__setattr__(self, name, value)
self.__dict__[name] = value
- def finalize_options (self):
+ def finalize_options(self):
_build_ext.build_ext.finalize_options(self)
if self.cython_include_dirs is None:
self.cython_include_dirs = []
@@ -185,14 +191,11 @@ class old_build_ext(_build_ext.build_ext):
_build_ext.build_ext.run(self)
- def build_extensions(self):
- # First, sanity-check the 'extensions' list
- self.check_extensions_list(self.extensions)
-
+ def check_extensions_list(self, extensions):
+ # Note: might get called multiple times.
+ _build_ext.build_ext.check_extensions_list(self, extensions)
for ext in self.extensions:
ext.sources = self.cython_sources(ext.sources, ext)
- # Call original build_extensions
- _build_ext.build_ext.build_extensions(self)
def cython_sources(self, sources, extension):
"""
@@ -201,17 +204,6 @@ class old_build_ext(_build_ext.build_ext):
found, and return a modified 'sources' list with Cython source
files replaced by the generated C (or C++) files.
"""
- try:
- from Cython.Compiler.Main \
- import CompilationOptions, \
- default_options as cython_default_options, \
- compile as cython_compile
- from Cython.Compiler.Errors import PyrexError
- except ImportError:
- e = sys.exc_info()[1]
- print("failed to import Cython: %s" % e)
- raise DistutilsPlatformError("Cython does not appear to be installed")
-
new_sources = []
cython_sources = []
cython_targets = {}
@@ -252,10 +244,10 @@ class old_build_ext(_build_ext.build_ext):
# 2. Add in any (unique) paths from the extension
# cython_include_dirs (if Cython.Distutils.extension is used).
# 3. Add in any (unique) paths from the extension include_dirs
- includes = self.cython_include_dirs
+ includes = list(self.cython_include_dirs)
try:
for i in extension.cython_include_dirs:
- if not i in includes:
+ if i not in includes:
includes.append(i)
except AttributeError:
pass
@@ -264,19 +256,18 @@ class old_build_ext(_build_ext.build_ext):
# result
extension.include_dirs = list(extension.include_dirs)
for i in extension.include_dirs:
- if not i in includes:
+ if i not in includes:
includes.append(i)
# Set up Cython compiler directives:
# 1. Start with the command line option.
# 2. Add in any (unique) entries from the extension
# cython_directives (if Cython.Distutils.extension is used).
- directives = self.cython_directives
+ directives = dict(self.cython_directives)
if hasattr(extension, "cython_directives"):
directives.update(extension.cython_directives)
- # Set the target_ext to '.c'. Cython will change this to '.cpp' if
- # needed.
+ # Set the target file extension for C/C++ mode.
if cplus:
target_ext = '.cpp'
else:
@@ -314,13 +305,24 @@ class old_build_ext(_build_ext.build_ext):
if not cython_sources:
return new_sources
+ try:
+ from Cython.Compiler.Main \
+ import CompilationOptions, \
+ default_options as cython_default_options, \
+ compile as cython_compile
+ from Cython.Compiler.Errors import PyrexError
+ except ImportError:
+ e = sys.exc_info()[1]
+ print("failed to import Cython: %s" % e)
+ raise DistutilsPlatformError("Cython does not appear to be installed")
+
module_name = extension.name
for source in cython_sources:
target = cython_targets[source]
depends = [source] + list(extension.depends or ())
- if(source[-4:].lower()==".pyx" and os.path.isfile(source[:-3]+"pxd")):
- depends += [source[:-3]+"pxd"]
+ if source[-4:].lower() == ".pyx" and os.path.isfile(source[:-3] + "pxd"):
+ depends += [source[:-3] + "pxd"]
rebuild = self.force or newer_group(depends, target, 'newer')
if not rebuild and newest_dependency is not None:
rebuild = newer(newest_dependency, target)