Can an object of a C type have random members added? tp_dictoffset?
Denis S. Otkidach
ods at strana.ru
Thu Apr 21 11:19:04 EDT 2005
On Thu, 21 Apr 2005 17:55:24 +1000 Gregory Bond wrote:
GB> I have a class implemented in C using tp_members to specify (read-only)
GB> access to data members in the C struct. I'd like to be able to do the
GB> decoration trick with objects of this class. Is this possible?
GB>
GB> [The documentation for tp_dictoffset eseems to hint that this is
GB> possible..... how do I use it?]
Something like the following:
typedef struct {
PyObject_HEAD
PyObject *dict;
} YourObject;
static PyMemberDef module_members[] = {
{"__dict__", T_OBJECT, offsetof(YourObject, dict), READONLY},
{0}
};
PyObject *
Your_New()
{
YourObject *obj;
obj = PyObject_GC_New(YourObject, &Your_Type);
if (obj==NUL) return NULL;
obj->dict = PyDict_New();
if (obj->dict==NULL) {
Py_DECREF(obj);
return NULL;
}
PyObject_GC_Track(obj);
return (PyObject *)obj;
}
PyTypeObject Your_Type = {
PyObject_HEAD_INIT(&PyType_Type)
# ...skipped...
offsetof(YourObject, dict), /* tp_dictoffset */
# ...skipped...
}
--
Denis S. Otkidach
http://www.python.ru/ [ru]
More information about the Python-list
mailing list