Creating a Python Type in C - tp_init and tp_new
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu May 17 21:16:28 EDT 2007
En Thu, 17 May 2007 16:10:54 -0300, <ajayre at gmail.com> escribió:
> I'm creating a type in a C function as follows:
>
> static PyObject *Receive(PyObject *self, PyObject *args) {
> pyMessageObject *msgobj = PyObject_New(pyMessageObject,
> &pyMessageType);
> return (PyObject *)msgobj;
> }
>
> I have (some lines omitted):
>
> static PyTypeObject pyMessageType =
> {
> PyObject_HEAD_INIT(NULL)
> ...
> pyMessage_Init, /*tp_init*/
> 0, /*tp_alloc*/
> pyMessage_New, /*tp_new*/
> };
>
> I have noticed that pyMessage_New and pyMessage_Init are not called.
> However if the type is created in Python then they are called. Why is
> this and how can I solve it?
I think tp_new and tp_init are used when you create an instance by calling
the type (in Python would be your_type())
At least it's in type's tp_call (type_call in typeobject.c) where __new__
and __init__ are checked and processed.
So I think you should create your object using
PyObject_CallObject(pyMessageType, NULL) but I'm not sure... just try and
post your results! Or perhaps there is another way, I don't know.
--
Gabriel Genellina
More information about the Python-list
mailing list