[Python-Dev] ctypes: is it intentional that id() is the only way to get the address of an object?
eryksun at gmail.com
Thu Jan 17 08:50:51 EST 2019
On 1/17/19, Steven D'Aprano <steve at pearwood.info> wrote:
> I understand that the only way to pass the address of an object to
> ctypes is to use that id. Is that intentional?
It's kind of dangerous to pass an object to C without an increment of
its reference count. The proper way is to use a simple pointer of type
"O" (object), which is already created for you as the "py_object"
It keeps a reference in the readonly _objects attribute. For example:
>>> b = bytearray(b'spam')
>>> cb = ctypes.py_object(b)
>>> del cb
If you need the address without relying on id(), cast to a void pointer:
>>> ctypes.POINTER(ctypes.c_void_p)(cb) == id(b)
Or instantiate a c_void_p from the py_object as a buffer:
>>> ctypes.c_void_p.from_buffer(cb).value == id(b)
Note that ctypes.cast() doesn't work in this case. It's implemented as
an FFI function that takes the object address as a void pointer. The
from_param method of c_void_p doesn't support py_object:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: wrong type
More information about the Python-Dev