]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: keyword args for Color.__init__
authorAndrew Burgess <aburgess@redhat.com>
Wed, 23 Apr 2025 09:07:09 +0000 (10:07 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 23 Apr 2025 23:13:23 +0000 (00:13 +0100)
GDB's Python API documentation is clear:

     Functions and methods which have two or more optional arguments allow
  them to be specified using keyword syntax.

The gdb.Color.__init__ method matches this description, but doesn't
support keyword arguments.

This commit fixes this by adding keyword argument support.

There's a new test to cover this functionality.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/python/py-color.c
gdb/testsuite/gdb.python/py-color.exp

index c48d14e1418ee2202b0c2d0539f4dc26ddf3ac56..97801f859f0d22e775aeb735fa48c582826fc3fa 100644 (file)
@@ -176,7 +176,10 @@ colorpy_init (PyObject *self, PyObject *args, PyObject *kwds)
   PyObject *colorspace_obj = nullptr;
   color_space colorspace = color_space::MONOCHROME;
 
-  if (!PyArg_ParseTuple (args, "|OO", &value_obj, &colorspace_obj))
+  static const char *keywords[] = { "value", "color_space", nullptr };
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwds, "|OO", keywords,
+                                       &value_obj, &colorspace_obj))
     return -1;
 
   try
index 99b4689028990df098ca2e2279541bf5e64d2854..7f711583b2c4c7cc1e2bef5188d2066e243d4793 100644 (file)
@@ -22,7 +22,10 @@ require allow_python_tests
 # Start with a fresh gdb.
 clean_restart
 
-gdb_test_no_output "python print_color_attrs = lambda c: print (c, c.colorspace, c.is_none, c.is_indexed, c.is_direct)" \
+gdb_test_no_output "python get_color_attrs = lambda c: \"%s %s %s %s %s\" % (str(c), c.colorspace, c.is_none, c.is_indexed, c.is_direct)" \
+    "get_color_attrs helper"
+
+gdb_test_no_output "python print_color_attrs = lambda c: print (get_color_attrs (c))" \
     "print_color_attrs helper"
 
 gdb_test_no_output "python c = gdb.Color ()" \
@@ -58,6 +61,15 @@ gdb_test "python print_color_attrs (c)" "green 1 False True False" \
 gdb_test "python print (c.index)" "2" \
     "print index of a basic color with ansi colorspace"
 
+# Create a color using keyword arguments, and check it matches the
+# non-keyword color.
+gdb_test_no_output "python c2 = gdb.Color (color_space = gdb.COLORSPACE_ANSI_8COLOR, value = 2)" \
+    "create color from basic index and ansi colorspace using keywords"
+gdb_test "python print(get_color_attrs (c) == get_color_attrs (c2))" "True" \
+    "check attributes match"
+gdb_test "python print(c.index == c2.index)" "True" \
+    "check index matches"
+
 gdb_test_no_output "python c = gdb.Color (2, gdb.COLORSPACE_XTERM_256COLOR)" \
     "create color from basic index and xterm256 colorspace"
 gdb_test "python print_color_attrs (c)" "2 3 False True False" \