[Python-Dev] Advanced C Types (original) (raw)
Michael Hudson mwh at python.net
Fri Jul 2 13:00:07 CEST 2004
- Previous message: [Python-Dev] Advanced C Types
- Next message: [Python-Dev] Re: Advanced C Types
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Pete Shinners <pete at shinners.org> writes:
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) { PyINCREF(self); return self; } Now my external C code call something like this: PyObject *GetBaseObjectFromInstance(PyObject *o) { PyObject *meth = PyObjectGetAttrString(o, "dirtydirty"); PyObject *baseobj = PyObjectCallObject(meth, NULL); PyDECREF(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.
Um. What am I missing that makes this not totally pointless? The "instance PyObject*" is the "base type's PyObject* data". Cast it.
Second, I am very uncomfortable with the newstyle type allocation mechanisms. I understand the use of separate tpinit, tpalloc, and tpnew. 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?
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 PyObjectNew() 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
- Previous message: [Python-Dev] Advanced C Types
- Next message: [Python-Dev] Re: Advanced C Types
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]