From: Daniel Elstner Date: Sat, 10 Oct 2015 02:38:54 +0000 (+0200) Subject: python: Wrap session stop callback X-Git-Tag: libsigrok-0.4.0~214 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2c3c9b999cd5c1130afeeee4094400592c9e1bde;p=libsigrok.git python: Wrap session stop callback --- 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);