From ee81567c7cdf6aaae4b565c2afc3d4b798717177 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 4 Oct 2023 11:22:05 -0600 Subject: 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 --- gdb/python/py-lazy-string.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'gdb/python/py-lazy-string.c') 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*/ -- cgit v1.1