From: Matthieu Longo Date: Mon, 5 Jan 2026 11:14:28 +0000 (+0100) Subject: gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b0f0d5fbf8d870c433dc62ceaaf740740af9923;p=thirdparty%2Fbinutils-gdb.git gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF When enabling the Python limited API, pointers to Python C extension objects can no longer be implicitly converted to 'PyObject *' by the compiler. The lookup() method of gbdpy_registry returns a new reference to the type object of the looked-up entry. It does so by calling Py_XINCREF() to increment the reference counter of the returned type object. The template parameter obj_type corresponds to the type of C extension object type. With the Python limited API enabled, obj_type can no longer be implicitly converted to 'PyObject *' when passed to Py_XINCREF(). This patch fixes the resulting compilation issue by adding an explicit static_cast to 'PyObject *' before passing the value to Py_XINCREF(). As a side effect, this cast enforces, at compile time, that the template type 'Storage::obj_type' passed to gdbpy_registry is a subclass of PyObject. To provide a clearer diagnostic when an incorrect type is used, a static_assert is added to gdbpy_registry, avoiding obscure errors originating from the static_cast. Finally, the relevant C extension types passed to gdbpy_registry are updated to inherit publicly from PyObject. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830 Approved-By: Tom Tromey --- diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index bd4023fa31a..736774bd94f 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -25,8 +25,8 @@ #include "objfiles.h" #include "symfile.h" -struct symbol_object { - PyObject_HEAD +struct symbol_object : public PyObject +{ /* The GDB symbol structure this object is wrapping. */ struct symbol *symbol; }; diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c index 9c093e70fc8..2dca0083277 100644 --- a/gdb/python/py-symtab.c +++ b/gdb/python/py-symtab.c @@ -24,8 +24,8 @@ #include "objfiles.h" #include "block.h" -struct symtab_object { - PyObject_HEAD +struct symtab_object : public PyObject +{ /* The GDB Symbol table structure. */ struct symtab *symtab; }; @@ -47,8 +47,8 @@ static const gdbpy_registry::value, + "obj_type must be a subclass of PyObject"); + /* Register Python object OBJ as being "owned" by OWNER. When OWNER is about to be freed, OBJ will be invalidated. */ template @@ -1180,7 +1183,7 @@ public: obj_type *lookup (O *owner, val_type *val) const { obj_type *obj = get_storage (owner)->lookup (val); - Py_XINCREF (obj); + Py_XINCREF (static_cast (obj)); return obj; }