-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
69 lines (58 loc) · 2.24 KB
/
Copy pathsetup.py
File metadata and controls
69 lines (58 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# SPDX-License-Identifier: 0BSD
"""Build script for the optional compiled accelerator.
The _fast extension is built when a C compiler is available. If the
compiler or Cython is missing the package still installs, with the
pure-Python codec as the only backend.
"""
import sys
from pathlib import Path
from setuptools import Extension, setup
from setuptools._distutils.errors import DistutilsPlatformError
from setuptools.command.build_ext import build_ext
class OptionalBuildExt(build_ext):
"""Build extensions but downgrade failures to a pure-Python install."""
def run(self):
try:
super().run()
except (DistutilsPlatformError, FileNotFoundError) as e:
print(f"cborx: skipping extension build: {e}", file=sys.stderr)
self.extensions = []
def build_extension(self, ext):
try:
super().build_extension(ext)
except Exception as e: # noqa: BLE001
# Any compile failure (missing compiler, bad flags, target
# mismatch) must degrade to the pure-Python install, not
# abort it. The exception family varies by platform, so a
# broad catch is intentional.
print(
f"cborx: _fast extension failed, using pure Python: {e}",
file=sys.stderr,
)
def _extensions():
if sys.implementation.name != "cpython":
# The accelerator targets the CPython API. PyPy's cpyext layer
# cannot handle its recursion depth safely, and the pure codec
# is faster under the JIT anyway.
return []
pyx = "src/cborx/_fast.pyx"
try:
from Cython.Build import cythonize
except ImportError:
c_source = str(Path(pyx).with_suffix(".c"))
if Path(c_source).exists():
return [Extension("cborx._fast", [c_source])]
return []
return cythonize(
[Extension("cborx._fast", [pyx])],
compiler_directives={
"language_level": "3",
# The module holds no shared mutable state, so it is safe
# to run with the GIL disabled on free-threaded builds.
"freethreading_compatible": True,
},
)
setup(
ext_modules=_extensions(),
cmdclass={"build_ext": OptionalBuildExt},
)