Python/C API and setattr
Michael P. Reilly
arcege at shore.net
Wed Jun 9 16:38:57 CEST 1999
Thomas S. Strinnhed <thstr at serop.abb.se> wrote:
: I'm building an extention type for Python in C using NT, MSVC++.
: It's built as a dll to link in at Runtime.
: I'm doing this for practice so the type is quite simple, it's
: a CounterType, with an attribute called value (an int)
: and the simple operations/methods inc() and dec().
: I've looked pretty much at Mark Lutz's example StackType from
: Programming Python and I think I'm doing the same, but I've added
: setattrfunc to be able to directly manipulate value as c.value = 23
: and when I use it something goes wrong.
: (inc() and dec() work fine)
: Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
: Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
:>>> from CounterType import *
:>>> c = Counter()
:>>> c.value = 23
: Traceback (innermost last):
: File "<stdin>", line 1, in ?
: SystemError: error return without exception set
: The value is set properly but I can't figure out the system error, it
: seems to be something wrong with my setattrfunc.
: static void counter_setattr
: (counterobject *self, char *name, PyObject *v)
: if(strcmp(name, "value") == 0)
: self->value = (int)PyInt_AsLong(v);
: But what would that be??
: I have a hard time finding examples of how to do setattr so
: any pointers would be great. Also if someone understands this error,
: please post an answer.
: My counterobject is this:
: typedef struct
: int value;
: } counterobject;
: -- Thomas S. Strinnhed, thstr at serop.abb.se
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!"
More information about the Python-list