]>
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 | ||
21 | #include "config.h" | |
22 | #include <sigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ | |
23 | ||
24 | ||
25 | /** | |
8b4bbd2a BV |
26 | * Helper function to get the value of a python object's attribute, |
27 | * returned as a newly allocated char *. | |
b2c19614 | 28 | * |
8b4bbd2a BV |
29 | * @param py_obj The object to probe. |
30 | * @param key Name of the attribute to retrieve. | |
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 | */ | |
8b4bbd2a | 36 | int h_str(PyObject *py_obj, const char *key, char **outstr) |
b2c19614 | 37 | { |
5f802ec6 | 38 | PyObject *py_str, *py_encstr; |
b2c19614 BV |
39 | char *str; |
40 | int ret; | |
41 | ||
8b4bbd2a BV |
42 | py_str = py_encstr = NULL; |
43 | str = NULL; | |
44 | ret = SRD_OK; | |
45 | ||
46 | if (!(py_str = PyObject_GetAttrString(py_obj, (char *)key))) { | |
47 | /* TODO: log level 4 debug message */ | |
48 | ret = SRD_ERR_PYTHON; | |
49 | goto err_out; | |
b2c19614 BV |
50 | } |
51 | ||
5f802ec6 | 52 | if (!(py_encstr = PyUnicode_AsEncodedString(py_str, "utf-8", NULL))) { |
8b4bbd2a BV |
53 | /* TODO: log level 4 debug message */ |
54 | ret = SRD_ERR_PYTHON; | |
55 | goto err_out; | |
5f802ec6 BV |
56 | } |
57 | if (!(str = PyBytes_AS_STRING(py_encstr))) { | |
8b4bbd2a BV |
58 | /* TODO: log level 4 debug message */ |
59 | ret = SRD_ERR_PYTHON; | |
60 | goto err_out; | |
b2c19614 BV |
61 | } |
62 | ||
63 | if (!(*outstr = g_strdup(str))) { | |
8b4bbd2a | 64 | /* TODO: log level 4 debug message */ |
b2c19614 | 65 | ret = SRD_ERR_MALLOC; |
8b4bbd2a | 66 | goto err_out; |
b2c19614 BV |
67 | } |
68 | ||
8b4bbd2a BV |
69 | err_out: |
70 | if (py_str) | |
71 | Py_XDECREF(py_str); | |
72 | if (py_encstr) | |
73 | Py_XDECREF(py_encstr); | |
b2c19614 BV |
74 | |
75 | if (PyErr_Occurred()) | |
8b4bbd2a | 76 | PyErr_Print(); |
b2c19614 BV |
77 | |
78 | return ret; | |
79 | } | |
80 |