[icinga-checkins] icinga.org: icinga2/master: Exception handling for Python function calls.

git at icinga.org git at icinga.org
Sat Feb 16 08:09:00 CET 2013


Module: icinga2
Branch: master
Commit: acf4e746c0f79e7fa77710de82f975664f17229d
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=acf4e746c0f79e7fa77710de82f975664f17229d

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Sat Feb 16 08:08:51 2013 +0100

Exception handling for Python function calls.

---

 lib/python/pythoninterpreter.cpp |   18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/lib/python/pythoninterpreter.cpp b/lib/python/pythoninterpreter.cpp
index 2ca68fa..ebbbe66 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);





More information about the icinga-checkins mailing list