From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30649 invoked by alias); 3 Jan 2009 02:28:15 -0000 Received: (qmail 30637 invoked by uid 22791); 3 Jan 2009 02:28:14 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_37,J_CHICKENPOX_56,SPF_PASS X-Spam-Check-By: sourceware.org Received: from igw2.br.ibm.com (HELO igw2.br.ibm.com) (32.104.18.25) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 03 Jan 2009 02:27:31 +0000 Received: from d24relay01.br.ibm.com (unknown [9.8.31.16]) by igw2.br.ibm.com (Postfix) with ESMTP id EC71417F423 for ; Fri, 2 Jan 2009 23:09:41 -0200 (BRDT) Received: from d24av02.br.ibm.com (d24av02.br.ibm.com [9.18.232.47]) by d24relay01.br.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id n033R04C3973352 for ; Sat, 3 Jan 2009 00:27:00 -0300 Received: from d24av02.br.ibm.com (loopback [127.0.0.1]) by d24av02.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n032RRun022259 for ; Sat, 3 Jan 2009 00:27:27 -0200 Received: from [9.8.7.235] ([9.8.7.235]) by d24av02.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n032RQ0u022234 for ; Sat, 3 Jan 2009 00:27:26 -0200 Subject: [RFA][python] Add gdb.Value.string method. From: Thiago Jung Bauermann To: gdb-patches ml Content-Type: text/plain Date: Sat, 03 Jan 2009 02:28:00 -0000 Message-Id: <1230949647.8380.145.camel@localhost.localdomain> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-01/txt/msg00004.txt.bz2 Hi, This patch adds a method to the Value python class which returns the value as a Python Unicode string. It depends on the patch I just posted in the "Add la_getstr member to language_defn", and assumes that the "Fixes and improvements to gdb.Value" patch has been applied. Ok? -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ 2009-01-03 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-01-03 Tom Tromey * gdb.texinfo (Values From Inferior): Document Value.string. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 030d4b3..3b1cd91 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -18202,6 +18202,31 @@ The result @code{bar} will be a @code{gdb.Value} object holding the value pointed to by @code{foo}. @end defmethod +@defmethod Value string @r{[}encoding @r{[}errors@r{]}@r{]} +If this @code{gdb.Value} represents a string, then this method +converts the contents to a Python string. Otherwise, this method will +throw an exception. + +Strings are recognized in a language-specific way; whether a given +@code{gdb.Value} represents a string is determined by the current +language. + +For C-like languages, a value is a string if it is a pointer to or an +array of characters or ints. The string is assumed to be terminated +by a zero of the appropriate width. + +If the optional @var{encoding} argument is given, it must be a string +naming the encoding of the string in the @code{gdb.Value}. The Python +codec machinery will be used to convert the string. If @var{encoding} +is not given, or if @var{encoding} is the empty string, then either +the @code{target-charset} (@pxref{Character Sets}) will be used, or a +language-specific encoding will be used, if the current language is +able to supply one. + +The optional @var{errors} argument is the same as the corresponding +argument to Python's @code{string.decode} method. +@end defmethod + @node Interpreters @chapter Command Interpreters @cindex command interpreters diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index bcd37e4..9c2e8c6 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -88,6 +88,7 @@ PyObject *python_string_to_unicode (PyObject *obj); char *unicode_to_target_string (PyObject *unicode_str); char *python_string_to_target_string (PyObject *obj); char *python_string_to_host_string (PyObject *obj); +PyObject *target_string_to_unicode (const gdb_byte *str, int length); int gdbpy_is_string (PyObject *obj); #endif /* GDB_PYTHON_INTERNAL_H */ diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c index c27a930..1e6d952 100644 --- a/gdb/python/python-utils.c +++ b/gdb/python/python-utils.c @@ -160,6 +160,19 @@ python_string_to_host_string (PyObject *obj) return unicode_to_encoded_string (str, host_charset ()); } +/* Converts a target string of LENGTH bytes in the target's charset to a + Python Unicode string. If LENGTH is -1, convert until a null byte is found. + + Returns NULL on error, with a python exception set. */ +PyObject * +target_string_to_unicode (const gdb_byte *str, int length) +{ + if (length == -1) + length = strlen (str); + + return PyUnicode_Decode (str, length, target_charset (), NULL); +} + /* Return true if OBJ is a Python string or unicode object, false otherwise. */ diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index 4d1f864..c6775b2 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -148,6 +148,47 @@ 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) + { + ret = LA_GET_STRING (value, &buffer, &length, &la_encoding); + } + GDB_PY_HANDLE_EXCEPTION (except); + + if (ret != 0) + { + /* We may have read a partial string before the error happened, but + we will ignore it and throw an exception anyway. */ + PyErr_SetString (PyExc_RuntimeError, safe_strerror (ret)); + xfree (buffer); + + return NULL; + } + + 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) { @@ -773,6 +814,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 */ };