Python Forum

Full Version: How to convert c_void_p PyObject back to void*
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm able to create a c_void_p PyObject like so 
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(my_void*));
Now I need to get my pointer back from python 
I would expect doing something like this:
PyLong_AsVoidPtr(p)
But it fails with class TypeError: an integer is required.

According to ctypes doc https://docs.python.org/3/library/ctypes.html (16.16.1.4. Fundamental data types) c_void_p type corresponds to Python type "int or None". So it looks like I get none.

So how can I possibly convert my c_void_p PyObject to a long PyObject in this case?

Thanks,
-Damien