]> sigrok.org Git - libsigrokdecode.git/blame - util.c
microwire/eeprom93cxx: Use 'es' instead of 'se' abbrevation.
[libsigrokdecode.git] / util.c
CommitLineData
b2c19614 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
b2c19614
BV
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
4fadb128 5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
b2c19614
BV
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
36784362 21#include <config.h>
f6c7eade 22#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
b2c19614 23
e9dd2fea
DE
24/**
25 * Import a Python module by name.
26 *
27 * This function is implemented in terms of PyImport_Import() rather than
28 * PyImport_ImportModule(), so that the import hooks are not bypassed.
29 *
30 * @param[in] name The name of the module to load as UTF-8 string.
31 * @return The Python module object, or NULL if an exception occurred. The
32 * caller is responsible for evaluating and clearing the Python error state.
33 *
34 * @private
35 */
36SRD_PRIV PyObject *py_import_by_name(const char *name)
37{
38 PyObject *py_mod, *py_modname;
39
40 py_modname = PyUnicode_FromString(name);
41 if (!py_modname)
42 return NULL;
43
44 py_mod = PyImport_Import(py_modname);
45 Py_DECREF(py_modname);
46
47 return py_mod;
48}
49
b2c19614 50/**
511e2123 51 * Get the value of a Python object's attribute, returned as a newly
451680f1 52 * allocated char *.
b2c19614 53 *
201a85a8
DE
54 * @param[in] py_obj The object to probe.
55 * @param[in] attr Name of the attribute to retrieve.
56 * @param[out] outstr ptr to char * storage to be filled in.
b2c19614
BV
57 *
58 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 59 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
60 *
61 * @private
b2c19614 62 */
201a85a8 63SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
b2c19614 64{
451680f1 65 PyObject *py_str;
b2c19614
BV
66 int ret;
67
201a85a8
DE
68 if (!PyObject_HasAttrString(py_obj, attr)) {
69 srd_dbg("Object has no attribute '%s'.", attr);
451680f1
BV
70 return SRD_ERR_PYTHON;
71 }
72
201a85a8
DE
73 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
74 srd_exception_catch("Failed to get attribute '%s'", attr);
d42fc6ee
BV
75 return SRD_ERR_PYTHON;
76 }
77
451680f1 78 ret = py_str_as_str(py_str, outstr);
201a85a8 79 Py_DECREF(py_str);
451680f1
BV
80
81 return ret;
82}
83
d42fc6ee 84/**
511e2123 85 * Get the value of a Python dictionary item, returned as a newly
d42fc6ee
BV
86 * allocated char *.
87 *
201a85a8
DE
88 * @param[in] py_obj The dictionary to probe.
89 * @param[in] key Key of the item to retrieve.
90 * @param[out] outstr Pointer to char * storage to be filled in.
d42fc6ee
BV
91 *
92 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 93 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
94 *
95 * @private
d42fc6ee 96 */
201a85a8 97SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
55c3c5f4 98 char **outstr)
d42fc6ee
BV
99{
100 PyObject *py_value;
d42fc6ee 101
201a85a8
DE
102 if (!PyDict_Check(py_obj)) {
103 srd_dbg("Object is not a dictionary.");
d42fc6ee
BV
104 return SRD_ERR_PYTHON;
105 }
106
201a85a8 107 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
7a1712c4 108 srd_dbg("Dictionary has no attribute '%s'.", key);
d42fc6ee
BV
109 return SRD_ERR_PYTHON;
110 }
111
201a85a8 112 return py_str_as_str(py_value, outstr);
d42fc6ee
BV
113}
114
21dfd91d
UH
115/**
116 * Get the value of a Python dictionary item, returned as a newly
117 * allocated char *.
118 *
119 * @param py_obj The dictionary to probe.
120 * @param py_key Key of the item to retrieve.
121 * @param outstr Pointer to char * storage to be filled in.
122 *
123 * @return SRD_OK upon success, a (negative) error code otherwise.
124 * The 'outstr' argument points to a malloc()ed string upon success.
125 *
126 * @private
127 */
128SRD_PRIV int py_pydictitem_as_str(PyObject *py_obj, PyObject *py_key,
129 char **outstr)
130{
131 PyObject *py_value;
132
133 if (!py_obj || !py_key || !outstr)
134 return SRD_ERR_ARG;
135
136 if (!PyDict_Check(py_obj)) {
137 srd_dbg("Object is not a dictionary.");
138 return SRD_ERR_PYTHON;
139 }
140
141 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
142 srd_dbg("Dictionary has no such key.");
143 return SRD_ERR_PYTHON;
144 }
145
146 if (!PyUnicode_Check(py_value)) {
147 srd_dbg("Dictionary value should be a string.");
148 return SRD_ERR_PYTHON;
149 }
150
151 return py_str_as_str(py_value, outstr);
152}
153
154/**
155 * Get the value of a Python dictionary item, returned as a newly
156 * allocated char *.
157 *
158 * @param py_obj The dictionary to probe.
159 * @param py_key Key of the item to retrieve.
160 * @param out TODO.
161 *
162 * @return SRD_OK upon success, a (negative) error code otherwise.
163 *
164 * @private
165 */
166SRD_PRIV int py_pydictitem_as_long(PyObject *py_obj, PyObject *py_key, uint64_t *out)
167{
168 PyObject *py_value;
169
170 if (!py_obj || !py_key || !out)
171 return SRD_ERR_ARG;
172
173 if (!PyDict_Check(py_obj)) {
174 srd_dbg("Object is not a dictionary.");
175 return SRD_ERR_PYTHON;
176 }
177
178 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
179 srd_dbg("Dictionary has no such key.");
180 return SRD_ERR_PYTHON;
181 }
182
183 if (!PyLong_Check(py_value)) {
184 srd_dbg("Dictionary value should be a long.");
185 return SRD_ERR_PYTHON;
186 }
187
188 *out = PyLong_AsUnsignedLongLong(py_value);
189
190 return SRD_OK;
191}
192
451680f1 193/**
511e2123 194 * Get the value of a Python unicode string object, returned as a newly
451680f1
BV
195 * allocated char *.
196 *
201a85a8
DE
197 * @param[in] py_str The unicode string object.
198 * @param[out] outstr ptr to char * storage to be filled in.
451680f1
BV
199 *
200 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 201 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
202 *
203 * @private
451680f1 204 */
201a85a8 205SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
451680f1 206{
201a85a8 207 PyObject *py_bytes;
451680f1
BV
208 char *str;
209
201a85a8
DE
210 if (!PyUnicode_Check(py_str)) {
211 srd_dbg("Object is not a string object.");
212 return SRD_ERR_PYTHON;
b2c19614
BV
213 }
214
201a85a8
DE
215 py_bytes = PyUnicode_AsUTF8String(py_str);
216 if (py_bytes) {
217 str = g_strdup(PyBytes_AsString(py_bytes));
218 Py_DECREF(py_bytes);
219 if (str) {
220 *outstr = str;
221 return SRD_OK;
222 }
451680f1 223 }
201a85a8 224 srd_exception_catch("Failed to extract string");
b2c19614 225
201a85a8 226 return SRD_ERR_PYTHON;
b2c19614
BV
227}
228
15969949 229/**
201a85a8
DE
230 * Convert a Python list of unicode strings to a C string vector.
231 * On success, a pointer to a newly allocated NULL-terminated array of
232 * allocated C strings is written to @a out_strv. The caller must g_free()
233 * each string and the array itself.
451680f1 234 *
201a85a8
DE
235 * @param[in] py_strseq The sequence object.
236 * @param[out] out_strv Address of string vector to be filled in.
451680f1
BV
237 *
238 * @return SRD_OK upon success, a (negative) error code otherwise.
57790bc8
UH
239 *
240 * @private
15969949 241 */
201a85a8 242SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
15969949 243{
201a85a8
DE
244 PyObject *py_item, *py_bytes;
245 char **strv, *str;
246 ssize_t seq_len, i;
247
248 if (!PySequence_Check(py_strseq)) {
249 srd_err("Object does not provide sequence protocol.");
250 return SRD_ERR_PYTHON;
251 }
252
253 seq_len = PySequence_Size(py_strseq);
254 if (seq_len < 0) {
255 srd_exception_catch("Failed to obtain sequence size");
256 return SRD_ERR_PYTHON;
257 }
15969949 258
201a85a8
DE
259 strv = g_try_new0(char *, seq_len + 1);
260 if (!strv) {
261 srd_err("Failed to allocate result string vector.");
15969949 262 return SRD_ERR_MALLOC;
a61ece20 263 }
201a85a8
DE
264
265 for (i = 0; i < seq_len; i++) {
266 py_item = PySequence_GetItem(py_strseq, i);
267 if (!py_item)
268 goto err_out;
269
270 if (!PyUnicode_Check(py_item)) {
271 Py_DECREF(py_item);
272 goto err_out;
273 }
274 py_bytes = PyUnicode_AsUTF8String(py_item);
275 Py_DECREF(py_item);
276 if (!py_bytes)
277 goto err_out;
278
279 str = g_strdup(PyBytes_AsString(py_bytes));
280 Py_DECREF(py_bytes);
281 if (!str)
282 goto err_out;
283
284 strv[i] = str;
15969949 285 }
201a85a8 286 *out_strv = strv;
15969949
BV
287
288 return SRD_OK;
201a85a8
DE
289
290err_out:
291 g_strfreev(strv);
292 srd_exception_catch("Failed to obtain string item");
293
294 return SRD_ERR_PYTHON;
15969949 295}
6d67d057
DE
296
297/**
298 * Convert a Python scalar object to a GLib variant.
299 * Supported variant types are string, int64 and double.
300 *
301 * @param[in] py_obj The Python object. Must not be NULL.
302 * @return A floating reference to a new variant, or NULL on failure.
e9dd2fea
DE
303 *
304 * @private
6d67d057
DE
305 */
306SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
307{
308 GVariant *var = NULL;
309
310 if (PyUnicode_Check(py_obj)) { /* string */
311 PyObject *py_bytes;
312 const char *str;
313
314 py_bytes = PyUnicode_AsUTF8String(py_obj);
315 if (py_bytes) {
316 str = PyBytes_AsString(py_bytes);
317 if (str)
318 var = g_variant_new_string(str);
319 Py_DECREF(py_bytes);
320 }
321 if (!var)
322 srd_exception_catch("Failed to extract string value");
323
324 } else if (PyLong_Check(py_obj)) { /* integer */
325 int64_t val;
326
327 val = PyLong_AsLongLong(py_obj);
328 if (!PyErr_Occurred())
329 var = g_variant_new_int64(val);
330 else
331 srd_exception_catch("Failed to extract integer value");
332
333 } else if (PyFloat_Check(py_obj)) { /* float */
334 double val;
335
336 val = PyFloat_AsDouble(py_obj);
337 if (!PyErr_Occurred())
338 var = g_variant_new_double(val);
339 else
340 srd_exception_catch("Failed to extract float value");
341
342 } else {
343 srd_err("Failed to extract value of unsupported type.");
344 }
345
346 return var;
347}