[PATCHv3 0/2] Python Style API

Andrew Burgess
2025-06-07T10:46:19Z

In v3:

- Another test issue. This time, the error message given when
parsing an argument of the wrong type changes with Python version.
Relax the regexp to accept any 'TypeError' message.

In v2:

- Fixed an issue with the py-style-parameter-set.exp test; GDB was
not started in a terminal with styling support in one case, so
tests would fail depending on how the testsuite was being run.

---

After the gdb.Color support was added to the Python API I wondered if
this would be enough to easily add styling to custom Python commands.
But I don't think it is, there's still lots that would need to be
handled in user code, for example, the intensity setting. And having
styles that track named styles (e.g. if I want to apply the 'filename'
style to some text from Python).

So this series builds on top of the gdb.Color API to provide a new
gdb.Style API which, I hope, makes it easy to start writing Python
commands that can use GDB's styles.

Here's an example of a simple custome command that uses styles:

import os

class user_home_cmd(gdb.Command):
def __init__(self):
super().__init__("user-home", gdb.COMMAND_USER)
self._style = gdb.Style('filename')

def invoke(self, args, from_tty):
print("The filename is %s."
% (self._style.apply(os.environ['HOME'])))

user_home_cmd()

The new 'user-home' command prints the current users home directory
using 'filename' style. Changing the 'filename' style and re-running
the command will track the style changes.

Thanks,
Andrew

---

Andrew Burgess (2):
gdb/python: add gdb.Style class
gdb/python: new class gdb.StyleParameterSet

gdb/Makefile.in | 1 +
gdb/NEWS | 11 +
gdb/doc/python.texi | 253 ++++++
gdb/python/lib/gdb/__init__.py | 209 +++++
gdb/python/py-style.c | 797 ++++++++++++++++++
.../gdb.python/py-style-parameter-set.exp | 366 ++++++++
gdb/testsuite/gdb.python/py-style.exp | 342 ++++++++
gdb/ui-style.h | 6 +
8 files changed, 1985 insertions(+)
create mode 100644 gdb/python/py-style.c
create mode 100644 gdb/testsuite/gdb.python/py-style-parameter-set.exp
create mode 100644 gdb/testsuite/gdb.python/py-style.exp

base-commit: 41cb00ce25900aadbf089f80bb2830494bf2a780
--
2.47.1