PyErr_SetString() != raise ?

Donn Cave donn at u.washington.edu
Tue Jul 15 12:44:54 EDT 2003


In article <bevkb3$mg8$1 at solaria.cc.gatech.edu>,
 David Eger <eger at cc.gatech.edu> wrote:

> When I use PyErr_SetString() in an extension, python just keeps on
> chugging instead of acting as though a Python exception had been
> 'raise'd.  Why is this, and do I really have to write the Python code
> to raise an exception manually after I've used PyErr_SetString()?
> 
> Most recently, writing an iterator -- I write:
> 
>     if (nextEl == NULL) {
>       PyErr_SetString(PyExc_StopIteration, "Array index out of bounds");
>       return NULL;
>       }
> 
> in my iterator extension, but when I use my extension, I get an infinite loop:
...
> (and so on, and so on...)  The thing is, even though I *set* the
> exception with PyErr_SetString, to the interpreter, it doesn't get
> raised.  Am I just misinterpretting how PyErr_SetString is supposed 
> to work?

Sounds like it.  PyErr_SetString installs the exception, but it
doesn't raise it.  The most common way a C function causes an
exception to be raised is to return 0 (or NULL, if you prefer)
where a non-zero (PyObject *) is expected.  So you are doing that,
but I don't know if it's effective in that particular context.
Some functions have other conventions, for example type initialization
returns 0 for success.

More research required.  At any rate, don't worry about whether
the exception has been initialized, at the moment that is not
your problem.

   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list