2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
25 * Get the value of a Python object's attribute, returned as a newly
28 * @param[in] py_obj The object to probe.
29 * @param[in] attr Name of the attribute to retrieve.
30 * @param[out] outstr ptr to char * storage to be filled in.
32 * @return SRD_OK upon success, a (negative) error code otherwise.
33 * The 'outstr' argument points to a g_malloc()ed string upon success.
37 SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
42 if (!PyObject_HasAttrString(py_obj, attr)) {
43 srd_dbg("Object has no attribute '%s'.", attr);
44 return SRD_ERR_PYTHON;
47 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
48 srd_exception_catch("Failed to get attribute '%s'", attr);
49 return SRD_ERR_PYTHON;
52 ret = py_str_as_str(py_str, outstr);
59 * Get the value of a Python dictionary item, returned as a newly
62 * @param[in] py_obj The dictionary to probe.
63 * @param[in] key Key of the item to retrieve.
64 * @param[out] outstr Pointer to char * storage to be filled in.
66 * @return SRD_OK upon success, a (negative) error code otherwise.
67 * The 'outstr' argument points to a g_malloc()ed string upon success.
71 SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
76 if (!PyDict_Check(py_obj)) {
77 srd_dbg("Object is not a dictionary.");
78 return SRD_ERR_PYTHON;
81 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
82 srd_dbg("Dictionary has no attribute '%s'.", key);
83 return SRD_ERR_PYTHON;
86 return py_str_as_str(py_value, outstr);
90 * Get the value of a Python unicode string object, returned as a newly
93 * @param[in] py_str The unicode string object.
94 * @param[out] outstr ptr to char * storage to be filled in.
96 * @return SRD_OK upon success, a (negative) error code otherwise.
97 * The 'outstr' argument points to a g_malloc()ed string upon success.
101 SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
106 if (!PyUnicode_Check(py_str)) {
107 srd_dbg("Object is not a string object.");
108 return SRD_ERR_PYTHON;
111 py_bytes = PyUnicode_AsUTF8String(py_str);
113 str = g_strdup(PyBytes_AsString(py_bytes));
120 srd_exception_catch("Failed to extract string");
122 return SRD_ERR_PYTHON;
126 * Convert a Python list of unicode strings to a C string vector.
127 * On success, a pointer to a newly allocated NULL-terminated array of
128 * allocated C strings is written to @a out_strv. The caller must g_free()
129 * each string and the array itself.
131 * @param[in] py_strseq The sequence object.
132 * @param[out] out_strv Address of string vector to be filled in.
134 * @return SRD_OK upon success, a (negative) error code otherwise.
138 SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
140 PyObject *py_item, *py_bytes;
144 if (!PySequence_Check(py_strseq)) {
145 srd_err("Object does not provide sequence protocol.");
146 return SRD_ERR_PYTHON;
149 seq_len = PySequence_Size(py_strseq);
151 srd_exception_catch("Failed to obtain sequence size");
152 return SRD_ERR_PYTHON;
155 strv = g_try_new0(char *, seq_len + 1);
157 srd_err("Failed to allocate result string vector.");
158 return SRD_ERR_MALLOC;
161 for (i = 0; i < seq_len; i++) {
162 py_item = PySequence_GetItem(py_strseq, i);
166 if (!PyUnicode_Check(py_item)) {
170 py_bytes = PyUnicode_AsUTF8String(py_item);
175 str = g_strdup(PyBytes_AsString(py_bytes));
188 srd_exception_catch("Failed to obtain string item");
190 return SRD_ERR_PYTHON;
194 * Convert a Python scalar object to a GLib variant.
195 * Supported variant types are string, int64 and double.
197 * @param[in] py_obj The Python object. Must not be NULL.
198 * @return A floating reference to a new variant, or NULL on failure.
200 SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
202 GVariant *var = NULL;
204 if (PyUnicode_Check(py_obj)) { /* string */
208 py_bytes = PyUnicode_AsUTF8String(py_obj);
210 str = PyBytes_AsString(py_bytes);
212 var = g_variant_new_string(str);
216 srd_exception_catch("Failed to extract string value");
218 } else if (PyLong_Check(py_obj)) { /* integer */
221 val = PyLong_AsLongLong(py_obj);
222 if (!PyErr_Occurred())
223 var = g_variant_new_int64(val);
225 srd_exception_catch("Failed to extract integer value");
227 } else if (PyFloat_Check(py_obj)) { /* float */
230 val = PyFloat_AsDouble(py_obj);
231 if (!PyErr_Occurred())
232 var = g_variant_new_double(val);
234 srd_exception_catch("Failed to extract float value");
237 srd_err("Failed to extract value of unsupported type.");