Generating exceptions from C

Jacob Kroon jacob.kroon at gmail.com
Sat Mar 18 06:48:38 EST 2006


Hi,

I'm working on a python module written in C, and I'm trying to figure 
out how to raise python
exceptions if a function fails. So far I've read 
http://docs.python.org/ext/errors.html and 
http://docs.python.org/api/exceptionHandling.html , but I've not fully 
understood it.

I've managed to create the custom exception type using 
PyErr_NewException(), and if I do
PyErr_SetString() the exception will get raised. Some questions:

1. PyErr_NewException() creates the exception _class_, not the instance 
right ?

2. Is PyErr_SetString() the correct way to raise exceptions ?

3. Besides the error message I pass to PyErr_SetString(), I also want to 
pass additional return
   data together with the exception. But this should be attached to the 
exception _instance_,
   not the class, am I right ?

4. If I am supposed to attach it to the exception instance, how would I 
do that ? I never have a
   pointer to the exception instance, just the class.

                               --Jacob




More information about the Python-list mailing list