]> sigrok.org Git - libsigrok.git/blobdiff - bindings/python/sigrok/core/classes.i
python: Fix the build for Python >= 3.
[libsigrok.git] / bindings / python / sigrok / core / classes.i
index 75f20ea51df165f9f2d2e7992aa2b6ba27dc5635..93e03ef29e5203d9e45035a22da2aa7e93815fc8 100644 (file)
@@ -45,9 +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;
@@ -63,8 +65,22 @@ 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");
+#if PY_VERSION_HEX >= 0x03000000
+        return NULL;
+#else
+        return;
+#endif
+    }
     IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
     PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
     import_array();
@@ -213,7 +229,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(
@@ -353,7 +369,7 @@ typedef guint pyg_flags_type;
 
 %{
 
-#include "libsigrok/libsigrokcxx.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)