Commit | Line | Data |
---|---|---|
d57a3c85 TJB |
1 | /* General utility routines for GDB/Python. |
2 | ||
0fb0cc75 | 3 | Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
d57a3c85 TJB |
4 | |
5 | This file is part of GDB. | |
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 | #include "defs.h" | |
21 | #include "charset.h" | |
22 | #include "python-internal.h" | |
23 | ||
24 | ||
25 | /* This is a cleanup function which decrements the refcount on a | |
26 | Python object. */ | |
27 | ||
28 | static void | |
29 | py_decref (void *p) | |
30 | { | |
31 | PyObject *py = p; | |
32 | /* Note that we need the extra braces in this 'if' to avoid a | |
33 | warning from gcc. */ | |
34 | if (py) | |
35 | { | |
36 | Py_DECREF (py); | |
37 | } | |
38 | } | |
39 | ||
40 | /* Return a new cleanup which will decrement the Python object's | |
41 | refcount when run. */ | |
42 | ||
43 | struct cleanup * | |
44 | make_cleanup_py_decref (PyObject *py) | |
45 | { | |
46 | return make_cleanup (py_decref, (void *) py); | |
47 | } | |
48 | ||
ca30a762 TT |
49 | /* A cleanup function to restore the thread state. */ |
50 | ||
51 | static void | |
52 | py_gil_restore (void *p) | |
53 | { | |
54 | PyGILState_STATE *state = p; | |
55 | PyGILState_Release (*state); | |
56 | } | |
57 | ||
58 | /* Return a new cleanup which will restore the Python GIL state. */ | |
59 | ||
60 | struct cleanup * | |
61 | make_cleanup_py_restore_gil (PyGILState_STATE *state) | |
62 | { | |
63 | return make_cleanup (py_gil_restore, state); | |
64 | } | |
65 | ||
d57a3c85 TJB |
66 | /* Converts a Python 8-bit string to a unicode string object. Assumes the |
67 | 8-bit string is in the host charset. If an error occurs during conversion, | |
68 | returns NULL with a python exception set. | |
69 | ||
70 | As an added bonus, the functions accepts a unicode string and returns it | |
71 | right away, so callers don't need to check which kind of string they've | |
72 | got. | |
73 | ||
74 | If the given object is not one of the mentioned string types, NULL is | |
75 | returned, with the TypeError python exception set. */ | |
76 | PyObject * | |
77 | python_string_to_unicode (PyObject *obj) | |
78 | { | |
79 | PyObject *unicode_str; | |
80 | ||
81 | /* If obj is already a unicode string, just return it. | |
82 | I wish life was always that simple... */ | |
83 | if (PyUnicode_Check (obj)) | |
83390453 PM |
84 | { |
85 | unicode_str = obj; | |
86 | Py_INCREF (obj); | |
87 | } | |
88 | ||
d57a3c85 TJB |
89 | else if (PyString_Check (obj)) |
90 | unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL); | |
91 | else | |
92 | { | |
93 | PyErr_SetString (PyExc_TypeError, | |
94 | _("Expected a string or unicode object.")); | |
95 | unicode_str = NULL; | |
96 | } | |
97 | ||
98 | return unicode_str; | |
99 | } | |
100 | ||
101 | /* Returns a newly allocated string with the contents of the given unicode | |
08c637de TJB |
102 | string object converted to CHARSET. If an error occurs during the |
103 | conversion, NULL will be returned and a python exception will be set. | |
d57a3c85 TJB |
104 | |
105 | The caller is responsible for xfree'ing the string. */ | |
08c637de TJB |
106 | static char * |
107 | unicode_to_encoded_string (PyObject *unicode_str, const char *charset) | |
d57a3c85 | 108 | { |
08c637de | 109 | char *result; |
d57a3c85 TJB |
110 | PyObject *string; |
111 | ||
08c637de TJB |
112 | /* Translate string to named charset. */ |
113 | string = PyUnicode_AsEncodedString (unicode_str, charset, NULL); | |
d57a3c85 TJB |
114 | if (string == NULL) |
115 | return NULL; | |
116 | ||
08c637de | 117 | result = xstrdup (PyString_AsString (string)); |
d57a3c85 TJB |
118 | |
119 | Py_DECREF (string); | |
120 | ||
08c637de TJB |
121 | return result; |
122 | } | |
123 | ||
124 | /* Returns a newly allocated string with the contents of the given unicode | |
125 | string object converted to the target's charset. If an error occurs during | |
126 | the conversion, NULL will be returned and a python exception will be set. | |
127 | ||
128 | The caller is responsible for xfree'ing the string. */ | |
129 | char * | |
130 | unicode_to_target_string (PyObject *unicode_str) | |
131 | { | |
132 | return unicode_to_encoded_string (unicode_str, target_charset ()); | |
d57a3c85 TJB |
133 | } |
134 | ||
135 | /* Converts a python string (8-bit or unicode) to a target string in | |
136 | the target's charset. Returns NULL on error, with a python exception set. | |
137 | ||
138 | The caller is responsible for xfree'ing the string. */ | |
139 | char * | |
140 | python_string_to_target_string (PyObject *obj) | |
141 | { | |
142 | PyObject *str; | |
83390453 | 143 | char *result; |
d57a3c85 TJB |
144 | |
145 | str = python_string_to_unicode (obj); | |
146 | if (str == NULL) | |
147 | return NULL; | |
148 | ||
83390453 PM |
149 | result = unicode_to_target_string (str); |
150 | Py_DECREF (str); | |
151 | return result; | |
d57a3c85 | 152 | } |
08c637de TJB |
153 | |
154 | /* Converts a python string (8-bit or unicode) to a target string in | |
155 | the host's charset. Returns NULL on error, with a python exception set. | |
156 | ||
157 | The caller is responsible for xfree'ing the string. */ | |
158 | char * | |
159 | python_string_to_host_string (PyObject *obj) | |
160 | { | |
161 | PyObject *str; | |
83390453 | 162 | char *result; |
08c637de TJB |
163 | |
164 | str = python_string_to_unicode (obj); | |
165 | if (str == NULL) | |
166 | return NULL; | |
167 | ||
83390453 PM |
168 | result = unicode_to_encoded_string (str, host_charset ()); |
169 | Py_DECREF (str); | |
170 | return result; | |
08c637de TJB |
171 | } |
172 | ||
b6cb8e7d TJB |
173 | /* Converts a target string of LENGTH bytes in the target's charset to a |
174 | Python Unicode string. If LENGTH is -1, convert until a null byte is found. | |
175 | ||
176 | Returns NULL on error, with a python exception set. */ | |
177 | PyObject * | |
178 | target_string_to_unicode (const gdb_byte *str, int length) | |
179 | { | |
180 | if (length == -1) | |
181 | length = strlen (str); | |
182 | ||
183 | return PyUnicode_Decode (str, length, target_charset (), NULL); | |
184 | } | |
185 | ||
08c637de TJB |
186 | /* Return true if OBJ is a Python string or unicode object, false |
187 | otherwise. */ | |
188 | ||
189 | int | |
190 | gdbpy_is_string (PyObject *obj) | |
191 | { | |
192 | return PyString_Check (obj) || PyUnicode_Check (obj); | |
193 | } |