Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 01/10] Return gdbpy_ref<> from symtab_and_line_to_sal_object
Date: Fri, 20 Feb 2026 21:13:40 -0500	[thread overview]
Message-ID: <2bd802a4-75e2-4af9-90c9-d9ff89163a9a@simark.ca> (raw)
In-Reply-To: <9eed1009-8563-45cf-b9d2-e0ebb3005683@simark.ca>



On 2026-02-20 20:58, Simon Marchi wrote:
> 
> 
> On 2026-02-20 16:03, Tom Tromey wrote:
>> 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.
> 
> I noted some stylistic suggestions below.  It's a bit subjective so it's
> up to you really.  I suppose that the same patterns happen in all the
> patches.
> 
>> @@ -560,7 +560,7 @@ pspy_find_pc_line (PyObject *o, PyObject *args)
>>        return gdbpy_handle_gdb_exception (nullptr, except);
>>      }
>>  
>> -  return result;
>> +  return result.release ();
> 
> In these cases I think I would return directly from the
> symtab_and_line_to_sal_object call:
> 
>     return symtab_and_line_to_sal_object (sal).release ();
> 
>> 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);
> 
> I think you could be even more pedantic and change sal_obj to be of type
> gdbpy_ref<>.  This way, the ref is managed from the very edge of the
> Python API.

Regarding this, I thought this would work, but it doesn't:

  gdbpy_ref<sal_object> sal_obj (PyObject_New (sal_object, &sal_object_type));
  if (sal_obj != nullptr)
    set_sal (sal_obj.get (), sal);

  return sal_obj;

I don't know if there is a magic way to make gdbpy_ref<sal_object>
implicitly convertible to gdbpy_ref<>.

I tried changing the return type of symtab_and_line_to_sal_object to be
gdbpy_ref<sal_object> instead.  I don't think it would be a bad thing,
it would convey what type of object it is exactly.  But this would require moving the
definition of struct sal_object in some header file, otherwise the other
files don't know that `sal_object *` is convertible to `PyObject *`,
when they try to pass the released value to the Python API.

Anyway, what you have here is fine, it is definitely an improvement over
the current state.

Simon

  reply	other threads:[~2026-02-21  2:14 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 [this message]
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=2bd802a4-75e2-4af9-90c9-d9ff89163a9a@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /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