diff options
| author | Matthieu Longo <matthieu.longo@arm.com> | 2026-01-05 12:14:28 +0100 |
|---|---|---|
| committer | Matthieu Longo <matthieu.longo@arm.com> | 2026-01-29 16:46:14 +0000 |
| commit | 8b0f0d5fbf8d870c433dc62ceaaf740740af9923 (patch) | |
| tree | a9ad6aaed13d7ea73124889df16f9c2a719f7623 /gdb/python/python-internal.h | |
| parent | c3023c9f083ea051729f00c7fa0ec6851d4d8e51 (diff) | |
| download | binutils-8b0f0d5fbf8d870c433dc62ceaaf740740af9923.tar.gz binutils-8b0f0d5fbf8d870c433dc62ceaaf740740af9923.tar.bz2 binutils-8b0f0d5fbf8d870c433dc62ceaaf740740af9923.zip | |
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 <tom@tromey.com>
Diffstat (limited to 'gdb/python/python-internal.h')
| -rw-r--r-- | gdb/python/python-internal.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 65d2eee38ed..95619bf775e 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1157,6 +1157,9 @@ public: using obj_type = typename Storage::obj_type; using val_type = typename Storage::val_type; + static_assert(std::is_base_of<PyObject, obj_type>::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 <typename O> @@ -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<PyObject *> (obj)); return obj; } |
