6

I am trying to pass a numpy array to C++ using Boost.Python.

The C++ code is:

#include <boost/python.hpp>
#include <boost/python/numpy.hpp>

void f(boost::python::numpy::ndarray& x){}

BOOST_PYTHON_MODULE(libtest)
{
  boost::python::def("f", f);
}

The Python code is:

import libtest
import numpy
x=numpy.array(range(3))
libtest.f(x)

This gives a segmentation fault. This happens when passing the variable by value and by reference.

I have found a way to do what I needed. However the purpose of using Boost.Python was to be able to simply call the functions from the module without having to write a wrapper on the Python side as is the case with ctypes where certain types or return values have to be dealt with.

Is is possible to simply pass a reference to a numpy array?

Thanks!

1 Answer 1

4

I had the same problem and apparently solved it by putting

boost::python::numpy::initialize();

at top of my BOOST_PYHON_MODULE definition.

Sign up to request clarification or add additional context in comments.

1 Comment

same for me while using Boost 1.77.0 along with python 2.7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.