]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
python: Fix error handling for callbacks.
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index be3b404a1054e45f4f6b5f4f0e77732551884af1..cab68a96b3515ddbf40731ad488e6fdf25873484 100644 (file)
@@ -173,15 +173,29 @@ typedef guint pyg_flags_type;
         Py_XDECREF(arglist);
         Py_XDECREF(revents_obj);
 
-        if (PyErr_Occurred() || !PyBool_Check(result))
-            throw sigrok::Error(SR_ERR);
+        bool completed = !PyErr_Occurred();
+
+        if (!completed)
+            PyErr_Print();
 
-        bool retval = (result == Py_True);
+        bool valid_result = (completed && PyBool_Check(result));
+
+        if (completed && !valid_result)
+        {
+            PyErr_SetString(PyExc_TypeError,
+                "EventSource callback did not return a boolean");
+            PyErr_Print();
+        }
+
+        bool retval = (valid_result && result == Py_True);
 
         Py_XDECREF(result);
 
         PyGILState_Release(gstate);
 
+        if (!valid_result)
+            throw sigrok::Error(SR_ERR);
+
         return retval;
     };
 
@@ -212,9 +226,27 @@ typedef guint pyg_flags_type;
         Py_XDECREF(arglist);
         Py_XDECREF(log_obj);
         Py_XDECREF(string_obj);
+
+        bool completed = !PyErr_Occurred();
+
+        if (!completed)
+            PyErr_Print();
+
+        bool valid_result = (completed && result == Py_None);
+
         Py_XDECREF(result);
 
+        if (completed && !valid_result)
+        {
+            PyErr_SetString(PyExc_TypeError,
+                "Log callback did not return None");
+            PyErr_Print();
+        }
+
         PyGILState_Release(gstate);
+
+        if (!valid_result)
+            throw sigrok::Error(SR_ERR);
     };
 
     Py_XINCREF($input);
@@ -248,9 +280,27 @@ typedef guint pyg_flags_type;
         Py_XDECREF(arglist);
         Py_XDECREF(device_obj);
         Py_XDECREF(packet_obj);
+
+        bool completed = !PyErr_Occurred();
+
+        if (!completed)
+            PyErr_Print();
+
+        bool valid_result = (completed && result == Py_None);
+
         Py_XDECREF(result);
 
+        if (completed && !valid_result)
+        {
+            PyErr_SetString(PyExc_TypeError,
+                "Datafeed callback did not return None");
+            PyErr_Print();
+        }
+
         PyGILState_Release(gstate);
+
+        if (!valid_result)
+            throw sigrok::Error(SR_ERR);
     };
 
     Py_XINCREF($input);
@@ -287,7 +337,7 @@ std::map<std::string, std::string> dict_to_map_string(PyObject *dict)
 /* Convert from a Python type to Glib::Variant, according to config key data type. */
 Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::ConfigKey *key)
 {
-    enum sr_datatype type = key->data_type()->id();
+    enum sr_datatype type = (enum sr_datatype) key->data_type()->id();
 
     if (type == SR_T_UINT64 && PyInt_Check(input))
         return Glib::Variant<guint64>::create(PyInt_AsLong(input));
@@ -359,6 +409,47 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
 
 %include "doc.i"
 
+%define %attributevector(Class, Type, Name, Get)
+%rename(_ ## Get) sigrok::Class::Get;
+%extend sigrok::Class
+{
+%pythoncode
+{
+  Name = property(_ ## Get)
+}
+}
+%enddef
+
+%define %attributemap(Class, Type, Name, Get)
+%rename(_ ## Get) sigrok::Class::Get;
+%extend sigrok::Class
+{
+%pythoncode
+{
+  Name = property(fget = lambda x: x._ ## Get().asdict(), doc=_ ## Get.__doc__)
+}
+}
+%enddef
+
+%define %enumextras(Class)
+%extend sigrok::Class
+{
+  long __hash__()
+  {
+    return (long) $self;
+  }
+
+%pythoncode
+{
+  def __eq__(self, other):
+    return (type(self) is type(other) and hash(self) == hash(other))
+
+  def __ne__(self, other):
+    return (type(self) is not type(other) or hash(self) != hash(other))
+}
+}
+%enddef
+
 %include "../../../swig/classes.i"
 
 /* Support Driver.scan() with keyword arguments. */
@@ -377,7 +468,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
         {
             if (!PyString_Check(py_key))
                 throw sigrok::Error(SR_ERR_ARG);
-            auto key = sigrok::ConfigKey::get(PyString_AsString(py_key));
+            auto key = sigrok::ConfigKey::get_by_identifier(PyString_AsString(py_key));
             auto value = python_to_variant_by_key(py_value, key);
             options[key] = value;
         }