2 * This file is part of the sigrok 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/>.
21 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "sigrokdecode-internal.h"
26 * Get the value of a Python object's attribute, returned as a newly
29 * @param py_obj The object to probe.
30 * @param attr Name of the attribute to retrieve.
31 * @param outstr ptr to char * storage to be filled in.
33 * @return SRD_OK upon success, a (negative) error code otherwise.
34 * The 'outstr' argument points to a malloc()ed string upon success.
36 SRD_PRIV int py_attr_as_str(const PyObject *py_obj, const char *attr,
42 if (!PyObject_HasAttrString((PyObject *)py_obj, attr)) {
43 srd_dbg("%s object has no attribute '%s'.",
44 Py_TYPE(py_obj)->tp_name, attr);
45 return SRD_ERR_PYTHON;
48 if (!(py_str = PyObject_GetAttrString((PyObject *)py_obj, attr))) {
49 srd_exception_catch("");
50 return SRD_ERR_PYTHON;
53 if (!PyUnicode_Check(py_str)) {
54 srd_dbg("%s attribute should be a string, but is a %s.",
55 attr, Py_TYPE(py_str)->tp_name);
57 return SRD_ERR_PYTHON;
60 ret = py_str_as_str(py_str, outstr);
67 * Get the value of a Python dictionary item, returned as a newly
70 * @param py_obj The dictionary to probe.
71 * @param attr Key of the item to retrieve.
72 * @param outstr ptr to char * storage to be filled in.
74 * @return SRD_OK upon success, a (negative) error code otherwise.
75 * The 'outstr' argument points to a malloc()ed string upon success.
77 SRD_PRIV int py_dictitem_as_str(const PyObject *py_obj, const char *key,
83 if (!PyDict_Check((PyObject *)py_obj)) {
84 srd_dbg("Object is a %s, not a dictionary.",
85 Py_TYPE((PyObject *)py_obj)->tp_name);
86 return SRD_ERR_PYTHON;
89 if (!(py_value = PyDict_GetItemString((PyObject *)py_obj, key))) {
90 srd_dbg("Dictionary has no attribute '%s'.", key);
91 return SRD_ERR_PYTHON;
94 if (!PyUnicode_Check(py_value)) {
95 srd_dbg("Dictionary value for %s should be a string, but is "
96 "a %s.", key, Py_TYPE(py_value)->tp_name);
97 return SRD_ERR_PYTHON;
100 ret = py_str_as_str(py_value, outstr);
106 * Get the value of a Python unicode string object, returned as a newly
109 * @param py_str The unicode string object.
110 * @param outstr ptr to char * storage to be filled in.
112 * @return SRD_OK upon success, a (negative) error code otherwise.
113 * The 'outstr' argument points to a malloc()ed string upon success.
115 SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr)
125 if (!PyUnicode_Check((PyObject *)py_str)) {
126 srd_dbg("Object is a %s, not a string object.",
127 Py_TYPE((PyObject *)py_str)->tp_name);
128 ret = SRD_ERR_PYTHON;
132 if (!(py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str,
134 ret = SRD_ERR_PYTHON;
137 if (!(str = PyBytes_AS_STRING(py_encstr))) {
138 ret = SRD_ERR_PYTHON;
142 if (!(*outstr = g_strdup(str))) {
143 srd_dbg("Failed to g_malloc() outstr.");
144 ret = SRD_ERR_MALLOC;
150 Py_XDECREF(py_encstr);
152 if (PyErr_Occurred()) {
153 srd_exception_catch("string conversion failed");
160 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
161 * char * array. The caller must g_free() each string when finished.
163 * @param py_strlist The list object.
164 * @param outstr ptr to char ** storage to be filled in.
166 * @return SRD_OK upon success, a (negative) error code otherwise.
167 * The 'outstr' argument points to a g_malloc()ed char** upon success.
169 SRD_PRIV int py_strlist_to_char(const PyObject *py_strlist, char ***outstr)
175 list_len = PyList_Size((PyObject *)py_strlist);
176 if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
177 srd_err("Failed to g_malloc() 'out'.");
178 return SRD_ERR_MALLOC;
180 for (i = 0; i < list_len; i++) {
181 if (!(py_str = PyUnicode_AsEncodedString(
182 PyList_GetItem((PyObject *)py_strlist, i), "utf-8", NULL)))
183 return SRD_ERR_PYTHON;
184 if (!(str = PyBytes_AS_STRING(py_str)))
185 return SRD_ERR_PYTHON;
186 out[i] = g_strdup(str);