From acf4e746c0f79e7fa77710de82f975664f17229d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 16 Feb 2013 08:08:51 +0100 Subject: [PATCH] Exception handling for Python function calls. --- lib/python/pythoninterpreter.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/python/pythoninterpreter.cpp b/lib/python/pythoninterpreter.cpp index 2ca68fafe..ebbbe66b9 100644 --- a/lib/python/pythoninterpreter.cpp +++ b/lib/python/pythoninterpreter.cpp @@ -94,8 +94,22 @@ void PythonInterpreter::ProcessCall(const ScriptTask::Ptr& task, const String& f Py_DECREF(args); - if (result == NULL) { - // re-throw python exception + if (PyErr_Occurred()) { + PyObject *ptype, *pvalue, *ptraceback; + + PyErr_Fetch(&ptype, &pvalue, &ptraceback); + PyObject *pstr_msg = PyObject_Str(pvalue); + PyObject *pstr_tb = PyObject_Str(ptraceback); + Py_DECREF(pvalue); + PyErr_Clear(); + + String msg = PyString_AsString(pstr_msg); + Py_DECREF(pstr_msg); + + String tb = PyString_AsString(pstr_tb); + Py_DECREF(pstr_tb); + + BOOST_THROW_EXCEPTION(runtime_error("Error in Python script:" + msg + " at " + tb)); } Value vresult = PythonLanguage::MarshalFromPython(result);