Python Forum
How to convert c_void_p PyObject back to void*
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert c_void_p PyObject back to void*
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert dataframe from str back to datafarme Creepy 1 617 Jul-07-2023, 02:13 PM
Last Post: snippsat
  How do I convert this string back to a list of integers? donmerch 6 3,691 Apr-05-2020, 06:43 PM
Last Post: donmerch
  Unable to install python-wnck python-imaging on Void Linux linuxlight 1 3,192 Mar-08-2020, 03:53 AM
Last Post: Larz60+
  convert a character to numeric and back Skaperen 2 2,093 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  Python Extensions with C Custom PyObject? babaliaris 9 5,489 Feb-14-2018, 08:39 PM
Last Post: Gribouillis
  How to create ctypes.c_void_p object from C++ and set it as claas attribute lfdm 2 5,548 Feb-02-2017, 09:27 AM
Last Post: lfdm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020