0

I try to do a python wrapper to bind some C++ functions and types to python. My issue is when I try to convert a custom matrix type to a numpy ndarray. The most convincing solution is to use PyArray_SimpleNewFromData.

To test its behaviour, as I didn't manage to do what I wanted I tried to implement a simple test:

PyObject* ConvertToPython(...) {
    uint8_t test[10] = {12, 15, 82, 254, 10, 32, 0, 8, 127, 54};
    int32_t ndims = 1;
    npy_intp dims[1];
    dims[0] = 10;
    int32_t typenum = (int32_t)NPY_UBYTE;
    PyObject* python_object = PyArray_SimpleNewFromData(ndims, dims, typenum, (void*)test);
    Py_XINCREF(python_object);
    return python_object;
}

And then I got in python these results:

type(test) = <type 'numpy.ndarray'>
test.ndim = 1
test.dtype = uint8
test.shape = (10,)

But the values inside the array are:

test.values = [ 1  0  0  0  0  0  0  0 80  8]

I cannot figure out, what am I doing wrong ? And I am not very experienced doing a python Wrapper so any help would be appreciable !

2
  • How did you make it to work? Commented Jul 24, 2019 at 5:51
  • 1
    The PyArray_SimpleNewFromData steals a reference to the array so you need to handle yourself the reference to the input array. You must be sure it won't be deleted during its use in Python. In my example, the test array was deleted at the end of the function which was making the python array to wrap an unallocated chunk of memory. Check the accepted answer but to be short, I think this function cannot work with statically allocated arrays. Commented Jul 24, 2019 at 8:22

1 Answer 1

3

I would try with an array that has been allocated by malloc, and then perhaps settings some flag named OWNDATA in order to avoid a memory leak.

At least the garbage data can be explained if the instance of numpy.ndarray does not copy the data but just stores a pointer to the supplied array. After the functions returns, a pointer to stack allocated array points to memory that may change any time the stack is changed.

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

Comments

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.