aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/py-lazy-string.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2023-10-04 11:22:05 -0600
committerTom Tromey <tromey@adacore.com>2023-10-16 09:27:28 -0600
commitee81567c7cdf6aaae4b565c2afc3d4b798717177 (patch)
treecbee986f38d57d67a8152f95f7239e0185a7817f /gdb/python/py-lazy-string.c
parent138c7d2661044d80dddc0445616aada039af1a95 (diff)
downloadbinutils-ee81567c7cdf6aaae4b565c2afc3d4b798717177.zip
binutils-ee81567c7cdf6aaae4b565c2afc3d4b798717177.tar.gz
binutils-ee81567c7cdf6aaae4b565c2afc3d4b798717177.tar.bz2
Handle gdb.LazyString in DAP
Andry pointed out that the DAP code did not properly handle gdb.LazyString results from a pretty-printer, yielding: TypeError: Object of type LazyString is not JSON serializable This patch fixes the problem, partly with a small patch in varref.py, but mainly by implementing tp_str for LazyString. Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Diffstat (limited to 'gdb/python/py-lazy-string.c')
-rw-r--r--gdb/python/py-lazy-string.c28
1 files changed, 27 insertions, 1 deletions
diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index 25a22b7..03393b0 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -296,6 +296,32 @@ gdbpy_extract_lazy_string (PyObject *string, CORE_ADDR *addr,
encoding->reset (lazy->encoding ? xstrdup (lazy->encoding) : NULL);
}
+/* __str__ for LazyString. */
+
+static PyObject *
+stpy_str (PyObject *self)
+{
+ lazy_string_object *str = (lazy_string_object *) self;
+
+ struct value_print_options opts;
+ get_user_print_options (&opts);
+ opts.addressprint = false;
+
+ string_file stream;
+ try
+ {
+ struct type *type = stpy_lazy_string_elt_type (str);
+ val_print_string (type, str->encoding, str->address, str->length,
+ &stream, &opts);
+ }
+ catch (const gdb_exception &exc)
+ {
+ GDB_PY_HANDLE_EXCEPTION (exc);
+ }
+
+ return host_string_to_python_string (stream.c_str ()).release ();
+}
+
GDBPY_INITIALIZE_FILE (gdbpy_initialize_lazy_string);
@@ -331,7 +357,7 @@ PyTypeObject lazy_string_object_type = {
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
- 0, /*tp_str*/
+ stpy_str, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/