ctypes - Pointer from Address April 27, 2011
There are times you need to construct a
ctypes pointer from an integer address you have,
id of a python object. I scratched my head for quite a while until I found out a how
to do it properly (with some help from the stackoverflow guys). Here's what I got:
import ctypes def deref(addr, typ): return ctypes.cast(addr, ctypes.POINTER(typ)).contents
# get the ref count of an object (in a very nasty way :) >>> x="hello world" >>> deref(id(x), ctypes.c_int) c_long(1) >>> y=x >>> z=x >>> deref(id(x), ctypes.c_int) c_long(3)
Some words of caution:
* I'm relying here on
id returning the address of an object. This is a weak assumption,
but it holds (and is likely to continue to hold) for CPython.
* There are APIs for what I showed in the example above... use them instead!