views:

32

answers:

1

Hi all,

I have a function that takes an int-pointer and exposed it via boost::python. How can I call this function from python?

in C++ with boost::python:

void foo(int* i);
...
def("foo", foo);

in python:

import foo_ext
i = 12
foo_ext.foo(i)

results in

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
foo(int)
did not match C++ signature:
foo(int* i)

So how to pass a pointer?

A: 

From python.org's boost.python HowTo

Perhaps you'd like the resulting Python object to contain a raw pointer to the argument? In that case, the caveat is that if the lifetime of the C++ object ends before that of the Python object, that pointer will dangle and using the Python object may cause a crash.

Here's how to expose mutable C++ object during module initialisation:

scope().attr("a") = object(ptr(&class_instance));
synthesizerpatel