From 030f56e0e82cd98f52a4afa468e6612d3454c57f Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Sun, 22 Feb 2026 17:37:34 +0000 Subject: gdb/python: introduce gdb.Symtab.source_lines method This commit adds a new method gdb.Symtab.source_lines. This method can be used to read the lines from a symtab's source file. This is similar to GDB's internal source_cache::get_source_lines function. Currently using the Python API, if a user wants to display source lines then they need to use Symtab.fullname() to get the source file name, then open this file and parse out the lines themselves. This isn't too much effort, but the problem is that these lines will not be styled. The user could style the source content themselves, but will this be styled exactly as GDB would style it? The new Symtab.source_lines() method returns source lines with styling included (as ANSI terminal escape sequences), assuming of course, that styling is currently enabled. Of course, in some cases, a user of the Python API might want source code without styling. That's supported too, the new method has an 'unstyled' argument. If this is True then the output is forced to be unstyled. The argument is named 'unstyled' rather than 'styled' because the API call cannot force styling on. If 'set style enabled off' is in effect then making the API call will never return styled source lines. The new API call allows for a range of lines to be requested if desired. As part of this commit I've updated the host_string_to_python_string utility function to take a std::string_view. Reviewed-By: Eli Zaretskii Approved-By: Tom Tromey --- gdb/python/python-internal.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'gdb/python/python-internal.h') diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index fdd353ffbeb..bdc960ed208 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -939,7 +939,11 @@ gdb::unique_xmalloc_ptr unicode_to_target_string (PyObject *unicode_str); gdb::unique_xmalloc_ptr python_string_to_target_string (PyObject *obj); gdbpy_ref<> python_string_to_target_python_string (PyObject *obj); gdb::unique_xmalloc_ptr python_string_to_host_string (PyObject *obj); -gdbpy_ref<> host_string_to_python_string (const char *str); + +/* Convert a host string STR to a python string. */ + +extern gdbpy_ref<> host_string_to_python_string (std::string_view str); + int gdbpy_is_string (PyObject *obj); gdb::unique_xmalloc_ptr gdbpy_obj_to_string (PyObject *obj); -- cgit v1.2.3