From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 06/10] Return gdbpy_ref<> from type_to_type_object
Date: Fri, 20 Feb 2026 14:03:52 -0700 [thread overview]
Message-ID: <20260220-python-safety-minor-v1-6-4c4b12e445af@adacore.com> (raw)
In-Reply-To: <20260220-python-safety-minor-v1-0-4c4b12e445af@adacore.com>
This changes type_to_type_object to return a gdbpy_ref<>,
using the type system to convey that a new reference is always
returned.
---
gdb/python/py-arch.c | 4 ++--
gdb/python/py-lazy-string.c | 2 +-
gdb/python/py-symbol.c | 2 +-
gdb/python/py-type.c | 45 ++++++++++++++++++++++----------------------
gdb/python/py-value.c | 4 ++--
gdb/python/py-xmethods.c | 2 +-
gdb/python/python-internal.h | 2 +-
gdb/python/python.c | 2 +-
8 files changed, 31 insertions(+), 32 deletions(-)
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index eb4a7c63530..cf740821dfa 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -314,7 +314,7 @@ archpy_integer_type (PyObject *self, PyObject *args, PyObject *kw)
return nullptr;
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Implementation of gdb.void_type. */
@@ -324,7 +324,7 @@ archpy_void_type (PyObject *self, PyObject *args)
struct gdbarch *gdbarch;
ARCHPY_REQUIRE_VALID (self, gdbarch);
- return type_to_type_object (builtin_type (gdbarch)->builtin_void);
+ return type_to_type_object (builtin_type (gdbarch)->builtin_void).release ();
}
/* __repr__ implementation for gdb.Architecture. */
diff --git a/gdb/python/py-lazy-string.c b/gdb/python/py-lazy-string.c
index fe248f6e78f..26998064206 100644
--- a/gdb/python/py-lazy-string.c
+++ b/gdb/python/py-lazy-string.c
@@ -235,7 +235,7 @@ gdbpy_create_lazy_string_object (CORE_ADDR address, long length,
str_obj->encoding = NULL;
else
str_obj->encoding = xstrdup (encoding);
- str_obj->type = type_to_type_object (type);
+ str_obj->type = type_to_type_object (type).release ();
return (PyObject *) str_obj;
}
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 80c7705aa68..7b6164fb20d 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -73,7 +73,7 @@ sympy_get_type (PyObject *self, void *closure)
return Py_None;
}
- return type_to_type_object (symbol->type ());
+ return type_to_type_object (symbol->type ()).release ();
}
static PyObject *
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index 017937c1c0a..8f8f6e326a8 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -132,7 +132,7 @@ convert_field (struct type *type, int field)
if (result == NULL)
return NULL;
- gdbpy_ref<> arg (type_to_type_object (type));
+ gdbpy_ref<> arg = type_to_type_object (type);
if (arg == NULL)
return NULL;
if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
@@ -202,7 +202,7 @@ convert_field (struct type *type, int field)
if (type->field (field).type () == NULL)
arg = gdbpy_ref<>::new_reference (Py_None);
else
- arg.reset (type_to_type_object (type->field (field).type ()));
+ arg = type_to_type_object (type->field (field).type ());
if (arg == NULL)
return NULL;
if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0)
@@ -283,7 +283,7 @@ typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind)
gdbpy_ref<> type_holder;
if (checked_type != type)
{
- type_holder.reset (type_to_type_object (checked_type));
+ type_holder = type_to_type_object (checked_type);
if (type_holder == nullptr)
return nullptr;
py_type = type_holder.get ();
@@ -489,7 +489,7 @@ typy_strip_typedefs (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Strip typedefs and pointers/reference from a type. Then check that
@@ -580,7 +580,7 @@ typy_array_1 (PyObject *self, PyObject *args, int is_vector)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (array);
+ return type_to_type_object (array).release ();
}
/* Return an array type. */
@@ -614,7 +614,7 @@ typy_pointer (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Return the range of a type represented by SELF. The return type is
@@ -686,7 +686,7 @@ typy_reference (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Return a Type object which represents the target type of SELF. */
@@ -702,7 +702,7 @@ typy_target (PyObject *self, PyObject *args)
return NULL;
}
- return type_to_type_object (type->target_type ());
+ return type_to_type_object (type->target_type ()).release ();
}
/* Return a const-qualified type variant. */
@@ -720,7 +720,7 @@ typy_const (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Return a volatile-qualified type variant. */
@@ -738,7 +738,7 @@ typy_volatile (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Return an unqualified type variant. */
@@ -756,7 +756,7 @@ typy_unqualified (PyObject *self, PyObject *args)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Return the size of the type represented by SELF, in bytes. */
@@ -978,7 +978,7 @@ typy_legacy_template_argument (struct type *type, const struct block *block,
if (! argtype)
return NULL;
- return type_to_type_object (argtype);
+ return type_to_type_object (argtype).release ();
}
static PyObject *
@@ -1037,7 +1037,7 @@ typy_template_argument (PyObject *self, PyObject *args)
sym = TYPE_TEMPLATE_ARGUMENT (type, argno);
if (sym->loc_class () == LOC_TYPEDEF)
- return type_to_type_object (sym->type ());
+ return type_to_type_object (sym->type ()).release ();
else if (sym->loc_class () == LOC_OPTIMIZED_OUT)
{
PyErr_Format (PyExc_RuntimeError,
@@ -1423,7 +1423,7 @@ typy_iterator_dealloc (PyObject *obj)
}
/* Create a new Type referring to TYPE. */
-PyObject *
+gdbpy_ref<>
type_to_type_object (struct type *type)
{
type_object *type_obj;
@@ -1450,14 +1450,13 @@ type_to_type_object (struct type *type)
else
type_obj = typy_registry.lookup (type->arch_owner (), type);
- if (type_obj != nullptr)
- return (PyObject*)type_obj;
-
- type_obj = PyObject_New (type_object, &type_object_type);
- if (type_obj)
- set_type (type_obj, type);
-
- return (PyObject *) type_obj;
+ if (type_obj == nullptr)
+ {
+ type_obj = PyObject_New (type_object, &type_object_type);
+ if (type_obj)
+ set_type (type_obj, type);
+ }
+ return gdbpy_ref<> (type_obj);
}
struct type *
@@ -1499,7 +1498,7 @@ gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw)
if (! type)
return NULL;
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
static int
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index c0fcfb882e1..a5b2303728c 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -432,7 +432,7 @@ valpy_get_type (PyObject *self, void *closure)
{
value_object *obj = (value_object *) self;
- return type_to_type_object (obj->value->type ());
+ return type_to_type_object (obj->value->type ()).release ();
}
/* Return dynamic type of the value. */
@@ -484,7 +484,7 @@ valpy_get_dynamic_type (PyObject *self, void *closure)
return gdbpy_handle_gdb_exception (nullptr, except);
}
- return type_to_type_object (type);
+ return type_to_type_object (type).release ();
}
/* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index 64f9807cfee..1fafafb4d24 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -123,7 +123,7 @@ gdbpy_get_matching_xmethod_workers
gdbpy_enter enter_py;
- gdbpy_ref<> py_type (type_to_type_object (obj_type));
+ gdbpy_ref<> py_type = type_to_type_object (obj_type);
if (py_type == NULL)
{
gdbpy_print_stack ();
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index c7e812d6893..6fa37cf85bd 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -501,7 +501,7 @@ gdbpy_ref<> 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);
-PyObject *type_to_type_object (struct type *);
+gdbpy_ref<> type_to_type_object (struct type *);
PyObject *frame_info_to_frame_object (const frame_info_ptr &frame);
PyObject *symtab_to_linetable_object (PyObject *symtab);
gdbpy_ref<> pspace_to_pspace_object (struct program_space *);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index aec2c7559a8..9862d1a726f 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -2105,7 +2105,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
gdbpy_enter enter_py;
- gdbpy_ref<> type_obj (type_to_type_object (type));
+ gdbpy_ref<> type_obj = type_to_type_object (type);
if (type_obj == NULL)
{
gdbpy_print_stack ();
--
2.53.0
next prev parent reply other threads:[~2026-02-20 21:08 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 ` [PATCH 01/10] Return gdbpy_ref<> from symtab_and_line_to_sal_object Tom Tromey
2026-02-21 1:58 ` 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 ` Tom Tromey [this message]
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-6-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