Skip to content

Commit c3429ab

Browse files
committed
got cython example working
1 parent bc4e791 commit c3429ab

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" Example cython interface definition """
2+
3+
cdef extern from "cppmult.hpp":
4+
float cppmult(int int_param, float float_param)
5+
6+
def pymult( ip, fp ):
7+
return cppmult( ip, fp )
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
import cython_example
3+
4+
# sample data for our call:
5+
x, y = 6, 2.3
6+
7+
answer = cython_example.pymult(x, y)
8+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
9+

python-bindings/overview_article/tasks.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@
55
import os
66
import pathlib
77

8-
COMPILE = "gcc -c -Wall -Werror -fpic cmult.c -I /usr/include/python3.7"
9-
LINK = "gcc -shared -o libcmult.so cmult.o"
10-
11-
128
@invoke.task
139
def clean(c):
1410
""" Remove any built objects. """
15-
for pattern in ["*.o", "*.so", "cffi_example*", ]:
11+
for pattern in ["*.o", "*.so", "cffi_example* cython_wrapper.cpp", ]:
1612
c.run("rm -rf {}".format(pattern))
1713

14+
def print_banner(msg):
15+
print("==================================================")
16+
print("= {} ".format(msg))
17+
1818
@invoke.task
1919
def build_cmult(c):
2020
""" Compile and link the shared library (DLL) for the sample C++ code."""
21-
invoke.run(COMPILE)
22-
invoke.run(LINK)
21+
print_banner("Building C Library")
22+
invoke.run("gcc -c -Wall -Werror -fpic cmult.c -I /usr/include/python3.7")
23+
invoke.run("gcc -shared -o libcmult.so cmult.o")
24+
print("* Complete")
2325

2426

2527
@invoke.task(build_cmult)
2628
def test_ctypes(c):
29+
print_banner("Testing ctypes Module")
2730
invoke.run("python3 ctypes_test.py", pty=True)
2831

2932
@invoke.task(build_cmult)
3033
def build_cffi(c):
34+
print_banner("Building CFFI Module")
3135
ffi = cffi.FFI()
3236

3337
this_dir = pathlib.Path().absolute()
@@ -49,33 +53,64 @@ def build_cffi(c):
4953
)
5054

5155
ffi.compile()
56+
print("* Complete")
5257

5358
@invoke.task()
5459
def test_cffi(c):
60+
print_banner("Testing CFFI Module")
5561
invoke.run("python3 cffi_test.py", pty=True)
5662

63+
5764
@invoke.task()
58-
def build_pybind11(c):
65+
def build_cppmult(c):
5966
""" Compile and link the shared library (DLL) for the sample C++ code."""
60-
# compile and link the cmult c++ library
61-
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
62-
"cppmult.cpp -o libcppmult.so "
63-
)
64-
# compile and link the pybind11 wrapper library
67+
print_banner("Building C++ Library")
68+
invoke.run(
69+
"g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC cppmult.cpp -o libcppmult.so "
70+
)
71+
print("* Complete")
72+
73+
@invoke.task(build_cppmult)
74+
def build_pybind11(c):
75+
""" compile and link the pybind11 wrapper library """
76+
print_banner("Building PyBind11 Module")
6577
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
6678
"`python3 -m pybind11 --includes` "
6779
"-I /usr/include/python3.7 -I . "
6880
"pybind11_wrapper.cpp "
6981
"-o pybind11_example`python3.7-config --extension-suffix` "
7082
"-L. -lcppmult -Wl,-rpath,. "
7183
)
84+
print("* Complete")
7285

7386
@invoke.task()
7487
def test_pybind11(c):
88+
print_banner("Testing PyBind11 Module")
7589
invoke.run("python3 pybind11_test.py", pty=True)
7690

91+
@invoke.task(build_cppmult)
92+
def build_cython(c):
93+
""" build the cython extension module """
94+
print_banner("Building Cython Module")
95+
# run cython on the pyx file to create a .cpp file
96+
invoke.run("cython --cplus -3 cython_example.pyx -o cython_wrapper.cpp")
97+
98+
# compile and link the cython wrapper library
99+
invoke.run("g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
100+
"-I /usr/include/python3.7 -I . "
101+
"cython_wrapper.cpp "
102+
"-o cython_example`python3.7-config --extension-suffix` "
103+
"-L. -lcppmult -Wl,-rpath,."
104+
)
105+
print("* Complete")
106+
107+
@invoke.task()
108+
def test_cython(c):
109+
print_banner("Testing Cython Module")
110+
invoke.run("python3 cython_test.py", pty=True)
111+
77112
@invoke.task(clean, build_cmult, test_ctypes, build_cffi, test_cffi,
78-
build_pybind11, test_pybind11)
113+
build_pybind11, test_pybind11, build_cython, test_cython)
79114
def all(c):
80115
pass
81116

0 commit comments

Comments
 (0)