binutils-gdb/gdb/python
Andrew Burgess 030f56e0e8 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 <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
2026-03-05 10:17:08 +00:00
..
lib/gdb Update black to 26.1.0 2026-01-23 06:45:46 -07:00
py-all-events.def gdb/python: new selected_context event 2026-03-05 09:42:18 +00:00
py-arch.c Return gdbpy_ref<> from gdbarch_to_arch_object 2026-02-23 05:29:12 -07:00
py-auto-load.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-block.c Return gdbpy_ref<> from block_to_block_object 2026-02-23 05:29:11 -07:00
py-bpevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-breakpoint.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-cmd.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-color.c Use ref_ptr upcasts in Python code 2026-03-04 10:09:33 -07:00
py-color.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-connection.c Use ref_ptr upcasts in Python code 2026-03-04 10:09:33 -07:00
py-continueevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-corefile.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-dap.c Rewrite output redirection and logging 2026-02-09 08:15:44 -07:00
py-disasm.c Use the "O!" format more in the Python code 2026-02-27 12:01:56 -07:00
py-event-types.def gdb/python: new selected_context event 2026-03-05 09:42:18 +00:00
py-event.c Use ref_ptr upcasts in Python code 2026-03-04 10:09:33 -07:00
py-event.h gdb: new setters and getters for __dict__, and attributes 2026-01-29 16:46:14 +00:00
py-events.h gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-evtregistry.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-evts.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-exitedevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-finishbreakpoint.c Return gdbpy_ref<> from value_to_value_object 2026-02-23 05:29:11 -07:00
py-frame.c Use ref_ptr upcasts in Python code 2026-03-04 10:09:33 -07:00
py-framefilter.c Return gdbpy_ref<> from frame_info_to_frame_object 2026-02-23 05:29:12 -07:00
py-function.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
py-gdb-readline.c Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString 2026-01-28 09:41:25 +00:00
py-inferior.c gdb/python: new selected_context event 2026-03-05 09:42:18 +00:00
py-infevents.c Return gdbpy_ref<> from frame_info_to_frame_object 2026-02-23 05:29:12 -07:00
py-infthread.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
py-instruction.c gdb: make remaining Python extension objects inherit from PyObject 2026-01-29 16:46:14 +00:00
py-instruction.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-lazy-string.c Return gdbpy_ref<> from type_to_type_object 2026-02-23 05:29:12 -07:00
py-linetable.c Return gdbpy_ref<> from symtab_to_linetable_object 2026-02-23 05:29:12 -07:00
py-membuf.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-mi.c gdb: switch sequence protocol helpers to Python limited API equivalents 2026-02-27 11:38:57 +00:00
py-micmd.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-newobjfileevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-objfile.c gdb/python: remove some unreachable Py_RETURN_NONE 2026-02-23 08:21:46 -05:00
py-param.c gdb: make remaining Python extension objects inherit from PyObject 2026-01-29 16:46:14 +00:00
py-prettyprint.c gdb: switch bytes object helpers to Python limited API equivalents 2026-03-04 15:38:28 +00:00
py-progspace.c Return gdbpy_ref<> from block_to_block_object 2026-02-23 05:29:11 -07:00
py-record-btrace.c Use the "O!" format more in the Python code 2026-02-27 12:01:56 -07:00
py-record-btrace.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-record-full.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-record-full.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-record.c gdb: make remaining Python extension objects inherit from PyObject 2026-01-29 16:46:14 +00:00
py-record.h gdb: make remaining Python extension objects inherit from PyObject 2026-01-29 16:46:14 +00:00
py-ref.h Don't use template for gdbpy_ref_policy 2026-03-04 10:07:33 -07:00
py-registers.c Use ref_ptr upcasts in Python code 2026-03-04 10:09:33 -07:00
py-signalevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-stopevent.c Return gdbpy_ref<> from value_to_value_object 2026-02-23 05:29:11 -07:00
py-stopevent.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-style.c gdb: make remaining Python extension objects inherit from PyObject 2026-01-29 16:46:14 +00:00
py-symbol.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
py-symtab.c gdb/python: introduce gdb.Symtab.source_lines method 2026-03-05 10:17:08 +00:00
py-threadevent.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-tui.c gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 2026-01-29 16:46:14 +00:00
py-type.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
py-uiout.h Remove m_applied_style from ui_file 2026-02-09 08:15:29 -07:00
py-unwind.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
py-utils.c gdb/python: introduce gdb.Symtab.source_lines method 2026-03-05 10:17:08 +00:00
py-value.c Return gdbpy_ref<> from type_to_type_object 2026-02-23 05:29:12 -07:00
py-varobj.c Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00
py-xmethods.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
python-config.py Run isort 2024-04-02 11:21:39 -06:00
python-internal.h gdb/python: introduce gdb.Symtab.source_lines method 2026-03-05 10:17:08 +00:00
python.c gdb: switch tuple object helpers to Python limited API equivalents 2026-03-04 15:38:11 +00:00
python.h Update copyright dates to include 2026 2026-01-05 13:16:46 -07:00