summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorDaniel Elstner <daniel.kitta@gmail.com>2015-10-06 21:07:47 +0200
committerDaniel Elstner <daniel.kitta@gmail.com>2015-10-06 23:25:40 +0200
commit6d67d057d13318deb0a11de2589b5442df389092 (patch)
tree56fb0036a359503adad67104941ab78af15e3472 /util.c
parent201a85a8ea071d37f4fda2668c0a1c488d852f4e (diff)
downloadlibsigrokdecode-6d67d057d13318deb0a11de2589b5442df389092.tar.gz
libsigrokdecode-6d67d057d13318deb0a11de2589b5442df389092.zip
decoder: Refactor loading code and plug leaks
Diffstat (limited to 'util.c')
-rw-r--r--util.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/util.c b/util.c
index fcadf47..6f80ff9 100644
--- a/util.c
+++ b/util.c
@@ -189,3 +189,53 @@ err_out:
return SRD_ERR_PYTHON;
}
+
+/**
+ * Convert a Python scalar object to a GLib variant.
+ * Supported variant types are string, int64 and double.
+ *
+ * @param[in] py_obj The Python object. Must not be NULL.
+ * @return A floating reference to a new variant, or NULL on failure.
+ */
+SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
+{
+ GVariant *var = NULL;
+
+ if (PyUnicode_Check(py_obj)) { /* string */
+ PyObject *py_bytes;
+ const char *str;
+
+ py_bytes = PyUnicode_AsUTF8String(py_obj);
+ if (py_bytes) {
+ str = PyBytes_AsString(py_bytes);
+ if (str)
+ var = g_variant_new_string(str);
+ Py_DECREF(py_bytes);
+ }
+ if (!var)
+ srd_exception_catch("Failed to extract string value");
+
+ } else if (PyLong_Check(py_obj)) { /* integer */
+ int64_t val;
+
+ val = PyLong_AsLongLong(py_obj);
+ if (!PyErr_Occurred())
+ var = g_variant_new_int64(val);
+ else
+ srd_exception_catch("Failed to extract integer value");
+
+ } else if (PyFloat_Check(py_obj)) { /* float */
+ double val;
+
+ val = PyFloat_AsDouble(py_obj);
+ if (!PyErr_Occurred())
+ var = g_variant_new_double(val);
+ else
+ srd_exception_catch("Failed to extract float value");
+
+ } else {
+ srd_err("Failed to extract value of unsupported type.");
+ }
+
+ return var;
+}