binutils-gdb/gdb/python/py-events.h
Matthieu Longo 1fe7c3b749 gdb: cast all Python extension objects passed to gdbpy_ref_policy to PyObject*
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
Approved-By: Tom Tromey <tom@tromey.com>
2026-01-29 16:46:14 +00:00

55 lines
1.6 KiB
C

/* Python interface to inferior events.
Copyright (C) 2009-2026 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDB_PYTHON_PY_EVENTS_H
#define GDB_PYTHON_PY_EVENTS_H
#include "command.h"
#include "python-internal.h"
#include "inferior.h"
/* Stores a list of objects to be notified when the event for which this
registry tracks occurs. */
struct eventregistry_object : public PyObject
{
PyObject *callbacks;
};
/* Struct holding references to event registries both in python and c.
This is meant to be a singleton. */
struct events_object
{
#define GDB_PY_DEFINE_EVENT(name) \
eventregistry_object *name;
#include "py-all-events.def"
#undef GDB_PY_DEFINE_EVENT
PyObject *mod;
};
/* Python events singleton. */
extern events_object gdb_py_events;
extern eventregistry_object *create_eventregistry_object (void);
extern bool evregpy_no_listeners_p (eventregistry_object *registry);
#endif /* GDB_PYTHON_PY_EVENTS_H */