X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=126991b7c247d58e8fd8ae75d7eabbee76f0e24c;hb=58aa1f8359007804f48a4f881e6782a06e1b729a;hp=f8620abe993ba7af380ba531cbbe8177cb3e44de;hpb=f774095496a5ab9b68ce79503ae7d45f717c0006;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index f8620abe..126991b7 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -26,6 +26,14 @@ PyObject *GLib; PyTypeObject *IOChannel; PyTypeObject *PollFD; +#include "../../../../config.h" + +#if PYGOBJECT_FLAGS_SIGNED +typedef gint pyg_flags_type; +#else +typedef guint pyg_flags_type; +#endif + %} %init %{ @@ -64,7 +72,7 @@ PyTypeObject *PollFD; /* Map from Glib::IOCondition to GLib.IOCondition. */ %typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition { - gint flags; + pyg_flags_type flags; $1 = pygobject_check($input, &PyGFlags_Type) && (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1); } @@ -72,7 +80,7 @@ PyTypeObject *PollFD; %typemap(in) Glib::IOCondition { if (!pygobject_check($input, &PyGFlags_Type)) SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value"); - gint flags; + 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; @@ -97,7 +105,7 @@ PyTypeObject *PollFD; SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD"); PyObject *fd_obj = PyObject_GetAttrString($input, "fd"); PyObject *events_obj = PyObject_GetAttrString($input, "events"); - gint flags; + 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; @@ -228,7 +236,7 @@ PyTypeObject *PollFD; #include "libsigrok/libsigrok.hpp" /* Convert from a Python dict to a std::map */ -std::map dict_to_map(PyObject *dict) +std::map dict_to_map_string(PyObject *dict) { if (!PyDict_Check(dict)) throw sigrok::Error(SR_ERR_ARG); @@ -272,6 +280,51 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config throw sigrok::Error(SR_ERR_ARG); } +/* Convert from a Python type to Glib::Variant, according to Option data type. */ +Glib::VariantBase python_to_variant_by_option(PyObject *input, + std::shared_ptr option) +{ + GVariantType *type = option->get_default_value().get_type().gobj(); + + if (type == G_VARIANT_TYPE_UINT64 && PyInt_Check(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_BOOLEAN && PyBool_Check(input)) + return Glib::Variant::create(input == Py_True); + else if (type == G_VARIANT_TYPE_DOUBLE && PyFloat_Check(input)) + return Glib::Variant::create(PyFloat_AsDouble(input)); + else if (type == G_VARIANT_TYPE_INT32 && PyInt_Check(input)) + return Glib::Variant::create(PyInt_AsLong(input)); + else + throw sigrok::Error(SR_ERR_ARG); +} + +/* Convert from a Python dict to a std::map */ +std::map dict_to_map_options(PyObject *dict, + std::map > options) +{ + if (!PyDict_Check(dict)) + throw sigrok::Error(SR_ERR_ARG); + + std::map output; + + PyObject *py_key, *py_value; + Py_ssize_t pos = 0; + + while (PyDict_Next(dict, &pos, &py_key, &py_value)) { + if (!PyString_Check(py_key)) + throw sigrok::Error(SR_ERR_ARG); + auto key = PyString_AsString(py_key); + auto value = python_to_variant_by_option(py_value, options[key]); + output[key] = value; + } + + return output; +} + %} /* Ignore these methods, we will override them below. */ @@ -319,7 +372,7 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config { std::shared_ptr _open_file_kwargs(std::string filename, PyObject *dict) { - return $self->open_file(filename, dict_to_map(dict)); + return $self->open_file(filename, dict_to_map_string(dict)); } } @@ -337,7 +390,8 @@ Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::Config std::shared_ptr _create_output_kwargs( std::shared_ptr device, PyObject *dict) { - return $self->create_output(device, dict_to_map(dict)); + return $self->create_output(device, + dict_to_map_options(dict, $self->get_options())); } }