Generating exceptions from C

Jacob Kroon jacob.kroon at gmail.com
Sat Mar 18 09:57:37 EST 2006


I'll just reply to myself what I've found out so far:

> 1. PyErr_NewException() creates the exception _class_, not the 
> instance right ?
>
Looks like it does yes. It doesn't even seem right to talk about an 
_instance_ of an exception...

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

After looking at error.c in the python sources, it looks like that 
function sets
the global exception type and value variables to the provided arguments.

> 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 ?
>
The right way seems to be to create a tuple that consists of the string 
message, and any additional data,
and pass the tuple in PyErr_SetObject().

> 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.

As said previously, there is never an instance of the exception, data is 
passed in the "value" argument.


Does the comments above make sense?

                                     --Jacob




More information about the Python-list mailing list