* [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute
@ 2026-09-14 19:19 Simon Marchi
2026-09-14 19:19 ` [PATCH 2/4] gdb/python: collapse some nested ifs Simon Marchi
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:19 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
Found by Claude while I was reviewing some patches: gdbpy_borrowed_ref's
constructor asserts that the object is not nullptr. Some call sites can
in theory pass nullptr (although very unlikely, only on memory
allocation failure kind of errors), fix them.
Change-Id: I75141ce7a4d6392eaffaf3e4cfd06a6e84c1e81f
---
gdb/python/py-connection.c | 3 ++-
gdb/python/py-tui.c | 3 ++-
gdb/python/python.c | 3 ++-
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index d2085960be4d..e2769871d3d7 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -143,7 +143,8 @@ emit_connection_event (process_stratum_target *target,
return -1;
gdbpy_ref<> conn = target_to_connection_object (target);
- if (evpy_add_attribute (event_obj, "connection", conn) < 0)
+ if (conn == nullptr
+ || evpy_add_attribute (event_obj, "connection", conn) < 0)
return -1;
return evpy_emit_event (event_obj, registry);
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 465fbd2fe6d1..7213158cdbe3 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -605,7 +605,8 @@ gdbpy_tui_enabled (bool state)
}
gdbpy_ref<> code (PyBool_FromLong (state));
- if (evpy_add_attribute (event_obj, "enabled", code) < 0
+ if (code == nullptr
+ || evpy_add_attribute (event_obj, "enabled", code) < 0
|| evpy_emit_event (event_obj, gdb_py_events.tui_enabled) < 0)
gdbpy_print_stack ();
}
diff --git a/gdb/python/python.c b/gdb/python/python.c
index df9f7c690064..01f4aa275ef9 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2500,7 +2500,8 @@ emit_exiting_event (int exit_code)
return -1;
gdbpy_ref<> code = gdb_py_object_from_longest (exit_code);
- if (evpy_add_attribute (event_obj, "exit_code", code) < 0)
+ if (code == nullptr
+ || evpy_add_attribute (event_obj, "exit_code", code) < 0)
return -1;
return evpy_emit_event (event_obj, gdb_py_events.gdb_exiting);
base-commit: 45e21c702fb0c4e273398b24a26096fd43dc512d
--
2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 2/4] gdb/python: collapse some nested ifs
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
@ 2026-09-14 19:19 ` Simon Marchi
2026-09-14 19:35 ` Tom Tromey
2026-09-14 19:19 ` [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p Simon Marchi
` (2 subsequent siblings)
3 siblings, 1 reply; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:19 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
While reviewing another patch in gdb/python, I found that some nested
"ifs" could be collapsed into one. I had Claude hunt for more cases
like that throughout gdb/python, and this patch changes them.
Change-Id: I7cc50a08ec18d85106c2dfebca803a1e354a6582
---
gdb/python/py-breakpoint.c | 31 +++++++++---------------
gdb/python/py-connection.c | 6 ++---
gdb/python/py-framefilter.c | 48 ++++++++++++++++---------------------
gdb/python/py-function.c | 14 ++++-------
gdb/python/py-param.c | 10 ++++----
gdb/python/py-prettyprint.c | 18 +++++++-------
gdb/python/py-progspace.c | 8 +++----
gdb/python/python.c | 12 ++++------
8 files changed, 59 insertions(+), 88 deletions(-)
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 2d8076ad2a73..2db756111d68 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1263,11 +1263,9 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
gdbpy_print_stack ();
}
- if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
- {
- if (evpy_emit_event (newbp, gdb_py_events.breakpoint_created) < 0)
- gdbpy_print_stack ();
- }
+ if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created)
+ && evpy_emit_event (newbp, gdb_py_events.breakpoint_created) < 0)
+ gdbpy_print_stack ();
}
/* Callback that is used when a breakpoint is deleted. This will
@@ -1291,12 +1289,10 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
if (bp_obj->is_finish_bp)
bpfinishpy_pre_delete_hook (bp_obj.get ());
- if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
- {
- if (evpy_emit_event (bp_obj,
- gdb_py_events.breakpoint_deleted) < 0)
- gdbpy_print_stack ();
- }
+ if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)
+ && evpy_emit_event (bp_obj,
+ gdb_py_events.breakpoint_deleted) < 0)
+ gdbpy_print_stack ();
bp_obj->bp = NULL;
--bppy_live;
@@ -1320,15 +1316,10 @@ gdbpy_breakpoint_modified (struct breakpoint *b)
gdbpy_enter enter_py (b->gdbarch);
PyObject *bp_obj = bp->py_bp_object;
- if (bp_obj)
- {
- if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
- {
- if (evpy_emit_event (bp_obj,
- gdb_py_events.breakpoint_modified) < 0)
- gdbpy_print_stack ();
- }
- }
+ if (bp_obj != nullptr
+ && !evregpy_no_listeners_p (gdb_py_events.breakpoint_modified)
+ && evpy_emit_event (bp_obj, gdb_py_events.breakpoint_modified) < 0)
+ gdbpy_print_stack ();
}
}
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index e2769871d3d7..8f476d4461d9 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -160,9 +160,9 @@ connpy_connection_removed (process_stratum_target *target)
gdbpy_enter enter_py;
- if (!evregpy_no_listeners_p (gdb_py_events.connection_removed))
- if (emit_connection_event (target, gdb_py_events.connection_removed) < 0)
- gdbpy_print_stack ();
+ if (!evregpy_no_listeners_p (gdb_py_events.connection_removed)
+ && emit_connection_event (target, gdb_py_events.connection_removed) < 0)
+ gdbpy_print_stack ();
auto conn_obj_iter = all_connection_objects.find (target);
if (conn_obj_iter != all_connection_objects.end ())
diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c
index 2777b9440974..c3eee22653a1 100644
--- a/gdb/python/py-framefilter.c
+++ b/gdb/python/py-framefilter.c
@@ -329,11 +329,9 @@ py_print_single_arg (struct ui_out *out,
if the value is a frame argument. This is denoted in this
function with PRINT_ARGS_FIELD which is flag from the caller to
emit the ARGS field. */
- if (out->is_mi_like_p ())
- {
- if (print_args_field || args_type != NO_VALUES)
- maybe_tuple.emplace (out, nullptr);
- }
+ if (out->is_mi_like_p ()
+ && (print_args_field || args_type != NO_VALUES))
+ maybe_tuple.emplace (out, nullptr);
annotate_arg_begin ();
@@ -585,11 +583,9 @@ enumerate_locals (PyObject *iter,
/* With PRINT_NO_VALUES, MI does not emit a tuple normally as
each output contains only one field. The exception is
-stack-list-variables, which always provides a tuple. */
- if (out->is_mi_like_p ())
- {
- if (print_args_field || args_type != NO_VALUES)
- tuple.emplace (out, nullptr);
- }
+ if (out->is_mi_like_p ()
+ && (print_args_field || args_type != NO_VALUES))
+ tuple.emplace (out, nullptr);
/* If the output is not MI we indent locals. */
out->spaces (local_indent);
@@ -886,19 +882,17 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
{
/* Print address to the address field. If an address is not provided,
print nothing. */
- if (opts.addressprint && has_addr)
- {
- if (!sal.symtab
+ if (opts.addressprint && has_addr
+ && (!sal.symtab
|| frame_show_address (frame, sal)
- || print_what == LOC_AND_ADDRESS)
- {
- annotate_frame_address ();
- out->field_core_addr ("addr", gdbarch, address);
- if (get_frame_pc_masked (frame))
- out->field_string ("pac", " [PAC]");
- annotate_frame_address_end ();
- out->text (" in ");
- }
+ || print_what == LOC_AND_ADDRESS))
+ {
+ annotate_frame_address ();
+ out->field_core_addr ("addr", gdbarch, address);
+ if (get_frame_pc_masked (frame))
+ out->field_string ("pac", " [PAC]");
+ annotate_frame_address_end ();
+ out->text (" in ");
}
/* Print frame function name. */
@@ -1034,12 +1028,10 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
out->text ("\n");
}
- if (print_locals)
- {
- if (py_print_locals (filter, out, args_type, indent,
- frame) == EXT_LANG_BT_ERROR)
- return EXT_LANG_BT_ERROR;
- }
+ if (print_locals
+ && py_print_locals (filter, out, args_type, indent,
+ frame) == EXT_LANG_BT_ERROR)
+ return EXT_LANG_BT_ERROR;
if ((flags & PRINT_HIDE) == 0)
{
diff --git a/gdb/python/py-function.c b/gdb/python/py-function.c
index 23e0be0ea432..1e8be6658216 100644
--- a/gdb/python/py-function.c
+++ b/gdb/python/py-function.c
@@ -113,16 +113,12 @@ fnpy_init (PyObject *self, PyObject *args, PyObject *kwds)
if (PyObject_HasAttrString (self, "__doc__"))
{
gdbpy_ref<> ds_obj (PyObject_GetAttrString (self, "__doc__"));
- if (ds_obj != NULL)
+ if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
{
- if (gdbpy_is_string (ds_obj.get ()))
- {
- docstring = python_string_to_host_string (ds_obj.get ());
- if (docstring == NULL)
- return -1;
- docstring
- = gdbpy_fix_doc_string_indentation (std::move (docstring));
- }
+ docstring = python_string_to_host_string (ds_obj.get ());
+ if (docstring == NULL)
+ return -1;
+ docstring = gdbpy_fix_doc_string_indentation (std::move (docstring));
}
}
if (! docstring)
diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c
index c5f7ff57e1a8..3cc55e49157f 100644
--- a/gdb/python/py-param.c
+++ b/gdb/python/py-param.c
@@ -376,15 +376,13 @@ set_parameter_value (parmpy_object *self, PyObject *value)
}
}
- if (allowed == TRIBOOL_UNKNOWN)
- {
- if (val > UINT_MAX || val < INT_MIN
+ if (allowed == TRIBOOL_UNKNOWN
+ && (val > UINT_MAX || val < INT_MIN
|| (var_type == var_uinteger && val < 0)
|| (var_type == var_integer && val > INT_MAX)
|| (var_type == var_pinteger && val < 0)
- || (var_type == var_pinteger && val > INT_MAX))
- allowed = TRIBOOL_FALSE;
- }
+ || (var_type == var_pinteger && val > INT_MAX)))
+ allowed = TRIBOOL_FALSE;
if (allowed == TRIBOOL_FALSE)
{
PyErr_SetString (PyExc_RuntimeError,
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index ce6ed699f3e5..c81db67e93ff 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -204,17 +204,15 @@ pretty_print_one_value (PyObject *printer, struct value **out_value)
{
result.reset (PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst,
NULL));
- if (result != NULL)
+ if (result != NULL
+ && !gdbpy_is_string (result.get ())
+ && !gdbpy_is_lazy_string (result.get ())
+ && result != Py_None)
{
- if (! gdbpy_is_string (result.get ())
- && ! gdbpy_is_lazy_string (result.get ())
- && result != Py_None)
- {
- *out_value = convert_value_from_python (result.get ());
- if (PyErr_Occurred ())
- *out_value = NULL;
- result = NULL;
- }
+ *out_value = convert_value_from_python (result.get ());
+ if (PyErr_Occurred ())
+ *out_value = NULL;
+ result = NULL;
}
}
}
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index de6bfc653462..b27950c172c4 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -662,10 +662,10 @@ gdbpy_executable_changed (struct program_space *pspace, bool reload_p)
gdbpy_enter enter_py;
- if (!evregpy_no_listeners_p (gdb_py_events.executable_changed))
- if (emit_executable_changed_event (gdb_py_events.executable_changed,
- pspace, reload_p) < 0)
- gdbpy_print_stack ();
+ if (!evregpy_no_listeners_p (gdb_py_events.executable_changed)
+ && emit_executable_changed_event (gdb_py_events.executable_changed,
+ pspace, reload_p) < 0)
+ gdbpy_print_stack ();
}
/* Helper function to emit NewProgspaceEvent (when ADDING_P is true) or
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 01f4aa275ef9..08ee471000cb 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -905,11 +905,8 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
for (const symbol_search &p : symbols)
{
/* Minimal symbols included? */
- if (minsyms_p)
- {
- if (p.msymbol.minsym != NULL)
- count++;
- }
+ if (minsyms_p && p.msymbol.minsym != NULL)
+ count++;
if (p.symbol != NULL)
count++;
@@ -936,9 +933,8 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw)
std::string symbol_name;
/* Skipping minimal symbols? */
- if (minsyms_p == 0)
- if (p.msymbol.minsym != NULL)
- continue;
+ if (minsyms_p == 0 && p.msymbol.minsym != NULL)
+ continue;
if (p.msymbol.minsym == NULL)
{
--
2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
2026-09-14 19:19 ` [PATCH 2/4] gdb/python: collapse some nested ifs Simon Marchi
@ 2026-09-14 19:19 ` Simon Marchi
2026-09-14 19:38 ` Tom Tromey
2026-09-14 19:19 ` [PATCH 4/4] gdb/python: remove more useless casts Simon Marchi
2026-09-14 19:24 ` [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Tom Tromey
3 siblings, 1 reply; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:19 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
Most call sites of evregpy_no_listeners_p do:
!evregpy_no_listeners_p (...)
Rename the function
to evregpy_has_listeners_p, to avoid the double negative.
Of course, other spots have to change from
evregpy_no_listeners_p (...)
to
!evregpy_has_listeners_p (...)
... but I think it's clearer than the other way around.
While at it, fix the function comment, which described the function as
returning a number of listeners, and move it to the header file.
Change-Id: I07c8c3fc8b5d246559d4fafdcd2ad94957475b34
---
gdb/python/py-breakpoint.c | 6 +++---
gdb/python/py-connection.c | 2 +-
gdb/python/py-continueevent.c | 2 +-
gdb/python/py-corefile.c | 2 +-
gdb/python/py-events.h | 5 ++++-
gdb/python/py-evtregistry.c | 7 +++----
gdb/python/py-exitedevent.c | 2 +-
gdb/python/py-inferior.c | 8 ++++----
gdb/python/py-infevents.c | 6 +++---
gdb/python/py-newobjfileevent.c | 6 +++---
gdb/python/py-progspace.c | 4 ++--
gdb/python/py-stopevent.c | 2 +-
gdb/python/py-threadevent.c | 2 +-
gdb/python/py-tui.c | 2 +-
gdb/python/python.c | 4 ++--
15 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 2db756111d68..02fd00b1008a 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -1263,7 +1263,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
gdbpy_print_stack ();
}
- if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created)
+ if (evregpy_has_listeners_p (gdb_py_events.breakpoint_created)
&& evpy_emit_event (newbp, gdb_py_events.breakpoint_created) < 0)
gdbpy_print_stack ();
}
@@ -1289,7 +1289,7 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
if (bp_obj->is_finish_bp)
bpfinishpy_pre_delete_hook (bp_obj.get ());
- if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)
+ if (evregpy_has_listeners_p (gdb_py_events.breakpoint_deleted)
&& evpy_emit_event (bp_obj,
gdb_py_events.breakpoint_deleted) < 0)
gdbpy_print_stack ();
@@ -1317,7 +1317,7 @@ gdbpy_breakpoint_modified (struct breakpoint *b)
PyObject *bp_obj = bp->py_bp_object;
if (bp_obj != nullptr
- && !evregpy_no_listeners_p (gdb_py_events.breakpoint_modified)
+ && evregpy_has_listeners_p (gdb_py_events.breakpoint_modified)
&& evpy_emit_event (bp_obj, gdb_py_events.breakpoint_modified) < 0)
gdbpy_print_stack ();
}
diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c
index 8f476d4461d9..c382e2ee2bcb 100644
--- a/gdb/python/py-connection.c
+++ b/gdb/python/py-connection.c
@@ -160,7 +160,7 @@ connpy_connection_removed (process_stratum_target *target)
gdbpy_enter enter_py;
- if (!evregpy_no_listeners_p (gdb_py_events.connection_removed)
+ if (evregpy_has_listeners_p (gdb_py_events.connection_removed)
&& emit_connection_event (target, gdb_py_events.connection_removed) < 0)
gdbpy_print_stack ();
diff --git a/gdb/python/py-continueevent.c b/gdb/python/py-continueevent.c
index 48c24cc710d5..408e48eef595 100644
--- a/gdb/python/py-continueevent.c
+++ b/gdb/python/py-continueevent.c
@@ -46,7 +46,7 @@ create_continue_event_object (ptid_t ptid)
int
emit_continue_event (ptid_t ptid)
{
- if (evregpy_no_listeners_p (gdb_py_events.cont))
+ if (!evregpy_has_listeners_p (gdb_py_events.cont))
return 0;
gdbpy_ref<> event = create_continue_event_object (ptid);
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 58eac984feb8..e19ad80fabd8 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -332,7 +332,7 @@ static int
emit_corefile_changed_event (inferior *inf)
{
/* If there are no listeners then we are done. */
- if (evregpy_no_listeners_p (gdb_py_events.corefile_changed))
+ if (!evregpy_has_listeners_p (gdb_py_events.corefile_changed))
return 0;
gdbpy_ref<> event_obj
diff --git a/gdb/python/py-events.h b/gdb/python/py-events.h
index 169a5f4a4dbe..066ba63c3933 100644
--- a/gdb/python/py-events.h
+++ b/gdb/python/py-events.h
@@ -52,6 +52,9 @@ struct events_object
extern events_object gdb_py_events;
extern eventregistry_object *create_eventregistry_object (void);
-extern bool evregpy_no_listeners_p (eventregistry_object *registry);
+
+/* Return true if at least one listener is connected to REGISTRY. */
+
+extern bool evregpy_has_listeners_p (eventregistry_object *registry);
#endif /* GDB_PYTHON_PY_EVENTS_H */
diff --git a/gdb/python/py-evtregistry.c b/gdb/python/py-evtregistry.c
index 8d2a68da2a4c..018f4aa04d61 100644
--- a/gdb/python/py-evtregistry.c
+++ b/gdb/python/py-evtregistry.c
@@ -106,15 +106,14 @@ gdbpy_initialize_eventregistry ()
return gdbpy_type_ready (&eventregistry_object_type);
}
-/* Return the number of listeners currently connected to this
- registry. */
+/* See py-events.h. */
bool
-evregpy_no_listeners_p (eventregistry_object *registry)
+evregpy_has_listeners_p (eventregistry_object *registry)
{
/* REGISTRY can be nullptr if gdb failed to find the data directory
at startup. */
- return registry == nullptr || PyList_Size (registry->callbacks) == 0;
+ return registry != nullptr && PyList_Size (registry->callbacks) > 0;
}
GDBPY_INITIALIZE_FILE (gdbpy_initialize_eventregistry);
diff --git a/gdb/python/py-exitedevent.c b/gdb/python/py-exitedevent.c
index 83ccebb91275..b9ede1e701d1 100644
--- a/gdb/python/py-exitedevent.c
+++ b/gdb/python/py-exitedevent.c
@@ -51,7 +51,7 @@ create_exited_event_object (const LONGEST *exit_code, struct inferior *inf)
int
emit_exited_event (const LONGEST *exit_code, struct inferior *inf)
{
- if (evregpy_no_listeners_p (gdb_py_events.exited))
+ if (!evregpy_has_listeners_p (gdb_py_events.exited))
return 0;
gdbpy_ref<> event = create_exited_event_object (exit_code, inf);
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 4386489a8e7f..4c32a99e3ce7 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -243,7 +243,7 @@ python_new_inferior (struct inferior *inf)
gdbpy_enter enter_py;
- if (evregpy_no_listeners_p (gdb_py_events.new_inferior))
+ if (!evregpy_has_listeners_p (gdb_py_events.new_inferior))
return;
gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
@@ -270,7 +270,7 @@ python_inferior_deleted (struct inferior *inf)
gdbpy_enter enter_py;
- if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted))
+ if (!evregpy_has_listeners_p (gdb_py_events.inferior_deleted))
return;
gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
@@ -328,7 +328,7 @@ add_thread_object (struct thread_info *tp)
if (!ins_result.second)
return;
- if (evregpy_no_listeners_p (gdb_py_events.new_thread))
+ if (!evregpy_has_listeners_p (gdb_py_events.new_thread))
return;
gdbpy_ref<> event = create_thread_event_object
@@ -1000,7 +1000,7 @@ python_context_changed (user_selected_what selection)
gdbpy_enter enter_py (current_inferior ()->arch ());
- if (evregpy_no_listeners_p (gdb_py_events.selected_context))
+ if (!evregpy_has_listeners_p (gdb_py_events.selected_context))
return;
gdbpy_ref<> inf_obj (gdbpy_selected_inferior (nullptr, nullptr));
diff --git a/gdb/python/py-infevents.c b/gdb/python/py-infevents.c
index b472c809197c..efdf3b284e68 100644
--- a/gdb/python/py-infevents.c
+++ b/gdb/python/py-infevents.c
@@ -122,7 +122,7 @@ int
emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
CORE_ADDR addr)
{
- if (evregpy_no_listeners_p (gdb_py_events.inferior_call))
+ if (!evregpy_has_listeners_p (gdb_py_events.inferior_call))
return 0;
gdbpy_ref<> event = create_inferior_call_event_object (flag, thread, addr);
@@ -137,7 +137,7 @@ emit_inferior_call_event (inferior_call_kind flag, ptid_t thread,
int
emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
{
- if (evregpy_no_listeners_p (gdb_py_events.memory_changed))
+ if (!evregpy_has_listeners_p (gdb_py_events.memory_changed))
return 0;
gdbpy_ref<> event = create_memory_changed_event_object (addr, len);
@@ -152,7 +152,7 @@ emit_memory_changed_event (CORE_ADDR addr, ssize_t len)
int
emit_register_changed_event (const frame_info_ptr &frame, int regnum)
{
- if (evregpy_no_listeners_p (gdb_py_events.register_changed))
+ if (!evregpy_has_listeners_p (gdb_py_events.register_changed))
return 0;
gdbpy_ref<> event = create_register_changed_event_object (frame, regnum);
diff --git a/gdb/python/py-newobjfileevent.c b/gdb/python/py-newobjfileevent.c
index cac49b2a39c4..d108cc347f12 100644
--- a/gdb/python/py-newobjfileevent.c
+++ b/gdb/python/py-newobjfileevent.c
@@ -42,7 +42,7 @@ create_new_objfile_event_object (struct objfile *objfile)
int
emit_new_objfile_event (struct objfile *objfile)
{
- if (evregpy_no_listeners_p (gdb_py_events.new_objfile))
+ if (!evregpy_has_listeners_p (gdb_py_events.new_objfile))
return 0;
gdbpy_ref<> event = create_new_objfile_event_object (objfile);
@@ -77,7 +77,7 @@ create_free_objfile_event_object (struct objfile *objfile)
int
emit_free_objfile_event (struct objfile *objfile)
{
- if (evregpy_no_listeners_p (gdb_py_events.free_objfile))
+ if (!evregpy_has_listeners_p (gdb_py_events.free_objfile))
return 0;
gdbpy_ref<> event = create_free_objfile_event_object (objfile);
@@ -113,7 +113,7 @@ create_clear_objfiles_event_object (program_space *pspace)
int
emit_clear_objfiles_event (program_space *pspace)
{
- if (evregpy_no_listeners_p (gdb_py_events.clear_objfiles))
+ if (!evregpy_has_listeners_p (gdb_py_events.clear_objfiles))
return 0;
gdbpy_ref<> event = create_clear_objfiles_event_object (pspace);
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index b27950c172c4..020b60cc2637 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -662,7 +662,7 @@ gdbpy_executable_changed (struct program_space *pspace, bool reload_p)
gdbpy_enter enter_py;
- if (!evregpy_no_listeners_p (gdb_py_events.executable_changed)
+ if (evregpy_has_listeners_p (gdb_py_events.executable_changed)
&& emit_executable_changed_event (gdb_py_events.executable_changed,
pspace, reload_p) < 0)
gdbpy_print_stack ();
@@ -692,7 +692,7 @@ gdbpy_program_space_event (program_space *pspace, bool adding_p)
event_type = &free_progspace_event_object_type;
}
- if (evregpy_no_listeners_p (registry))
+ if (!evregpy_has_listeners_p (registry))
return;
gdbpy_ref<> pspace_obj = pspace_to_pspace_object (pspace);
diff --git a/gdb/python/py-stopevent.c b/gdb/python/py-stopevent.c
index 01c95b66e9d0..2b7aab587bd3 100644
--- a/gdb/python/py-stopevent.c
+++ b/gdb/python/py-stopevent.c
@@ -110,7 +110,7 @@ emit_stop_event (struct bpstat *bs, enum gdb_signal stop_signal)
PyObject *first_bp = NULL;
struct bpstat *current_bs;
- if (evregpy_no_listeners_p (gdb_py_events.stop))
+ if (!evregpy_has_listeners_p (gdb_py_events.stop))
return 0;
gdbpy_ref<> dict = py_print_bpstat (bs, stop_signal);
diff --git a/gdb/python/py-threadevent.c b/gdb/python/py-threadevent.c
index 35caba7a5aab..f303d40a6f05 100644
--- a/gdb/python/py-threadevent.c
+++ b/gdb/python/py-threadevent.c
@@ -54,7 +54,7 @@ create_thread_event_object (PyTypeObject *py_type, gdbpy_borrowed_ref<> thread)
int
emit_thread_exit_event (thread_info * thread)
{
- if (evregpy_no_listeners_p (gdb_py_events.thread_exited))
+ if (!evregpy_has_listeners_p (gdb_py_events.thread_exited))
return 0;
auto py_thr = thread_to_thread_object (thread);
diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 7213158cdbe3..3c2d035bba51 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -594,7 +594,7 @@ gdbpy_tui_enabled (bool state)
{
gdbpy_enter enter_py;
- if (evregpy_no_listeners_p (gdb_py_events.tui_enabled))
+ if (!evregpy_has_listeners_p (gdb_py_events.tui_enabled))
return;
gdbpy_ref<> event_obj = create_event_object (&tui_enabled_event_object_type);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 08ee471000cb..ede3e98571d3 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1233,7 +1233,7 @@ gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
gdbpy_enter enter_py;
- if (!evregpy_no_listeners_p (gdb_py_events.before_prompt)
+ if (evregpy_has_listeners_p (gdb_py_events.before_prompt)
&& evpy_emit_event (NULL, gdb_py_events.before_prompt) < 0)
return EXT_LANG_RC_ERROR;
@@ -2488,7 +2488,7 @@ init__gdb_module (void)
static int
emit_exiting_event (int exit_code)
{
- if (evregpy_no_listeners_p (gdb_py_events.gdb_exiting))
+ if (!evregpy_has_listeners_p (gdb_py_events.gdb_exiting))
return 0;
gdbpy_ref<> event_obj = create_event_object (&gdb_exiting_event_object_type);
--
2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 4/4] gdb/python: remove more useless casts
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
2026-09-14 19:19 ` [PATCH 2/4] gdb/python: collapse some nested ifs Simon Marchi
2026-09-14 19:19 ` [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p Simon Marchi
@ 2026-09-14 19:19 ` Simon Marchi
2026-09-14 19:39 ` Tom Tromey
2026-09-14 19:24 ` [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Tom Tromey
3 siblings, 1 reply; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:19 UTC (permalink / raw)
To: gdb-patches; +Cc: Simon Marchi
Remove a few (more) casts made unnecessary now that the GDB Python
objects inherit from PyObject. A bunch of them were removed already by
commit 45e21c702fb ("Remove unneeded casts to PyObject*").
In one spot, I remove some now unnecessary parentheses where a cast was
removed by that previous patch.
Change-Id: I04e09497560b484c82d2bf29bdde34d3ffc31aec
---
gdb/python/py-disasm.c | 5 ++---
gdb/python/py-finishbreakpoint.c | 6 ++----
gdb/python/py-inferior.c | 2 +-
gdb/python/py-objfile.c | 2 +-
gdb/python/python-internal.h | 2 +-
5 files changed, 7 insertions(+), 10 deletions(-)
diff --git a/gdb/python/py-disasm.c b/gdb/python/py-disasm.c
index 48a2a4f6f8b8..c9dfd1f4571a 100644
--- a/gdb/python/py-disasm.c
+++ b/gdb/python/py-disasm.c
@@ -608,7 +608,7 @@ disasmpy_builtin_disassemble (PyObject *self, PyObject *args, PyObject *kw)
((disasm_result_object *) type->tp_alloc (type, 0));
auto content = disassembler.release ();
disasmpy_init_disassembler_result (res.get (), length, std::move (content));
- return reinterpret_cast<PyObject *> (res.release ());
+ return res.release ();
}
/* Implement gdb._set_enabled function. Takes a boolean parameter, and
@@ -1215,8 +1215,7 @@ gdbpy_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
/* Create the new DisassembleInfo object we will pass into Python. */
gdbpy_ref<disasm_info_object> disasm_info
- ((disasm_info_object *) PyObject_New (disasm_info_object,
- &disasm_info_object_type));
+ (PyObject_New (disasm_info_object, &disasm_info_object_type));
if (disasm_info == nullptr)
{
gdbpy_print_stack ();
diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpoint.c
index ec7399af4842..e81a9156ae6d 100644
--- a/gdb/python/py-finishbreakpoint.c
+++ b/gdb/python/py-finishbreakpoint.c
@@ -363,14 +363,12 @@ bpfinishpy_detect_out_scope_cb (struct breakpoint *b,
struct breakpoint *bp_stopped,
bool delete_bp)
{
- PyObject *py_bp = b->py_bp_object;
-
/* Trigger out_of_scope if this is a FinishBreakpoint and its frame is
not anymore in the current callstack. */
- if (py_bp != NULL && b->py_bp_object->is_finish_bp)
+ if (b->py_bp_object != nullptr && b->py_bp_object->is_finish_bp)
{
struct finish_breakpoint_object *finish_bp =
- (struct finish_breakpoint_object *) py_bp;
+ (struct finish_breakpoint_object *) b->py_bp_object;
/* Check scope if not currently stopped at the FinishBreakpoint. */
if (b != bp_stopped)
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 4c32a99e3ce7..82c0717d68f5 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -984,7 +984,7 @@ infpy_dealloc (PyObject *obj)
PyObject *
gdbpy_selected_inferior (PyObject *self, PyObject *args)
{
- return (inferior_to_inferior_object (current_inferior ()).release ());
+ return inferior_to_inferior_object (current_inferior ()).release ();
}
/* Implement the selected_context event handler. This is called when some
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index dcd326b696cf..414965c89aed 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -674,7 +674,7 @@ objfile_to_objfile_object (struct objfile *objfile)
if (result == NULL)
{
gdbpy_ref<objfile_object> object
- ((objfile_object *) PyObject_New (objfile_object, &objfile_object_type));
+ (PyObject_New (objfile_object, &objfile_object_type));
if (object == NULL)
return NULL;
if (!objfpy_initialize (object))
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 2e8f35729cd6..acec6b346eff 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1214,7 +1214,7 @@ class gdbpy_registry
gdbpy_ref<> lookup (O *owner, val_type *val) const
{
obj_type *obj = get_storage (owner)->lookup (val);
- Py_XINCREF (static_cast<PyObject *> (obj));
+ Py_XINCREF (obj);
return gdbpy_ref<> (obj);
}
--
2.55.0
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
` (2 preceding siblings ...)
2026-09-14 19:19 ` [PATCH 4/4] gdb/python: remove more useless casts Simon Marchi
@ 2026-09-14 19:24 ` Tom Tromey
2026-09-14 19:43 ` Simon Marchi
3 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-09-14 19:24 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> Found by Claude while I was reviewing some patches: gdbpy_borrowed_ref's
Simon> constructor asserts that the object is not nullptr. Some call sites can
Simon> in theory pass nullptr (although very unlikely, only on memory
Simon> allocation failure kind of errors), fix them.
It's an oversight that these weren't already checked, and probably they
are all latent bugs.
This is the kind of thing the safety API prevents, but (1) that's mostly
on hold pending Longo's work; and (2) we aren't really ready to switch
over the event stuff fully anyway (or at least for me it's one of the
last things to do).
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 2/4] gdb/python: collapse some nested ifs
2026-09-14 19:19 ` [PATCH 2/4] gdb/python: collapse some nested ifs Simon Marchi
@ 2026-09-14 19:35 ` Tom Tromey
2026-09-14 19:46 ` Simon Marchi
0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-09-14 19:35 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> While reviewing another patch in gdb/python, I found that some nested
Simon> "ifs" could be collapsed into one. I had Claude hunt for more cases
Simon> like that throughout gdb/python, and this patch changes them.
Simon> - if (ds_obj != NULL)
Simon> + if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
nullptr
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p
2026-09-14 19:19 ` [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p Simon Marchi
@ 2026-09-14 19:38 ` Tom Tromey
0 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-09-14 19:38 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> !evregpy_has_listeners_p (...)
Simon> ... but I think it's clearer than the other way around.
Yeah.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 4/4] gdb/python: remove more useless casts
2026-09-14 19:19 ` [PATCH 4/4] gdb/python: remove more useless casts Simon Marchi
@ 2026-09-14 19:39 ` Tom Tromey
2026-09-14 19:47 ` Simon Marchi
0 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-09-14 19:39 UTC (permalink / raw)
To: Simon Marchi; +Cc: gdb-patches
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
Simon> Remove a few (more) casts made unnecessary now that the GDB Python
Simon> objects inherit from PyObject. A bunch of them were removed already by
Simon> commit 45e21c702fb ("Remove unneeded casts to PyObject*").
Simon> In one spot, I remove some now unnecessary parentheses where a cast was
Simon> removed by that previous patch.
Thanks.
Approved-By: Tom Tromey <tom@tromey.com>
Tom
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute
2026-09-14 19:24 ` [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Tom Tromey
@ 2026-09-14 19:43 ` Simon Marchi
0 siblings, 0 replies; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:43 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On 9/14/26 3:24 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> Found by Claude while I was reviewing some patches: gdbpy_borrowed_ref's
> Simon> constructor asserts that the object is not nullptr. Some call sites can
> Simon> in theory pass nullptr (although very unlikely, only on memory
> Simon> allocation failure kind of errors), fix them.
>
> It's an oversight that these weren't already checked, and probably they
> are all latent bugs.
>
> This is the kind of thing the safety API prevents, but (1) that's mostly
> on hold pending Longo's work; and (2) we aren't really ready to switch
> over the event stuff fully anyway (or at least for me it's one of the
> last things to do).
Ack. It's not super important, but it's also easy to fix, so why not.
Simon
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 2/4] gdb/python: collapse some nested ifs
2026-09-14 19:35 ` Tom Tromey
@ 2026-09-14 19:46 ` Simon Marchi
0 siblings, 0 replies; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:46 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On 9/14/26 3:35 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> While reviewing another patch in gdb/python, I found that some nested
> Simon> "ifs" could be collapsed into one. I had Claude hunt for more cases
> Simon> like that throughout gdb/python, and this patch changes them.
>
> Simon> - if (ds_obj != NULL)
> Simon> + if (ds_obj != NULL && gdbpy_is_string (ds_obj.get ()))
>
> nullptr
I fixed that and all the other "NULL" in the lines that I touched (there
was a handful).
Simon
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH 4/4] gdb/python: remove more useless casts
2026-09-14 19:39 ` Tom Tromey
@ 2026-09-14 19:47 ` Simon Marchi
0 siblings, 0 replies; 11+ messages in thread
From: Simon Marchi @ 2026-09-14 19:47 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On 9/14/26 3:39 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
>
> Simon> Remove a few (more) casts made unnecessary now that the GDB Python
> Simon> objects inherit from PyObject. A bunch of them were removed already by
> Simon> commit 45e21c702fb ("Remove unneeded casts to PyObject*").
>
> Simon> In one spot, I remove some now unnecessary parentheses where a cast was
> Simon> removed by that previous patch.
>
> Thanks.
> Approved-By: Tom Tromey <tom@tromey.com>
>
> Tom
Thanks, I pushed the series.
Simon
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-09-14 19:48 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-14 19:19 [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Simon Marchi
2026-09-14 19:19 ` [PATCH 2/4] gdb/python: collapse some nested ifs Simon Marchi
2026-09-14 19:35 ` Tom Tromey
2026-09-14 19:46 ` Simon Marchi
2026-09-14 19:19 ` [PATCH 3/4] gdb/python: rename evregpy_no_listeners_p to evregpy_has_listeners_p Simon Marchi
2026-09-14 19:38 ` Tom Tromey
2026-09-14 19:19 ` [PATCH 4/4] gdb/python: remove more useless casts Simon Marchi
2026-09-14 19:39 ` Tom Tromey
2026-09-14 19:47 ` Simon Marchi
2026-09-14 19:24 ` [PATCH 1/4] gdb/python: check for nullptr before calling evpy_add_attribute Tom Tromey
2026-09-14 19:43 ` Simon Marchi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox