[Python-Dev] ctypes: is it intentional that id() is the only way to get the address of an object?

eryk sun 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"

    >>> ctypes.py_object._type_
    >>> ctypes.py_object.__bases__
    (<class '_ctypes._SimpleCData'>,)

It keeps a reference in the readonly _objects attribute. For example:

    >>> b = bytearray(b'spam')
    >>> sys.getrefcount(b)
    >>> cb = ctypes.py_object(b)
    >>> sys.getrefcount(b)
    >>> cb._objects
    >>> del cb
    >>> sys.getrefcount(b)

If you need the address without relying on id(), cast to a void pointer:

    >>> ctypes.POINTER(ctypes.c_void_p)(cb)[0] == 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:

    >>> ctypes.c_void_p.from_param(cb)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: wrong type

More information about the Python-Dev mailing list