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 # Prefer static linking over dynamic
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so") #set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
set(CMAKE_BUILD_TYPE "Debug") #set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release") set(CMAKE_BUILD_TYPE "Release")
# enable C++11 # enable C++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")

View File

@ -1 +1 @@
8f880f994b0855ace23e27818896fc34ba1677a8 0c20a678370e38a9a8da650e04445088d892a49f

View File

@ -22,36 +22,36 @@ PyMethodDef Effect::effectMethods[] = {
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
// create the hyperion module // create the hyperion module
struct PyModuleDef Effect::moduleDef = { struct PyModuleDef Effect::moduleDef = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"hyperion", /* m_name */ "hyperion", /* m_name */
"Hyperion module", /* m_doc */ "Hyperion module", /* m_doc */
-1, /* m_size */ -1, /* m_size */
Effect::effectMethods, /* m_methods */ Effect::effectMethods, /* m_methods */
NULL, /* m_reload */ NULL, /* m_reload */
NULL, /* m_traverse */ NULL, /* m_traverse */
NULL, /* m_clear */ NULL, /* m_clear */
NULL, /* m_free */ NULL, /* m_free */
}; };
PyObject* Effect::PyInit_hyperion() PyObject* Effect::PyInit_hyperion()
{ {
return PyModule_Create(&moduleDef); return PyModule_Create(&moduleDef);
} }
#else #else
void Effect::PyInit_hyperion() void Effect::PyInit_hyperion()
{ {
Py_InitModule("hyperion", effectMethods); Py_InitModule("hyperion", effectMethods);
} }
#endif #endif
void Effect::registerHyperionExtensionModule() void Effect::registerHyperionExtensionModule()
{ {
PyImport_AppendInittab("hyperion", &PyInit_hyperion); PyImport_AppendInittab("hyperion", &PyInit_hyperion);
} }
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) : Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) :
QThread(), QThread(),
_mainThreadState(mainThreadState), _mainThreadState(mainThreadState),
_priority(priority), _priority(priority),
_timeout(timeout), _timeout(timeout),
_script(script), _script(script),
@ -74,26 +74,26 @@ Effect::~Effect()
void Effect::run() void Effect::run()
{ {
// switch to the main thread state and acquire the GIL // switch to the main thread state and acquire the GIL
PyEval_RestoreThread(_mainThreadState); PyEval_RestoreThread(_mainThreadState);
// Initialize a new thread state // Initialize a new thread state
_interpreterThreadState = Py_NewInterpreter(); _interpreterThreadState = Py_NewInterpreter();
// import the buildtin Hyperion module // import the buildtin Hyperion module
PyObject * module = PyImport_ImportModule("hyperion"); PyObject * module = PyImport_ImportModule("hyperion");
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function // add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr)); PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
// add ledCount variable to the interpreter // add ledCount variable to the interpreter
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount())); PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
// add a args variable to the interpreter // add a args variable to the interpreter
PyObject_SetAttrString(module, "args", json2python(_args)); PyObject_SetAttrString(module, "args", json2python(_args));
// decref the module // decref the module
Py_XDECREF(module); Py_XDECREF(module);
// Set the end time if applicable // Set the end time if applicable
if (_timeout > 0) if (_timeout > 0)
@ -155,19 +155,23 @@ PyObject *Effect::json2python(const Json::Value &json) const
return Py_BuildValue("s", json.asCString()); return Py_BuildValue("s", json.asCString());
case Json::objectValue: case Json::objectValue:
{ {
PyObject * obj = PyDict_New(); PyObject * dict= PyDict_New();
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i) 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: case Json::arrayValue:
{ {
PyObject * list = PyList_New(json.size()); PyObject * list = PyList_New(json.size());
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i) 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; return list;
} }
@ -180,7 +184,7 @@ PyObject *Effect::json2python(const Json::Value &json) const
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
{ {
// get the effect // get the effect
Effect * effect = getEffect(); Effect * effect = getEffect();
// check if we have aborted already // check if we have aborted already
if (effect->_abortRequested) if (effect->_abortRequested)
@ -265,7 +269,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args) PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
{ {
// get the effect // get the effect
Effect * effect = getEffect(); Effect * effect = getEffect();
// check if we have aborted already // check if we have aborted already
if (effect->_abortRequested) if (effect->_abortRequested)
@ -328,7 +332,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
PyObject* Effect::wrapAbort(PyObject *self, PyObject *) PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
{ {
Effect * effect = getEffect(); Effect * effect = getEffect();
// Test if the effect has reached it end time // Test if the effect has reached it end time
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime) if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
@ -341,22 +345,31 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
Effect * Effect::getEffect() Effect * Effect::getEffect()
{ {
// extract the module from the runtime // extract the module from the runtime
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion"); PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
if (PyModule_Check(module)) if (!PyModule_Check(module))
{ {
// retrieve the capsule with the effect // something is wrong
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj"); Py_XDECREF(module);
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
return nullptr;
}
if (PyCapsule_CheckExact(effectCapsule)) // retrieve the capsule with the effect
{ PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
// Get the effect from the capsule Py_XDECREF(module);
return reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
}
}
// something is wrong if (!PyCapsule_CheckExact(effectCapsule))
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl; {
return nullptr; // 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;
} }