2

I have a C++ code which execute python script with boost_python package. Everything is fine, as longa as I extract int, string, or other not-array variables from python. However I have to extract a numpy::ndarray and convert it to cpp vector. I tried as follow:

main.cpp

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

using namespace boost::python;

int main()
double t_end=7
    try
    {
    Py_Initialize();
    object module = import("__main__");
    object name_space = module.attr("__dict__");
    exec_file("MyModule.py", name_space, name_space);

    object MyFunc = name_space["MyFunc"];
    object result = MyFunc(t_end);

    auto result_array = extract<numpy::ndarray>(result);
    const numpy::ndarray& ret = result_array();
    int input_size = ret.shape(0);
    double* input_ptr = reinterpret_cast<double*>(ret.get_data());
    std::vector<double> v(input_size);
    for (int i = 0; i < input_size; ++i)
        v[i] = *(input_ptr + i);
}
catch (error_already_set)
{
    PyErr_Print();
}

Py_Finalize();

And example py script:

MyModule.py

import numpy as np
def MyFunc(t_end):
    result = np.array([2,3,1,t_end])
    return results

However it ends with error:

read access violation BOOST_NUMPY_ARRAY_API was nullptr

I also was trying to declare numpy::ndarray directly like numpy::ndarray result_array = extract<numpy::ndarray>(result); But the error is exactly the same. I've checked if my ndarray is not empty by printing it directly from python, and it is not. At the python step all seems to be correct. So what is causing the violation and how to fix it?

7
  • boost.org/doc/libs/release/libs/python/doc/html/numpy/tutorial/… Commented Feb 27, 2019 at 11:48
  • 1
    If you mean setting headers, I did it. I know this tutorial however it doesn't help with my issue. It's more general about working with arrays, and in my case I think the problem is on the extracting step. I can comment all starting from int input_size = ret.shape(0);, and it still fails with the same error. Commented Feb 27, 2019 at 11:57
  • Provide MVCE. Commented Feb 27, 2019 at 12:32
  • OK. Question edited. Commented Feb 27, 2019 at 12:41
  • 3
    You forget to initialize the numpy module before using it. Add numpy::initialize(); after the call to Py_Initialize();. Commented Feb 27, 2019 at 12:57

1 Answer 1

2

That error occurs since you're using the numpy module without first initializing it.

Notice the beginning of the official tutorial:

Initialise the Python runtime, and the numpy module. Failure to call these results in segmentation errors:

namespace np = boost::python::numpy;
int main(int argc, char **argv)
{
  Py_Initialize();
  np::initialize();

Your code is lacking the call to np::initialize();.

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

1 Comment

Thank you, now it works. I was in the wrong conviction, that Py_Initialize(); would also initialise derivatives. This small detale was out of my mind when I was reading tutorial.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.