Is there any way to catch expections when call python method in C++

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Jun 14 13:20:58 EDT 2007


En Wed, 13 Jun 2007 12:03:00 -0300, Robert Bauck Hamar  
<roberth+news at ifi.uio.no> escribió:

> Allen wrote:
>
>> I use try catch, but cannot catch the execeptions of execution python
>> method.
>
> No. CPython is written in C, not C++, and C has no concept of exceptions.
> Exceptions in Python is usually indicated by return value in the
> interpreter, and has no mapping to the C++ exception model. You should
> never let C++ exceptions propagate into the python functions either.
> PyImport_ImportModule will return NULL if an exception occured, and so  
> will
> also PyObject_GetAttrString and PyEval_CallObject do.

For an example on how to do this, see "Extending and Embedding the Python  
Interpreter" <http://docs.python.org/ext/intro.html> specially section 1.2

-- 
Gabriel Genellina




More information about the Python-list mailing list