Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the sigrok project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> |
| 5 | * Copyright (C) 2011 Bert Vermeulen <bert@biot.com> |
| 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 | |
Uwe Hermann | 7319141 | 2012-01-03 19:56:01 +0100 | [diff] [blame] | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 22 | #include "sigrokdecode-internal.h" |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 23 | #include "config.h" |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /** |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 27 | * Get the value of a python object's attribute, returned as a newly |
| 28 | * allocated char *. |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 29 | * |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 30 | * @param py_obj The object to probe. |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 31 | * @param attr Name of the attribute to retrieve. |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 32 | * @param outstr ptr to char * storage to be filled in. |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 33 | * |
| 34 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 35 | * The 'outstr' argument points to a malloc()ed string upon success. |
| 36 | */ |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 37 | int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr) |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 38 | { |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 39 | PyObject *py_str; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 40 | int ret; |
| 41 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 42 | if (!PyObject_HasAttrString(py_obj, attr)) { |
| 43 | srd_dbg("object has no attribute '%s'", attr); |
| 44 | return SRD_ERR_PYTHON; |
| 45 | } |
| 46 | |
| 47 | if (!(py_str = PyObject_GetAttrString(py_obj, attr))) { |
| 48 | /* TODO: report exception message/traceback to err/dbg */ |
| 49 | PyErr_Clear(); |
| 50 | return SRD_ERR_PYTHON; |
| 51 | } |
| 52 | |
Bert Vermeulen | d42fc6e | 2012-01-20 22:08:22 +0100 | [diff] [blame^] | 53 | if (!PyUnicode_Check(py_str)) { |
| 54 | srd_err("%s attribute should be a string, but is a %s.", |
| 55 | attr, py_str->ob_type->tp_name); |
| 56 | Py_DecRef(py_str); |
| 57 | return SRD_ERR_PYTHON; |
| 58 | } |
| 59 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 60 | ret = py_str_as_str(py_str, outstr); |
Bert Vermeulen | d42fc6e | 2012-01-20 22:08:22 +0100 | [diff] [blame^] | 61 | Py_DecRef(py_str); |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /** |
Bert Vermeulen | d42fc6e | 2012-01-20 22:08:22 +0100 | [diff] [blame^] | 68 | * Get the value of a python dictionary item, returned as a newly |
| 69 | * allocated char *. |
| 70 | * |
| 71 | * @param py_obj The dictionary to probe. |
| 72 | * @param attr Key of the item to retrieve. |
| 73 | * @param outstr ptr to char * storage to be filled in. |
| 74 | * |
| 75 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 76 | * The 'outstr' argument points to a malloc()ed string upon success. |
| 77 | */ |
| 78 | int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr) |
| 79 | { |
| 80 | PyObject *py_value; |
| 81 | int ret; |
| 82 | |
| 83 | if (!PyDict_Check(py_obj)) { |
| 84 | srd_err("Object is not a dictionary."); |
| 85 | return SRD_ERR_PYTHON; |
| 86 | } |
| 87 | |
| 88 | if (!(py_value = PyDict_GetItemString(py_obj, key))) { |
| 89 | srd_err("Dictionary has no attribute '%s'", key); |
| 90 | return SRD_ERR_PYTHON; |
| 91 | } |
| 92 | |
| 93 | if (!PyUnicode_Check(py_value)) { |
| 94 | srd_err("Dictionary value should be a string, but is a %s.", |
| 95 | key, py_value->ob_type->tp_name); |
| 96 | return SRD_ERR_PYTHON; |
| 97 | } |
| 98 | |
| 99 | ret = py_str_as_str(py_value, outstr); |
| 100 | |
| 101 | return SRD_OK; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 106 | * Get the value of a python unicode string object, returned as a newly |
| 107 | * allocated char *. |
| 108 | * |
| 109 | * @param py_str The unicode string object. |
| 110 | * @param outstr ptr to char * storage to be filled in. |
| 111 | * |
| 112 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 113 | * The 'outstr' argument points to a malloc()ed string upon success. |
| 114 | */ |
| 115 | int py_str_as_str(PyObject *py_str, char **outstr) |
| 116 | { |
| 117 | PyObject *py_encstr; |
| 118 | int ret; |
| 119 | char *str; |
| 120 | |
| 121 | py_encstr = NULL; |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 122 | str = NULL; |
| 123 | ret = SRD_OK; |
| 124 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 125 | if (!PyUnicode_Check(py_str)) { |
| 126 | srd_dbg("not a string object"); |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 127 | ret = SRD_ERR_PYTHON; |
| 128 | goto err_out; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Bert Vermeulen | 5f802ec | 2011-12-27 22:15:53 +0100 | [diff] [blame] | 131 | if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) { |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 132 | ret = SRD_ERR_PYTHON; |
| 133 | goto err_out; |
Bert Vermeulen | 5f802ec | 2011-12-27 22:15:53 +0100 | [diff] [blame] | 134 | } |
| 135 | if (!(str = PyBytes_AS_STRING(py_encstr))) { |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 136 | ret = SRD_ERR_PYTHON; |
| 137 | goto err_out; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (!(*outstr = g_strdup(str))) { |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 141 | srd_dbg("malloc failed"); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 142 | ret = SRD_ERR_MALLOC; |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 143 | goto err_out; |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 146 | err_out: |
Bert Vermeulen | 8b4bbd2 | 2011-12-28 13:40:23 +0100 | [diff] [blame] | 147 | if (py_encstr) |
| 148 | Py_XDECREF(py_encstr); |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 149 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 150 | if (PyErr_Occurred()) { |
| 151 | srd_dbg("string conversion failed"); |
| 152 | /* TODO: dump exception to srd_dbg */ |
| 153 | PyErr_Clear(); |
| 154 | } |
Bert Vermeulen | b2c1961 | 2011-12-04 10:33:02 +0100 | [diff] [blame] | 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 159 | |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 160 | /** |
| 161 | * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded |
| 162 | * char * array. The caller must free each string when finished. |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 163 | * |
| 164 | * @param py_strlist The list object. |
| 165 | * @param outstr ptr to char ** storage to be filled in. |
| 166 | * |
| 167 | * @return SRD_OK upon success, a (negative) error code otherwise. |
| 168 | * The 'outstr' argument points to a malloc()ed char ** upon success. |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 169 | */ |
| 170 | int py_strlist_to_char(PyObject *py_strlist, char ***outstr) |
| 171 | { |
| 172 | PyObject *py_str; |
| 173 | int list_len, i; |
| 174 | char **out, *str; |
| 175 | |
| 176 | list_len = PyList_Size(py_strlist); |
| 177 | if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) |
| 178 | return SRD_ERR_MALLOC; |
| 179 | for (i = 0; i < list_len; i++) { |
Bert Vermeulen | 451680f | 2012-01-15 03:58:27 +0100 | [diff] [blame] | 180 | if (!(py_str = PyUnicode_AsEncodedString( |
| 181 | PyList_GetItem(py_strlist, i), "utf-8", NULL))) |
Bert Vermeulen | 1596994 | 2012-01-07 02:50:14 +0100 | [diff] [blame] | 182 | return SRD_ERR_PYTHON; |
| 183 | if (!(str = PyBytes_AS_STRING(py_str))) |
| 184 | return SRD_ERR_PYTHON; |
| 185 | out[i] = g_strdup(str); |
| 186 | } |
| 187 | out[i] = NULL; |
| 188 | *outstr = out; |
| 189 | |
| 190 | return SRD_OK; |
| 191 | } |
| 192 | |