Python/C API and setattr
Thomas S. Strinnhed
thstr at serop.abb.se
Thu Jun 10 07:37:56 CEST 1999
Michael P. Reilly wrote:
> [quoted my message]
> Thomas, the prototype for functions in the setattr slot of a Python type
> typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
> Your counter_setattr function should return an int, true (0) for
> success, false (-1) for error. I believe the source code and Mark's
> book (somewhere in Ch 15? but I don't have the book with me) are about
> the only places this is documented, and it is very obscure.
> "This looks like a job for Documentation Man!"
Thanks everyone, I finally managed to get it all to work.
Embarrassing enough, the answer was in the Programming Python-book all
the time (in ch.14 at p.561), but as said below: "it is very obscure".
Actually we were all almost right, the prototype in the book is
static int mytype_setattr(mytype *x, char *name, PyObject *value);
so the problem was me trying to return nothing at all, None, the
int value assigned and so on.
I guess this answers a question from an earlier thread:
"Programming Python" still worthwhile?
with a big: "Yes, very much so!" (as goes for c.l.py :-)
If anyone is interrested I've pasted the code to my function
as it turned out below.
-- Thomas S. Strinnhed. thstr at serop.abb.se
static int counter_setattr(counterobject *self, char *name, PyObject *v)
if(strcmp(name, "value") == 0)
self->value = (int)PyInt_AsLong(v);
More information about the Python-list