C/API Clarification

Sam sam at email-scan.com
Sat Oct 8 02:50:26 CEST 2005


Jeremy Moles writes:

> in the world I got the idea from! According to the docs, METH_VARARGS is
> (PyObject*, PyObject*)?

Right.

>                         My prototype shouldn't even compile... but it

Why wouldn't it compile?

> 2. No where in the docs does it show how to "correctly" create an
> instance of your custom PyTypeObject in C (rather than in the
> interpreter). I'm using:

That's what tp_alloc is for.  Then you call tp_init to initialize it.

But, your tp_new handler should already invoke tp_alloc, so it's cleaner to 
call tp_new, followed by tp_init.

> 3. I'm not able to return the "self" argument (useful, for instance,
> when you want to chain a group of method calls) without screwing things
> up really, really bad. For example:
> 
> PyObject* somefunc(cstruct* self, PyObject* args, PyObject* kargs) {
> 	self->pointerToData->doSomeStuff();
> 	
> 	return (PyObject*)(self);
> }
> 
> ...returns "something"; it has the methods you would expect, but trying
> to use the object when it gets back to the interpreter is pretty much
> undefined behavior. :) It's seen as a "<refcnt...>" instance in the
> interpreter, even though a dir() shows it still has the methods you
> would expect.

You need to increment the reference count of the object, in this case.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20051007/6adf14a2/attachment.pgp>


More information about the Python-list mailing list