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

A

Allen

I use try catch, but cannot catch the execeptions of execution python
method.

PYCALL_API void PyCall(const char * pszModule, const char * pszFunc,
void * pArg)
{
if (pszModule == NULL || pszFunc == NULL)
{
return;
}

Py_Initialize();

PyObject * pModule = NULL;
PyObject * pFunc = NULL;

try {

pModule = PyImport_ImportModule(pszModule);
pFunc = PyObject_GetAttrString(pModule, pszFunc);

PyEval_CallObject(pFunc, (PyObject*)pArg);
} catch (...) {
fprintf(stderr, "Error: call python method failed");
}

Py_Finalize();
}

Can I catch it from C++? Thank you.

Regards,
Allen Chen
 
C

cptnwillard

One way is to create an intermediate python function, which returns a
special value when an exception is caught.

def ExceptionCatcher(FunctionToCall):
def F():
try: FunctionToCall()
except: return -1
return 0
return F

Then instead of calling your function, you would call
ExceptionCatcher(YourFunction). You can then check the return value in
your C++ code.
 
R

Robert Bauck Hamar

Allen said:
I use try catch, but cannot catch the execeptions of execution python
method.

PYCALL_API void PyCall(const char * pszModule, const char * pszFunc,
void * pArg)
{
if (pszModule == NULL || pszFunc == NULL)
{
return;
}

Py_Initialize();

PyObject * pModule = NULL;
PyObject * pFunc = NULL;

try {

pModule = PyImport_ImportModule(pszModule);
pFunc = PyObject_GetAttrString(pModule, pszFunc);

PyEval_CallObject(pFunc, (PyObject*)pArg);
} catch (...) {
fprintf(stderr, "Error: call python method failed");
}

Py_Finalize();
}

Can I catch it from C++?

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.
 
G

Gabriel Genellina

En Wed, 13 Jun 2007 12:03:00 -0300, Robert Bauck Hamar
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top