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/>.
21 #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "libsigrokdecode-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.
38 SRD_PRIV
int py_attr_as_str(const PyObject
*py_obj
, const char *attr
,
44 if (!PyObject_HasAttrString((PyObject
*)py_obj
, attr
)) {
45 srd_dbg("%s object has no attribute '%s'.",
46 Py_TYPE(py_obj
)->tp_name
, attr
);
47 return SRD_ERR_PYTHON
;
50 if (!(py_str
= PyObject_GetAttrString((PyObject
*)py_obj
, attr
))) {
51 srd_exception_catch("");
52 return SRD_ERR_PYTHON
;
55 if (!PyUnicode_Check(py_str
)) {
56 srd_dbg("%s attribute should be a string, but is a %s.",
57 attr
, Py_TYPE(py_str
)->tp_name
);
59 return SRD_ERR_PYTHON
;
62 ret
= py_str_as_str(py_str
, outstr
);
69 * Get the value of a Python dictionary item, returned as a newly
72 * @param py_obj The dictionary to probe.
73 * @param key Key of the item to retrieve.
74 * @param outstr Pointer to char * storage to be filled in.
76 * @return SRD_OK upon success, a (negative) error code otherwise.
77 * The 'outstr' argument points to a malloc()ed string upon success.
81 SRD_PRIV
int py_dictitem_as_str(const PyObject
*py_obj
, const char *key
,
87 if (!PyDict_Check((PyObject
*)py_obj
)) {
88 srd_dbg("Object is a %s, not a dictionary.",
89 Py_TYPE((PyObject
*)py_obj
)->tp_name
);
90 return SRD_ERR_PYTHON
;
93 if (!(py_value
= PyDict_GetItemString((PyObject
*)py_obj
, key
))) {
94 srd_dbg("Dictionary has no attribute '%s'.", key
);
95 return SRD_ERR_PYTHON
;
98 if (!PyUnicode_Check(py_value
)) {
99 srd_dbg("Dictionary value for %s should be a string, but is "
100 "a %s.", key
, Py_TYPE(py_value
)->tp_name
);
101 return SRD_ERR_PYTHON
;
104 ret
= py_str_as_str(py_value
, outstr
);
110 * Get the value of a Python unicode string object, returned as a newly
113 * @param py_str The unicode string object.
114 * @param outstr ptr to char * storage to be filled in.
116 * @return SRD_OK upon success, a (negative) error code otherwise.
117 * The 'outstr' argument points to a malloc()ed string upon success.
121 SRD_PRIV
int py_str_as_str(const PyObject
*py_str
, char **outstr
)
131 if (!PyUnicode_Check((PyObject
*)py_str
)) {
132 srd_dbg("Object is a %s, not a string object.",
133 Py_TYPE((PyObject
*)py_str
)->tp_name
);
134 ret
= SRD_ERR_PYTHON
;
138 if (!(py_encstr
= PyUnicode_AsEncodedString((PyObject
*)py_str
,
140 ret
= SRD_ERR_PYTHON
;
143 if (!(str
= PyBytes_AS_STRING(py_encstr
))) {
144 ret
= SRD_ERR_PYTHON
;
148 if (!(*outstr
= g_strdup(str
))) {
149 srd_dbg("Failed to g_malloc() outstr.");
150 ret
= SRD_ERR_MALLOC
;
156 Py_XDECREF(py_encstr
);
158 if (PyErr_Occurred()) {
159 srd_exception_catch("string conversion failed");
166 * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded
167 * char * array. The caller must g_free() each string when finished.
169 * @param py_strlist The list object.
170 * @param outstr ptr to char ** storage to be filled in.
172 * @return SRD_OK upon success, a (negative) error code otherwise.
173 * The 'outstr' argument points to a g_malloc()ed char** upon success.
177 SRD_PRIV
int py_strseq_to_char(const PyObject
*py_strseq
, char ***outstr
)
183 list_len
= PySequence_Size((PyObject
*)py_strseq
);
184 if (!(out
= g_try_malloc(sizeof(char *) * (list_len
+ 1)))) {
185 srd_err("Failed to g_malloc() 'out'.");
186 return SRD_ERR_MALLOC
;
188 for (i
= 0; i
< list_len
; i
++) {
189 if (!(py_str
= PyUnicode_AsEncodedString(
190 PySequence_GetItem((PyObject
*)py_strseq
, i
), "utf-8", NULL
)))
191 return SRD_ERR_PYTHON
;
192 if (!(str
= PyBytes_AS_STRING(py_str
)))
193 return SRD_ERR_PYTHON
;
194 out
[i
] = g_strdup(str
);