]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: remove Py_TPFLAGS_BASETYPE from gdb.Color
authorAndrew Burgess <aburgess@redhat.com>
Tue, 22 Apr 2025 17:01:07 +0000 (18:01 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Wed, 23 Apr 2025 14:46:22 +0000 (15:46 +0100)
Remove the Py_TPFLAGS_BASETYPE flag from the gdb.Color type.  This
effectively makes gdb.Color final; users can no longer create classes
that inherit from gdb.Color.

Right now I cannot think of any cases where inheritance would be
needed over composition for a simple type like gdb.Color.  If I'm
wrong, then it's easy to add Py_TPFLAGS_BASETYPE back in later, this
would be an extension of the API.  But it's much harder to remove the
flag later as that might break existing user code (note: there has
been no release of GDB yet that includes the gdb.Color type).

Introducing this restriction makes the next commit easier.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
gdb/doc/python.texi
gdb/python/py-color.c
gdb/testsuite/gdb.python/py-color.exp

index 80f4c143f9f955689bb7b5ab275eeb513508230e..45f5f70e4ad39436ce2ed29b9d7c238b629256bf 100644 (file)
@@ -7136,6 +7136,8 @@ Direct 24-bit RGB colors.
 
 @end table
 
+It is not possible to sub-class the @code{Color} class.
+
 @node Architectures In Python
 @subsubsection Python representation of architectures
 @cindex Python architectures
index 9e29ee2fef6f75ca180223941cecb360815b0ec6..fb4b80e41cc8de0e419795ff545a37634931c5ba 100644 (file)
@@ -312,7 +312,7 @@ PyTypeObject colorpy_object_type =
   get_attr,                      /*tp_getattro*/
   0,                             /*tp_setattro*/
   0,                             /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+  Py_TPFLAGS_DEFAULT,            /*tp_flags*/
   "GDB color object",            /* tp_doc */
   0,                             /* tp_traverse */
   0,                             /* tp_clear */
index 1b8e0c5485b664ef0b3c58b27de998cc501a8943..88967d4d43ea5f5708f3253c99e17a366b649609 100644 (file)
@@ -97,3 +97,13 @@ gdb_test [concat "python print (c_red.escape_sequence (True) + " \
     "\033\\\[31m\033\\\[42mred on green\033\\\[49m red on default\033\\\[39m" \
     "escape sequences"
 
+gdb_test_multiline "Try to sub-class gdb.Color" \
+    "python" "" \
+    "class my_color(gdb.Color):" "" \
+    "  def __init__(self):" "" \
+    "    super().__init__('red')" "" \
+    "end" \
+    [multi_line \
+        "Python Exception <class 'TypeError'>: type 'gdb\\.Color' is not an acceptable base type" \
+        "Error occurred in Python: type 'gdb\\.Color' is not an acceptable base type"]
+