Custom C Exception Subclasses
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Dec 24 12:24:59 EST 2008
En Wed, 24 Dec 2008 13:42:38 -0200, Ross <rlight2 at gmail.com> escribió:
> 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.
In fact you can, you could store those pointers as attributes of the
exception object, using a PyCObject.
Accessing those attributes isn't as easy as doing exc->field, but I think
it's easy enough. Inheriting from some exception type requires you to
define the type structure and fill it right, and IMHO is a lot harder.
> 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!
Perhaps there is a misunderstanding here. To subclass a type, you need the
type, not an instance of such type. The "Exception" class you use in
Python code is "PyExc_Exception" in C code. Usually one would inherit a
subclass, RuntimeError by example, so use PyExc_RuntimeError as the base
type.
--
Gabriel Genellina
More information about the Python-list
mailing list