Creating a Python Type in C - tp_init and tp_new

A

ajayre

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?

Thanks, Andy
(please reply to the newsgroup only - thanks)
 
G

Gabriel Genellina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top