]> sigrok.org Git - libsigrok.git/commitdiff
python: Wrap session stop callback
authorDaniel Elstner <redacted>
Sat, 10 Oct 2015 02:38:54 +0000 (04:38 +0200)
committerDaniel Elstner <redacted>
Sat, 17 Oct 2015 15:40:42 +0000 (17:40 +0200)
bindings/python/sigrok/core/classes.i

index 856038eeb37cf01a0929c6db6e04a1eeea023cf3..be4df153080d5149b897b574cb1c5c0c109e38bd 100644 (file)
@@ -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);