Python: Implement support for dictionaries.

This commit is contained in:
Gunnar Beutner 2013-02-16 08:58:23 +01:00
parent acf4e746c0
commit 544c9ac662
2 changed files with 31 additions and 2 deletions

View File

@ -115,9 +115,9 @@ void PythonInterpreter::ProcessCall(const ScriptTask::Ptr& task, const String& f
Value vresult = PythonLanguage::MarshalFromPython(result); Value vresult = PythonLanguage::MarshalFromPython(result);
Py_DECREF(result); Py_DECREF(result);
task->FinishResult(vresult); Application::GetEQ().Post(boost::bind(&ScriptTask::FinishResult, task, vresult));
} catch (...) { } catch (...) {
task->FinishException(boost::current_exception()); Application::GetEQ().Post(boost::bind(&ScriptTask::FinishException, task, boost::current_exception()));
} }
m_Language->SetCurrentInterpreter(interp); m_Language->SetCurrentInterpreter(interp);

View File

@ -125,6 +125,19 @@ PyObject *PythonLanguage::MarshalToPython(const Value& value)
(void) PyTuple_SetItem(result, 1, pname); (void) PyTuple_SetItem(result, 1, pname);
return result; 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); Py_INCREF(Py_None);
@ -139,6 +152,20 @@ Value PythonLanguage::MarshalFromPython(PyObject *value)
{ {
if (value == Py_None) { if (value == Py_None) {
return Empty; 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) { } else if (PyTuple_Check(value) && PyTuple_Size(value) == 2) {
PyObject *ptype, *pname; PyObject *ptype, *pname;
@ -164,6 +191,8 @@ Value PythonLanguage::MarshalFromPython(PyObject *value)
return object; return object;
} else if (PyFloat_Check(value)) { } else if (PyFloat_Check(value)) {
return PyFloat_AsDouble(value); return PyFloat_AsDouble(value);
} else if (PyInt_Check(value)) {
return PyInt_AsLong(value);
} else if (PyString_Check(value)) { } else if (PyString_Check(value)) {
return PyString_AsString(value); return PyString_AsString(value);
} else { } else {