From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Tom Tromey <tom@tromey.com>, Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v2 3/6] gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF
Date: Tue, 27 Jan 2026 17:02:12 +0000 [thread overview]
Message-ID: <20260127170215.1803582-4-matthieu.longo@arm.com> (raw)
In-Reply-To: <20260127170215.1803582-1-matthieu.longo@arm.com>
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
---
gdb/python/py-symbol.c | 4 ++--
gdb/python/py-symtab.c | 8 ++++----
gdb/python/py-type.c | 3 +--
gdb/python/python-internal.h | 5 ++++-
4 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index bd4023fa31a..29389739841 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..04d2d7d0289 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<gdbpy_memoizing_registry_storage<symtab_object,
} \
} while (0)
-struct sal_object {
- PyObject_HEAD
+struct sal_object: public PyObject
+{
/* The GDB Symbol table and line structure. */
struct symtab_and_line *sal;
/* A Symtab and line object is associated with an objfile, so keep
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 46004b93acd..40adbf0bb7d 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -28,9 +28,8 @@
#include "typeprint.h"
#include "ada-lang.h"
-struct type_object
+struct type_object: public PyObject
{
- PyObject_HEAD
struct type *type;
};
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 @@ class gdbpy_registry
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 @@ class gdbpy_registry
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;
}
--
2.52.0
next prev parent reply other threads:[~2026-01-27 17:07 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-27 17:02 [PATCH v2 0/6] gdb: minor fixes for Python limited C API support Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 1/6] Python limited API: migrate Py_CompileStringExFlags and PyRun_SimpleString Matthieu Longo
2026-01-27 17:54 ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 2/6] Python limited API: migrate PyImport_ExtendInittab Matthieu Longo
2026-01-27 17:54 ` Tom Tromey
2026-01-27 17:02 ` Matthieu Longo [this message]
2026-01-27 18:01 ` [PATCH v2 3/6] gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF Tom Tromey
2026-01-27 18:29 ` Tom Tromey
2026-01-28 11:58 ` Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 4/6] gdb: new setters and getters for __dict__, and attributes Matthieu Longo
2026-01-27 19:06 ` Tom Tromey
2026-01-28 11:57 ` Matthieu Longo
2026-01-28 17:43 ` Matthieu Longo
2026-01-28 17:51 ` Tom Tromey
2026-01-27 17:02 ` [PATCH v2 5/6] gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* Matthieu Longo
2026-01-27 18:28 ` Tom Tromey
2026-01-28 11:58 ` Matthieu Longo
2026-01-27 17:02 ` [PATCH v2 6/6] gdb: make remaining Python extension objects inherit from PyObject Matthieu Longo
2026-01-27 18:29 ` Tom Tromey
2026-01-28 11:58 ` Matthieu Longo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260127170215.1803582-4-matthieu.longo@arm.com \
--to=matthieu.longo@arm.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox