X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=bindings%2Fpython%2Fsigrok%2Fcore%2Fclasses.i;h=2afd9cf131ed6ae7f7977dce357ae881fa4b9a66;hb=1e95832b3e17f7dc5f48046ec9cf6e03a96a1779;hp=49cfba98791443e48c7b0f4fe8d7eec246d7a873;hpb=e66728886ff684e286a2544986116251e4c5531e;p=libsigrok.git diff --git a/bindings/python/sigrok/core/classes.i b/bindings/python/sigrok/core/classes.i index 49cfba98..2afd9cf1 100644 --- a/bindings/python/sigrok/core/classes.i +++ b/bindings/python/sigrok/core/classes.i @@ -61,6 +61,16 @@ 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 %{ @@ -83,6 +93,8 @@ typedef guint pyg_flags_type; import_array(); %} +%include "../../../swig/templates.i" + /* Map file objects to file descriptors. */ %typecheck(SWIG_TYPECHECK_POINTER) int fd { $1 = (PyObject_AsFileDescriptor($input) != -1); @@ -115,7 +127,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); @@ -296,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; } @@ -317,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)) @@ -339,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)) @@ -364,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; } @@ -382,7 +394,7 @@ std::map dict_to_map_options(PyObject *dict, %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; @@ -414,6 +426,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): @@ -441,9 +463,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; } @@ -525,3 +547,5 @@ std::map dict_to_map_options(PyObject *dict, data = property(_data) } } + +%include "doc_end.i"