
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.

Pete Shinners <pete@shinners.org> writes:
Um. What am I missing that makes this not totally pointless? The "instance PyObject*" *is* the "base type's PyObject* data". Cast it.
The same way _randommodule.c does it :-)
Especially when my C type is derived from other C type?
Hmm, not sure about that. Have you looked at xxsubtype in the Python source? (Mind you, almost all C types derive from another C type: PyBaseObjectType, so maybe this isn't a big deal).
I feel wrong digging into my PyTypeObject for various tp_ pointers.
Why?
I was comfortable with the PyObject_New() type functions, but from what I can see those are illegal for newstyle types?
I think so, yes. Cheers, mwh -- Considering that this thread is completely on-topic in the way only c.l.py threads can be, I think I can say that you should replace "Oblivion" with "Gravity", and increase your Radiohead quotient. -- Ben Wolfson, comp.lang.python

Michael Hudson wrote:
Um. What am I missing that makes this not totally pointless? The "instance PyObject*" *is* the "base type's PyObject* data". Cast it.
If this is the case, then terrific. I didn't think it could be since the PyObject must have to be a PyClassObject, and not PyMyBaseObject. I'm still working with it all, so I'll test this out. If this just works I am in good shape.
Hmm, not sure about that. Have you looked at xxsubtype in the Python source?
No, but thanks for this tip, this is great! It is tucked into the Modules directory.
Up until tp_alloc, and tp_init, there was a PyObject_MACRO or PyObject_Function to deal with these for me. I've never once before looked inside the PyTypeObject itself. It just seems strange to start doing it now. That's what makes me feel I am not doing something right. but if that is correct, I will access with confidence (like the pros).
participants (2)
-
Michael Hudson
-
Pete Shinners