[C++-sig] Re: Calling a python function from C++ (from Jeff Holle)

David Abrahams dave at boost-consulting.com
Wed Jun 30 18:30:33 CEST 2004


Jeff Holle <jeff.holle at verizon.net> writes:

>>
>>
>>> An underlining concern I have is error handling.  When
>>> PyImport_ImportModule fails, a NULL pointer is returned.
>>> Seems like the underlining machinery in "handle<>" is intolerant of
>>> this. 
>>  
>>
>>
>>What makes you say that?
>>
> You are correct about the success in compiling your much cleaner
> example, and it works :-) , but has a flaw.
>
> When I hide the needed python script, the call to
> PyImport_ImportModule returns NULL.
> When this occurres, both your example and what I had created, but
> commented out throws something because I can catch it with
> "catch(...)".  I don't however know how to be more specific.  Any
> hints?  Whatever it is, it doesn't inherit from "exception".

It's "boost::python::error_already_set".  It means that Python knows
what the error is already.  If you're in an extension module, it's
handled automatically.  Otherwise, you can look at
www.boost.org/libs/python/test/embedding.cpp for examples of how to
get at this exception.

This is all in the documentation, BTW:

http://www.boost.org/libs/python/doc/v2/object.html#object-spec-ctors
http://www.boost.org/libs/python/doc/v2/errors.html

> If I was operating on windows, I would anticipate it was a "structured
> exception", or whatever Microsoft calls their propritary stuff.  

Never!

> Don't know of the equivalent on linux though.  Without the "catch
> (...)", the command line program exits with "Aborted".

'course it does.  That's C++. ;-)

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list