[icinga-checkins] icinga.org: icinga2/master: Python: Implement support for dictionaries.

git at icinga.org git at icinga.org
Sat Feb 16 08:58:37 CET 2013


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

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

Python: Implement support for dictionaries.

---

 lib/python/pythoninterpreter.cpp |    4 ++--
 lib/python/pythonlanguage.cpp    |   29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/lib/python/pythoninterpreter.cpp b/lib/python/pythoninterpreter.cpp
index ebbbe66..ea66519 100644
--- a/lib/python/pythoninterpreter.cpp
+++ b/lib/python/pythoninterpreter.cpp
@@ -115,9 +115,9 @@ void PythonInterpreter::ProcessCall(const ScriptTask::Ptr& task, const String& f
 		Value vresult = PythonLanguage::MarshalFromPython(result);
 		Py_DECREF(result);
 
-		task->FinishResult(vresult);
+		Application::GetEQ().Post(boost::bind(&ScriptTask::FinishResult, task, vresult));
 	} catch (...) {
-		task->FinishException(boost::current_exception());
+		Application::GetEQ().Post(boost::bind(&ScriptTask::FinishException, task, boost::current_exception()));
 	}
 
 	m_Language->SetCurrentInterpreter(interp);
diff --git a/lib/python/pythonlanguage.cpp b/lib/python/pythonlanguage.cpp
index 680a7d7..e51a941 100644
--- a/lib/python/pythonlanguage.cpp
+++ b/lib/python/pythonlanguage.cpp
@@ -125,6 +125,19 @@ PyObject *PythonLanguage::MarshalToPython(const Value& value)
 				(void) PyTuple_SetItem(result, 1, pname);
 
 				return result;
+			} else if (value.IsObjectType<Dictionary>()) {
+				Dictionary::Ptr dict = value;
+				PyObject *pdict = PyDict_New();
+
+				String key;
+				Value value;
+				BOOST_FOREACH(tie(key, value), dict) {
+					PyObject *dv = MarshalToPython(value);
+
+					PyDict_SetItemString(pdict, key.CStr(), dv);
+				}
+
+				return pdict;
 			}
 
 			Py_INCREF(Py_None);
@@ -139,6 +152,20 @@ Value PythonLanguage::MarshalFromPython(PyObject *value)
 {
 	if (value == Py_None) {
 		return Empty;
+	} else if (PyDict_Check(value)) {
+		Dictionary::Ptr dict = boost::make_shared<Dictionary>();
+
+		PyObject *dk, *dv;
+		Py_ssize_t pos = 0;
+
+		while (PyDict_Next(value, &pos, &dk, &dv)) {
+		    String ik = PyString_AsString(dk);
+		    Value iv = MarshalFromPython(dv);
+
+		    dict->Set(ik, iv);
+		}
+
+		return dict;
 	} else if (PyTuple_Check(value) && PyTuple_Size(value) == 2) {
 		PyObject *ptype, *pname;
 
@@ -164,6 +191,8 @@ Value PythonLanguage::MarshalFromPython(PyObject *value)
 		return object;
 	} else if (PyFloat_Check(value)) {
 		return PyFloat_AsDouble(value);
+	} else if (PyInt_Check(value)) {
+		return PyInt_AsLong(value);
 	} else if (PyString_Check(value)) {
 		return PyString_AsString(value);
 	} else {





More information about the icinga-checkins mailing list