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/>.
22 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
25 * Import a Python module by name.
27 * This function is implemented in terms of PyImport_Import() rather than
28 * PyImport_ImportModule(), so that the import hooks are not bypassed.
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.
36 SRD_PRIV PyObject *py_import_by_name(const char *name)
38 PyObject *py_mod, *py_modname;
39 PyGILState_STATE gstate;
41 gstate = PyGILState_Ensure();
43 py_modname = PyUnicode_FromString(name);
45 PyGILState_Release(gstate);
49 py_mod = PyImport_Import(py_modname);
50 Py_DECREF(py_modname);
52 PyGILState_Release(gstate);
58 * Get the value of a Python object's attribute, returned as a newly
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.
65 * @return SRD_OK upon success, a (negative) error code otherwise.
66 * The 'outstr' argument points to a g_malloc()ed string upon success.
70 SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr)
74 PyGILState_STATE gstate;
76 gstate = PyGILState_Ensure();
78 if (!PyObject_HasAttrString(py_obj, attr)) {
79 srd_dbg("Object has no attribute '%s'.", attr);
83 if (!(py_str = PyObject_GetAttrString(py_obj, attr))) {
84 srd_exception_catch("Failed to get attribute '%s'", attr);
88 ret = py_str_as_str(py_str, outstr);
91 PyGILState_Release(gstate);
96 PyGILState_Release(gstate);
98 return SRD_ERR_PYTHON;
102 * Get the value of a Python object's attribute, returned as a newly
103 * allocated GSList of char *.
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.
109 * @return SRD_OK upon success, a (negative) error code otherwise.
110 * The 'outstrlist' argument points to a GSList of g_malloc()ed strings
115 SRD_PRIV int py_attr_as_strlist(PyObject *py_obj, const char *attr, GSList **outstrlist)
121 PyGILState_STATE gstate;
123 gstate = PyGILState_Ensure();
125 if (!PyObject_HasAttrString(py_obj, attr)) {
126 srd_dbg("Object has no attribute '%s'.", attr);
130 if (!(py_list = PyObject_GetAttrString(py_obj, attr))) {
131 srd_exception_catch("Failed to get attribute '%s'", attr);
135 if (!PyList_Check(py_list)) {
136 srd_dbg("Object is not a list.");
142 for (i = 0; i < PyList_Size(py_list); i++) {
143 ret = py_listitem_as_str(py_list, i, &outstr);
145 srd_dbg("Couldn't get item %" PY_FORMAT_SIZE_T "d.", i);
148 *outstrlist = g_slist_append(*outstrlist, outstr);
153 PyGILState_Release(gstate);
158 PyGILState_Release(gstate);
160 return SRD_ERR_PYTHON;
164 * Get the value of a Python dictionary item, returned as a newly
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.
171 * @return SRD_OK upon success, a (negative) error code otherwise.
172 * The 'outstr' argument points to a g_malloc()ed string upon success.
176 SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key,
180 PyGILState_STATE gstate;
182 gstate = PyGILState_Ensure();
184 if (!PyDict_Check(py_obj)) {
185 srd_dbg("Object is not a dictionary.");
189 if (!(py_value = PyDict_GetItemString(py_obj, key))) {
190 srd_dbg("Dictionary has no attribute '%s'.", key);
194 PyGILState_Release(gstate);
196 return py_str_as_str(py_value, outstr);
199 PyGILState_Release(gstate);
201 return SRD_ERR_PYTHON;
205 * Get the value of a Python list item, returned as a newly
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.
212 * @return SRD_OK upon success, a (negative) error code otherwise.
213 * The 'outstr' argument points to a g_malloc()ed string upon success.
217 SRD_PRIV int py_listitem_as_str(PyObject *py_obj, Py_ssize_t idx,
221 PyGILState_STATE gstate;
223 gstate = PyGILState_Ensure();
225 if (!PyList_Check(py_obj)) {
226 srd_dbg("Object is not a list.");
230 if (!(py_value = PyList_GetItem(py_obj, idx))) {
231 srd_dbg("Couldn't get list item %" PY_FORMAT_SIZE_T "d.", idx);
235 PyGILState_Release(gstate);
237 return py_str_as_str(py_value, outstr);
240 PyGILState_Release(gstate);
242 return SRD_ERR_PYTHON;
246 * Get the value of a Python dictionary item, returned as a newly
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.
253 * @return SRD_OK upon success, a (negative) error code otherwise.
254 * The 'outstr' argument points to a malloc()ed string upon success.
258 SRD_PRIV int py_pydictitem_as_str(PyObject *py_obj, PyObject *py_key,
262 PyGILState_STATE gstate;
264 if (!py_obj || !py_key || !outstr)
267 gstate = PyGILState_Ensure();
269 if (!PyDict_Check(py_obj)) {
270 srd_dbg("Object is not a dictionary.");
274 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
275 srd_dbg("Dictionary has no such key.");
279 if (!PyUnicode_Check(py_value)) {
280 srd_dbg("Dictionary value should be a string.");
284 PyGILState_Release(gstate);
286 return py_str_as_str(py_value, outstr);
289 PyGILState_Release(gstate);
291 return SRD_ERR_PYTHON;
295 * Get the value of a Python dictionary item, returned as a newly
298 * @param py_obj The dictionary to probe.
299 * @param py_key Key of the item to retrieve.
302 * @return SRD_OK upon success, a (negative) error code otherwise.
306 SRD_PRIV int py_pydictitem_as_long(PyObject *py_obj, PyObject *py_key, uint64_t *out)
309 PyGILState_STATE gstate;
311 if (!py_obj || !py_key || !out)
314 gstate = PyGILState_Ensure();
316 if (!PyDict_Check(py_obj)) {
317 srd_dbg("Object is not a dictionary.");
321 if (!(py_value = PyDict_GetItem(py_obj, py_key))) {
322 srd_dbg("Dictionary has no such key.");
326 if (!PyLong_Check(py_value)) {
327 srd_dbg("Dictionary value should be a long.");
331 *out = PyLong_AsUnsignedLongLong(py_value);
333 PyGILState_Release(gstate);
338 PyGILState_Release(gstate);
340 return SRD_ERR_PYTHON;
344 * Get the value of a Python unicode string object, returned as a newly
347 * @param[in] py_str The unicode string object.
348 * @param[out] outstr ptr to char * storage to be filled in.
350 * @return SRD_OK upon success, a (negative) error code otherwise.
351 * The 'outstr' argument points to a g_malloc()ed string upon success.
355 SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr)
359 PyGILState_STATE gstate;
361 gstate = PyGILState_Ensure();
363 if (!PyUnicode_Check(py_str)) {
364 srd_dbg("Object is not a string object.");
365 PyGILState_Release(gstate);
366 return SRD_ERR_PYTHON;
369 py_bytes = PyUnicode_AsUTF8String(py_str);
371 str = g_strdup(PyBytes_AsString(py_bytes));
375 PyGILState_Release(gstate);
379 srd_exception_catch("Failed to extract string");
381 PyGILState_Release(gstate);
383 return SRD_ERR_PYTHON;
387 * Convert a Python list of unicode strings to a C string vector.
388 * On success, a pointer to a newly allocated NULL-terminated array of
389 * allocated C strings is written to @a out_strv. The caller must g_free()
390 * each string and the array itself.
392 * @param[in] py_strseq The sequence object.
393 * @param[out] out_strv Address of string vector to be filled in.
395 * @return SRD_OK upon success, a (negative) error code otherwise.
399 SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv)
401 PyObject *py_item, *py_bytes;
404 PyGILState_STATE gstate;
405 int ret = SRD_ERR_PYTHON;
407 gstate = PyGILState_Ensure();
409 if (!PySequence_Check(py_strseq)) {
410 srd_err("Object does not provide sequence protocol.");
414 seq_len = PySequence_Size(py_strseq);
416 srd_exception_catch("Failed to obtain sequence size");
420 strv = g_try_new0(char *, seq_len + 1);
422 srd_err("Failed to allocate result string vector.");
423 ret = SRD_ERR_MALLOC;
427 for (i = 0; i < seq_len; i++) {
428 py_item = PySequence_GetItem(py_strseq, i);
432 if (!PyUnicode_Check(py_item)) {
436 py_bytes = PyUnicode_AsUTF8String(py_item);
441 str = g_strdup(PyBytes_AsString(py_bytes));
450 PyGILState_Release(gstate);
456 srd_exception_catch("Failed to obtain string item");
459 PyGILState_Release(gstate);
465 * Convert a Python scalar object to a GLib variant.
466 * Supported variant types are string, int64 and double.
468 * @param[in] py_obj The Python object. Must not be NULL.
469 * @return A floating reference to a new variant, or NULL on failure.
473 SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
475 GVariant *var = NULL;
476 PyGILState_STATE gstate;
478 gstate = PyGILState_Ensure();
480 if (PyUnicode_Check(py_obj)) { /* string */
484 py_bytes = PyUnicode_AsUTF8String(py_obj);
486 str = PyBytes_AsString(py_bytes);
488 var = g_variant_new_string(str);
492 srd_exception_catch("Failed to extract string value");
493 } else if (PyLong_Check(py_obj)) { /* integer */
496 val = PyLong_AsLongLong(py_obj);
497 if (!PyErr_Occurred())
498 var = g_variant_new_int64(val);
500 srd_exception_catch("Failed to extract integer value");
501 } else if (PyFloat_Check(py_obj)) { /* float */
504 val = PyFloat_AsDouble(py_obj);
505 if (!PyErr_Occurred())
506 var = g_variant_new_double(val);
508 srd_exception_catch("Failed to extract float value");
510 srd_err("Failed to extract value of unsupported type.");
513 PyGILState_Release(gstate);