[Python-Dev] Advanced C Types
Pete Shinners
pete at shinners.org
Fri Jul 2 08:19:35 CEST 2004
Hello Python developers, I have been learning my way through new style
type in Python, but it hasn't been exactly easy. I have narrowed my
remaining troubles down to two specific questions for this list. Thanks
for pointers.
First, I have a base type written in C. Python classes are inheriting
from these and those instances are being passed to another C function.
This function needs access to data from the original type's PyObject
structure. How do I get from the instance PyObject* to my base type's
PyObject* data? My workaround is a hackish, the base has a method like this:
PyObject *dirtydirty(PyObject *self, PyObject *args) {
Py_INCREF(self);
return self;
}
Now my external C code call something like this:
PyObject *GetBaseObjectFromInstance(PyObject *o) {
PyObject *meth = PyObject_GetAttrString(o, "dirtydirty");
PyObject *baseobj = PyObject_CallObject(meth, NULL);
Py_DECREF(meth);
return baseobj;
}
How can I recreate this without requiring the 'dirtydirty' method?
Obviously Python is internally keeping this data around, as it is passed
to the C methods on my type. I cannot find a more direct route.
Second, I am very uncomfortable with the newstyle type allocation
mechanisms. I understand the use of separate tp_init, tp_alloc, and
tp_new. I do not understand how to use these from C. This is more a
matter of clarification, my question is this; What is the proper way for
one of my C functions to create my C type? Especially when my C type is
derived from other C type? I feel wrong digging into my PyTypeObject for
various tp_ pointers. I was comfortable with the PyObject_New() type
functions, but from what I can see those are illegal for newstyle types?
Hmm, it appears my questions have expanded from simple and consise.
Thanks again.
More information about the Python-Dev
mailing list