Add python gdb.GdbError and gdb.string_to_argv.
[platform/upstream/binutils.git] / gdb / python / py-utils.c
1 /* General utility routines for GDB/Python.
2
3    Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
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
33   /* Note that we need the extra braces in this 'if' to avoid a
34      warning from gcc.  */
35   if (py)
36     {
37       Py_DECREF (py);
38     }
39 }
40
41 /* Return a new cleanup which will decrement the Python object's
42    refcount when run.  */
43
44 struct cleanup *
45 make_cleanup_py_decref (PyObject *py)
46 {
47   return make_cleanup (py_decref, (void *) py);
48 }
49
50 /* Converts a Python 8-bit string to a unicode string object.  Assumes the
51    8-bit string is in the host charset.  If an error occurs during conversion,
52    returns NULL with a python exception set.
53
54    As an added bonus, the functions accepts a unicode string and returns it
55    right away, so callers don't need to check which kind of string they've
56    got.
57
58    If the given object is not one of the mentioned string types, NULL is
59    returned, with the TypeError python exception set.  */
60 PyObject *
61 python_string_to_unicode (PyObject *obj)
62 {
63   PyObject *unicode_str;
64
65   /* If obj is already a unicode string, just return it.
66      I wish life was always that simple...  */
67   if (PyUnicode_Check (obj))
68     {
69       unicode_str = obj;
70       Py_INCREF (obj);
71     }
72   
73   else if (PyString_Check (obj))
74     unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
75   else
76     {
77       PyErr_SetString (PyExc_TypeError,
78                        _("Expected a string or unicode object."));
79       unicode_str = NULL;
80     }
81
82   return unicode_str;
83 }
84
85 /* Returns a newly allocated string with the contents of the given unicode
86    string object converted to CHARSET.  If an error occurs during the
87    conversion, NULL will be returned and a python exception will be set.
88
89    The caller is responsible for xfree'ing the string.  */
90 static char *
91 unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
92 {
93   char *result;
94   PyObject *string;
95
96   /* Translate string to named charset.  */
97   string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
98   if (string == NULL)
99     return NULL;
100
101   result = xstrdup (PyString_AsString (string));
102
103   Py_DECREF (string);
104
105   return result;
106 }
107
108 /* Returns a PyObject with the contents of the given unicode string
109    object converted to a named charset.  If an error occurs during
110    the conversion, NULL will be returned and a python exception will
111    be set.  */
112 static PyObject *
113 unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
114 {
115   PyObject *string;
116
117   /* Translate string to named charset.  */
118   string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
119   if (string == NULL)
120     return NULL;
121
122   return string;
123 }
124
125 /* Returns a newly allocated string with the contents of the given unicode
126    string object converted to the target's charset.  If an error occurs during
127    the conversion, NULL will be returned and a python exception will be set.
128
129    The caller is responsible for xfree'ing the string.  */
130 char *
131 unicode_to_target_string (PyObject *unicode_str)
132 {
133   return unicode_to_encoded_string (unicode_str,
134                                     target_charset (python_gdbarch));
135 }
136
137 /* Returns a PyObject with the contents of the given unicode string
138    object converted to the target's charset.  If an error occurs
139    during the conversion, NULL will be returned and a python exception
140    will be set.  */
141 PyObject *
142 unicode_to_target_python_string (PyObject *unicode_str)
143 {
144   return unicode_to_encoded_python_string (unicode_str,
145                                            target_charset (python_gdbarch));
146 }
147
148 /* Converts a python string (8-bit or unicode) to a target string in
149    the target's charset.  Returns NULL on error, with a python exception set.
150
151    The caller is responsible for xfree'ing the string.  */
152 char *
153 python_string_to_target_string (PyObject *obj)
154 {
155   PyObject *str;
156   char *result;
157
158   str = python_string_to_unicode (obj);
159   if (str == NULL)
160     return NULL;
161
162   result = unicode_to_target_string (str);
163   Py_DECREF (str);
164   return result;
165 }
166
167 /* Converts a python string (8-bit or unicode) to a target string in the
168    target's charset.  Returns NULL on error, with a python exception
169    set.  */
170 PyObject *
171 python_string_to_target_python_string (PyObject *obj)
172 {
173   PyObject *str;
174   PyObject *result;
175
176   str = python_string_to_unicode (obj);
177   if (str == NULL)
178     return NULL;
179
180   result = unicode_to_target_python_string (str);
181   Py_DECREF (str);
182   return result;
183 }
184
185 /* Converts a python string (8-bit or unicode) to a target string in
186    the host's charset.  Returns NULL on error, with a python exception set.
187
188    The caller is responsible for xfree'ing the string.  */
189 char *
190 python_string_to_host_string (PyObject *obj)
191 {
192   PyObject *str;
193   char *result;
194
195   str = python_string_to_unicode (obj);
196   if (str == NULL)
197     return NULL;
198
199   result = unicode_to_encoded_string (str, host_charset ()); 
200   Py_DECREF (str);
201   return result;
202 }
203
204 /* Converts a target string of LENGTH bytes in the target's charset to a
205    Python Unicode string. If LENGTH is -1, convert until a null byte is found.
206
207    Returns NULL on error, with a python exception set.  */
208 PyObject *
209 target_string_to_unicode (const gdb_byte *str, int length)
210 {
211   if (length == -1)
212     length = strlen (str);
213
214   return PyUnicode_Decode (str, length, target_charset (python_gdbarch), NULL);
215 }
216
217 /* Return true if OBJ is a Python string or unicode object, false
218    otherwise.  */
219
220 int
221 gdbpy_is_string (PyObject *obj)
222 {
223   return PyString_Check (obj) || PyUnicode_Check (obj);
224 }
225
226 /* Return the string representation of OBJ, i.e., str (obj).
227    Space for the result is malloc'd, the caller must free.
228    If the result is NULL a python error occurred, the caller must clear it.  */
229
230 char *
231 gdbpy_obj_to_string (PyObject *obj)
232 {
233   PyObject *str_obj = PyObject_Str (obj);
234
235   if (str_obj != NULL)
236     {
237       char *msg = xstrdup (PyString_AsString (str_obj));
238
239       Py_DECREF (str_obj);
240       return msg;
241     }
242
243   return NULL;
244 }
245
246 /* Return the string representation of the exception represented by
247    TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
248    i.e., the error indicator is currently clear.
249    Space for the result is malloc'd, the caller must free.
250    If the result is NULL a python error occurred, the caller must clear it.  */
251
252 char *
253 gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
254 {
255   PyObject *str_obj = PyObject_Str (pvalue);
256   char *str;
257
258   /* There are a few cases to consider.
259      For example:
260      pvalue is a string when PyErr_SetString is used.
261      pvalue is not a string when raise "foo" is used, instead it is None
262      and ptype is "foo".
263      So the algorithm we use is to print `str (pvalue)' if it's not
264      None, otherwise we print `str (ptype)'.
265      Using str (aka PyObject_Str) will fetch the error message from
266      gdb.GdbError ("message").  */
267
268   if (pvalue && pvalue != Py_None)
269     str = gdbpy_obj_to_string (pvalue);
270   else
271     str = gdbpy_obj_to_string (ptype);
272
273   return str;
274 }