]> sigrok.org Git - libsigrokdecode.git/blame - util.c
mrf24j40: rename reset() helper method
[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;
514b2edc
UH
39 PyGILState_STATE gstate;
40
41 gstate = PyGILState_Ensure();
e9dd2fea
DE
42
43 py_modname = PyUnicode_FromString(name);
514b2edc
UH
44 if (!py_modname) {
45 PyGILState_Release(gstate);
e9dd2fea 46 return NULL;
514b2edc 47 }
e9dd2fea
DE
48
49 py_mod = PyImport_Import(py_modname);
50 Py_DECREF(py_modname);
51
514b2edc
UH
52 PyGILState_Release(gstate);
53
e9dd2fea
DE
54 return py_mod;
55}
56
b2c19614 57/**
511e2123 58 * Get the value of a Python object's attribute, returned as a newly
451680f1 59 * allocated char *.
b2c19614 60 *
201a85a8
DE
61 * @param[in] py_obj The object to probe.
62 * @param[in] attr Name of the attribute to retrieve.
63 * @param[out] outstr ptr to char * storage to be filled in.
b2c19614
BV
64 *
65 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 66 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
67 *
68 * @private
b2c19614 69 */
201a85a8 70SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
b2c19614 71{
451680f1 72 PyObject *py_str;
b2c19614 73 int ret;
514b2edc
UH
74 PyGILState_STATE gstate;
75
76 gstate = PyGILState_Ensure();
b2c19614 77
201a85a8
DE
78 if (!PyObject_HasAttrString(py_obj, attr)) {
79 srd_dbg("Object has no attribute '%s'.", attr);
514b2edc 80 goto err;
451680f1
BV
81 }
82
201a85a8
DE
83 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
84 srd_exception_catch("Failed to get attribute '%s'", attr);
514b2edc 85 goto err;
d42fc6ee
BV
86 }
87
451680f1 88 ret = py_str_as_str(py_str, outstr);
201a85a8 89 Py_DECREF(py_str);
451680f1 90
514b2edc
UH
91 PyGILState_Release(gstate);
92
451680f1 93 return ret;
514b2edc
UH
94
95err:
96 PyGILState_Release(gstate);
97
98 return SRD_ERR_PYTHON;
451680f1
BV
99}
100
d480174d
UH
101/**
102 * Get the value of a Python object's attribute, returned as a newly
103 * allocated GSList of char *.
104 *
105 * @param[in] py_obj The object to probe.
106 * @param[in] attr Name of the attribute to retrieve.
107 * @param[out] outstrlist ptr to GSList of char * storage to be filled in.
108 *
109 * @return SRD_OK upon success, a (negative) error code otherwise.
110 * The 'outstrlist' argument points to a GSList of g_malloc()ed strings
111 * upon success.
112 *
113 * @private
114 */
115SRD_PRIV int py_attr_as_strlist(PyObject *py_obj, const char *attr, GSList **outstrlist)
116{
117 PyObject *py_list;
118 Py_ssize_t i;
119 int ret;
120 char *outstr;
514b2edc
UH
121 PyGILState_STATE gstate;
122
123 gstate = PyGILState_Ensure();
d480174d
UH
124
125 if (!PyObject_HasAttrString(py_obj, attr)) {
126 srd_dbg("Object has no attribute '%s'.", attr);
514b2edc 127 goto err;
d480174d
UH
128 }
129
130 if (!(py_list = PyObject_GetAttrString(py_obj, attr))) {
131 srd_exception_catch("Failed to get attribute '%s'", attr);
514b2edc 132 goto err;
d480174d
UH
133 }
134
135 if (!PyList_Check(py_list)) {
136 srd_dbg("Object is not a list.");
514b2edc 137 goto err;
d480174d
UH
138 }
139
140 *outstrlist = NULL;
141
142 for (i = 0; i < PyList_Size(py_list); i++) {
143 ret = py_listitem_as_str(py_list, i, &outstr);
144 if (ret < 0) {
145 srd_dbg("Couldn't get item %" PY_FORMAT_SIZE_T "d.", i);
514b2edc 146 goto err;
d480174d
UH
147 }
148 *outstrlist = g_slist_append(*outstrlist, outstr);
149 }
150
151 Py_DECREF(py_list);
152
514b2edc
UH
153 PyGILState_Release(gstate);
154
d480174d 155 return SRD_OK;
514b2edc
UH
156
157err:
158 PyGILState_Release(gstate);
159
160 return SRD_ERR_PYTHON;
d480174d
UH
161}
162
d42fc6ee 163/**
511e2123 164 * Get the value of a Python dictionary item, returned as a newly
d42fc6ee
BV
165 * allocated char *.
166 *
201a85a8
DE
167 * @param[in] py_obj The dictionary to probe.
168 * @param[in] key Key of the item to retrieve.
169 * @param[out] outstr Pointer to char * storage to be filled in.
d42fc6ee
BV
170 *
171 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 172 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
173 *
174 * @private
d42fc6ee 175 */
201a85a8 176SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
55c3c5f4 177 char **outstr)
d42fc6ee
BV
178{
179 PyObject *py_value;
514b2edc
UH
180 PyGILState_STATE gstate;
181
182 gstate = PyGILState_Ensure();
d42fc6ee 183
201a85a8
DE
184 if (!PyDict_Check(py_obj)) {
185 srd_dbg("Object is not a dictionary.");
514b2edc 186 goto err;
d42fc6ee
BV
187 }
188
201a85a8 189 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
7a1712c4 190 srd_dbg("Dictionary has no attribute '%s'.", key);
514b2edc 191 goto err;
d42fc6ee
BV
192 }
193
514b2edc
UH
194 PyGILState_Release(gstate);
195
201a85a8 196 return py_str_as_str(py_value, outstr);
514b2edc
UH
197
198err:
199 PyGILState_Release(gstate);
200
201 return SRD_ERR_PYTHON;
d42fc6ee
BV
202}
203
d480174d
UH
204/**
205 * Get the value of a Python list item, returned as a newly
206 * allocated char *.
207 *
208 * @param[in] py_obj The list to probe.
209 * @param[in] idx Index of the list item to retrieve.
210 * @param[out] outstr Pointer to char * storage to be filled in.
211 *
212 * @return SRD_OK upon success, a (negative) error code otherwise.
213 * The 'outstr' argument points to a g_malloc()ed string upon success.
214 *
215 * @private
216 */
217SRD_PRIV int py_listitem_as_str(PyObject *py_obj, Py_ssize_t idx,
218 char **outstr)
219{
220 PyObject *py_value;
514b2edc
UH
221 PyGILState_STATE gstate;
222
223 gstate = PyGILState_Ensure();
d480174d
UH
224
225 if (!PyList_Check(py_obj)) {
226 srd_dbg("Object is not a list.");
514b2edc 227 goto err;
d480174d
UH
228 }
229
230 if (!(py_value = PyList_GetItem(py_obj, idx))) {
231 srd_dbg("Couldn't get list item %" PY_FORMAT_SIZE_T "d.", idx);
514b2edc 232 goto err;
d480174d
UH
233 }
234
514b2edc
UH
235 PyGILState_Release(gstate);
236
d480174d 237 return py_str_as_str(py_value, outstr);
514b2edc
UH
238
239err:
240 PyGILState_Release(gstate);
241
242 return SRD_ERR_PYTHON;
d480174d
UH
243}
244
21dfd91d
UH
245/**
246 * Get the value of a Python dictionary item, returned as a newly
247 * allocated char *.
248 *
249 * @param py_obj The dictionary to probe.
250 * @param py_key Key of the item to retrieve.
251 * @param outstr Pointer to char * storage to be filled in.
252 *
253 * @return SRD_OK upon success, a (negative) error code otherwise.
254 * The 'outstr' argument points to a malloc()ed string upon success.
255 *
256 * @private
257 */
258SRD_PRIV int py_pydictitem_as_str(PyObject *py_obj, PyObject *py_key,
259 char **outstr)
260{
261 PyObject *py_value;
514b2edc 262 PyGILState_STATE gstate;
21dfd91d
UH
263
264 if (!py_obj || !py_key || !outstr)
265 return SRD_ERR_ARG;
266
514b2edc
UH
267 gstate = PyGILState_Ensure();
268
21dfd91d
UH
269 if (!PyDict_Check(py_obj)) {
270 srd_dbg("Object is not a dictionary.");
514b2edc 271 goto err;
21dfd91d
UH
272 }
273
274 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
275 srd_dbg("Dictionary has no such key.");
514b2edc 276 goto err;
21dfd91d
UH
277 }
278
279 if (!PyUnicode_Check(py_value)) {
280 srd_dbg("Dictionary value should be a string.");
514b2edc 281 goto err;
21dfd91d
UH
282 }
283
284 return py_str_as_str(py_value, outstr);
514b2edc
UH
285
286err:
287 PyGILState_Release(gstate);
288
289 return SRD_ERR_PYTHON;
21dfd91d
UH
290}
291
292/**
293 * Get the value of a Python dictionary item, returned as a newly
294 * allocated char *.
295 *
296 * @param py_obj The dictionary to probe.
297 * @param py_key Key of the item to retrieve.
298 * @param out TODO.
299 *
300 * @return SRD_OK upon success, a (negative) error code otherwise.
301 *
302 * @private
303 */
304SRD_PRIV int py_pydictitem_as_long(PyObject *py_obj, PyObject *py_key, uint64_t *out)
305{
306 PyObject *py_value;
514b2edc 307 PyGILState_STATE gstate;
21dfd91d
UH
308
309 if (!py_obj || !py_key || !out)
310 return SRD_ERR_ARG;
311
514b2edc
UH
312 gstate = PyGILState_Ensure();
313
21dfd91d
UH
314 if (!PyDict_Check(py_obj)) {
315 srd_dbg("Object is not a dictionary.");
514b2edc 316 goto err;
21dfd91d
UH
317 }
318
319 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
320 srd_dbg("Dictionary has no such key.");
514b2edc 321 goto err;
21dfd91d
UH
322 }
323
324 if (!PyLong_Check(py_value)) {
325 srd_dbg("Dictionary value should be a long.");
514b2edc 326 goto err;
21dfd91d
UH
327 }
328
329 *out = PyLong_AsUnsignedLongLong(py_value);
330
514b2edc
UH
331 PyGILState_Release(gstate);
332
21dfd91d 333 return SRD_OK;
514b2edc
UH
334
335err:
336 PyGILState_Release(gstate);
337
338 return SRD_ERR_PYTHON;
21dfd91d
UH
339}
340
451680f1 341/**
511e2123 342 * Get the value of a Python unicode string object, returned as a newly
451680f1
BV
343 * allocated char *.
344 *
201a85a8
DE
345 * @param[in] py_str The unicode string object.
346 * @param[out] outstr ptr to char * storage to be filled in.
451680f1
BV
347 *
348 * @return SRD_OK upon success, a (negative) error code otherwise.
201a85a8 349 * The 'outstr' argument points to a g_malloc()ed string upon success.
57790bc8
UH
350 *
351 * @private
451680f1 352 */
201a85a8 353SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
451680f1 354{
201a85a8 355 PyObject *py_bytes;
451680f1 356 char *str;
514b2edc
UH
357 PyGILState_STATE gstate;
358
359 gstate = PyGILState_Ensure();
451680f1 360
201a85a8
DE
361 if (!PyUnicode_Check(py_str)) {
362 srd_dbg("Object is not a string object.");
514b2edc 363 PyGILState_Release(gstate);
201a85a8 364 return SRD_ERR_PYTHON;
b2c19614
BV
365 }
366
201a85a8
DE
367 py_bytes = PyUnicode_AsUTF8String(py_str);
368 if (py_bytes) {
369 str = g_strdup(PyBytes_AsString(py_bytes));
370 Py_DECREF(py_bytes);
371 if (str) {
372 *outstr = str;
514b2edc 373 PyGILState_Release(gstate);
201a85a8
DE
374 return SRD_OK;
375 }
451680f1 376 }
201a85a8 377 srd_exception_catch("Failed to extract string");
b2c19614 378
514b2edc
UH
379 PyGILState_Release(gstate);
380
201a85a8 381 return SRD_ERR_PYTHON;
b2c19614
BV
382}
383
15969949 384/**
201a85a8
DE
385 * Convert a Python list of unicode strings to a C string vector.
386 * On success, a pointer to a newly allocated NULL-terminated array of
387 * allocated C strings is written to @a out_strv. The caller must g_free()
388 * each string and the array itself.
451680f1 389 *
201a85a8
DE
390 * @param[in] py_strseq The sequence object.
391 * @param[out] out_strv Address of string vector to be filled in.
451680f1
BV
392 *
393 * @return SRD_OK upon success, a (negative) error code otherwise.
57790bc8
UH
394 *
395 * @private
15969949 396 */
201a85a8 397SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
15969949 398{
201a85a8
DE
399 PyObject *py_item, *py_bytes;
400 char **strv, *str;
401 ssize_t seq_len, i;
514b2edc
UH
402 PyGILState_STATE gstate;
403 int ret = SRD_ERR_PYTHON;
404
405 gstate = PyGILState_Ensure();
201a85a8
DE
406
407 if (!PySequence_Check(py_strseq)) {
408 srd_err("Object does not provide sequence protocol.");
514b2edc 409 goto err;
201a85a8
DE
410 }
411
412 seq_len = PySequence_Size(py_strseq);
413 if (seq_len < 0) {
414 srd_exception_catch("Failed to obtain sequence size");
514b2edc 415 goto err;
201a85a8 416 }
15969949 417
201a85a8
DE
418 strv = g_try_new0(char *, seq_len + 1);
419 if (!strv) {
420 srd_err("Failed to allocate result string vector.");
514b2edc
UH
421 ret = SRD_ERR_MALLOC;
422 goto err;
a61ece20 423 }
201a85a8
DE
424
425 for (i = 0; i < seq_len; i++) {
426 py_item = PySequence_GetItem(py_strseq, i);
427 if (!py_item)
428 goto err_out;
429
430 if (!PyUnicode_Check(py_item)) {
431 Py_DECREF(py_item);
432 goto err_out;
433 }
434 py_bytes = PyUnicode_AsUTF8String(py_item);
435 Py_DECREF(py_item);
436 if (!py_bytes)
437 goto err_out;
438
439 str = g_strdup(PyBytes_AsString(py_bytes));
440 Py_DECREF(py_bytes);
441 if (!str)
442 goto err_out;
443
444 strv[i] = str;
15969949 445 }
201a85a8 446 *out_strv = strv;
15969949
BV
447
448 return SRD_OK;
201a85a8
DE
449
450err_out:
451 g_strfreev(strv);
452 srd_exception_catch("Failed to obtain string item");
453
514b2edc
UH
454err:
455 PyGILState_Release(gstate);
456
457 return ret;
15969949 458}
6d67d057
DE
459
460/**
461 * Convert a Python scalar object to a GLib variant.
462 * Supported variant types are string, int64 and double.
463 *
464 * @param[in] py_obj The Python object. Must not be NULL.
465 * @return A floating reference to a new variant, or NULL on failure.
e9dd2fea
DE
466 *
467 * @private
6d67d057
DE
468 */
469SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
470{
471 GVariant *var = NULL;
514b2edc
UH
472 PyGILState_STATE gstate;
473
474 gstate = PyGILState_Ensure();
6d67d057
DE
475
476 if (PyUnicode_Check(py_obj)) { /* string */
477 PyObject *py_bytes;
478 const char *str;
479
480 py_bytes = PyUnicode_AsUTF8String(py_obj);
481 if (py_bytes) {
482 str = PyBytes_AsString(py_bytes);
483 if (str)
484 var = g_variant_new_string(str);
485 Py_DECREF(py_bytes);
486 }
487 if (!var)
488 srd_exception_catch("Failed to extract string value");
489
490 } else if (PyLong_Check(py_obj)) { /* integer */
491 int64_t val;
492
493 val = PyLong_AsLongLong(py_obj);
494 if (!PyErr_Occurred())
495 var = g_variant_new_int64(val);
496 else
497 srd_exception_catch("Failed to extract integer value");
498
499 } else if (PyFloat_Check(py_obj)) { /* float */
500 double val;
501
502 val = PyFloat_AsDouble(py_obj);
503 if (!PyErr_Occurred())
504 var = g_variant_new_double(val);
505 else
506 srd_exception_catch("Failed to extract float value");
507
508 } else {
509 srd_err("Failed to extract value of unsupported type.");
510 }
511
514b2edc
UH
512 PyGILState_Release(gstate);
513
6d67d057
DE
514 return var;
515}