Skip to content

Commit f4bca67

Browse files
authored
Add a setuptools/CFFI example (#8)
* add CFFI example * switch to a setup.cfg file * fix MANIFEST.in * list cffi under backends dependency-group * modify abi3 extension test to work with cffi * add newline
1 parent 98182e2 commit f4bca67

File tree

8 files changed

+62
-2
lines changed

8 files changed

+62
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ backends = [
55
"scikit-build-core",
66
"setuptools @ git+https://github.com/ngoldbaum/setuptools@abi3.abi3t",
77
"packaging @ git+https://github.com/ngoldbaum/packaging@abi3.abi3t",
8+
"cffi @ git+https://github.com/ngoldbaum/cffi@abi3t.abi3",
89
]
910
build-tools = [
1011
"cython @ git+https://github.com/cython/cython@freethreading-limited-api-preview",

setuptools/cffi/MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
recursive-include src/limited *.py
2+
include setup.py
3+
include pyproject.toml

setuptools/cffi/pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[build-system]
2+
build-backend = "setuptools.build_meta"
3+
requires = [
4+
"setuptools@git+https://github.com/ngoldbaum/setuptools@abi3.abi3t",
5+
"packaging@git+https://github.com/ngoldbaum/packaging@abi3.abi3t",
6+
"cffi@git+https://github.com/ngoldbaum/cffi@abi3t.abi3",
7+
]
8+
9+
[project]
10+
name = "limited-api"
11+
version = "1.2.3"
12+
dependencies = [
13+
"cffi@git+https://github.com/ngoldbaum/cffi@abi3t.abi3",
14+
]

setuptools/cffi/setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bdist_wheel]
2+
py_limited_api = cp315

setuptools/cffi/setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CFFI doesn't support declarative setup for this
2+
# see https://github.com/python-cffi/cffi/issues/55
3+
from setuptools import setup
4+
5+
setup(
6+
cffi_modules=["src/limited/gen_limited.py:ffibuilder"],
7+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from ._limited import lib
2+
3+
add = lib.add
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from cffi import FFI
2+
3+
ffibuilder = FFI()
4+
5+
ffibuilder.set_source(
6+
"limited._limited",
7+
r"""
8+
#include <stdint.h>
9+
10+
static int64_t add(int64_t a, int64_t b) {
11+
return a + b;
12+
}
13+
""",
14+
libraries=[],
15+
)
16+
17+
ffibuilder.cdef(
18+
r"""
19+
int64_t add(int64_t a, int64_t b);
20+
"""
21+
)
22+
23+
if __name__ == "__main__":
24+
ffibuilder.compile(verbose=True)

tests/test_wheel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@
3838
3939
import limited
4040
41-
# if the build system created a package, import the actual extension
4241
if hasattr(limited, "__path__"):
43-
from limited import limited
42+
if hasattr(limited, "lib"):
43+
# limited.lib is present for CFFI. We had to create a Python wrapper package
44+
# because CFFI doesn't directly expose wrapped functions in the top-level
45+
# namespace of an extension module
46+
limited = limited._limited
47+
else:
48+
# if the build system created a package, import the actual extension
49+
from limited import limited
4450
4551
assert ".abi3" in Path(limited.__file__).name, (
4652
f"{limited.__file__} is not abi3 extension")

0 commit comments

Comments
 (0)