@@ -22,36 +22,36 @@ PyMethodDef Effect::effectMethods[] = {
22
22
#if PY_MAJOR_VERSION >= 3
23
23
// create the hyperion module
24
24
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 */
34
34
};
35
35
36
36
PyObject* Effect::PyInit_hyperion ()
37
37
{
38
- return PyModule_Create (&moduleDef);
38
+ return PyModule_Create (&moduleDef);
39
39
}
40
40
#else
41
41
void Effect::PyInit_hyperion ()
42
42
{
43
- Py_InitModule (" hyperion" , effectMethods);
43
+ Py_InitModule (" hyperion" , effectMethods);
44
44
}
45
45
#endif
46
46
47
47
void Effect::registerHyperionExtensionModule ()
48
48
{
49
- PyImport_AppendInittab (" hyperion" , &PyInit_hyperion);
49
+ PyImport_AppendInittab (" hyperion" , &PyInit_hyperion);
50
50
}
51
51
52
52
Effect::Effect (PyThreadState * mainThreadState, int priority, int timeout, const std::string & script, const Json::Value & args) :
53
53
QThread(),
54
- _mainThreadState(mainThreadState),
54
+ _mainThreadState(mainThreadState),
55
55
_priority(priority),
56
56
_timeout(timeout),
57
57
_script(script),
@@ -74,26 +74,26 @@ Effect::~Effect()
74
74
75
75
void Effect::run ()
76
76
{
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);
79
79
80
80
// Initialize a new thread state
81
- _interpreterThreadState = Py_NewInterpreter ();
81
+ _interpreterThreadState = Py_NewInterpreter ();
82
82
83
- // import the buildtin Hyperion module
84
- PyObject * module = PyImport_ImportModule (" hyperion" );
83
+ // import the buildtin Hyperion module
84
+ PyObject * module = PyImport_ImportModule (" hyperion" );
85
85
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 ));
88
88
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 ()));
91
91
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));
94
94
95
- // decref the module
96
- Py_XDECREF (module );
95
+ // decref the module
96
+ Py_XDECREF (module );
97
97
98
98
// Set the end time if applicable
99
99
if (_timeout > 0 )
@@ -155,19 +155,23 @@ PyObject *Effect::json2python(const Json::Value &json) const
155
155
return Py_BuildValue (" s" , json.asCString ());
156
156
case Json::objectValue:
157
157
{
158
- PyObject * obj = PyDict_New ();
158
+ PyObject * dict = PyDict_New ();
159
159
for (Json::Value::iterator i = json.begin (); i != json.end (); ++i)
160
160
{
161
- PyDict_SetItemString (obj, i.memberName (), json2python (*i));
161
+ PyObject * obj = json2python (*i);
162
+ PyDict_SetItemString (dict, i.memberName (), obj);
163
+ Py_XDECREF (obj);
162
164
}
163
- return obj ;
165
+ return dict ;
164
166
}
165
167
case Json::arrayValue:
166
168
{
167
169
PyObject * list = PyList_New (json.size ());
168
170
for (Json::Value::iterator i = json.begin (); i != json.end (); ++i)
169
171
{
170
- PyList_SetItem (list, i.index (), json2python (*i));
172
+ PyObject * obj = json2python (*i);
173
+ PyList_SetItem (list, i.index (), obj);
174
+ Py_XDECREF (obj);
171
175
}
172
176
return list;
173
177
}
@@ -180,7 +184,7 @@ PyObject *Effect::json2python(const Json::Value &json) const
180
184
PyObject* Effect::wrapSetColor (PyObject *self, PyObject *args)
181
185
{
182
186
// get the effect
183
- Effect * effect = getEffect ();
187
+ Effect * effect = getEffect ();
184
188
185
189
// check if we have aborted already
186
190
if (effect->_abortRequested )
@@ -265,7 +269,7 @@ PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args)
265
269
PyObject* Effect::wrapSetImage (PyObject *self, PyObject *args)
266
270
{
267
271
// get the effect
268
- Effect * effect = getEffect ();
272
+ Effect * effect = getEffect ();
269
273
270
274
// check if we have aborted already
271
275
if (effect->_abortRequested )
@@ -328,7 +332,7 @@ PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args)
328
332
329
333
PyObject* Effect::wrapAbort (PyObject *self, PyObject *)
330
334
{
331
- Effect * effect = getEffect ();
335
+ Effect * effect = getEffect ();
332
336
333
337
// Test if the effect has reached it end time
334
338
if (effect->_timeout > 0 && QDateTime::currentMSecsSinceEpoch () > effect->_endTime )
@@ -341,22 +345,31 @@ PyObject* Effect::wrapAbort(PyObject *self, PyObject *)
341
345
342
346
Effect * Effect::getEffect ()
343
347
{
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;
362
375
}
0 commit comments