X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=a00efff2536d78911cd7a8b41f2744ad418c8479;hb=62bd644f55b674730957be4e3dcd3d3362148cfc;hp=7d4304fca13e9644a2117d74571576fd24cd2495;hpb=ed6b4c4747ed98374feb59ac5758a24bd4d9a63f;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 7d4304fc..a00efff2 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -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,14 +45,15 @@ which provides access to the error code and description." %module(docstring=DOCSTRING) classes %{ +#include "config.h" + +#include #include +#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include +PyObject *PyGObject_lib; PyObject *GLib; -PyTypeObject *IOChannel; -PyTypeObject *PollFD; - -#include "config.h" #if PYGOBJECT_FLAGS_SIGNED typedef gint pyg_flags_type; @@ -60,30 +61,45 @@ typedef gint pyg_flags_type; typedef guint pyg_flags_type; #endif +#if PY_VERSION_HEX >= 0x03000000 +#define string_check PyUnicode_Check +#define string_from_python PyUnicode_AsUTF8 +#define string_to_python PyUnicode_FromString +#else +#define string_check PyString_Check +#define string_from_python PyString_AsString +#define string_to_python PyString_FromString +#endif + %} %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"); - IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel"); - PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD"); + /* + * 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 nullptr; +#else + return; +#endif + } import_array(); %} +%include "../../../swig/templates.i" + /* Map file objects to file descriptors. */ %typecheck(SWIG_TYPECHECK_POINTER) int fd { $1 = (PyObject_AsFileDescriptor($input) != -1); } -%typemap(in) int fd { - int fd = PyObject_AsFileDescriptor($input); - if (fd == -1) - SWIG_exception(SWIG_TypeError, - "Expected file object or integer file descriptor"); - else - $1 = fd; -} - /* Map from Glib::Variant to native Python types. */ %typemap(out) Glib::VariantBase { GValue *value = g_new0(GValue, 1); @@ -91,160 +107,85 @@ 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("unpack"), - const_cast(""), NULL); + const_cast("unpack"), nullptr); Py_XDECREF(variant); g_free(value); } -/* Map from Glib::IOCondition to GLib.IOCondition. */ -%typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition { - pyg_flags_type flags; - $1 = pygobject_check($input, &PyGFlags_Type) && - (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1); -} - -%typemap(in) Glib::IOCondition { - if (!pygobject_check($input, &PyGFlags_Type)) - SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value"); - pyg_flags_type flags; - if (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) == -1) - SWIG_exception(SWIG_TypeError, "Not a valid Glib.IOCondition value"); - $1 = (Glib::IOCondition) flags; -} - -/* And back */ -%typemap(out) Glib::IOCondition { - GValue *value = g_new0(GValue, 1); - g_value_init(value, G_TYPE_IO_CONDITION); - g_value_set_flags(value, &$1); - $result = pyg_value_as_pyobject(value, true); - g_free(value); -} - -/* Map from GLib.PollFD to Glib::PollFD *. */ -%typecheck(SWIG_TYPECHECK_POINTER) Glib::PollFD { - $1 = pygobject_check($input, PollFD); -} - -%typemap(in) Glib::PollFD { - if (!pygobject_check($input, PollFD)) - SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD"); - PyObject *fd_obj = PyObject_GetAttrString($input, "fd"); - PyObject *events_obj = PyObject_GetAttrString($input, "events"); - pyg_flags_type flags; - pyg_flags_get_value(G_TYPE_IO_CONDITION, events_obj, &flags); - int fd = PyInt_AsLong(fd_obj); - Glib::IOCondition events = (Glib::IOCondition) flags; - $1 = Glib::PollFD(fd, events); -} - -/* Map from GLib.IOChannel to Glib::IOChannel *. */ -%typecheck(SWIG_TYPECHECK_POINTER) Glib::RefPtr { - $1 = pygobject_check($input, IOChannel); -} - -%typemap(in) Glib::RefPtr { - if (!pygobject_check($input, IOChannel)) - SWIG_exception(SWIG_TypeError, "Expected GLib.IOChannel"); - $1 = Glib::wrap((GIOChannel *) PyObject_Hash($input), true); -} - -/* Map from callable PyObject to SourceCallbackFunction. */ -%typecheck(SWIG_TYPECHECK_POINTER) sigrok::SourceCallbackFunction { +/* Map from callable PyObject to LogCallbackFunction */ +%typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction { $1 = PyCallable_Check($input); } -%typemap(in) sigrok::SourceCallbackFunction { +%typemap(in) sigrok::LogCallbackFunction { if (!PyCallable_Check($input)) SWIG_exception(SWIG_TypeError, "Expected a callable Python object"); - $1 = [=] (Glib::IOCondition revents) { + $1 = [=] (const sigrok::LogLevel *loglevel, std::string message) { auto gstate = PyGILState_Ensure(); - GValue *value = g_new0(GValue, 1); - g_value_init(value, G_TYPE_IO_CONDITION); - g_value_set_flags(value, revents); - auto revents_obj = pyg_value_as_pyobject(value, true); - g_free(value); + auto log_obj = SWIG_NewPointerObj( + SWIG_as_voidptr(loglevel), SWIGTYPE_p_sigrok__LogLevel, 0); + + auto string_obj = string_to_python(message.c_str()); - auto arglist = Py_BuildValue("(O)", revents_obj); + auto arglist = Py_BuildValue("(OO)", log_obj, string_obj); auto result = PyEval_CallObject($input, arglist); Py_XDECREF(arglist); - Py_XDECREF(revents_obj); + Py_XDECREF(log_obj); + Py_XDECREF(string_obj); bool completed = !PyErr_Occurred(); if (!completed) PyErr_Print(); - bool valid_result = (completed && PyBool_Check(result)); + bool valid_result = (completed && result == Py_None); + + Py_XDECREF(result); if (completed && !valid_result) { PyErr_SetString(PyExc_TypeError, - "EventSource callback did not return a boolean"); + "Log callback did not return None"); 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; }; Py_XINCREF($input); } -/* Map from callable PyObject to LogCallbackFunction */ -%typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction { +/* Map from callable PyObject to SessionStoppedCallback */ +%typecheck(SWIG_TYPECHECK_POINTER) sigrok::SessionStoppedCallback { $1 = PyCallable_Check($input); } -%typemap(in) sigrok::LogCallbackFunction { +%typemap(in) sigrok::SessionStoppedCallback { if (!PyCallable_Check($input)) SWIG_exception(SWIG_TypeError, "Expected a callable Python object"); - $1 = [=] (const sigrok::LogLevel *loglevel, string message) { - auto gstate = PyGILState_Ensure(); - - auto log_obj = SWIG_NewPointerObj( - SWIG_as_voidptr(loglevel), SWIGTYPE_p_sigrok__LogLevel, 0); - - auto string_obj = PyString_FromString(message.c_str()); - - auto arglist = Py_BuildValue("(OO)", log_obj, string_obj); - - auto result = PyEval_CallObject($input, arglist); - - Py_XDECREF(arglist); - Py_XDECREF(log_obj); - Py_XDECREF(string_obj); - - bool completed = !PyErr_Occurred(); + $1 = [=] () { + const auto gstate = PyGILState_Ensure(); - if (!completed) - PyErr_Print(); + const auto result = PyEval_CallObject($input, nullptr); + const bool completed = !PyErr_Occurred(); + const bool valid_result = (completed && result == Py_None); - bool valid_result = (completed && result == Py_None); - - Py_XDECREF(result); - - if (completed && !valid_result) - { + if (completed && !valid_result) { PyErr_SetString(PyExc_TypeError, - "Log callback did not return None"); - PyErr_Print(); + "Session stop callback did not return None"); } + if (!valid_result) + PyErr_Print(); + Py_XDECREF(result); PyGILState_Release(gstate); if (!valid_result) @@ -353,7 +294,7 @@ typedef guint pyg_flags_type; %{ -#include "libsigrok/libsigrok.hpp" +#include "libsigrokcxx/libsigrokcxx.hpp" /* Convert from a Python dict to a std::map */ std::map dict_to_map_string(PyObject *dict) @@ -367,12 +308,12 @@ std::map dict_to_map_string(PyObject *dict) Py_ssize_t pos = 0; while (PyDict_Next(dict, &pos, &py_key, &py_value)) { - if (!PyString_Check(py_key)) + if (!string_check(py_key)) throw sigrok::Error(SR_ERR_ARG); - if (!PyString_Check(py_value)) + if (!string_check(py_value)) throw sigrok::Error(SR_ERR_ARG); - auto key = PyString_AsString(py_key); - auto value = PyString_AsString(py_value); + auto key = string_from_python(py_key); + auto value = string_from_python(py_value); output[key] = value; } @@ -388,8 +329,8 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config return Glib::Variant::create(PyInt_AsLong(input)); if (type == SR_T_UINT64 && PyLong_Check(input)) return Glib::Variant::create(PyLong_AsLong(input)); - else if (type == SR_T_STRING && PyString_Check(input)) - return Glib::Variant::create(PyString_AsString(input)); + else if (type == SR_T_STRING && string_check(input)) + return Glib::Variant::create(string_from_python(input)); else if (type == SR_T_BOOL && PyBool_Check(input)) return Glib::Variant::create(input == Py_True); else if (type == SR_T_FLOAT && PyFloat_Check(input)) @@ -410,8 +351,8 @@ Glib::VariantBase python_to_variant_by_option(PyObject *input, return Glib::Variant::create(PyInt_AsLong(input)); if (type == G_VARIANT_TYPE_UINT64 && PyLong_Check(input)) return Glib::Variant::create(PyLong_AsLong(input)); - else if (type == G_VARIANT_TYPE_STRING && PyString_Check(input)) - return Glib::Variant::create(PyString_AsString(input)); + else if (type == G_VARIANT_TYPE_STRING && string_check(input)) + return Glib::Variant::create(string_from_python(input)); else if (type == G_VARIANT_TYPE_BOOLEAN && PyBool_Check(input)) return Glib::Variant::create(input == Py_True); else if (type == G_VARIANT_TYPE_DOUBLE && PyFloat_Check(input)) @@ -435,9 +376,9 @@ std::map dict_to_map_options(PyObject *dict, Py_ssize_t pos = 0; while (PyDict_Next(dict, &pos, &py_key, &py_value)) { - if (!PyString_Check(py_key)) + if (!string_check(py_key)) throw sigrok::Error(SR_ERR_ARG); - auto key = PyString_AsString(py_key); + auto key = string_from_python(py_key); auto value = python_to_variant_by_option(py_value, options[key]); output[key] = value; } @@ -449,11 +390,12 @@ std::map dict_to_map_options(PyObject *dict, /* Ignore these methods, we will override them below. */ %ignore sigrok::Analog::data; +%ignore sigrok::Logic::data; %ignore sigrok::Driver::scan; %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; @@ -485,6 +427,16 @@ std::map dict_to_map_options(PyObject *dict, return (long) $self; } + std::string __str__() + { + return $self->name(); + } + + std::string __repr__() + { + return "Class." + $self->name(); + } + %pythoncode { def __eq__(self, other): @@ -512,9 +464,9 @@ std::map dict_to_map_options(PyObject *dict, while (PyDict_Next(dict, &pos, &py_key, &py_value)) { - if (!PyString_Check(py_key)) + if (!string_check(py_key)) throw sigrok::Error(SR_ERR_ARG); - auto key = sigrok::ConfigKey::get_by_identifier(PyString_AsString(py_key)); + auto key = sigrok::ConfigKey::get_by_identifier(string_from_python(py_key)); auto value = python_to_variant_by_key(py_value, key); options[key] = value; } @@ -596,3 +548,43 @@ std::map dict_to_map_options(PyObject *dict, data = property(_data) } } + +/* Return NumPy array from Logic::data(). */ +%extend sigrok::Logic +{ + PyObject * _data() + { + npy_intp dims[2]; + dims[0] = $self->data_length() / $self->unit_size(); + dims[1] = $self->unit_size(); + int typenum = NPY_UINT8; + void *data = $self->data_pointer(); + return PyArray_SimpleNewFromData(2, dims, typenum, data); + } + +%pythoncode +{ + data = property(_data) +} +} + +/* Create logic packet from Python buffer. */ +%extend sigrok::Context +{ + std::shared_ptr _create_logic_packet_buf(PyObject *buf, unsigned int unit_size) + { + Py_buffer view; + PyObject_GetBuffer(buf, &view, PyBUF_SIMPLE); + return $self->create_logic_packet(view.buf, view.len, unit_size); + } +} + +%pythoncode +{ + def _Context_create_logic_packet(self, buf, unit_size): + return self._create_logic_packet_buf(buf, unit_size) + + Context.create_logic_packet = _Context_create_logic_packet +} + +%include "doc_end.i"