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) { > 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. 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 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? 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4