Custom C Exception Subclasses

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 24 12:48:34 EST 2008


En Wed, 24 Dec 2008 15:00:36 -0200, Ivan Illarionov  
<ivan.illarionov at gmail.com> escribió:

> On Dec 24, 6:42 pm, Ross <rlig... at gmail.com> wrote:
>> For a project that I am doing, it would be useful to have an exception
>> class that stores some additional data along with the message.
>> However, I want to be able to store a couple pointers to C++ classes,
>> so I can't just use an exception created with PyExc_NewException.  If
>> I were to subclass the built-in Exception type, I would need to have
>> access to the PyExc_ExceptionObject, but the headers only give
>> PyExc_Exception, the type object.  This seems like a rather
>> straightforward task, but I can't seem to find any documentation for
>> it.  Does anyone know how to do this?  Thanks!
>
> When you raise an exception in C++ you can set it to ANY Python object
> via PyErr_SetObject and that object could store pointers to C++
> classes.

Remember that exceptions should inherit from BaseException; although this  
rule isn't enforced in Python 2.6, 3.0 doesn't allow that.
It isn't explicitely written in the docs, but I think that PyErr_SetObject  
won't allow you to pass an object which is not an instance of its first  
argument.

-- 
Gabriel Genellina




More information about the Python-list mailing list