]>
Commit | Line | Data |
---|---|---|
b2c19614 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
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 | ||
73191416 | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
451680f1 | 22 | #include "sigrokdecode-internal.h" |
15969949 | 23 | #include "config.h" |
b2c19614 | 24 | |
b2c19614 | 25 | /** |
511e2123 | 26 | * Get the value of a Python object's attribute, returned as a newly |
451680f1 | 27 | * allocated char *. |
b2c19614 | 28 | * |
8b4bbd2a | 29 | * @param py_obj The object to probe. |
451680f1 | 30 | * @param attr Name of the attribute to retrieve. |
8b4bbd2a | 31 | * @param outstr ptr to char * storage to be filled in. |
b2c19614 BV |
32 | * |
33 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
34 | * The 'outstr' argument points to a malloc()ed string upon success. | |
35 | */ | |
abeeed8b UH |
36 | SRD_PRIV int py_attr_as_str(const PyObject *py_obj, const char *attr, |
37 | char **outstr) | |
b2c19614 | 38 | { |
451680f1 | 39 | PyObject *py_str; |
b2c19614 BV |
40 | int ret; |
41 | ||
abeeed8b | 42 | if (!PyObject_HasAttrString((PyObject *)py_obj, attr)) { |
7a1712c4 UH |
43 | srd_dbg("%s object has no attribute '%s'.", |
44 | Py_TYPE(py_obj)->tp_name, attr); | |
451680f1 BV |
45 | return SRD_ERR_PYTHON; |
46 | } | |
47 | ||
abeeed8b | 48 | if (!(py_str = PyObject_GetAttrString((PyObject *)py_obj, attr))) { |
aafeeaea | 49 | srd_exception_catch(""); |
451680f1 BV |
50 | return SRD_ERR_PYTHON; |
51 | } | |
52 | ||
d42fc6ee | 53 | if (!PyUnicode_Check(py_str)) { |
7a1712c4 UH |
54 | srd_dbg("%s attribute should be a string, but is a %s.", |
55 | attr, Py_TYPE(py_str)->tp_name); | |
d42fc6ee BV |
56 | Py_DecRef(py_str); |
57 | return SRD_ERR_PYTHON; | |
58 | } | |
59 | ||
451680f1 | 60 | ret = py_str_as_str(py_str, outstr); |
d42fc6ee | 61 | Py_DecRef(py_str); |
451680f1 BV |
62 | |
63 | return ret; | |
64 | } | |
65 | ||
d42fc6ee | 66 | /** |
511e2123 | 67 | * Get the value of a Python dictionary item, returned as a newly |
d42fc6ee BV |
68 | * allocated char *. |
69 | * | |
70 | * @param py_obj The dictionary to probe. | |
29590b14 UH |
71 | * @param key Key of the item to retrieve. |
72 | * @param outstr Pointer to char * storage to be filled in. | |
d42fc6ee BV |
73 | * |
74 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
75 | * The 'outstr' argument points to a malloc()ed string upon success. | |
76 | */ | |
abeeed8b | 77 | SRD_PRIV int py_dictitem_as_str(const PyObject *py_obj, const char *key, |
55c3c5f4 | 78 | char **outstr) |
d42fc6ee BV |
79 | { |
80 | PyObject *py_value; | |
81 | int ret; | |
82 | ||
abeeed8b | 83 | if (!PyDict_Check((PyObject *)py_obj)) { |
c9bfccc6 | 84 | srd_dbg("Object is a %s, not a dictionary.", |
abeeed8b | 85 | Py_TYPE((PyObject *)py_obj)->tp_name); |
d42fc6ee BV |
86 | return SRD_ERR_PYTHON; |
87 | } | |
88 | ||
abeeed8b | 89 | if (!(py_value = PyDict_GetItemString((PyObject *)py_obj, key))) { |
7a1712c4 | 90 | srd_dbg("Dictionary has no attribute '%s'.", key); |
d42fc6ee BV |
91 | return SRD_ERR_PYTHON; |
92 | } | |
93 | ||
94 | if (!PyUnicode_Check(py_value)) { | |
c9bfccc6 UH |
95 | srd_dbg("Dictionary value for %s should be a string, but is " |
96 | "a %s.", key, Py_TYPE(py_value)->tp_name); | |
d42fc6ee BV |
97 | return SRD_ERR_PYTHON; |
98 | } | |
99 | ||
100 | ret = py_str_as_str(py_value, outstr); | |
101 | ||
708b3b84 | 102 | return ret; |
d42fc6ee BV |
103 | } |
104 | ||
451680f1 | 105 | /** |
511e2123 | 106 | * Get the value of a Python unicode string object, returned as a newly |
451680f1 BV |
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 | */ | |
abeeed8b | 115 | SRD_PRIV int py_str_as_str(const PyObject *py_str, char **outstr) |
451680f1 BV |
116 | { |
117 | PyObject *py_encstr; | |
118 | int ret; | |
119 | char *str; | |
120 | ||
121 | py_encstr = NULL; | |
8b4bbd2a BV |
122 | str = NULL; |
123 | ret = SRD_OK; | |
124 | ||
abeeed8b | 125 | if (!PyUnicode_Check((PyObject *)py_str)) { |
c9bfccc6 | 126 | srd_dbg("Object is a %s, not a string object.", |
abeeed8b | 127 | Py_TYPE((PyObject *)py_str)->tp_name); |
8b4bbd2a BV |
128 | ret = SRD_ERR_PYTHON; |
129 | goto err_out; | |
b2c19614 BV |
130 | } |
131 | ||
abeeed8b UH |
132 | if (!(py_encstr = PyUnicode_AsEncodedString((PyObject *)py_str, |
133 | "utf-8", NULL))) { | |
8b4bbd2a BV |
134 | ret = SRD_ERR_PYTHON; |
135 | goto err_out; | |
5f802ec6 BV |
136 | } |
137 | if (!(str = PyBytes_AS_STRING(py_encstr))) { | |
8b4bbd2a BV |
138 | ret = SRD_ERR_PYTHON; |
139 | goto err_out; | |
b2c19614 BV |
140 | } |
141 | ||
142 | if (!(*outstr = g_strdup(str))) { | |
a61ece20 | 143 | srd_dbg("Failed to g_malloc() outstr."); |
b2c19614 | 144 | ret = SRD_ERR_MALLOC; |
8b4bbd2a | 145 | goto err_out; |
b2c19614 BV |
146 | } |
147 | ||
8b4bbd2a | 148 | err_out: |
8b4bbd2a BV |
149 | if (py_encstr) |
150 | Py_XDECREF(py_encstr); | |
b2c19614 | 151 | |
451680f1 | 152 | if (PyErr_Occurred()) { |
aafeeaea | 153 | srd_exception_catch("string conversion failed"); |
451680f1 | 154 | } |
b2c19614 BV |
155 | |
156 | return ret; | |
157 | } | |
158 | ||
15969949 | 159 | /** |
511e2123 | 160 | * Convert a Python list of unicode strings to a NULL-terminated UTF8-encoded |
56bf4c20 | 161 | * char * array. The caller must g_free() each string when finished. |
451680f1 BV |
162 | * |
163 | * @param py_strlist The list object. | |
164 | * @param outstr ptr to char ** storage to be filled in. | |
165 | * | |
166 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
a61ece20 | 167 | * The 'outstr' argument points to a g_malloc()ed char** upon success. |
15969949 | 168 | */ |
abeeed8b | 169 | SRD_PRIV int py_strlist_to_char(const PyObject *py_strlist, char ***outstr) |
15969949 BV |
170 | { |
171 | PyObject *py_str; | |
172 | int list_len, i; | |
173 | char **out, *str; | |
174 | ||
abeeed8b | 175 | list_len = PyList_Size((PyObject *)py_strlist); |
a61ece20 UH |
176 | if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) { |
177 | srd_err("Failed to g_malloc() 'out'."); | |
15969949 | 178 | return SRD_ERR_MALLOC; |
a61ece20 | 179 | } |
15969949 | 180 | for (i = 0; i < list_len; i++) { |
451680f1 | 181 | if (!(py_str = PyUnicode_AsEncodedString( |
abeeed8b | 182 | PyList_GetItem((PyObject *)py_strlist, i), "utf-8", NULL))) |
15969949 BV |
183 | return SRD_ERR_PYTHON; |
184 | if (!(str = PyBytes_AS_STRING(py_str))) | |
185 | return SRD_ERR_PYTHON; | |
186 | out[i] = g_strdup(str); | |
187 | } | |
188 | out[i] = NULL; | |
189 | *outstr = out; | |
190 | ||
191 | return SRD_OK; | |
192 | } |