Python/C API and setattr

Gordon McMillan gmcm at hypernet.com
Wed Jun 9 10:27:31 EDT 1999


Thomas S. Strinnhed writes:
> 
> I'm building an extention type for Python in C using NT, MSVC++.

> 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

You returned NULL (or 0, or the compiler generated a return of NULL 
because you didn't state anything) from your C code. That signifies 
an error to Python, which expects you to have set an error string.

Return (an increfed) None and Python will be happy.

> >>> 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
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list

- Gordon




More information about the Python-list mailing list