ctypes - Pointer from Address
April 27, 2011

There are times you need to construct a ctypes pointer from an integer address you have, say the 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

Example

# 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!

I'm using this code to dig into the vicious OVERLAPPED structure that's held inside PyOVERLAPPED for really low-level hacking... Anyway, if anyone finds this recipe useful, feel free to use it.