]>
Commit | Line | Data |
---|---|---|
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 | |
b2c19614 | 24 | /** |
511e2123 | 25 | * Get the value of a Python object's attribute, returned as a newly |
451680f1 | 26 | * allocated char *. |
b2c19614 | 27 | * |
201a85a8 DE |
28 | * @param[in] py_obj The object to probe. |
29 | * @param[in] attr Name of the attribute to retrieve. | |
30 | * @param[out] outstr ptr to char * storage to be filled in. | |
b2c19614 BV |
31 | * |
32 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
201a85a8 | 33 | * The 'outstr' argument points to a g_malloc()ed string upon success. |
57790bc8 UH |
34 | * |
35 | * @private | |
b2c19614 | 36 | */ |
201a85a8 | 37 | SRD_PRIV int py_attr_as_str(PyObject *py_obj, const char *attr, char **outstr) |
b2c19614 | 38 | { |
451680f1 | 39 | PyObject *py_str; |
b2c19614 BV |
40 | int ret; |
41 | ||
201a85a8 DE |
42 | if (!PyObject_HasAttrString(py_obj, attr)) { |
43 | srd_dbg("Object has no attribute '%s'.", attr); | |
451680f1 BV |
44 | return SRD_ERR_PYTHON; |
45 | } | |
46 | ||
201a85a8 DE |
47 | if (!(py_str = PyObject_GetAttrString(py_obj, attr))) { |
48 | srd_exception_catch("Failed to get attribute '%s'", attr); | |
d42fc6ee BV |
49 | return SRD_ERR_PYTHON; |
50 | } | |
51 | ||
451680f1 | 52 | ret = py_str_as_str(py_str, outstr); |
201a85a8 | 53 | Py_DECREF(py_str); |
451680f1 BV |
54 | |
55 | return ret; | |
56 | } | |
57 | ||
d42fc6ee | 58 | /** |
511e2123 | 59 | * Get the value of a Python dictionary item, returned as a newly |
d42fc6ee BV |
60 | * allocated char *. |
61 | * | |
201a85a8 DE |
62 | * @param[in] py_obj The dictionary to probe. |
63 | * @param[in] key Key of the item to retrieve. | |
64 | * @param[out] outstr Pointer to char * storage to be filled in. | |
d42fc6ee BV |
65 | * |
66 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
201a85a8 | 67 | * The 'outstr' argument points to a g_malloc()ed string upon success. |
57790bc8 UH |
68 | * |
69 | * @private | |
d42fc6ee | 70 | */ |
201a85a8 | 71 | SRD_PRIV int py_dictitem_as_str(PyObject *py_obj, const char *key, |
55c3c5f4 | 72 | char **outstr) |
d42fc6ee BV |
73 | { |
74 | PyObject *py_value; | |
d42fc6ee | 75 | |
201a85a8 DE |
76 | if (!PyDict_Check(py_obj)) { |
77 | srd_dbg("Object is not a dictionary."); | |
d42fc6ee BV |
78 | return SRD_ERR_PYTHON; |
79 | } | |
80 | ||
201a85a8 | 81 | if (!(py_value = PyDict_GetItemString(py_obj, key))) { |
7a1712c4 | 82 | srd_dbg("Dictionary has no attribute '%s'.", key); |
d42fc6ee BV |
83 | return SRD_ERR_PYTHON; |
84 | } | |
85 | ||
201a85a8 | 86 | return py_str_as_str(py_value, outstr); |
d42fc6ee BV |
87 | } |
88 | ||
451680f1 | 89 | /** |
511e2123 | 90 | * Get the value of a Python unicode string object, returned as a newly |
451680f1 BV |
91 | * allocated char *. |
92 | * | |
201a85a8 DE |
93 | * @param[in] py_str The unicode string object. |
94 | * @param[out] outstr ptr to char * storage to be filled in. | |
451680f1 BV |
95 | * |
96 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
201a85a8 | 97 | * The 'outstr' argument points to a g_malloc()ed string upon success. |
57790bc8 UH |
98 | * |
99 | * @private | |
451680f1 | 100 | */ |
201a85a8 | 101 | SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr) |
451680f1 | 102 | { |
201a85a8 | 103 | PyObject *py_bytes; |
451680f1 BV |
104 | char *str; |
105 | ||
201a85a8 DE |
106 | if (!PyUnicode_Check(py_str)) { |
107 | srd_dbg("Object is not a string object."); | |
108 | return SRD_ERR_PYTHON; | |
b2c19614 BV |
109 | } |
110 | ||
201a85a8 DE |
111 | py_bytes = PyUnicode_AsUTF8String(py_str); |
112 | if (py_bytes) { | |
113 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
114 | Py_DECREF(py_bytes); | |
115 | if (str) { | |
116 | *outstr = str; | |
117 | return SRD_OK; | |
118 | } | |
451680f1 | 119 | } |
201a85a8 | 120 | srd_exception_catch("Failed to extract string"); |
b2c19614 | 121 | |
201a85a8 | 122 | return SRD_ERR_PYTHON; |
b2c19614 BV |
123 | } |
124 | ||
15969949 | 125 | /** |
201a85a8 DE |
126 | * Convert a Python list of unicode strings to a C string vector. |
127 | * On success, a pointer to a newly allocated NULL-terminated array of | |
128 | * allocated C strings is written to @a out_strv. The caller must g_free() | |
129 | * each string and the array itself. | |
451680f1 | 130 | * |
201a85a8 DE |
131 | * @param[in] py_strseq The sequence object. |
132 | * @param[out] out_strv Address of string vector to be filled in. | |
451680f1 BV |
133 | * |
134 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
57790bc8 UH |
135 | * |
136 | * @private | |
15969949 | 137 | */ |
201a85a8 | 138 | SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv) |
15969949 | 139 | { |
201a85a8 DE |
140 | PyObject *py_item, *py_bytes; |
141 | char **strv, *str; | |
142 | ssize_t seq_len, i; | |
143 | ||
144 | if (!PySequence_Check(py_strseq)) { | |
145 | srd_err("Object does not provide sequence protocol."); | |
146 | return SRD_ERR_PYTHON; | |
147 | } | |
148 | ||
149 | seq_len = PySequence_Size(py_strseq); | |
150 | if (seq_len < 0) { | |
151 | srd_exception_catch("Failed to obtain sequence size"); | |
152 | return SRD_ERR_PYTHON; | |
153 | } | |
15969949 | 154 | |
201a85a8 DE |
155 | strv = g_try_new0(char *, seq_len + 1); |
156 | if (!strv) { | |
157 | srd_err("Failed to allocate result string vector."); | |
15969949 | 158 | return SRD_ERR_MALLOC; |
a61ece20 | 159 | } |
201a85a8 DE |
160 | |
161 | for (i = 0; i < seq_len; i++) { | |
162 | py_item = PySequence_GetItem(py_strseq, i); | |
163 | if (!py_item) | |
164 | goto err_out; | |
165 | ||
166 | if (!PyUnicode_Check(py_item)) { | |
167 | Py_DECREF(py_item); | |
168 | goto err_out; | |
169 | } | |
170 | py_bytes = PyUnicode_AsUTF8String(py_item); | |
171 | Py_DECREF(py_item); | |
172 | if (!py_bytes) | |
173 | goto err_out; | |
174 | ||
175 | str = g_strdup(PyBytes_AsString(py_bytes)); | |
176 | Py_DECREF(py_bytes); | |
177 | if (!str) | |
178 | goto err_out; | |
179 | ||
180 | strv[i] = str; | |
15969949 | 181 | } |
201a85a8 | 182 | *out_strv = strv; |
15969949 BV |
183 | |
184 | return SRD_OK; | |
201a85a8 DE |
185 | |
186 | err_out: | |
187 | g_strfreev(strv); | |
188 | srd_exception_catch("Failed to obtain string item"); | |
189 | ||
190 | return SRD_ERR_PYTHON; | |
15969949 | 191 | } |