X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=be4df153080d5149b897b574cb1c5c0c109e38bd;hb=2c3c9b999cd5c1130afeeee4094400592c9e1bde;hp=856038eeb37cf01a0929c6db6e04a1eeea023cf3;hpb=ee9953ef12514d833c6719777dfc285d56c0ffc1;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 856038ee..be4df153 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -150,6 +150,39 @@ typedef guint pyg_flags_type; Py_XINCREF($input); } +/* Map from callable PyObject to SessionStoppedCallback */ +%typecheck(SWIG_TYPECHECK_POINTER) sigrok::SessionStoppedCallback { + $1 = PyCallable_Check($input); +} + +%typemap(in) sigrok::SessionStoppedCallback { + if (!PyCallable_Check($input)) + SWIG_exception(SWIG_TypeError, "Expected a callable Python object"); + + $1 = [=] () { + const auto gstate = PyGILState_Ensure(); + + const auto result = PyEval_CallObject($input, nullptr); + const bool completed = !PyErr_Occurred(); + const bool valid_result = (completed && result == Py_None); + + if (completed && !valid_result) { + PyErr_SetString(PyExc_TypeError, + "Session stop callback did not return None"); + } + if (!valid_result) + PyErr_Print(); + + Py_XDECREF(result); + PyGILState_Release(gstate); + + if (!valid_result) + throw sigrok::Error(SR_ERR); + }; + + Py_XINCREF($input); +} + /* Map from callable PyObject to DatafeedCallbackFunction */ %typecheck(SWIG_TYPECHECK_POINTER) sigrok::DatafeedCallbackFunction { $1 = PyCallable_Check($input);