[PATCHv6 0/4] Python Style API

Andrew Burgess
2025-07-07T14:45:17Z

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.

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.

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 v4:

- Two new patches. First patch extends pager_file so that
gdb.Color, and gdb.Style when it's added can work well with GDB's
pager. Last patch extends the gdb.write() Python function to also
support styling.

- I've renamed some of the classes in python/py-style.c, nothing
major, I replaced 'stylepy' with 'style' in some names. I think
the new names are clearer, and better match the rest of GDB's
Python API code.

- Unfortunately, the new pager_file changes mean that this patch
series now depends on this other patch:

https://inbox.sourceware.org/gdb-patches/444008aeae2bb3c68cf868fa317374b3d7973860.1750197766.git.aburgess@redhat.com

Without that patch there will be some failures in
gdb.python/py-color-pagination.exp. I'm not proposing to merge
this until that other patch, or something like it, is merged. But
this series could be reviewed independently.

In v5:

- The pager_file styling fixes mentioned for v4 have now been merged
to master. I've rebased this series on top of them, and all the
tests are now passing.

- I've tweaked the wording for a couple of the commit messages just
to make things clearer.

- No real code changes.

In v6:

- Rebased as there were some style related changes merged to master
recently. No conflicts, and everything still works fine, so there
are no real changes here.

Thanks,
Andrew

---

Andrew Burgess (4):
gdb: allow gdb.Color to work correctly with pagination
gdb/python: add gdb.Style class
gdb/python: new class gdb.StyleParameterSet
gdb/python: extend gdb.write to support styled output

gdb/Makefile.in | 1 +
gdb/NEWS | 14 +
gdb/doc/python.texi | 262 +++++-
gdb/python/lib/gdb/__init__.py | 209 +++++
gdb/python/py-style.c | 824 ++++++++++++++++++
gdb/python/python-internal.h | 15 +
gdb/python/python.c | 46 +-
.../gdb.python/py-color-pagination.exp | 166 ++++
.../gdb.python/py-color-pagination.py | 82 ++
.../gdb.python/py-style-parameter-set.exp | 366 ++++++++
gdb/testsuite/gdb.python/py-style.exp | 371 ++++++++
gdb/ui-style.h | 6 +
gdb/utils.c | 21 +-
13 files changed, 2366 insertions(+), 17 deletions(-)
create mode 100644 gdb/python/py-style.c
create mode 100644 gdb/testsuite/gdb.python/py-color-pagination.exp
create mode 100644 gdb/testsuite/gdb.python/py-color-pagination.py
create mode 100644 gdb/testsuite/gdb.python/py-style-parameter-set.exp
create mode 100644 gdb/testsuite/gdb.python/py-style.exp

base-commit: 1fe6ffd81e76a2799add18d9e1d5208a90c21ece
--
2.47.1