X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=94913d0f65caa052065a122070d9b48939e0d65a;hp=46336549609cfc3a46cd665fbb97b6b8c1168db3;hb=a2916ad01779e1cf8a5e220eb6288dab9734b146;hpb=df979d6dc6949b1d5c3814b177cb71d6c40d03d4 diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 46336549..94913d0f 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -45,6 +45,8 @@ 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 @@ -53,14 +55,22 @@ which provides access to the error code and description." PyObject *PyGObject_lib; PyObject *GLib; -#include "config.h" - #if PYGOBJECT_FLAGS_SIGNED typedef gint pyg_flags_type; #else 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 %{ @@ -102,6 +112,9 @@ typedef guint pyg_flags_type; g_free(value); } +/* Use the same typemap above for Glib::VariantContainerBase */ +%apply Glib::VariantBase { Glib::VariantContainerBase } + /* Map from callable PyObject to LogCallbackFunction */ %typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction { $1 = PyCallable_Check($input); @@ -117,7 +130,7 @@ typedef guint pyg_flags_type; auto log_obj = SWIG_NewPointerObj( SWIG_as_voidptr(loglevel), SWIGTYPE_p_sigrok__LogLevel, 0); - auto string_obj = PyString_FromString(message.c_str()); + auto string_obj = string_to_python(message.c_str()); auto arglist = Py_BuildValue("(OO)", log_obj, string_obj); @@ -298,12 +311,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; } @@ -319,16 +332,23 @@ 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)) return Glib::Variant::create(PyFloat_AsDouble(input)); else if (type == SR_T_INT32 && PyInt_Check(input)) return Glib::Variant::create(PyInt_AsLong(input)); - else - throw sigrok::Error(SR_ERR_ARG); + else if ((type == SR_T_RATIONAL_VOLT) && PyTuple_Check(input) && (PyTuple_Size(input) == 2)) { + PyObject *numObj = PyTuple_GetItem(input, 0); + PyObject *denomObj = PyTuple_GetItem(input, 1); + if ((PyInt_Check(numObj) || PyLong_Check(numObj)) && (PyInt_Check(denomObj) || PyLong_Check(denomObj))) { + std::tuple tpl = {PyInt_AsLong(numObj), PyInt_AsLong(denomObj)}; + return Glib::Variant< std::tuple >::Variant::create(tpl); + } + } + throw sigrok::Error(SR_ERR_ARG); } /* Convert from a Python type to Glib::Variant, according to Option data type. */ @@ -341,8 +361,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)) @@ -366,9 +386,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; } @@ -380,6 +400,7 @@ 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; @@ -416,6 +437,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): @@ -443,9 +474,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; } @@ -528,4 +559,42 @@ std::map dict_to_map_options(PyObject *dict, } } +/* 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"