Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Matthieu Longo <matthieu.longo@arm.com>
To: gdb-patches@sourceware.org
Subject: Re: [RFC 0/4] Better Python safety
Date: Wed, 4 Mar 2026 17:39:35 +0000	[thread overview]
Message-ID: <687d60e9-3a8c-411e-aa04-3d1aae896232@arm.com> (raw)
In-Reply-To: <20260222200759.1587070-1-tom@tromey.com>

On 22/02/2026 19:49, Tom Tromey wrote:
> * I think the approach to handling subclasses of PyObject could be
>    greatly improved.  In particular I think we could use real C++
>    classes by judicious use of placement new and explicit destructor
>    calls.  The "corresponding_object_type" stuff you'll see in here is
>    sort of a gesture in this direction (though that code also helps
>    with some type-safety elsewhere as well).  Essentially I think we
>    could end up making new instances with just 'new'.
> 
>    I didn't want to really touch this until the stable ABI work related
>    to type-instantiation is done.
> 

Yes please :)

> * Finally gdb is using PyObject_New a lot but my reading while
>    researching this series indicates that this is wrong.  However the
>    wrongness is still (I guess temporarily) preserved in a wrapper in
>    this series.> 
> Tom
> 

I haven't published the patch series migrating all the C extension types, but what I discovered is summarized in https://docs.python.org/3/howto/isolating-extensions.html#heap-types.

Specifically this paragraph:

   > Instances of heap types hold a reference to their type. This ensures
   > that the type isn’t destroyed before all its instances are, but may
   > result in reference cycles that need to be broken by the garbage collector.

   > To avoid memory leaks, instances of heap types must implement the
   > garbage collection protocol. That is, heap types should:
   >  * Have the Py_TPFLAGS_HAVE_GC flag.
   >  * Define a traverse function using Py_tp_traverse, which visits the
   >    type (e.g. using Py_VISIT(Py_TYPE(self))).

So after the migration, all the extension types have the Py_TPFLAGS_HAVE_GC flag, are allocated via PyObject_GC_New(), and every PyType_Slot sets Py_tp_traverse, Py_tp_clear, and Py_tp_dealloc.

Some of the types had circular dependencies due to a type referencing the other one similarly to what happens between two nodes of a doubly linked list. In those cases, the circular dependency have to be broken.
Also, some types already should have required garbage collection management even with static types.

Are your findings converging with mine ?

Matthieu

  parent reply	other threads:[~2026-03-04 17:41 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-22 19:49 Tom Tromey
2026-02-22 19:49 ` [RFC 1/4] Add gdbpy_borrowed_ref Tom Tromey
2026-02-24  4:57   ` Simon Marchi
2026-02-25  3:55     ` Tom Tromey
2026-02-25 15:24       ` Simon Marchi
2026-02-26  1:38       ` Tom Tromey
2026-02-22 19:49 ` [RFC 2/4] Add wrappers for some Python APIs Tom Tromey
2026-02-22 19:49 ` [RFC 3/4] Add constexpr functions to create PyMethodDef entries Tom Tromey
2026-02-22 19:49 ` [RFC 4/4] Convert some Python code to new-style Tom Tromey
2026-02-23 20:28 ` [RFC 0/4] Better Python safety Simon Marchi
2026-02-23 21:00 ` Simon Marchi
2026-02-23 23:23   ` Tom Tromey
2026-02-23 23:56     ` Tom Tromey
2026-02-24  1:05       ` Simon Marchi
2026-02-24 16:29         ` Tom Tromey
2026-02-23 21:22 ` Tom Tromey
2026-03-04 17:39 ` Matthieu Longo [this message]
2026-03-04 21:02   ` 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=687d60e9-3a8c-411e-aa04-3d1aae896232@arm.com \
    --to=matthieu.longo@arm.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