[C++-sig] Have error_already_set - now what?

Stefan Seefeld seefeld at sympatico.ca
Thu Jan 11 18:59:49 CET 2007


Johan Johansson wrote:
> Hi everyone
> 
> It's probably just me missing something obvious, but I'm having a very
> hard time to get a useful error message when I catch an
> error_already_set in code calling a python function/method. I've tried
> the following for lack of good ideas:
> 
> try {
>     ... some python call ...
>     return 0;
> } catch (error_already_set&) {
>     assert(PyErr_Occurred());
>     PyErr_Print();
>     return -1;
> }
> 
> Sure enough I'm getting -1 back, but nothing is printed to stderr AFAIK.
> Or is this one of those cases where Python doesn't actually care about
> sys.stderr and use C's notion of stderr instead?

I'm not sure which PyErr_Print() will print to, but print it will. :-)

> What's a good way to find out what error occurred (exhaustively matching
> doesn't strike me as good).

I believe at present you do need to use the Python C API to find out
what kind of python exception is set, and then act appropriately.

It would be nice to have some boost::python API to encapsulate that,
i.e. to query the exception type, object, and stack trace into boost::python
objects.

Patches are welcome, I guess. :-)

Thanks,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list