]> sigrok.org Git - libsigrokdecode.git/blame - util.c
decoder: Accept more forms of "unconditional wait()" (None, no args)
[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
d480174d
UH
84/**
85 * Get the value of a Python object's attribute, returned as a newly
86 * allocated GSList of char *.
87 *
88 * @param[in] py_obj The object to probe.
89 * @param[in] attr Name of the attribute to retrieve.
90 * @param[out] outstrlist ptr to GSList of char * storage to be filled in.
91 *
92 * @return SRD_OK upon success, a (negative) error code otherwise.
93 * The 'outstrlist' argument points to a GSList of g_malloc()ed strings
94 * upon success.
95 *
96 * @private
97 */
98SRD_PRIV int py_attr_as_strlist(PyObject *py_obj, const char *attr, GSList **outstrlist)
99{
100 PyObject *py_list;
101 Py_ssize_t i;
102 int ret;
103 char *outstr;
104
105 if (!PyObject_HasAttrString(py_obj, attr)) {
106 srd_dbg("Object has no attribute '%s'.", attr);
107 return SRD_ERR_PYTHON;
108 }
109
110 if (!(py_list = PyObject_GetAttrString(py_obj, attr))) {
111 srd_exception_catch("Failed to get attribute '%s'", attr);
112 return SRD_ERR_PYTHON;
113 }
114
115 if (!PyList_Check(py_list)) {
116 srd_dbg("Object is not a list.");
117 return SRD_ERR_PYTHON;
118 }
119
120 *outstrlist = NULL;
121
122 for (i = 0; i < PyList_Size(py_list); i++) {
123 ret = py_listitem_as_str(py_list, i, &outstr);
124 if (ret < 0) {
125 srd_dbg("Couldn't get item %" PY_FORMAT_SIZE_T "d.", i);
126 return SRD_ERR_PYTHON;
127 }
128 *outstrlist = g_slist_append(*outstrlist, outstr);
129 }
130
131 Py_DECREF(py_list);
132
133 return SRD_OK;
134}
135
d42fc6ee 136/**
511e2123 137 * Get the value of a Python dictionary item, returned as a newly
d42fc6ee
BV
138 * allocated char *.
139 *
201a85a8
DE
140 * @param[in] py_obj The dictionary to probe.
141 * @param[in] key Key of the item to retrieve.
142 * @param[out] outstr Pointer to char * storage to be filled in.
d42fc6ee
BV
143 *
144 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 145 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
146 *
147 * @private
d42fc6ee 148 */
201a85a8 149SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
55c3c5f4 150 char **outstr)
d42fc6ee
BV
151{
152 PyObject *py_value;
d42fc6ee 153
201a85a8
DE
154 if (!PyDict_Check(py_obj)) {
155 srd_dbg("Object is not a dictionary.");
d42fc6ee
BV
156 return SRD_ERR_PYTHON;
157 }
158
201a85a8 159 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
7a1712c4 160 srd_dbg("Dictionary has no attribute '%s'.", key);
d42fc6ee
BV
161 return SRD_ERR_PYTHON;
162 }
163
201a85a8 164 return py_str_as_str(py_value, outstr);
d42fc6ee
BV
165}
166
d480174d
UH
167/**
168 * Get the value of a Python list item, returned as a newly
169 * allocated char *.
170 *
171 * @param[in] py_obj The list to probe.
172 * @param[in] idx Index of the list item to retrieve.
173 * @param[out] outstr Pointer to char * storage to be filled in.
174 *
175 * @return SRD_OK upon success, a (negative) error code otherwise.
176 * The 'outstr' argument points to a g_malloc()ed string upon success.
177 *
178 * @private
179 */
180SRD_PRIV int py_listitem_as_str(PyObject *py_obj, Py_ssize_t idx,
181 char **outstr)
182{
183 PyObject *py_value;
184
185 if (!PyList_Check(py_obj)) {
186 srd_dbg("Object is not a list.");
187 return SRD_ERR_PYTHON;
188 }
189
190 if (!(py_value = PyList_GetItem(py_obj, idx))) {
191 srd_dbg("Couldn't get list item %" PY_FORMAT_SIZE_T "d.", idx);
192 return SRD_ERR_PYTHON;
193 }
194
195 return py_str_as_str(py_value, outstr);
196}
197
21dfd91d
UH
198/**
199 * Get the value of a Python dictionary item, returned as a newly
200 * allocated char *.
201 *
202 * @param py_obj The dictionary to probe.
203 * @param py_key Key of the item to retrieve.
204 * @param outstr Pointer to char * storage to be filled in.
205 *
206 * @return SRD_OK upon success, a (negative) error code otherwise.
207 * The 'outstr' argument points to a malloc()ed string upon success.
208 *
209 * @private
210 */
211SRD_PRIV int py_pydictitem_as_str(PyObject *py_obj, PyObject *py_key,
212 char **outstr)
213{
214 PyObject *py_value;
215
216 if (!py_obj || !py_key || !outstr)
217 return SRD_ERR_ARG;
218
219 if (!PyDict_Check(py_obj)) {
220 srd_dbg("Object is not a dictionary.");
221 return SRD_ERR_PYTHON;
222 }
223
224 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
225 srd_dbg("Dictionary has no such key.");
226 return SRD_ERR_PYTHON;
227 }
228
229 if (!PyUnicode_Check(py_value)) {
230 srd_dbg("Dictionary value should be a string.");
231 return SRD_ERR_PYTHON;
232 }
233
234 return py_str_as_str(py_value, outstr);
235}
236
237/**
238 * Get the value of a Python dictionary item, returned as a newly
239 * allocated char *.
240 *
241 * @param py_obj The dictionary to probe.
242 * @param py_key Key of the item to retrieve.
243 * @param out TODO.
244 *
245 * @return SRD_OK upon success, a (negative) error code otherwise.
246 *
247 * @private
248 */
249SRD_PRIV int py_pydictitem_as_long(PyObject *py_obj, PyObject *py_key, uint64_t *out)
250{
251 PyObject *py_value;
252
253 if (!py_obj || !py_key || !out)
254 return SRD_ERR_ARG;
255
256 if (!PyDict_Check(py_obj)) {
257 srd_dbg("Object is not a dictionary.");
258 return SRD_ERR_PYTHON;
259 }
260
261 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
262 srd_dbg("Dictionary has no such key.");
263 return SRD_ERR_PYTHON;
264 }
265
266 if (!PyLong_Check(py_value)) {
267 srd_dbg("Dictionary value should be a long.");
268 return SRD_ERR_PYTHON;
269 }
270
271 *out = PyLong_AsUnsignedLongLong(py_value);
272
273 return SRD_OK;
274}
275
451680f1 276/**
511e2123 277 * Get the value of a Python unicode string object, returned as a newly
451680f1
BV
278 * allocated char *.
279 *
201a85a8
DE
280 * @param[in] py_str The unicode string object.
281 * @param[out] outstr ptr to char * storage to be filled in.
451680f1
BV
282 *
283 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 284 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
285 *
286 * @private
451680f1 287 */
201a85a8 288SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
451680f1 289{
201a85a8 290 PyObject *py_bytes;
451680f1
BV
291 char *str;
292
201a85a8
DE
293 if (!PyUnicode_Check(py_str)) {
294 srd_dbg("Object is not a string object.");
295 return SRD_ERR_PYTHON;
b2c19614
BV
296 }
297
201a85a8
DE
298 py_bytes = PyUnicode_AsUTF8String(py_str);
299 if (py_bytes) {
300 str = g_strdup(PyBytes_AsString(py_bytes));
301 Py_DECREF(py_bytes);
302 if (str) {
303 *outstr = str;
304 return SRD_OK;
305 }
451680f1 306 }
201a85a8 307 srd_exception_catch("Failed to extract string");
b2c19614 308
201a85a8 309 return SRD_ERR_PYTHON;
b2c19614
BV
310}
311
15969949 312/**
201a85a8
DE
313 * Convert a Python list of unicode strings to a C string vector.
314 * On success, a pointer to a newly allocated NULL-terminated array of
315 * allocated C strings is written to @a out_strv. The caller must g_free()
316 * each string and the array itself.
451680f1 317 *
201a85a8
DE
318 * @param[in] py_strseq The sequence object.
319 * @param[out] out_strv Address of string vector to be filled in.
451680f1
BV
320 *
321 * @return SRD_OK upon success, a (negative) error code otherwise.
57790bc8
UH
322 *
323 * @private
15969949 324 */
201a85a8 325SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
15969949 326{
201a85a8
DE
327 PyObject *py_item, *py_bytes;
328 char **strv, *str;
329 ssize_t seq_len, i;
330
331 if (!PySequence_Check(py_strseq)) {
332 srd_err("Object does not provide sequence protocol.");
333 return SRD_ERR_PYTHON;
334 }
335
336 seq_len = PySequence_Size(py_strseq);
337 if (seq_len < 0) {
338 srd_exception_catch("Failed to obtain sequence size");
339 return SRD_ERR_PYTHON;
340 }
15969949 341
201a85a8
DE
342 strv = g_try_new0(char *, seq_len + 1);
343 if (!strv) {
344 srd_err("Failed to allocate result string vector.");
15969949 345 return SRD_ERR_MALLOC;
a61ece20 346 }
201a85a8
DE
347
348 for (i = 0; i < seq_len; i++) {
349 py_item = PySequence_GetItem(py_strseq, i);
350 if (!py_item)
351 goto err_out;
352
353 if (!PyUnicode_Check(py_item)) {
354 Py_DECREF(py_item);
355 goto err_out;
356 }
357 py_bytes = PyUnicode_AsUTF8String(py_item);
358 Py_DECREF(py_item);
359 if (!py_bytes)
360 goto err_out;
361
362 str = g_strdup(PyBytes_AsString(py_bytes));
363 Py_DECREF(py_bytes);
364 if (!str)
365 goto err_out;
366
367 strv[i] = str;
15969949 368 }
201a85a8 369 *out_strv = strv;
15969949
BV
370
371 return SRD_OK;
201a85a8
DE
372
373err_out:
374 g_strfreev(strv);
375 srd_exception_catch("Failed to obtain string item");
376
377 return SRD_ERR_PYTHON;
15969949 378}
6d67d057
DE
379
380/**
381 * Convert a Python scalar object to a GLib variant.
382 * Supported variant types are string, int64 and double.
383 *
384 * @param[in] py_obj The Python object. Must not be NULL.
385 * @return A floating reference to a new variant, or NULL on failure.
e9dd2fea
DE
386 *
387 * @private
6d67d057
DE
388 */
389SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
390{
391 GVariant *var = NULL;
392
393 if (PyUnicode_Check(py_obj)) { /* string */
394 PyObject *py_bytes;
395 const char *str;
396
397 py_bytes = PyUnicode_AsUTF8String(py_obj);
398 if (py_bytes) {
399 str = PyBytes_AsString(py_bytes);
400 if (str)
401 var = g_variant_new_string(str);
402 Py_DECREF(py_bytes);
403 }
404 if (!var)
405 srd_exception_catch("Failed to extract string value");
406
407 } else if (PyLong_Check(py_obj)) { /* integer */
408 int64_t val;
409
410 val = PyLong_AsLongLong(py_obj);
411 if (!PyErr_Occurred())
412 var = g_variant_new_int64(val);
413 else
414 srd_exception_catch("Failed to extract integer value");
415
416 } else if (PyFloat_Check(py_obj)) { /* float */
417 double val;
418
419 val = PyFloat_AsDouble(py_obj);
420 if (!PyErr_Occurred())
421 var = g_variant_new_double(val);
422 else
423 srd_exception_catch("Failed to extract float value");
424
425 } else {
426 srd_err("Failed to extract value of unsupported type.");
427 }
428
429 return var;
430}