diff --git a/build_rust.py b/build_rust.py
index 662474dcb4..d2726cee26 100644
--- a/build_rust.py
+++ b/build_rust.py
@@ -1,8 +1,10 @@
# A build script for poetry that adds the rust extension.
+import itertools
import os
from typing import Any, Dict
+from packaging.specifiers import SpecifierSet
from setuptools_rust import Binding, RustExtension
@@ -14,6 +16,8 @@ def build(setup_kwargs: Dict[str, Any]) -> None:
target="synapse.synapse_rust",
path=cargo_toml_path,
binding=Binding.PyO3,
+ # This flag is a no-op in the latest versions. Instead, we need to
+ # specify this in the `bdist_wheel` config below.
py_limited_api=True,
# We force always building in release mode, as we can't tell the
# difference between using `poetry` in development vs production.
@@ -21,3 +25,18 @@ def build(setup_kwargs: Dict[str, Any]) -> None:
)
setup_kwargs.setdefault("rust_extensions", []).append(extension)
setup_kwargs["zip_safe"] = False
+
+ # We lookup the minimum supported python version by looking at
+ # `python_requires` (e.g. ">=3.9.0,<4.0.0") and finding the first python
+ # version that matches. We then convert that into the `py_limited_api` form,
+ # e.g. cp39 for python 3.9.
+ py_limited_api: str
+ python_bounds = SpecifierSet(setup_kwargs["python_requires"])
+ for minor_version in itertools.count(start=8):
+ if f"3.{minor_version}.0" in python_bounds:
+ py_limited_api = f"cp3{minor_version}"
+ break
+
+ setup_kwargs.setdefault("options", {}).setdefault("bdist_wheel", {})[
+ "py_limited_api"
+ ] = py_limited_api
|