1 /* General utility routines for GDB/Python.
3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "python-internal.h"
26 /* This is a cleanup function which decrements the refcount on a
34 /* Note that we need the extra braces in this 'if' to avoid a
42 /* Return a new cleanup which will decrement the Python object's
46 make_cleanup_py_decref (PyObject *py)
48 return make_cleanup (py_decref, (void *) py);
51 /* Converts a Python 8-bit string to a unicode string object. Assumes the
52 8-bit string is in the host charset. If an error occurs during conversion,
53 returns NULL with a python exception set.
55 As an added bonus, the functions accepts a unicode string and returns it
56 right away, so callers don't need to check which kind of string they've
57 got. In Python 3, all strings are Unicode so this case is always the
60 If the given object is not one of the mentioned string types, NULL is
61 returned, with the TypeError python exception set. */
63 python_string_to_unicode (PyObject *obj)
65 PyObject *unicode_str;
67 /* If obj is already a unicode string, just return it.
68 I wish life was always that simple... */
69 if (PyUnicode_Check (obj))
75 else if (PyString_Check (obj))
76 unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
80 PyErr_SetString (PyExc_TypeError,
81 _("Expected a string or unicode object."));
88 /* Returns a newly allocated string with the contents of the given unicode
89 string object converted to CHARSET. If an error occurs during the
90 conversion, NULL will be returned and a python exception will be set.
92 The caller is responsible for xfree'ing the string. */
94 unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
99 /* Translate string to named charset. */
100 string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
105 result = xstrdup (PyBytes_AsString (string));
107 result = xstrdup (PyString_AsString (string));
115 /* Returns a PyObject with the contents of the given unicode string
116 object converted to a named charset. If an error occurs during
117 the conversion, NULL will be returned and a python exception will
120 unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
122 /* Translate string to named charset. */
123 return PyUnicode_AsEncodedString (unicode_str, charset, NULL);
126 /* Returns a newly allocated string with the contents of the given unicode
127 string object converted to the target's charset. If an error occurs during
128 the conversion, NULL will be returned and a python exception will be set.
130 The caller is responsible for xfree'ing the string. */
132 unicode_to_target_string (PyObject *unicode_str)
134 return unicode_to_encoded_string (unicode_str,
135 target_charset (python_gdbarch));
138 /* Returns a PyObject with the contents of the given unicode string
139 object converted to the target's charset. If an error occurs
140 during the conversion, NULL will be returned and a python exception
143 unicode_to_target_python_string (PyObject *unicode_str)
145 return unicode_to_encoded_python_string (unicode_str,
146 target_charset (python_gdbarch));
149 /* Converts a python string (8-bit or unicode) to a target string in
150 the target's charset. Returns NULL on error, with a python exception set.
152 The caller is responsible for xfree'ing the string. */
154 python_string_to_target_string (PyObject *obj)
159 str = python_string_to_unicode (obj);
163 result = unicode_to_target_string (str);
168 /* Converts a python string (8-bit or unicode) to a target string in the
169 target's charset. Returns NULL on error, with a python exception
172 In Python 3, the returned object is a "bytes" object (not a string). */
174 python_string_to_target_python_string (PyObject *obj)
179 str = python_string_to_unicode (obj);
183 result = unicode_to_target_python_string (str);
188 /* Converts a python string (8-bit or unicode) to a target string in
189 the host's charset. Returns NULL on error, with a python exception set.
191 The caller is responsible for xfree'ing the string. */
193 python_string_to_host_string (PyObject *obj)
198 str = python_string_to_unicode (obj);
202 result = unicode_to_encoded_string (str, host_charset ());
207 /* Return true if OBJ is a Python string or unicode object, false
211 gdbpy_is_string (PyObject *obj)
214 return PyUnicode_Check (obj);
216 return PyString_Check (obj) || PyUnicode_Check (obj);
220 /* Return the string representation of OBJ, i.e., str (obj).
221 Space for the result is malloc'd, the caller must free.
222 If the result is NULL a python error occurred, the caller must clear it. */
225 gdbpy_obj_to_string (PyObject *obj)
227 PyObject *str_obj = PyObject_Str (obj);
232 char *msg = python_string_to_host_string (str_obj);
234 char *msg = xstrdup (PyString_AsString (str_obj));
244 /* Return the string representation of the exception represented by
245 TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
246 i.e., the error indicator is currently clear.
247 Space for the result is malloc'd, the caller must free.
248 If the result is NULL a python error occurred, the caller must clear it. */
251 gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
255 /* There are a few cases to consider.
257 pvalue is a string when PyErr_SetString is used.
258 pvalue is not a string when raise "foo" is used, instead it is None
260 So the algorithm we use is to print `str (pvalue)' if it's not
261 None, otherwise we print `str (ptype)'.
262 Using str (aka PyObject_Str) will fetch the error message from
263 gdb.GdbError ("message"). */
265 if (pvalue && pvalue != Py_None)
266 str = gdbpy_obj_to_string (pvalue);
268 str = gdbpy_obj_to_string (ptype);
273 /* Convert a GDB exception to the appropriate Python exception.
275 This sets the Python error indicator, and returns NULL. */
278 gdbpy_convert_exception (struct gdb_exception exception)
282 if (exception.reason == RETURN_QUIT)
283 exc_class = PyExc_KeyboardInterrupt;
284 else if (exception.error == MEMORY_ERROR)
285 exc_class = gdbpy_gdb_memory_error;
287 exc_class = gdbpy_gdb_error;
289 return PyErr_Format (exc_class, "%s", exception.message);
292 /* Converts OBJ to a CORE_ADDR value.
294 Returns 1 on success or 0 on failure, with a Python exception set. This
295 function can also throw GDB exceptions.
299 get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
301 if (gdbpy_is_value_object (obj))
302 *addr = value_as_address (value_object_to_value (obj));
305 PyObject *num = PyNumber_Long (obj);
311 val = gdb_py_long_as_ulongest (num);
313 if (PyErr_Occurred ())
316 if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
318 PyErr_SetString (PyExc_ValueError,
319 _("Overflow converting to address."));
329 /* Convert a LONGEST to the appropriate Python object -- either an
330 integer object or a long object, depending on its value. */
333 gdb_py_object_from_longest (LONGEST l)
336 if (sizeof (l) > sizeof (long))
337 return PyLong_FromLongLong (l);
338 return PyLong_FromLong (l);
340 #ifdef HAVE_LONG_LONG /* Defined by Python. */
341 /* If we have 'long long', and the value overflows a 'long', use a
342 Python Long; otherwise use a Python Int. */
343 if (sizeof (l) > sizeof (long)
344 && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1))
345 return PyLong_FromLongLong (l);
347 return PyInt_FromLong (l);
351 /* Convert a ULONGEST to the appropriate Python object -- either an
352 integer object or a long object, depending on its value. */
355 gdb_py_object_from_ulongest (ULONGEST l)
358 if (sizeof (l) > sizeof (unsigned long))
359 return PyLong_FromUnsignedLongLong (l);
360 return PyLong_FromUnsignedLong (l);
362 #ifdef HAVE_LONG_LONG /* Defined by Python. */
363 /* If we have 'long long', and the value overflows a 'long', use a
364 Python Long; otherwise use a Python Int. */
365 if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ())
366 return PyLong_FromUnsignedLongLong (l);
369 if (l > PyInt_GetMax ())
370 return PyLong_FromUnsignedLong (l);
372 return PyInt_FromLong (l);
376 /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
377 the value into an out parameter. */
380 gdb_py_int_as_long (PyObject *obj, long *result)
382 *result = PyInt_AsLong (obj);
383 return ! (*result == -1 && PyErr_Occurred ());
388 /* Generic implementation of the __dict__ attribute for objects that
389 have a dictionary. The CLOSURE argument should be the type object.
390 This only handles positive values for tp_dictoffset. */
393 gdb_py_generic_dict (PyObject *self, void *closure)
396 PyTypeObject *type_obj = closure;
399 raw_ptr = (char *) self + type_obj->tp_dictoffset;
400 result = * (PyObject **) raw_ptr;