[C++-sig] call C-functions (by reference) from Python (using Boost.Python)

Lutz.Lauterbach at qimonda.com Lutz.Lauterbach at qimonda.com
Wed Dec 6 10:40:22 CET 2006


I have been successfully installing and using Boost.Python on Win2k for
a 
short while, providing C-functions to Python which uses call-by-value
and even a variable number of parameters. But since some days I get
stuck at the problem of passing parameters from Python to C using
call-by-reference. Here two little (simplified!) examples:

  void jz_void_float1(float a, float &b) { b=a; }
  void jz_void_float2(float a, float *b) { *b=a; }

  #include <boost/python.hpp>
  using namespace boost::python;
  BOOST_PYTHON_MODULE(hello) {
     def("jz_void_float1", jz_void_float1);
     def("jz_void_float2", jz_void_float2);
  }

It compiles without errors and I can call the function in Python, but
then it complains about the types:

  >>> var=3.3
  >>> jz_void_float1(4.4,var)
     Traceback (most recent call last): File "<stdin>", line 1, in ?
     Boost.Python.ArgumentError: Python argument types in
     hello.jz_void_float(float, float) did not match C++ signature:
     jz_void_float(f, f {lvalue})
 
I would like to know how to "convert" the types or whether this idea of 
implementation is doable at all (or whether another thin-wrapper "layer"
is 
necessary or whatever). I have seen the pointer-stuff working with char
types, but all the others were failing.

The intention of the call-by-reference is obviously to change the
Python-value directly via the pointer, since there is only one return
value, but we want to modify/return several data from the C-function.

Please do not say "Boost.Python was designed for C++ and not for C, so
better use SWIG or whatever", because I am sold on the
"minimal-invasive" methodology of Boost.Python which makes us able to
use/develop our C-code even further (in C).

Best regards, Lutz.



More information about the Cplusplus-sig mailing list