diff options
author | Andrew Burgess <aburgess@redhat.com> | 2025-04-23 10:22:32 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2025-04-24 00:13:23 +0100 |
commit | 90fa7d2fd5a26bd0eb933c5cd9297c0e0801dacb (patch) | |
tree | 73d3eebe09ba0e424945e09f19029e5bf6ab4e10 /gdb/python | |
parent | 2eead96aeba1ec15d258b0952b37cb5d8bfc4c4a (diff) | |
download | binutils-90fa7d2fd5a26bd0eb933c5cd9297c0e0801dacb.zip binutils-90fa7d2fd5a26bd0eb933c5cd9297c0e0801dacb.tar.gz binutils-90fa7d2fd5a26bd0eb933c5cd9297c0e0801dacb.tar.bz2 |
gdb/python: keyword arguments for gdb.Color.escape_sequence
GDB's Python documentation does make it clear that keywords arguments
are supported for functions that take 2 or more arguments. The
documentation makes no promise for keyword argument support on
functions that only take a single argument.
That said, I'm a fan of keyword arguments, I think they help document
the code, and make intentions clearer, even for single argument
functions.
As I'm changing gdb.Color anyway (see previous commit), I'd like to
add keyword argument support to gdb.Color.escape_sequence, even though
this is a single argument method. This should be harmless for anyone
who doesn't want to use keywords, but adds the option for those of us
that do.
I've also removed a redundant check that the 'self' argument was a
gdb.Color object; Python already ensures this is the case.
And I have folded the check that the single argument is a bool into
the gdb_PyArg_ParseTupleAndKeywords call, this means that the error
message will include the incorrect type name now, which should make
debugging issues easier.
Tests have been extended to cover both cases -- it appears the
incorrect argument type error was not previously tested, so it is
now.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/py-color.c | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c index 97801f8..e208506 100644 --- a/gdb/python/py-color.c +++ b/gdb/python/py-color.c @@ -136,21 +136,21 @@ get_attr (PyObject *obj, PyObject *attr_name) /* Implementation of Color.escape_sequence (self, is_fg) -> str. */ static PyObject * -colorpy_escape_sequence (PyObject *self, PyObject *is_fg_obj) +colorpy_escape_sequence (PyObject *self, PyObject *args, PyObject *kwargs) { - if (!gdbpy_is_color (self)) - { - PyErr_SetString (PyExc_RuntimeError, - _("Object is not gdb.Color.")); - return nullptr; - } + static const char *keywords[] = { "is_foreground", nullptr }; + PyObject *is_fg_obj; - if (!PyBool_Check (is_fg_obj)) - { - PyErr_SetString (PyExc_RuntimeError, - _("A boolean argument is required.")); - return nullptr; - } + /* Parse method arguments. */ + if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "O!", keywords, + &PyBool_Type, &is_fg_obj)) + return nullptr; + + /* Python ensures the type of SELF. */ + gdb_assert (gdbpy_is_color (self)); + + /* The argument parsing ensures we have a bool. */ + gdb_assert (PyBool_Check (is_fg_obj)); bool is_fg = is_fg_obj == Py_True; std::string s = gdbpy_get_color (self).to_ansi (is_fg); @@ -288,7 +288,8 @@ gdbpy_initialize_color (void) static PyMethodDef color_methods[] = { - { "escape_sequence", colorpy_escape_sequence, METH_O, + { "escape_sequence", (PyCFunction) colorpy_escape_sequence, + METH_VARARGS | METH_KEYWORDS, "escape_sequence (is_foreground) -> str.\n\ Return the ANSI escape sequence for this color.\n\ IS_FOREGROUND indicates whether this is a foreground or background color."}, |