PyObject_New not running tp_new for iterators?

Gregory Bond gnb at itga.com.au
Thu Apr 21 03:26:43 EDT 2005


I wrote:

[snip]

 > What am I missing?

The fundamental problem is that this:

>>    if (!(ro = PyObject_New(MyIter, &MyIterType)))
>>       return NULL;

is really only a malloc() - it doesn't call the tp_new function at all. 
  This is not really clear in the 2.3 version of the C API document that 
I was consulting - the 2.4 version is much clearer.

I have no idea how to do in C all the things that calling a class 
constructor in Python does.  Am I supposed to be calling tp_new & 
tp_init directly?

In the end, this particular class really only needs to be created from a 
factory function, so I removed the tp_new member altogether and just 
wrote a C function to initialise the object, and called that from the C 
factory function.

[The above is not particular to the fact that my class is an iterator - 
all C classes have the same behaviour.  My test of a plain-old class 
probably looked like it worked due to lucky memory patterns.]





More information about the Python-list mailing list