Skip to content

Commit c63c70e

Browse files
authored
Merge pull request #91 from realpython/jima-python-bindings
Jima python bindings
2 parents 626ed97 + 72b7345 commit c63c70e

File tree

12 files changed

+240
-0
lines changed

12 files changed

+240
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
import cffi_example
3+
4+
if __name__ == "__main__":
5+
# Sample data for our call:
6+
x, y = 6, 2.3
7+
8+
answer = cffi_example.lib.cmult(x, y)
9+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include "cmult.h"
3+
4+
5+
float cmult(int int_param, float float_param) {
6+
float return_value = int_param * float_param;
7+
printf(" In cmult : int: %d float %.1f returning %.1f\n", int_param,
8+
float_param, return_value);
9+
return return_value;
10+
}
11+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
float cmult(int int_param, float float_param);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include "cppmult.hpp"
4+
5+
6+
float cppmult(int int_param, float float_param) {
7+
float return_value = int_param * float_param;
8+
std::cout << std::setprecision(1) << std::fixed
9+
<< " In cppmul: int: " << int_param
10+
<< " float " << float_param
11+
<< " returning " << return_value
12+
<< std::endl;
13+
return return_value;
14+
}
15+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
float cppmult(int int_param, float float_param);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
""" Simple examples of calling C functions through ctypes module. """
3+
import ctypes
4+
import pathlib
5+
6+
7+
if __name__ == "__main__":
8+
# Load the shared library into c types.
9+
libname = pathlib.Path().absolute() / "libcmult.so"
10+
c_lib = ctypes.CDLL(libname)
11+
12+
# Sample data for our call:
13+
x, y = 6, 2.3
14+
15+
# This will not work:
16+
# answer = c_lib.cmult(x, y)
17+
18+
# This produces a bad answer:
19+
answer = c_lib.cmult(x, ctypes.c_float(y))
20+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
21+
print()
22+
23+
# You need tell ctypes that the function returns a float
24+
c_lib.cmult.restype = ctypes.c_float
25+
answer = c_lib.cmult(x, ctypes.c_float(y))
26+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
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( int_param, float_param ):
7+
return cppmult( int_param, float_param )
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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}")
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 pybind11_example
3+
4+
if __name__ == "__main__":
5+
# Sample data for our call:
6+
x, y = 6, 2.3
7+
8+
answer = pybind11_example.cpp_function(x, y)
9+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <pybind11/pybind11.h>
2+
#include <cppmult.hpp>
3+
4+
PYBIND11_MODULE(pybind11_example, m) {
5+
m.doc() = "pybind11 example plugin"; // Optional module docstring
6+
m.def("cpp_function", &cppmult, "A function which multiplies two numbers");
7+
}

0 commit comments

Comments
 (0)