Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement speedups with rust v2 #438

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ dist/
htmlcov/
.tox/
docs/_build/
/src/rust/target/
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ prune docs/_build
graft tests
include src/markupsafe/py.typed
include src/markupsafe/*.pyi
graft src/rust
prune src/rust/target
global-exclude *.pyc
2 changes: 1 addition & 1 deletion bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
("long plain", '"Hello, World!" * 1000'),
("long suffix", '"<strong>Hello, World!</strong>" + "x" * 100_000'),
):
for mod in "native", "speedups":
for mod in "native", "speedups", "rust_speedups":
subprocess.run(
[
sys.executable,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Source = "https://github.com/pallets/markupsafe/"
Chat = "https://discord.gg/pallets"

[build-system]
requires = ["setuptools"]
requires = ["setuptools", "setuptools-rust"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
Expand Down
94 changes: 20 additions & 74 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,30 @@
import os
import platform
import sys

from setuptools import Extension
from setuptools import setup
from setuptools.command.build_ext import build_ext
from setuptools.errors import CCompilerError
from setuptools.errors import ExecError
from setuptools.errors import PlatformError
from setuptools_rust import RustExtension

ext_modules = [Extension("markupsafe._speedups", ["src/markupsafe/_speedups.c"])]


class BuildFailed(Exception):
pass


class ve_build_ext(build_ext):
"""This class allows C extension building to fail."""

def run(self):
try:
super().run()
except PlatformError as e:
raise BuildFailed() from e

def build_extension(self, ext):
try:
super().build_extension(ext)
except (CCompilerError, ExecError, PlatformError) as e:
raise BuildFailed() from e
except ValueError as e:
# this can happen on Windows 64 bit, see Python issue 7511
if "'path'" in str(sys.exc_info()[1]): # works with Python 2 and 3
raise BuildFailed() from e
raise


def run_setup(with_binary):
setup(
cmdclass={"build_ext": ve_build_ext},
ext_modules=ext_modules if with_binary else [],
)


def show_message(*lines):
print("=" * 74)
for line in lines:
print(line)
print("=" * 74)


supports_speedups = platform.python_implementation() not in {
if platform.python_implementation() not in {
"PyPy",
"Jython",
"GraalVM",
}

if os.environ.get("CIBUILDWHEEL", "0") == "1" and supports_speedups:
run_setup(True)
elif supports_speedups:
try:
run_setup(True)
except BuildFailed:
show_message(
"WARNING: The C extension could not be compiled, speedups"
" are not enabled.",
"Failure information, if any, is above.",
"Retrying the build without the C extension now.",
)
run_setup(False)
show_message(
"WARNING: The C extension could not be compiled, speedups"
" are not enabled.",
"Plain-Python build succeeded.",
)
else:
run_setup(False)
show_message(
"WARNING: C extensions are not supported on this Python"
" platform, speedups are not enabled.",
"Plain-Python build succeeded.",
}:
local = os.environ.get("CIBUILDWHEEL", "0") != "1"
setup(
ext_modules=[
Extension(
"markupsafe._speedups", ["src/markupsafe/_speedups.c"], optional=local
)
],
rust_extensions=[
RustExtension(
"markupsafe._rust_speedups",
"src/rust/Cargo.toml",
optional=local,
debug=False,
)
],
)
else:
setup()
2 changes: 1 addition & 1 deletion src/markupsafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import typing as t

try:
from ._speedups import _escape_inner
from ._rust_speedups import _escape_inner
except ImportError:
from ._native import _escape_inner

Expand Down
1 change: 1 addition & 0 deletions src/markupsafe/_rust_speedups.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def _escape_inner(s: str, /) -> str: ...