]>
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> | |
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 | ||
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 BV |
24 | |
25 | ||
26 | /** | |
451680f1 BV |
27 | * Get the value of a python object's attribute, returned as a newly |
28 | * allocated char *. | |
b2c19614 | 29 | * |
8b4bbd2a | 30 | * @param py_obj The object to probe. |
451680f1 | 31 | * @param attr Name of the attribute to retrieve. |
8b4bbd2a | 32 | * @param outstr ptr to char * storage to be filled in. |
b2c19614 BV |
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 | */ | |
451680f1 | 37 | 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 | ||
451680f1 | 42 | if (!PyObject_HasAttrString(py_obj, attr)) { |
d906d3f9 BV |
43 | srd_dbg("srd: %s object has no attribute '%s'.", |
44 | Py_TYPE(py_obj)->tp_name, attr); | |
451680f1 BV |
45 | return SRD_ERR_PYTHON; |
46 | } | |
47 | ||
48 | if (!(py_str = PyObject_GetAttrString(py_obj, attr))) { | |
49 | /* TODO: report exception message/traceback to err/dbg */ | |
50 | PyErr_Clear(); | |
51 | return SRD_ERR_PYTHON; | |
52 | } | |
53 | ||
d42fc6ee | 54 | if (!PyUnicode_Check(py_str)) { |
d906d3f9 BV |
55 | srd_dbg("srd: %s attribute should be a string, but is a %s.", |
56 | attr, Py_TYPE(py_str)->tp_name); | |
d42fc6ee BV |
57 | Py_DecRef(py_str); |
58 | return SRD_ERR_PYTHON; | |
59 | } | |
60 | ||
451680f1 | 61 | ret = py_str_as_str(py_str, outstr); |
d42fc6ee | 62 | Py_DecRef(py_str); |
451680f1 BV |
63 | |
64 | return ret; | |
65 | } | |
66 | ||
67 | ||
d42fc6ee BV |
68 | /** |
69 | * Get the value of a python dictionary item, returned as a newly | |
70 | * allocated char *. | |
71 | * | |
72 | * @param py_obj The dictionary to probe. | |
73 | * @param attr Key of the item to retrieve. | |
74 | * @param outstr ptr to char * storage to be filled in. | |
75 | * | |
76 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
77 | * The 'outstr' argument points to a malloc()ed string upon success. | |
78 | */ | |
79 | int py_dictitem_as_str(PyObject *py_obj, const char *key, char **outstr) | |
80 | { | |
81 | PyObject *py_value; | |
82 | int ret; | |
83 | ||
84 | if (!PyDict_Check(py_obj)) { | |
d906d3f9 | 85 | srd_dbg("srd: Object is a %s, not a dictionary.", Py_TYPE(py_obj)->tp_name); |
d42fc6ee BV |
86 | return SRD_ERR_PYTHON; |
87 | } | |
88 | ||
89 | if (!(py_value = PyDict_GetItemString(py_obj, key))) { | |
d906d3f9 | 90 | srd_dbg("srd: Dictionary has no attribute '%s'.", key); |
d42fc6ee BV |
91 | return SRD_ERR_PYTHON; |
92 | } | |
93 | ||
94 | if (!PyUnicode_Check(py_value)) { | |
d906d3f9 BV |
95 | srd_dbg("srd: Dictionary value for %s should be a string, but is a %s.", |
96 | 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 | ||
102 | return SRD_OK; | |
103 | } | |
104 | ||
105 | ||
451680f1 BV |
106 | /** |
107 | * Get the value of a python unicode string object, returned as a newly | |
108 | * allocated char *. | |
109 | * | |
110 | * @param py_str The unicode string object. | |
111 | * @param outstr ptr to char * storage to be filled in. | |
112 | * | |
113 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
114 | * The 'outstr' argument points to a malloc()ed string upon success. | |
115 | */ | |
116 | int py_str_as_str(PyObject *py_str, char **outstr) | |
117 | { | |
118 | PyObject *py_encstr; | |
119 | int ret; | |
120 | char *str; | |
121 | ||
122 | py_encstr = NULL; | |
8b4bbd2a BV |
123 | str = NULL; |
124 | ret = SRD_OK; | |
125 | ||
451680f1 | 126 | if (!PyUnicode_Check(py_str)) { |
d906d3f9 | 127 | srd_dbg("srd: object is a %s, not a string object", Py_TYPE(py_str)->tp_name); |
8b4bbd2a BV |
128 | ret = SRD_ERR_PYTHON; |
129 | goto err_out; | |
b2c19614 BV |
130 | } |
131 | ||
5f802ec6 | 132 | if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) { |
8b4bbd2a BV |
133 | ret = SRD_ERR_PYTHON; |
134 | goto err_out; | |
5f802ec6 BV |
135 | } |
136 | if (!(str = PyBytes_AS_STRING(py_encstr))) { | |
8b4bbd2a BV |
137 | ret = SRD_ERR_PYTHON; |
138 | goto err_out; | |
b2c19614 BV |
139 | } |
140 | ||
141 | if (!(*outstr = g_strdup(str))) { | |
d906d3f9 | 142 | srd_dbg("srd: malloc failed"); |
b2c19614 | 143 | ret = SRD_ERR_MALLOC; |
8b4bbd2a | 144 | goto err_out; |
b2c19614 BV |
145 | } |
146 | ||
8b4bbd2a | 147 | err_out: |
8b4bbd2a BV |
148 | if (py_encstr) |
149 | Py_XDECREF(py_encstr); | |
b2c19614 | 150 | |
451680f1 | 151 | if (PyErr_Occurred()) { |
d906d3f9 | 152 | srd_dbg("srd: string conversion failed"); |
451680f1 BV |
153 | /* TODO: dump exception to srd_dbg */ |
154 | PyErr_Clear(); | |
155 | } | |
b2c19614 BV |
156 | |
157 | return ret; | |
158 | } | |
159 | ||
451680f1 | 160 | |
15969949 BV |
161 | /** |
162 | * Convert a python list of unicode strings to a NULL-terminated UTF8-encoded | |
163 | * char * array. The caller must free each string when finished. | |
451680f1 BV |
164 | * |
165 | * @param py_strlist The list object. | |
166 | * @param outstr ptr to char ** storage to be filled in. | |
167 | * | |
168 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
169 | * The 'outstr' argument points to a malloc()ed char ** upon success. | |
15969949 BV |
170 | */ |
171 | int py_strlist_to_char(PyObject *py_strlist, char ***outstr) | |
172 | { | |
173 | PyObject *py_str; | |
174 | int list_len, i; | |
175 | char **out, *str; | |
176 | ||
177 | list_len = PyList_Size(py_strlist); | |
178 | if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) | |
179 | return SRD_ERR_MALLOC; | |
180 | for (i = 0; i < list_len; i++) { | |
451680f1 BV |
181 | if (!(py_str = PyUnicode_AsEncodedString( |
182 | PyList_GetItem(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 | } | |
193 |