SIGN IN SIGN UP

[mypyc] Clear only subtype refs in tp_dealloc of built-in subtypes (#21872)

Fixes #21859

Deallocating an instance of a class that inherits from a built-in type
might fail an assertion because when we clear references during the
deallocation, we call the generated `tp_clear` function that in turn
calls `tp_clear` of the base class.

The functions put in the `tp_clear` slot of built-in types require that
the instance is still alive (references > 0) which is not the case
during deallocation.

To avoid this, split the `tp_clear` function for types with built-in
bases into two: first (`subtype_clear`) clears only the references owned
by the subtype, second (`clear`) does the same plus calls base type
`tp_clear`.

`clear` is put into the `tp_clear` slot while `subtype_clear` can be
called from `tp_dealloc`. `tp_dealloc` also calls base type `tp_dealloc`
which clears the references owned by the base type, so the base type
`tp_clear` call was not necessary.

For classes without a builtin base we continue to clear all their
references in a single function.
P
Piotr Sawicki committed
7f88c95bbf9af181a4de13e2c242c89216f15b19
Parent: bd08f95
Committed by GitHub <noreply@github.com> on 8/19/2026, 4:07:04 PM