From a90d5e228923117313913862aca587c668f7beec Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 1 Nov 2025 17:51:25 +0100 Subject: [PATCH] Supported builds with limited API --- .github/workflows/build.yaml | 30 +++++++++++++++++++++++++++- .github/workflows/publish-twine.yaml | 4 ++-- README.md | 5 ++++- pyproject.toml | 2 +- setup.py | 11 ++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 0e01d96..8a0f19b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -25,7 +25,35 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install setuptools "cython >= 3" pytest + pip install setuptools "cython >= 3.1" pytest + - name: Build with Python ${{ matrix.python-version }} + run: | + python setup.py build + python setup.py build_ext -i + - name: Test with pytest + run: | + pytest + env: + LC_ALL: C.UTF-8 + LANG: C.UTF-8 + + build-limited-api: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + env: + USE_LIMITED_API: "1" + steps: + - uses: actions/checkout@v5 + - name: Set up Python + uses: actions/setup-python@v6 + python-version: "3.x" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools "cython >= 3.1" pytest - name: Build with Python ${{ matrix.python-version }} run: | python setup.py build diff --git a/.github/workflows/publish-twine.yaml b/.github/workflows/publish-twine.yaml index 28193c7..8ce6667 100644 --- a/.github/workflows/publish-twine.yaml +++ b/.github/workflows/publish-twine.yaml @@ -23,7 +23,7 @@ jobs: - name: Install cibuildwheel run: | python -m pip install --upgrade pip - python -m pip install cibuildwheel + python -m pip install "cython >= 3.1" cibuildwheel - name: Build wheels run: | python -m cibuildwheel --output-dir dist @@ -46,7 +46,7 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install setuptools "cython >= 3" + pip install setuptools "cython >= 3.1" - name: Build sdist run: | python setup.py build diff --git a/README.md b/README.md index ea66a16..9a4df24 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ functionality. ## Dependencies -* `Cython >= 3` (optional, only for building). If Cython is not available, the C files are not +* `Cython >= 3.1` (optional, only for building). If Cython is not available, the C files are not regenerated from their source. ## Quick installation guide @@ -25,6 +25,9 @@ or by running: python3 setup.py install ``` +In case the extension should be built with the limited Python API, set the environment variable +`USE_LIMITED_API` to any value. + ## Usage ```python3 diff --git a/pyproject.toml b/pyproject.toml index 0da637b..7b5a73a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [build-system] requires = [ "setuptools >= 43", - "Cython >= 3", + "Cython >= 3.1", ] build-backend = "setuptools.build_meta" diff --git a/setup.py b/setup.py index 420b1e9..5b493de 100644 --- a/setup.py +++ b/setup.py @@ -1,9 +1,13 @@ #!/usr/bin/python3 +import os import platform from setuptools import setup, Extension +use_limited_api = "USE_LIMITED_API" in os.environ + + extension_sources = ["cwcwidth/_impl.pyx"] define_macros = [] if platform.system() == "Windows": @@ -12,11 +16,17 @@ if platform.system() == "Windows": ("USE_MK_WCWIDTH", None), ) +options = {} +if use_limited_api: + define_macros.append(("Py_LIMITED_API", 0x030B0000)) # 3.11 + options["bdist_wheel"] = {"py_limited_api": "cp311"} + ext_modules = [ Extension( "cwcwidth._impl", extension_sources, define_macros=define_macros, + py_limited_api=use_limited_api, ) ] @@ -26,4 +36,5 @@ setup( package_data={ "cwcwidth": ["_impl.pyi", "py.typed"], }, + options=options, )