From bd7a2f22b11e37ce35c3a78f6fb54ef74270b903 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Tue, 22 Apr 2025 18:01:07 +0100 Subject: [PATCH] gdb/python: remove Py_TPFLAGS_BASETYPE from gdb.Color 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 Approved-By: Tom Tromey --- gdb/doc/python.texi | 2 ++ gdb/python/py-color.c | 2 +- gdb/testsuite/gdb.python/py-color.exp | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 80f4c143f9f..45f5f70e4ad 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -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 diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c index 9e29ee2fef6..fb4b80e41cc 100644 --- a/gdb/python/py-color.c +++ b/gdb/python/py-color.c @@ -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 */ diff --git a/gdb/testsuite/gdb.python/py-color.exp b/gdb/testsuite/gdb.python/py-color.exp index 1b8e0c5485b..88967d4d43e 100644 --- a/gdb/testsuite/gdb.python/py-color.exp +++ b/gdb/testsuite/gdb.python/py-color.exp @@ -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 : type 'gdb\\.Color' is not an acceptable base type" \ + "Error occurred in Python: type 'gdb\\.Color' is not an acceptable base type"] + -- 2.39.5