By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Creating a Python Type in C - tp_init and tp_new

P: n/a
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)

May 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
En Thu, 17 May 2007 16:10:54 -0300, <aj****@gmail.comescribió:
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

May 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.