From b6cb8e7dead0c48df80c28f95ee7543799672d11 Mon Sep 17 00:00:00 2001 From: Thiago Jung Bauermann Date: Thu, 5 Feb 2009 21:16:09 +0000 Subject: gdb/ 2009-02-05 Thiago Jung Bauermann Tom Tromey * python/python-utils.c (target_string_to_unicode): New function. * python/python-internal.h (target_string_to_unicode): New prototype. * python/python-value.c (valpy_string): New function. (value_object_methods): Add `string' entry. gdb/doc/ 2009-02-05 Tom Tromey * gdb.texinfo (Values From Inferior): Document Value.string. --- gdb/python/python-value.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'gdb/python/python-value.c') diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index 420d26f..bc077b6 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -143,6 +143,37 @@ valpy_address (PyObject *self, PyObject *args) return value_to_value_object (res_val); } +/* Return Unicode string with value contents (assumed to be encoded in the + target's charset). */ +static PyObject * +valpy_string (PyObject *self, PyObject *args) +{ + int length, ret = 0; + gdb_byte *buffer; + struct value *value = ((value_object *) self)->value; + volatile struct gdb_exception except; + PyObject *unicode; + const char *encoding = NULL; + const char *errors = NULL; + const char *user_encoding = NULL; + const char *la_encoding = NULL; + + if (!PyArg_ParseTuple (args, "|ss", &user_encoding, &errors)) + return NULL; + + TRY_CATCH (except, RETURN_MASK_ALL) + { + LA_GET_STRING (value, &buffer, &length, &la_encoding); + } + GDB_PY_HANDLE_EXCEPTION (except); + + encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; + unicode = PyUnicode_Decode (buffer, length, encoding, errors); + xfree (buffer); + + return unicode; +} + static Py_ssize_t valpy_length (PyObject *self) { @@ -794,6 +825,8 @@ gdbpy_initialize_values (void) static PyMethodDef value_object_methods[] = { { "address", valpy_address, METH_NOARGS, "Return the address of the value." }, { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, + { "string", valpy_string, METH_VARARGS, + "Return Unicode string representation of the value." }, {NULL} /* Sentinel */ }; -- cgit v1.1