diff options
author | Daniel Elstner <daniel.kitta@gmail.com> | 2015-10-06 21:07:47 +0200 |
---|---|---|
committer | Daniel Elstner <daniel.kitta@gmail.com> | 2015-10-06 23:25:40 +0200 |
commit | 6d67d057d13318deb0a11de2589b5442df389092 (patch) | |
tree | 56fb0036a359503adad67104941ab78af15e3472 /util.c | |
parent | 201a85a8ea071d37f4fda2668c0a1c488d852f4e (diff) | |
download | libsigrokdecode-6d67d057d13318deb0a11de2589b5442df389092.tar.gz libsigrokdecode-6d67d057d13318deb0a11de2589b5442df389092.zip |
decoder: Refactor loading code and plug leaks
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -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; +} |