pyrex exceptions

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Apr 28 09:06:10 EDT 2003


"Haris Bogdanovic" <haris at email.hinet.hr> wrote in
news:b8j67p$aqpk$1 at as201.hinet.hr: 

> How do I handle exceptions in Pyrex ?
> I have a C++ wrapper around some C library and I'm doing a Python
> wrapper around that library (copying C++ wrapper object model).
> In C++ wrapper there is a class Exception which is derived from C++
> standard exception class.

Pyrex doesn't really know about C++. In particular it doesn't have any 
facilities for handling C++ exceptions. If you are linking to C++ from 
Pyrex you'll need to wrap the C++ code in C++ functions that catch and 
handle all exceptions. You must never allow a C++ exception to propogate 
back into the Python interpreter, otherwise bad things may happen.

In this case it sounds as though you might be better to wrap Pyrex around 
the C library directly. Still follow the C++ object model if you want, but 
don't use any of the C++ code.

Alternatively you could look at Boost/Python which does know about C++ 
exceptions.

> In Pyrex it is not yet possible to derive an extension type from
> Python class so I would have to create extension type for handling
> exceptions from scratch.
> If I want to raise an exception like this :
> raise ExceptionClass(error)
> how should then ExceptionClass be defined ?

exactly as you would define it in Python:

   class ExceptionClass(Exception):
       pass

and then you can raise it from Pyrex just as you would in Python.

   raise ExceptionClass(error)

> I'm using wxPython GUI so I want to show the message box (window)
> which displays error text when raising exception and then let user to
> choose from options of ending or continuing the program from some
> previous place in code or correcting error which caused the exception.
> 
> Can you please give me just the bare bones of how should that
> exception class look like.(in Pyrex, as extension type)

The exception class must have Exception as a base class. Exception is not 
an extension type so your exception class shouldn't be one either.

> I'm also little bit unfamiliar with what exactly goes on when program
> raises an error with class instance as attribute. Could you explain
> that just in a few words ?
> 
I'm not sure exactly which bit you want explained. At the C level, or the 
Python level?

When a Python exception is raised in C code, and therefore in Pyrex code, 
the exception that is to be raised is saved and the C/Pyrex function 
returns a value indicating that an error occurred. The caller checks the 
return code, and if it indicates an error either handles the exception or 
returns an error back to its caller. Within Pyrex the checking of return 
codes is usually automated.

If one of the functions wants to handle an exception it checks whether the 
exception that was thrown matches something it wants to handle. If so it 
can clear the exception state and continue processing. If not it continues 
to propogate the error by returning an error code.


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list