Support for Python3 tested

Former-commit-id: 987571bec31e72c4c5dad9b4ceebbca62de6c794
This commit is contained in:
johan 2014-03-01 19:28:57 +01:00
parent e0d405034f
commit e761a30b58
3 changed files with 64 additions and 51 deletions

View File

@ -41,8 +41,8 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
# Prefer static linking over dynamic
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release")
#set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
# enable C++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")

View File

@ -1 +1 @@
8f880f994b0855ace23e27818896fc34ba1677a8
0c20a678370e38a9a8da650e04445088d892a49f

View File

@ -155,19 +155,23 @@ PyObject *Effect::json2python(const Json::Value &json) const
return Py_BuildValue("s", json.asCString());
case Json::objectValue:
{
PyObject * obj = PyDict_New();
PyObject * dict= PyDict_New();
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
{
PyDict_SetItemString(obj, i.memberName(), json2python(*i));
PyObject * obj = json2python(*i);
PyDict_SetItemString(dict, i.memberName(), obj);
Py_XDECREF(obj);
}
return obj;
return dict;
}
case Json::arrayValue:
{
PyObject * list = PyList_New(json.size());
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
{
PyList_SetItem(list, i.index(), json2python(*i));
PyObject * obj = json2python(*i);
PyList_SetItem(list, i.index(), obj);
Py_XDECREF(obj);
}
return list;
}
@ -344,19 +348,28 @@ Effect * Effect::getEffect()
// extract the module from the runtime
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
if (PyModule_Check(module))
if (!PyModule_Check(module))
{
// retrieve the capsule with the effect
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
if (PyCapsule_CheckExact(effectCapsule))
{
// Get the effect from the capsule
return reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
}
}
// something is wrong
Py_XDECREF(module);
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
return nullptr;
}
// retrieve the capsule with the effect
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
Py_XDECREF(module);
if (!PyCapsule_CheckExact(effectCapsule))
{
// something is wrong
Py_XDECREF(effectCapsule);
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
return nullptr;
}
// Get the effect from the capsule
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
Py_XDECREF(effectCapsule);
return effect;
}