[C++-sig] Returning values to Python in C++ reference arguments

Holger Joukl Holger.Joukl at LBBW.de
Tue May 26 14:14:24 CEST 2015


Hi,

> Von: Jason Addison
> You can return results via function arguments from C to Python (see
> code below).
>
> Why is something similar impossible with Boost Python?
>
> my_module2.c
> >>>>
> /*
> clang -c -I/usr/include/python2.7 my_module2.c
> clang -dynamiclib -o my_module2.so -lpython2.7 my_module2.o
>  */
>
> void foo_bar(int *a, double *b) {
>     *a = 12;
>     *b = 6.2832;
> }
> <<<<
>
> byref2.py
> >>>>
> from ctypes import *
> my_module2 = CDLL('my_module2.so')
>

Because you don't create Python built-in int or float objects here:

> x = c_int(0)
> y = c_double(0)

What does type(x) and type(y) give you?

> my_module2.foo_bar(byref(x), byref(y))
>
> assert(x.value == 12)
> assert(y.value == 6.2832)
> <<<<

(unrelated sidenote: it's probably not the best idea to test float/double
equality)

Best regards
Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart



More information about the Cplusplus-sig mailing list