I need to share an ArrayFire array creating in C++ to Python. That works OK:
PyObject* arrayToPyObject(const af::array& arr)
{
// Create arrayfire array and set
PyObject* afArray = createObject("arrayfire", "Array");
PyObject* ctypes_mod = PyImport_ImportModule("ctypes");
PyObject* c_void_p = PyObject_GetAttrString(ctypes_mod, "c_void_p");
PyObject* p = PyObject_CallFunction(c_void_p, "O", PyLong_FromVoidPtr(arr.get()));
if (PyObject_SetAttr(afArray, PyUnicode_FromString("arr"), p) == 0)
{
return afArray;
}
else
{
Py_XDECREF(afArray);
return nullptr;
}
}
Now if my Python script return an ArrayFire array I need to read the arr attribute and get my pointer back and assign it to a C++ array
af::array pyObjectToArray(PyObject* obj)
{
af::array tmp;
PyObject* arr = PyObject_GetAttr(obj, PyUnicode_FromString("arr"));
if (arr)
{
af_array ref = (af_array)(PyLong_AsVoidPtr(arr));
if (ref)
{
tmp.set(ref);
}
}
return tmp;
}
Issue here is that PyLong_AsVoidPtr failswith class 'TypeError': an integer is required.
ctypes doc (16.16.1.4. Fundamental data types) says the Python type for c_void_p is either int or None. Obviously it is None in my case
How can I convert c_void_p to python using the C API?
Thanks!