1 /* General utility routines for GDB/Python.
3 Copyright (C) 2008-2017 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
32 PyObject
*py
= (PyObject
*) p
;
37 /* Return a new cleanup which will decrement the Python object's
41 make_cleanup_py_decref (PyObject
*py
)
43 return make_cleanup (py_decref
, (void *) py
);
46 /* This is a cleanup function which decrements the refcount on a
47 Python object. This function accounts appropriately for NULL
53 PyObject
*py
= (PyObject
*) p
;
58 /* Return a new cleanup which will decrement the Python object's
59 refcount when run. Account for and operate on NULL references
63 make_cleanup_py_xdecref (PyObject
*py
)
65 return make_cleanup (py_xdecref
, py
);
68 /* Converts a Python 8-bit string to a unicode string object. Assumes the
69 8-bit string is in the host charset. If an error occurs during conversion,
70 returns NULL with a python exception set.
72 As an added bonus, the functions accepts a unicode string and returns it
73 right away, so callers don't need to check which kind of string they've
74 got. In Python 3, all strings are Unicode so this case is always the
77 If the given object is not one of the mentioned string types, NULL is
78 returned, with the TypeError python exception set. */
80 python_string_to_unicode (PyObject
*obj
)
82 PyObject
*unicode_str
;
84 /* If obj is already a unicode string, just return it.
85 I wish life was always that simple... */
86 if (PyUnicode_Check (obj
))
92 else if (PyString_Check (obj
))
93 unicode_str
= PyUnicode_FromEncodedObject (obj
, host_charset (), NULL
);
97 PyErr_SetString (PyExc_TypeError
,
98 _("Expected a string or unicode object."));
105 /* Returns a newly allocated string with the contents of the given unicode
106 string object converted to CHARSET. If an error occurs during the
107 conversion, NULL will be returned and a python exception will be set.
109 The caller is responsible for xfree'ing the string. */
110 static gdb::unique_xmalloc_ptr
<char>
111 unicode_to_encoded_string (PyObject
*unicode_str
, const char *charset
)
113 gdb::unique_xmalloc_ptr
<char> result
;
116 /* Translate string to named charset. */
117 string
= PyUnicode_AsEncodedString (unicode_str
, charset
, NULL
);
122 result
.reset (xstrdup (PyBytes_AsString (string
)));
124 result
.reset (xstrdup (PyString_AsString (string
)));
132 /* Returns a PyObject with the contents of the given unicode string
133 object converted to a named charset. If an error occurs during
134 the conversion, NULL will be returned and a python exception will
137 unicode_to_encoded_python_string (PyObject
*unicode_str
, const char *charset
)
139 /* Translate string to named charset. */
140 return PyUnicode_AsEncodedString (unicode_str
, charset
, NULL
);
143 /* Returns a newly allocated string with the contents of the given
144 unicode string object converted to the target's charset. If an
145 error occurs during the conversion, NULL will be returned and a
146 python exception will be set. */
147 gdb::unique_xmalloc_ptr
<char>
148 unicode_to_target_string (PyObject
*unicode_str
)
150 return unicode_to_encoded_string (unicode_str
,
151 target_charset (python_gdbarch
));
154 /* Returns a PyObject with the contents of the given unicode string
155 object converted to the target's charset. If an error occurs
156 during the conversion, NULL will be returned and a python exception
159 unicode_to_target_python_string (PyObject
*unicode_str
)
161 return unicode_to_encoded_python_string (unicode_str
,
162 target_charset (python_gdbarch
));
165 /* Converts a python string (8-bit or unicode) to a target string in
166 the target's charset. Returns NULL on error, with a python
168 gdb::unique_xmalloc_ptr
<char>
169 python_string_to_target_string (PyObject
*obj
)
173 str
= python_string_to_unicode (obj
);
177 gdb::unique_xmalloc_ptr
<char> result (unicode_to_target_string (str
));
182 /* Converts a python string (8-bit or unicode) to a target string in the
183 target's charset. Returns NULL on error, with a python exception
186 In Python 3, the returned object is a "bytes" object (not a string). */
188 python_string_to_target_python_string (PyObject
*obj
)
193 str
= python_string_to_unicode (obj
);
197 result
= unicode_to_target_python_string (str
);
202 /* Converts a python string (8-bit or unicode) to a target string in
203 the host's charset. Returns NULL on error, with a python exception
205 gdb::unique_xmalloc_ptr
<char>
206 python_string_to_host_string (PyObject
*obj
)
210 str
= python_string_to_unicode (obj
);
214 gdb::unique_xmalloc_ptr
<char>
215 result (unicode_to_encoded_string (str
, host_charset ()));
220 /* Convert a host string to a python string. */
223 host_string_to_python_string (const char *str
)
225 return PyString_Decode (str
, strlen (str
), host_charset (), NULL
);
228 /* Return true if OBJ is a Python string or unicode object, false
232 gdbpy_is_string (PyObject
*obj
)
235 return PyUnicode_Check (obj
);
237 return PyString_Check (obj
) || PyUnicode_Check (obj
);
241 /* Return the string representation of OBJ, i.e., str (obj).
242 If the result is NULL a python error occurred, the caller must clear it. */
244 gdb::unique_xmalloc_ptr
<char>
245 gdbpy_obj_to_string (PyObject
*obj
)
247 PyObject
*str_obj
= PyObject_Str (obj
);
251 gdb::unique_xmalloc_ptr
<char> msg
;
254 msg
= python_string_to_host_string (str_obj
);
256 msg
.reset (xstrdup (PyString_AsString (str_obj
)));
266 /* Return the string representation of the exception represented by
267 TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
268 i.e., the error indicator is currently clear.
269 If the result is NULL a python error occurred, the caller must clear it. */
271 gdb::unique_xmalloc_ptr
<char>
272 gdbpy_exception_to_string (PyObject
*ptype
, PyObject
*pvalue
)
274 /* There are a few cases to consider.
276 pvalue is a string when PyErr_SetString is used.
277 pvalue is not a string when raise "foo" is used, instead it is None
279 So the algorithm we use is to print `str (pvalue)' if it's not
280 None, otherwise we print `str (ptype)'.
281 Using str (aka PyObject_Str) will fetch the error message from
282 gdb.GdbError ("message"). */
284 if (pvalue
&& pvalue
!= Py_None
)
285 return gdbpy_obj_to_string (pvalue
);
287 return gdbpy_obj_to_string (ptype
);
290 /* Convert a GDB exception to the appropriate Python exception.
292 This sets the Python error indicator. */
295 gdbpy_convert_exception (struct gdb_exception exception
)
299 if (exception
.reason
== RETURN_QUIT
)
300 exc_class
= PyExc_KeyboardInterrupt
;
301 else if (exception
.error
== MEMORY_ERROR
)
302 exc_class
= gdbpy_gdb_memory_error
;
304 exc_class
= gdbpy_gdb_error
;
306 PyErr_Format (exc_class
, "%s", exception
.message
);
309 /* Converts OBJ to a CORE_ADDR value.
311 Returns 0 on success or -1 on failure, with a Python exception set.
315 get_addr_from_python (PyObject
*obj
, CORE_ADDR
*addr
)
317 if (gdbpy_is_value_object (obj
))
322 *addr
= value_as_address (value_object_to_value (obj
));
324 CATCH (except
, RETURN_MASK_ALL
)
326 GDB_PY_SET_HANDLE_EXCEPTION (except
);
332 PyObject
*num
= PyNumber_Long (obj
);
338 val
= gdb_py_long_as_ulongest (num
);
340 if (PyErr_Occurred ())
343 if (sizeof (val
) > sizeof (CORE_ADDR
) && ((CORE_ADDR
) val
) != val
)
345 PyErr_SetString (PyExc_ValueError
,
346 _("Overflow converting to address."));
356 /* Convert a LONGEST to the appropriate Python object -- either an
357 integer object or a long object, depending on its value. */
360 gdb_py_object_from_longest (LONGEST l
)
363 if (sizeof (l
) > sizeof (long))
364 return PyLong_FromLongLong (l
);
365 return PyLong_FromLong (l
);
367 #ifdef HAVE_LONG_LONG /* Defined by Python. */
368 /* If we have 'long long', and the value overflows a 'long', use a
369 Python Long; otherwise use a Python Int. */
370 if (sizeof (l
) > sizeof (long)
371 && (l
> PyInt_GetMax () || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
372 return PyLong_FromLongLong (l
);
374 return PyInt_FromLong (l
);
378 /* Convert a ULONGEST to the appropriate Python object -- either an
379 integer object or a long object, depending on its value. */
382 gdb_py_object_from_ulongest (ULONGEST l
)
385 if (sizeof (l
) > sizeof (unsigned long))
386 return PyLong_FromUnsignedLongLong (l
);
387 return PyLong_FromUnsignedLong (l
);
389 #ifdef HAVE_LONG_LONG /* Defined by Python. */
390 /* If we have 'long long', and the value overflows a 'long', use a
391 Python Long; otherwise use a Python Int. */
392 if (sizeof (l
) > sizeof (unsigned long) && l
> PyInt_GetMax ())
393 return PyLong_FromUnsignedLongLong (l
);
396 if (l
> PyInt_GetMax ())
397 return PyLong_FromUnsignedLong (l
);
399 return PyInt_FromLong (l
);
403 /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
404 the value into an out parameter. */
407 gdb_py_int_as_long (PyObject
*obj
, long *result
)
409 *result
= PyInt_AsLong (obj
);
410 return ! (*result
== -1 && PyErr_Occurred ());
415 /* Generic implementation of the __dict__ attribute for objects that
416 have a dictionary. The CLOSURE argument should be the type object.
417 This only handles positive values for tp_dictoffset. */
420 gdb_py_generic_dict (PyObject
*self
, void *closure
)
423 PyTypeObject
*type_obj
= (PyTypeObject
*) closure
;
426 raw_ptr
= (char *) self
+ type_obj
->tp_dictoffset
;
427 result
= * (PyObject
**) raw_ptr
;
433 /* Like PyModule_AddObject, but does not steal a reference to
437 gdb_pymodule_addobject (PyObject
*module
, const char *name
, PyObject
*object
)
442 /* Python 2.4 did not have a 'const' here. */
443 result
= PyModule_AddObject (module
, (char *) name
, object
);
This page took 0.041413 seconds and 4 git commands to generate.