Win32 dll / ctypes question
Thomas Heller
theller at python.net
Mon Dec 9 05:40:33 EST 2002
Even more interesting is to follow the 'ob_type' member
field of Python objects, which is possible with this code:
--------
from ctypes import windll, Structure, POINTER, SetPointerType
PTYPE = POINTER("PythonType")
class PythonObject(Structure):
_fields_ = [
("ob_refcnt", "i"),
("ob_type", PTYPE)]
class PythonType(Structure):
_fields_ = [
("ob_refcnt", "i"),
("ob_type", PTYPE),
("ob_size", "i"),
("tp_name", "z"),
("tp_basicsize", "i"),
("tp_itemsize", "i"),
("tp_dealloc", "i"),
("tp_print", "i"),
("tp_getattr", "i"),
("tp_setattr", "i"),
("tp_compare", "i"),
# and so on...
]
SetPointerType(PTYPE, PythonType)
for obj in (42, 1.2, (), [], {}, int, PythonType):
p = PythonObject.from_address(id(obj))
t = p.ob_type.contents
print repr(obj), t.tp_basicsize, t.tp_itemsize, t.tp_name
--------
The output of the script is:
42 12 0 int
1.2 16 0 float
() 12 4 tuple
[] 16 0 list
{} 124 0 dict
<type 'int'> 436 20 type
<class '__main__.PythonType'> 436 20 _ctypes.StructType
Thomas
More information about the Python-list
mailing list