]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
bindings/python: Handle import failures without crashing
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index cab68a96b3515ddbf40731ad488e6fdf25873484..2afe579124196e42f7782c95da099dc2e8c858f9 100644 (file)
@@ -24,7 +24,7 @@ Introduction
 ------------
 
 The pysigrok API provides an object-oriented Python interface to the
-functionality in libsigrok. It is built on top of the sigrok++ C++ API.
+functionality in libsigrok. It is built on top of the libsigrokcxx C++ API.
 
 Getting started
 ---------------
@@ -45,8 +45,11 @@ which provides access to the error code and description."
 %module(docstring=DOCSTRING) classes
 
 %{
+#include <stdio.h>
 #include <pygobject.h>
+#include <numpy/arrayobject.h>
 
+PyObject *PyGObject_lib;
 PyObject *GLib;
 PyTypeObject *IOChannel;
 PyTypeObject *PollFD;
@@ -62,10 +65,21 @@ typedef guint pyg_flags_type;
 %}
 
 %init %{
-    pygobject_init(-1, -1, -1);
+    PyGObject_lib = pygobject_init(-1, -1, -1);
+    if (!PyGObject_lib)
+        fprintf(stderr, "pygobject initialization failed.\n");
     GLib = PyImport_ImportModule("gi.repository.GLib");
+    /*
+     * This check can't save us if the import fails, but at least it gives us
+     * a starting point to trace the issue versus straight out crashing.
+     */
+    if (!GLib) {
+        fprintf(stderr, "Import of gi.repository.GLib failed.\n");
+        return;
+    }
     IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
     PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
+    import_array();
 %}
 
 /* Map file objects to file descriptors. */
@@ -211,7 +225,7 @@ typedef guint pyg_flags_type;
     if (!PyCallable_Check($input))
         SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
 
-    $1 = [=] (const sigrok::LogLevel *loglevel, string message) {
+    $1 = [=] (const sigrok::LogLevel *loglevel, std::string message) {
         auto gstate = PyGILState_Ensure();
 
         auto log_obj = SWIG_NewPointerObj(
@@ -306,9 +320,52 @@ typedef guint pyg_flags_type;
     Py_XINCREF($input);
 }
 
+/* Cast PacketPayload pointers to correct subclass type. */
+%ignore sigrok::Packet::payload;
+
+%extend sigrok::Packet
+{
+    std::shared_ptr<sigrok::Header> _payload_header()
+    {
+        return dynamic_pointer_cast<sigrok::Header>($self->payload());
+    }
+    std::shared_ptr<sigrok::Meta> _payload_meta()
+    {
+        return dynamic_pointer_cast<sigrok::Meta>($self->payload());
+    }
+    std::shared_ptr<sigrok::Analog> _payload_analog()
+    {
+        return dynamic_pointer_cast<sigrok::Analog>($self->payload());
+    }
+    std::shared_ptr<sigrok::Logic> _payload_logic()
+    {
+        return dynamic_pointer_cast<sigrok::Logic>($self->payload());
+    }
+}
+
+%extend sigrok::Packet
+{
+%pythoncode
+{
+    def _payload(self):
+        if self.type == PacketType.HEADER:
+            return self._payload_header()
+        elif self.type == PacketType.META:
+            return self._payload_meta()
+        elif self.type == PacketType.LOGIC:
+            return self._payload_logic()
+        elif self.type == PacketType.ANALOG:
+            return self._payload_analog()
+        else:
+            return None
+
+    payload = property(_payload)
+}
+}
+
 %{
 
-#include "libsigrok/libsigrok.hpp"
+#include "libsigrokcxx/libsigrokcxx.hpp"
 
 /* Convert from a Python dict to a std::map<std::string, std::string> */
 std::map<std::string, std::string> dict_to_map_string(PyObject *dict)
@@ -403,6 +460,7 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
 %}
 
 /* Ignore these methods, we will override them below. */
+%ignore sigrok::Analog::data;
 %ignore sigrok::Driver::scan;
 %ignore sigrok::InputFormat::create_input;
 %ignore sigrok::OutputFormat::create_output;
@@ -530,3 +588,23 @@ std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
         $self->config_set(key, python_to_variant_by_key(input, key));
     }
 }
+
+/* Return NumPy array from Analog::data(). */
+%extend sigrok::Analog
+{
+    PyObject * _data()
+    {
+        int nd = 2;
+        npy_intp dims[2];
+        dims[0] = $self->channels().size();
+        dims[1] = $self->num_samples();
+        int typenum = NPY_FLOAT;
+        void *data = $self->data_pointer();
+        return PyArray_SimpleNewFromData(nd, dims, typenum, data);
+    }
+
+%pythoncode
+{
+    data = property(_data)
+}
+}