| 1 |
import os |
| 2 |
import platform |
| 3 |
import sys |
| 4 |
from distutils.errors import CCompilerError |
| 5 |
from distutils.errors import DistutilsExecError |
| 6 |
from distutils.errors import DistutilsPlatformError |
| 7 |
|
| 8 |
from setuptools import Extension |
| 9 |
from setuptools import setup |
| 10 |
from setuptools.command.build_ext import build_ext |
| 11 |
|
| 12 |
ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])] |
| 13 |
|
| 14 |
|
| 15 |
class BuildFailed(Exception): |
| 16 |
pass |
| 17 |
|
| 18 |
|
| 19 |
class ve_build_ext(build_ext): |
| 20 |
"""This class allows C extension building to fail.""" |
| 21 |
|
| 22 |
def run(self): |
| 23 |
try: |
| 24 |
build_ext.run(self) |
| 25 |
except DistutilsPlatformError: |
| 26 |
raise BuildFailed() |
| 27 |
|
| 28 |
def build_extension(self, ext): |
| 29 |
try: |
| 30 |
build_ext.build_extension(self, ext) |
| 31 |
except (CCompilerError, DistutilsExecError, DistutilsPlatformError): |
| 32 |
raise BuildFailed() |
| 33 |
except ValueError: |
| 34 |
# this can happen on Windows 64 bit, see Python issue 7511 |
| 35 |
if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3 |
| 36 |
raise BuildFailed() |
| 37 |
raise |
| 38 |
|
| 39 |
|
| 40 |
def run_setup(with_binary): |
| 41 |
setup( |
| 42 |
name="MarkupSafe", |
| 43 |
cmdclass={"build_ext": ve_build_ext}, |
| 44 |
ext_modules=ext_modules if with_binary else [], |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
def show_message(*lines): |
| 49 |
print("=" * 74) |
| 50 |
for line in lines: |
| 51 |
print(line) |
| 52 |
print("=" * 74) |
| 53 |
|
| 54 |
|
| 55 |
supports_speedups = platform.python_implementation() not in {"PyPy", "Jython"} |
| 56 |
|
| 57 |
if os.environ.get("CIBUILDWHEEL", "0") == "1" and supports_speedups: |
| 58 |
run_setup(True) |
| 59 |
elif supports_speedups: |
| 60 |
try: |
| 61 |
run_setup(True) |
| 62 |
except BuildFailed: |
| 63 |
show_message( |
| 64 |
"WARNING: The C extension could not be compiled, speedups" |
| 65 |
" are not enabled.", |
| 66 |
"Failure information, if any, is above.", |
| 67 |
"Retrying the build without the C extension now.", |
| 68 |
) |
| 69 |
run_setup(False) |
| 70 |
show_message( |
| 71 |
"WARNING: The C extension could not be compiled, speedups" |
| 72 |
" are not enabled.", |
| 73 |
"Plain-Python build succeeded.", |
| 74 |
) |
| 75 |
else: |
| 76 |
run_setup(False) |
| 77 |
show_message( |
| 78 |
"WARNING: C extensions are not supported on this Python" |
| 79 |
" platform, speedups are not enabled.", |
| 80 |
"Plain-Python build succeeded.", |
| 81 |
) |