]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
python: Give all enum values __doc__ strings.
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index 856038eeb37cf01a0929c6db6e04a1eeea023cf3..46336549609cfc3a46cd665fbb97b6b8c1168db3 100644 (file)
@@ -47,6 +47,7 @@ which provides access to the error code and description."
 %{
 #include <stdio.h>
 #include <pygobject.h>
+#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
 #include <numpy/arrayobject.h>
 
 PyObject *PyGObject_lib;
@@ -74,7 +75,7 @@ typedef guint pyg_flags_type;
     if (!GLib) {
         fprintf(stderr, "Import of gi.repository.GLib failed.\n");
 #if PY_VERSION_HEX >= 0x03000000
-        return NULL;
+        return nullptr;
 #else
         return;
 #endif
@@ -82,6 +83,8 @@ typedef guint pyg_flags_type;
     import_array();
 %}
 
+%include "../../../swig/templates.i"
+
 /* Map file objects to file descriptors. */
 %typecheck(SWIG_TYPECHECK_POINTER) int fd {
     $1 = (PyObject_AsFileDescriptor($input) != -1);
@@ -94,8 +97,7 @@ typedef guint pyg_flags_type;
     g_value_set_variant(value, $1.gobj());
     PyObject *variant = pyg_value_as_pyobject(value, true);
     $result = PyObject_CallMethod(variant,
-        const_cast<char *>("unpack"),
-        const_cast<char *>(""), NULL);
+        const_cast<char *>("unpack"), nullptr);
     Py_XDECREF(variant);
     g_free(value);
 }
@@ -150,6 +152,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);
@@ -349,7 +384,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
 %ignore sigrok::InputFormat::create_input;
 %ignore sigrok::OutputFormat::create_output;
 
-%include "doc.i"
+%include "doc_start.i"
 
 %define %attributevector(Class, Type, Name, Get)
 %rename(_ ## Get) sigrok::Class::Get;
@@ -492,3 +527,5 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
     data = property(_data)
 }
 }
+
+%include "doc_end.i"