From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 01/10] Return gdbpy_ref<> from symtab_and_line_to_sal_object
Date: Fri, 20 Feb 2026 14:03:47 -0700 [thread overview]
Message-ID: <20260220-python-safety-minor-v1-1-4c4b12e445af@adacore.com> (raw)
In-Reply-To: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com>
This changes symtab_and_line_to_sal_object to return a gdbpy_ref<>,
using the type system to convey that a new reference is always
returned.
---
gdb/python/py-frame.c | 4 ++--
gdb/python/py-progspace.c | 4 ++--
gdb/python/py-record-btrace.c | 4 ++--
gdb/python/py-symtab.c | 4 ++--
gdb/python/py-unwind.c | 4 ++--
gdb/python/python-internal.h | 2 +-
gdb/python/python.c | 8 ++++----
7 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index ab7883b4e73..8b50307c099 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -466,7 +466,7 @@ static PyObject *
frapy_find_sal (PyObject *self, PyObject *args)
{
frame_info_ptr frame;
- PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */
+ gdbpy_ref<> sal_obj;
try
{
@@ -480,7 +480,7 @@ frapy_find_sal (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return sal_obj;
+ return sal_obj.release ();
}
/* Implementation of gdb.Frame.read_var_value (self, variable,
diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c
index 19f5e533b0a..8b5ada2dc2e 100644
--- a/gdb/python/py-progspace.c
+++ b/gdb/python/py-progspace.c
@@ -534,7 +534,7 @@ static PyObject *
pspy_find_pc_line (PyObject *o, PyObject *args)
{
CORE_ADDR pc;
- PyObject *result = NULL; /* init for gcc -Wall */
+ gdbpy_ref<> result;
PyObject *pc_obj;
pspace_object *self = (pspace_object *) o;
@@ -560,7 +560,7 @@ pspy_find_pc_line (PyObject *o, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return result;
+ return result.release ();
}
/* Implementation of is_valid (self) -> Boolean.
diff --git a/gdb/python/py-record-btrace.c b/gdb/python/py-record-btrace.c
index 3fcf653f4ba..90d9ecd95a8 100644
--- a/gdb/python/py-record-btrace.c
+++ b/gdb/python/py-record-btrace.c
@@ -205,7 +205,7 @@ PyObject *
recpy_bt_insn_sal (PyObject *self, void *closure)
{
const btrace_insn * const insn = btrace_insn_from_recpy_insn (self);
- PyObject *result = NULL;
+ gdbpy_ref<> result;
if (insn == NULL)
return NULL;
@@ -219,7 +219,7 @@ recpy_bt_insn_sal (PyObject *self, void *closure)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return result;
+ return result.release ();
}
/* Implementation of RecordInstruction.pc [int] for btrace.
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 2dca0083277..820542932ba 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -390,7 +390,7 @@ symtab_to_symtab_object (struct symtab *symtab)
/* Create a new symtab and line (gdb.Symtab_and_line) object
that encapsulates the symtab_and_line structure from GDB. */
-PyObject *
+gdbpy_ref<>
symtab_and_line_to_sal_object (struct symtab_and_line sal)
{
sal_object *sal_obj;
@@ -399,7 +399,7 @@ symtab_and_line_to_sal_object (struct symtab_and_line sal)
if (sal_obj != nullptr)
set_sal (sal_obj, sal);
- return (PyObject *) sal_obj;
+ return gdbpy_ref<> (sal_obj);
}
/* Return struct symtab_and_line reference that is wrapped by this
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index d4c35e17f14..96e11137be6 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -613,7 +613,7 @@ pending_framepy_find_sal (PyObject *self, PyObject *args)
PENDING_FRAMEPY_REQUIRE_VALID (pending_frame);
- PyObject *sal_obj = nullptr;
+ gdbpy_ref<> sal_obj;
try
{
@@ -627,7 +627,7 @@ pending_framepy_find_sal (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return sal_obj;
+ return sal_obj.release ();
}
/* Implement PendingFrame.block(). Return a gdb.Block for the pending
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 8925714f3c1..feabf270464 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -495,7 +495,7 @@ gdb::unique_xmalloc_ptr<char> gdbpy_parse_command_name
PyObject *gdbpy_register_tui_window (PyObject *self, PyObject *args,
PyObject *kw);
-PyObject *symtab_and_line_to_sal_object (struct symtab_and_line sal);
+gdbpy_ref<> symtab_and_line_to_sal_object (struct symtab_and_line sal);
PyObject *symtab_to_symtab_object (struct symtab *symtab);
PyObject *symbol_to_symbol_object (struct symbol *sym);
PyObject *block_to_block_object (const struct block *block,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 79b5d24d1cf..d2a77890072 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1020,11 +1020,11 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
return NULL;
for (size_t i = 0; i < sals.size (); ++i)
{
- PyObject *obj = symtab_and_line_to_sal_object (sals[i]);
- if (obj == NULL)
- return NULL;
+ gdbpy_ref<> obj = symtab_and_line_to_sal_object (sals[i]);
+ if (obj == nullptr)
+ return nullptr;
- PyTuple_SetItem (result.get (), i, obj);
+ PyTuple_SetItem (result.get (), i, obj.release ());
}
}
else
--
2.53.0
next prev parent reply other threads:[~2026-02-20 21:05 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-20 21:03 [PATCH 00/10] Mildly better refcount safety for Python Tom Tromey
2026-02-20 21:03 ` Tom Tromey [this message]
2026-02-21 1:58 ` [PATCH 01/10] Return gdbpy_ref<> from symtab_and_line_to_sal_object Simon Marchi
2026-02-21 2:13 ` Simon Marchi
2026-02-21 22:33 ` Tom Tromey
2026-02-23 12:28 ` Tom Tromey
2026-02-20 21:03 ` [PATCH 02/10] Return gdbpy_ref<> from symbol_to_symbol_object Tom Tromey
2026-02-21 2:28 ` Simon Marchi
2026-02-21 22:35 ` Tom Tromey
2026-02-20 21:03 ` [PATCH 03/10] Return gdbpy_ref<> from symtab_to_symtab_object Tom Tromey
2026-02-20 21:03 ` [PATCH 04/10] Return gdbpy_ref<> from block_to_block_object Tom Tromey
2026-02-20 21:03 ` [PATCH 05/10] Return gdbpy_ref<> from value_to_value_object Tom Tromey
2026-02-20 21:03 ` [PATCH 06/10] Return gdbpy_ref<> from type_to_type_object Tom Tromey
2026-02-20 21:03 ` [PATCH 07/10] Return gdbpy_ref<> from frame_info_to_frame_object Tom Tromey
2026-02-20 21:03 ` [PATCH 08/10] Return gdbpy_ref<> from symtab_to_linetable_object Tom Tromey
2026-02-20 21:03 ` [PATCH 09/10] Return gdbpy_ref<> from gdbarch_to_arch_object Tom Tromey
2026-02-20 21:03 ` [PATCH 10/10] Return gdbpy_ref<> from gdbpy_registry::lookup Tom Tromey
2026-02-21 2:31 ` [PATCH 00/10] Mildly better refcount safety for Python Simon Marchi
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=20260220-python-safety-minor-v1-1-4c4b12e445af@adacore.com \
--to=tromey@adacore.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