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 5/6] gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject*
Date: Tue, 27 Jan 2026 17:02:14 +0000 [thread overview]
Message-ID: <20260127170215.1803582-6-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.
gdbpy_ref_policy is a templated class that provides a generic interface
for incrementing and decrementing the reference counter on the given
object. It is used as a specialisation of the policy parameter in
gdb::ref_ptr, together with PyObject as the parameter type. As a result,
gdbpy_ref_policy always expects an argument derived from PyObject.
This patch fixes the resulting compilation issue by adding an explicit
static_cast to 'PyObject *' before passing the value to Py_INCREF and
Py_DECREF. As a side effect, these casts enforce, at compile time, that
the template type passed to gdbpy_ref_policy is a subclass of PyObject.
To provide a clearer diagnostic when an incorrect type is used, a
static_assert is added to gdbpy_ref_policy, avoiding obscure errors
originating from the static_cast. Finally, all C Python extension types
passed to gdbpy_ref_policy are updated to inherit from PyObject.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23830
---
gdb/python/py-breakpoint.c | 4 +---
gdb/python/py-cmd.c | 4 +---
gdb/python/py-color.c | 4 +---
gdb/python/py-connection.c | 4 +---
gdb/python/py-corefile.c | 8 ++------
gdb/python/py-disasm.c | 8 ++------
gdb/python/py-events.h | 4 +---
gdb/python/py-frame.c | 4 ++--
gdb/python/py-membuf.c | 5 ++---
gdb/python/py-micmd.c | 4 +---
gdb/python/py-ref.h | 7 +++++--
gdb/python/py-registers.c | 10 ++++------
gdb/python/py-tui.c | 4 +---
gdb/python/python-internal.h | 4 +---
14 files changed, 25 insertions(+), 49 deletions(-)
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index fc53213966a..3953c441c5c 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -37,10 +37,8 @@
extern PyTypeObject breakpoint_location_object_type;
-struct gdbpy_breakpoint_location_object
+struct gdbpy_breakpoint_location_object: public PyObject
{
- PyObject_HEAD
-
/* An owning reference to the gdb breakpoint location object. */
bp_location *bp_loc;
diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c
index 6b40b1796c6..88bc150b765 100644
--- a/gdb/python/py-cmd.c
+++ b/gdb/python/py-cmd.c
@@ -50,10 +50,8 @@ static const struct cmdpy_completer completers[] =
/* A gdb command. For the time being only ordinary commands (not
set/show commands) are allowed. */
-struct cmdpy_object
+struct cmdpy_object: public PyObject
{
- PyObject_HEAD
-
/* The corresponding gdb command object, or NULL if the command is
no longer installed. */
struct cmd_list_element *command;
diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c
index b41dff2edd7..e174a7b6858 100644
--- a/gdb/python/py-color.c
+++ b/gdb/python/py-color.c
@@ -37,10 +37,8 @@ static struct {
};
/* A color. */
-struct colorpy_object
+struct colorpy_object: public PyObject
{
- PyObject_HEAD
-
/* Underlying value. */
ui_file_style::color color;
};
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index bef50b07f18..e42939a995d 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -31,10 +31,8 @@
/* The Python object that represents a connection. */
-struct connection_object
+struct connection_object: public PyObject
{
- PyObject_HEAD
-
/* The process target that represents this connection. When a
connection_object is created this field will always point at a valid
target. Later, if GDB stops using this target (the target is popped
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 4a6982231c8..622456aaae3 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -45,10 +45,8 @@ extern PyTypeObject corefile_object_type;
/* A gdb.CorefileMapped object. */
-struct corefile_mapped_file_object
+struct corefile_mapped_file_object: public PyObject
{
- PyObject_HEAD
-
/* The name of a file that was mapped when the core file was created.
This is a 'str' object. */
PyObject *filename;
@@ -70,10 +68,8 @@ extern PyTypeObject corefile_mapped_file_object_type;
/* A gdb.CorefileMappedFileRegion object. */
-struct corefile_mapped_file_region_object
+struct corefile_mapped_file_region_object: public PyObject
{
- PyObject_HEAD
-
/* The start and end addresses for this mapping, these are addresses
within the inferior's address space. */
CORE_ADDR start;
diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index c1aa4d0586a..037223f05e3 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -28,10 +28,8 @@
/* Implement gdb.disassembler.DisassembleInfo type. An object of this type
represents a single disassembler request from GDB. */
-struct disasm_info_object
+struct disasm_info_object: public PyObject
{
- PyObject_HEAD
-
/* The architecture in which we are disassembling. */
struct gdbarch *gdbarch;
@@ -99,10 +97,8 @@ extern PyTypeObject disasm_part_object_type;
the disassembled instruction (in bytes), and the string representing the
disassembled instruction. */
-struct disasm_result_object
+struct disasm_result_object: public PyObject
{
- PyObject_HEAD
-
/* The length of the disassembled instruction in bytes. */
int length;
diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h
index 9fc7a86920e..a923a923e56 100644
--- a/gdb/python/py-events.h
+++ b/gdb/python/py-events.h
@@ -27,10 +27,8 @@
/* Stores a list of objects to be notified when the event for which this
registry tracks occurs. */
-struct eventregistry_object
+struct eventregistry_object: public PyObject
{
- PyObject_HEAD
-
PyObject *callbacks;
};
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 027ccb4112d..bb61b058671 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -28,8 +28,8 @@
#include "symfile.h"
#include "objfiles.h"
-struct frame_object {
- PyObject_HEAD
+struct frame_object: public PyObject
+{
struct frame_id frame_id;
struct gdbarch *gdbarch;
diff --git a/gdb/python/py-membuf.c b/gdb/python/py-membuf.c
index 832ab62cf52..46033a0dda3 100644
--- a/gdb/python/py-membuf.c
+++ b/gdb/python/py-membuf.c
@@ -19,9 +19,8 @@
#include "python-internal.h"
-struct membuf_object {
- PyObject_HEAD
-
+struct membuf_object: public PyObject
+{
/* Pointer to the raw data, and array of gdb_bytes. */
void *buffer;
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index c6a96dc3ad5..2b4b083d0e3 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -55,10 +55,8 @@ struct mi_command_py;
/* Representation of a Python gdb.MICommand object. */
-struct micmdpy_object
+struct micmdpy_object: public PyObject
{
- PyObject_HEAD
-
/* The object representing this command in the MI command table. This
pointer can be nullptr if the command is not currently installed into
the MI command table (see gdb.MICommand.installed property). */
diff --git a/gdb/python/py-ref.h b/gdb/python/py-ref.h
index a8f3ff9201b..7594d4a9fc3 100644
--- a/gdb/python/py-ref.h
+++ b/gdb/python/py-ref.h
@@ -26,14 +26,17 @@
template<typename T>
struct gdbpy_ref_policy
{
+ static_assert(std::is_base_of<PyObject, T>::value,
+ "T must be a subclass of PyObject");
+
static void incref (T *ptr)
{
- Py_INCREF (ptr);
+ Py_INCREF (static_cast<PyObject *> (ptr));
}
static void decref (T *ptr)
{
- Py_DECREF (ptr);
+ Py_DECREF (static_cast<PyObject *> (ptr));
}
};
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index ac7480caea9..30b333c444c 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -49,9 +49,8 @@ struct register_descriptor_iterator_object {
extern PyTypeObject register_descriptor_iterator_object_type;
/* A register descriptor. */
-struct register_descriptor_object {
- PyObject_HEAD
-
+struct register_descriptor_object: public PyObject
+{
/* The register this is a descriptor for. */
int regnum;
@@ -75,9 +74,8 @@ struct reggroup_iterator_object {
extern PyTypeObject reggroup_iterator_object_type;
/* A register group object. */
-struct reggroup_object {
- PyObject_HEAD
-
+struct reggroup_object: public PyObject
+{
/* The register group being described. */
const struct reggroup *reggroup;
};
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 578ddfbcc67..be19193770f 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -44,10 +44,8 @@ class tui_py_window;
/* A PyObject representing a TUI window. */
-struct gdbpy_tui_window
+struct gdbpy_tui_window: public PyObject
{
- PyObject_HEAD
-
/* The TUI window, or nullptr if the window has been deleted. */
tui_py_window *window;
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index cde2dfa2d52..1b4dcd4c5fe 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -350,10 +350,8 @@ extern PyTypeObject thread_object_type;
extern bool gdbpy_breakpoint_init_breakpoint_type ();
-struct gdbpy_breakpoint_object
+struct gdbpy_breakpoint_object: public PyObject
{
- PyObject_HEAD
-
/* The breakpoint number according to gdb. */
int number;
--
2.52.0
next prev parent reply other threads:[~2026-01-27 17:05 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 ` [PATCH v2 3/6] gdbpy_registry: cast C extension type object to PyObject * before Py_XINCREF Matthieu Longo
2026-01-27 18:01 ` 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 ` Matthieu Longo [this message]
2026-01-27 18:28 ` [PATCH v2 5/6] gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject* 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-6-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