Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 987571b

Browse files
committed
Support for Python3 tested
1 parent b6aec95 commit 987571b

File tree

3 files changed

+66
-53
lines changed

3 files changed

+66
-53
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ include_directories(${CMAKE_SOURCE_DIR}/include)
4141
# Prefer static linking over dynamic
4242
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
4343

44-
set(CMAKE_BUILD_TYPE "Debug")
45-
#set(CMAKE_BUILD_TYPE "Release")
44+
#set(CMAKE_BUILD_TYPE "Debug")
45+
set(CMAKE_BUILD_TYPE "Release")
4646

4747
# enable C++11
4848
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall")

deploy/hyperion.tar.gz

79 Bytes
Binary file not shown.

libsrc/effectengine/Effect.cpp

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,36 @@ PyMethodDef Effect::effectMethods[] = {
2222
#if PY_MAJOR_VERSION >= 3
2323
// create the hyperion module
2424
struct PyModuleDef Effect::moduleDef = {
25-
PyModuleDef_HEAD_INIT,
26-
"hyperion", /* m_name */
27-
"Hyperion module", /* m_doc */
28-
-1, /* m_size */
29-
Effect::effectMethods, /* m_methods */
30-
NULL, /* m_reload */
31-
NULL, /* m_traverse */
32-
NULL, /* m_clear */
33-
NULL, /* m_free */
25+
PyModuleDef_HEAD_INIT,
26+
"hyperion", /* m_name */
27+
"Hyperion module", /* m_doc */
28+
-1, /* m_size */
29+
Effect::effectMethods, /* m_methods */
30+
NULL, /* m_reload */
31+
NULL, /* m_traverse */
32+
NULL, /* m_clear */
33+
NULL, /* m_free */
3434
};
3535

3636
PyObject* Effect::PyInit_hyperion()
3737
{
38-
return PyModule_Create(&moduleDef);
38+
return PyModule_Create(&moduleDef);
3939
}
4040
#else
4141
void Effect::PyInit_hyperion()
4242
{
43-
Py_InitModule("hyperion", effectMethods);
43+
Py_InitModule("hyperion", effectMethods);
4444
}
4545
#endif
4646

4747
void Effect::registerHyperionExtensionModule()
4848
{
49-
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
49+
PyImport_AppendInittab("hyperion", &PyInit_hyperion);
5050
}
5151

5252
Effect::Effect(PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) :
5353
QThread(),
54-
_mainThreadState(mainThreadState),
54+
_mainThreadState(mainThreadState),
5555
_priority(priority),
5656
_timeout(timeout),
5757
_script(script),
@@ -74,26 +74,26 @@ Effect::~Effect()
7474

7575
void Effect::run()
7676
{
77-
// switch to the main thread state and acquire the GIL
78-
PyEval_RestoreThread(_mainThreadState);
77+
// switch to the main thread state and acquire the GIL
78+
PyEval_RestoreThread(_mainThreadState);
7979

8080
// Initialize a new thread state
81-
_interpreterThreadState = Py_NewInterpreter();
81+
_interpreterThreadState = Py_NewInterpreter();
8282

83-
// import the buildtin Hyperion module
84-
PyObject * module = PyImport_ImportModule("hyperion");
83+
// import the buildtin Hyperion module
84+
PyObject * module = PyImport_ImportModule("hyperion");
8585

86-
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
87-
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
86+
// add a capsule containing 'this' to the module to be able to retrieve the effect from the callback function
87+
PyObject_SetAttrString(module, "__effectObj", PyCapsule_New(this, nullptr, nullptr));
8888

89-
// add ledCount variable to the interpreter
90-
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
89+
// add ledCount variable to the interpreter
90+
PyObject_SetAttrString(module, "ledCount", Py_BuildValue("i", _imageProcessor->getLedCount()));
9191

92-
// add a args variable to the interpreter
93-
PyObject_SetAttrString(module, "args", json2python(_args));
92+
// add a args variable to the interpreter
93+
PyObject_SetAttrString(module, "args", json2python(_args));
9494

95-
// decref the module
96-
Py_XDECREF(module);
95+
// decref the module
96+
Py_XDECREF(module);
9797

9898
// Set the end time if applicable
9999
if (_timeout > 0)
@@ -155,19 +155,23 @@ PyObject *Effect::json2python(const Json::Value &json) const
155155
return Py_BuildValue("s", json.asCString());
156156
case Json::objectValue:
157157
{
158-
PyObject * obj = PyDict_New();
158+
PyObject * dict= PyDict_New();
159159
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
160160
{
161-
PyDict_SetItemString(obj, i.memberName(), json2python(*i));
161+
PyObject * obj = json2python(*i);
162+
PyDict_SetItemString(dict, i.memberName(), obj);
163+
Py_XDECREF(obj);
162164
}
163-
return obj;
165+
return dict;
164166
}
165167
case Json::arrayValue:
166168
{
167169
PyObject * list = PyList_New(json.size());
168170
for (Json::Value::iterator i = json.begin(); i != json.end(); ++i)
169171
{
170-
PyList_SetItem(list, i.index(), json2python(*i));
172+
PyObject * obj = json2python(*i);
173+
PyList_SetItem(list, i.index(), obj);
174+
Py_XDECREF(obj);
171175
}
172176
return list;
173177
}
@@ -180,7 +184,7 @@ PyObject *Effect::json2python(const Json::Value &json) const
180184
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
181185
{
182186
// get the effect
183-
Effect * effect = getEffect();
187+
Effect * effect = getEffect();
184188

185189
// check if we have aborted already
186190
if (effect->_abortRequested)
@@ -265,7 +269,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
265269
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
266270
{
267271
// get the effect
268-
Effect * effect = getEffect();
272+
Effect * effect = getEffect();
269273

270274
// check if we have aborted already
271275
if (effect->_abortRequested)
@@ -328,7 +332,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
328332

329333
PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
330334
{
331-
Effect * effect = getEffect();
335+
Effect * effect = getEffect();
332336

333337
// Test if the effect has reached it end time
334338
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch() > effect->_endTime)
@@ -341,22 +345,31 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
341345

342346
Effect * Effect::getEffect()
343347
{
344-
// extract the module from the runtime
345-
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
346-
347-
if (PyModule_Check(module))
348-
{
349-
// retrieve the capsule with the effect
350-
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
351-
352-
if (PyCapsule_CheckExact(effectCapsule))
353-
{
354-
// Get the effect from the capsule
355-
return reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
356-
}
357-
}
358-
359-
// something is wrong
360-
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
361-
return nullptr;
348+
// extract the module from the runtime
349+
PyObject * module = PyObject_GetAttrString(PyImport_AddModule("__main__"), "hyperion");
350+
351+
if (!PyModule_Check(module))
352+
{
353+
// something is wrong
354+
Py_XDECREF(module);
355+
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
356+
return nullptr;
357+
}
358+
359+
// retrieve the capsule with the effect
360+
PyObject * effectCapsule = PyObject_GetAttrString(module, "__effectObj");
361+
Py_XDECREF(module);
362+
363+
if (!PyCapsule_CheckExact(effectCapsule))
364+
{
365+
// something is wrong
366+
Py_XDECREF(effectCapsule);
367+
std::cerr << "Unable to retrieve the effect object from the Python runtime" << std::endl;
368+
return nullptr;
369+
}
370+
371+
// Get the effect from the capsule
372+
Effect * effect = reinterpret_cast<Effect *>(PyCapsule_GetPointer(effectCapsule, nullptr));
373+
Py_XDECREF(effectCapsule);
374+
return effect;
362375
}

0 commit comments

Comments
 (0)