Python/C API and setattr

Thomas S. Strinnhed thstr at serop.abb.se
Wed Jun 9 07:39:03 EDT 1999


Hi

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
>>> c.value
23
>>>
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
{
	PyObject_HEAD
	int value;
} counterobject;

Thanks 
 -- Thomas S. Strinnhed, thstr at serop.abb.se




More information about the Python-list mailing list