From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 4/6] Some symbol-related Python safety fixes
Date: Fri, 23 Oct 2026 13:15:49 -0600 [thread overview]
Message-ID: <20261023-python-safety-fixmes-v1-4-055b9e1d237b@tromey.com> (raw)
In-Reply-To: <20261023-python-safety-fixmes-v1-0-055b9e1d237b@tromey.com>
This makes some symbol-related Python safety fixes, with a goal of
simplifying some code and removing some FIXMEs.
A method can now return a 'symbol *'; this will be handled by a new
to_python overload.
A safe wrapper is now provided for symbol_to_symbol_object.
---
gdb/python/py-frame.c | 9 +++----
gdb/python/py-safety.h | 10 ++++++++
gdb/python/py-symbol.c | 59 ++++++++++++--------------------------------
gdb/python/python-internal.h | 9 ++++---
4 files changed, 34 insertions(+), 53 deletions(-)
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 58b08c3d454..e4dd9e9252e 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -103,7 +103,7 @@ struct frame_object : public PyObject
/* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
Returns the symbol for the function corresponding to this frame. */
- gdbpy_ref<> function ();
+ symbol *function ();
/* Implementation of gdb.Frame.older (self) -> gdb.Frame.
Returns the frame immediately older (outer) to this frame, or None if
@@ -267,7 +267,7 @@ frame_object::block ()
}
-gdbpy_ref<>
+symbol *
frame_object::function ()
{
frame_info_ptr frame = require_frame ();
@@ -277,10 +277,7 @@ frame_object::function ()
gdb::unique_xmalloc_ptr<char> funname
= find_frame_funname (frame, &funlang, &sym);
- if (sym != nullptr)
- return symbol_to_symbol_object (sym);
-
- return py_none ();
+ return sym;
}
/* Convert a frame_info struct to a Python Frame object.
diff --git a/gdb/python/py-safety.h b/gdb/python/py-safety.h
index 0653947fe90..0d5a45e72fd 100644
--- a/gdb/python/py-safety.h
+++ b/gdb/python/py-safety.h
@@ -120,6 +120,16 @@ to_python (gdbpy_ref<> &&value)
return value.release ();
}
+static inline PyObject *
+to_python (symbol *sym)
+{
+ if (sym == nullptr)
+ return py_none ().release ();
+ /* Note that currently this might return NULL on error; but that is
+ fine in this context. */
+ return symbol_to_symbol_object (sym).release ();
+}
+
/* An instantiation of this function is used when calling a gdb method
from Python. It accepts some number of arguments (normally
gdbpy_borrowed_ref or gdbpy_opt_borrowed_ref), and then calls the
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index b5dcca5658d..0d4515643f5 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -289,6 +289,15 @@ symbol_to_symbol_object (struct symbol *sym)
return gdbpy_ref<> (sym_obj);
}
+gdbpy_ref<>
+safe_symbol_to_symbol_object (symbol *sym)
+{
+ gdbpy_ref<> result = symbol_to_symbol_object (sym);
+ if (result == nullptr)
+ throw gdb_python_exception ();
+ return result;
+}
+
/* Return the symbol that is wrapped by this symbol object. */
struct symbol *
symbol_object_to_symbol (PyObject *obj)
@@ -363,14 +372,8 @@ gdbpy_lookup_symbol (gdbpy_borrowed_ref<> args, gdbpy_opt_borrowed_ref<> kw)
gdbpy_ref<> ret_tuple = gdbpy_tuple_new (2);
gdbpy_ref<> sym_obj;
- if (symbol)
- {
- sym_obj = symbol_to_symbol_object (symbol);
- /* FIXME: Python safety. symbol_to_symbol_object should throw,
- but the other callers aren't ready for this yet. */
- if (sym_obj == nullptr)
- throw gdb_python_exception ();
- }
+ if (symbol != nullptr)
+ sym_obj = safe_symbol_to_symbol_object (symbol);
else
sym_obj = py_none ();
@@ -386,7 +389,7 @@ gdbpy_lookup_symbol (gdbpy_borrowed_ref<> args, gdbpy_opt_borrowed_ref<> kw)
/* Implementation of
gdb.lookup_global_symbol (name [, domain]) -> symbol or None. */
-gdbpy_ref<>
+symbol *
gdbpy_lookup_global_symbol (gdbpy_borrowed_ref<> args,
gdbpy_opt_borrowed_ref<> kw)
{
@@ -398,27 +401,13 @@ gdbpy_lookup_global_symbol (gdbpy_borrowed_ref<> args,
&domain);
domain_search_flags flags = from_scripting_domain (domain);
- struct symbol *symbol = lookup_global_symbol (name, NULL, flags).symbol;
-
- gdbpy_ref<> sym_obj;
- if (symbol != nullptr)
- {
- /* FIXME: Python safety. symbol_to_symbol_object should throw,
- but the other callers aren't ready for this yet. */
- sym_obj = symbol_to_symbol_object (symbol);
- if (sym_obj == nullptr)
- throw gdb_python_exception ();
- }
- else
- sym_obj = py_none ();
-
- return sym_obj;
+ return lookup_global_symbol (name, NULL, flags).symbol;
}
/* Implementation of
gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */
-gdbpy_ref<>
+symbol *
gdbpy_lookup_static_symbol (gdbpy_borrowed_ref<> args,
gdbpy_opt_borrowed_ref<> kw)
{
@@ -455,19 +444,7 @@ gdbpy_lookup_static_symbol (gdbpy_borrowed_ref<> args,
if (symbol == nullptr)
symbol = lookup_static_symbol (name, flags).symbol;
- gdbpy_ref<> sym_obj;
- if (symbol != nullptr)
- {
- /* FIXME: Python safety. symbol_to_symbol_object should throw,
- but the other callers aren't ready for this yet. */
- sym_obj = symbol_to_symbol_object (symbol);
- if (sym_obj == nullptr)
- throw gdb_python_exception ();
- }
- else
- sym_obj = py_none ();
-
- return sym_obj;
+ return symbol;
}
/* Implementation of
@@ -512,11 +489,7 @@ gdbpy_lookup_static_symbols (gdbpy_borrowed_ref<> args,
if (symbol != nullptr)
{
- /* FIXME: Python safety. symbol_to_symbol_object should throw,
- but the other callers aren't ready for this yet. */
- gdbpy_ref<> sym_obj = symbol_to_symbol_object (symbol);
- if (sym_obj == nullptr)
- throw gdb_python_exception ();
+ gdbpy_ref<> sym_obj = safe_symbol_to_symbol_object (symbol);
gdbpy_list_append (return_list, sym_obj);
}
}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index b57f3b0123d..5515820411b 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -534,10 +534,10 @@ const char *gdbpy_frame_stop_reason_string (gdbpy_borrowed_ref<> args,
gdbpy_opt_borrowed_ref<> kw);
gdbpy_ref<> gdbpy_lookup_symbol (gdbpy_borrowed_ref<> args,
gdbpy_opt_borrowed_ref<> kw);
-gdbpy_ref<> gdbpy_lookup_global_symbol (gdbpy_borrowed_ref<> args,
- gdbpy_opt_borrowed_ref<> kw);
-gdbpy_ref<> gdbpy_lookup_static_symbol (gdbpy_borrowed_ref<> args,
- gdbpy_opt_borrowed_ref<> kw);
+symbol *gdbpy_lookup_global_symbol (gdbpy_borrowed_ref<> args,
+ gdbpy_opt_borrowed_ref<> kw);
+symbol *gdbpy_lookup_static_symbol (gdbpy_borrowed_ref<> args,
+ gdbpy_opt_borrowed_ref<> kw);
gdbpy_ref<> gdbpy_lookup_static_symbols (gdbpy_borrowed_ref<> args,
gdbpy_opt_borrowed_ref<> kw);
PyObject *gdbpy_start_recording (PyObject *self, PyObject *args);
@@ -572,6 +572,7 @@ void gdbpy_register_tui_window (gdbpy_borrowed_ref<> args,
gdbpy_ref<> symtab_and_line_to_sal_object (struct symtab_and_line sal);
gdbpy_ref<> symtab_to_symtab_object (struct symtab *symtab);
gdbpy_ref<> symbol_to_symbol_object (struct symbol *sym);
+gdbpy_ref<> safe_symbol_to_symbol_object (struct symbol *sym);
gdbpy_ref<> block_to_block_object (const struct block *block,
struct objfile *objfile);
gdbpy_ref<> value_to_value_object (struct value *v);
--
2.49.0
next prev parent reply other threads:[~2026-10-23 19:17 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-23 19:15 [PATCH 0/6] Remove some Python safety FIXME comments Tom Tromey
2026-10-23 19:15 ` [PATCH 1/6] Add safe_target_to_connection_object Tom Tromey
2026-10-23 19:15 ` [PATCH 2/6] Add safety wrappers for from_{longest,ulongest} Tom Tromey
2026-10-23 19:15 ` [PATCH 3/6] Add gdbpy_safe_invalid_object_repr Tom Tromey
2026-10-23 19:15 ` Tom Tromey [this message]
2026-10-23 19:15 ` [PATCH 5/6] Safe wrappers for some string conversions Tom Tromey
2026-10-23 19:15 ` [PATCH 6/6] Some frame-related Python safety fixes Tom Tromey
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=20261023-python-safety-fixmes-v1-4-055b9e1d237b@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/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