Question :
I know that this thing has been answered a lot of times and I have also read the documentation as well but still I am not able to clearly understand how is this working. As in, I am not able to understand how the values are populated in its arguments. The examples are not very clearly explaining it(or may be I am not able to). Can anyone please help me understand how are the arguments of this function populated? What should be their values?
I have to pass a vector from C++ to Python without reallocating the memory.
Any help is much appreciated. I am stuck on this from a lot of days.
My code which I am implementing:
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,*pXVec,*c, *xarr1;
int i;
float fArray[5] = {0,1,2,3,4};
//float *p = &fArray[0] ;
npy_intp m = 5;
//void* PyArray_GetPtr(PyArrayObject* aobj, npy_intp* ind)ΒΆ
// Initialize the Python Interpreter
Py_Initialize();
PySys_SetArgv(argc, argv);
// Build the name object
pName = PyString_FromString(argv[1]);
// Load the module object
pModule = PyImport_Import(pName);
printf("check0n");
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
printf("check1n");
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);
printf("check2n");
// if (PyCallable_Check(pFunc))
// {
// Prepare the argument list for the call
//xarr1 = PyFloat_FromDouble(xarr[1]);
printf("check3n");
c = PyArray_SimpleNewFromData(1,&m,NPY_FLOAT,(void *)fArray);
printf("check3n");
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs,0, c);
pValue = PyObject_CallObject(pFunc, pArgs);
if (pArgs != NULL)
{
Py_DECREF(pArgs);
}
//}
// else
// {
// PyErr_Print();
// }
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
Answer #1:
The function is:
PyObject *
PyArray_SimpleNewFromData(
int nd,
npy_intp* dims,
int typenum,
void* data)
-
The last argument (
data
) is a buffer to the data. Let’s dispense with that. -
The second argument (
dims
) is a buffer, whose each entry is a dimension; so for a 1d array, it could be a length-1 buffer (or even an integer, since each integer is a length-1 buffer) -
Since the second argument is a buffer, the first argument (
nd
) tells its length -
The third argument (
typenum
) indicates the type.
For example, say you have 4 64-bit ints at x
:
To create an array, use
int dims[1];
dims[0] = 4;
PyArray_SimpleNewFromData(1, dims, NPY_INT64, x)
To create a 2X2 matrix, use
int dims[2];
dims[0] = dims[1] = 2;
PyArray_SimpleNewFromData(2, dims, NPY_INT64, x)
Answer #2:
Make sure that you plug the memory leak that the above approach entrails. I am guessing in the above x
is a pointer of type void *
. Check this out.