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

Allen pruyu.chen at gmail.com
Wed Jun 13 05:41:44 EDT 2007


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




More information about the Python-list mailing list