You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mypyc] Generate more type methods for types with managed dicts (#21290)
Fixes#21133
Types with the `Py_TPFLAGS_MANAGED_DICT` flag must call
`PyObject_VisitManagedDict` and `PyObject_ClearManagedDict` in their
`tp_traverse` / `tp_clear` functions according to
[docs](https://docs.python.org/3/c-api/typeobj.html#c.Py_TPFLAGS_MANAGED_DICT)
but the types generated by mypyc currently don't do this. We don't
generate these functions at all so they get inherited from the base
class.
Failure to call these functions may result in a segfault in python 3.14
when accessing the managed dict after its owner has been deallocated. I
believe the crash happens because the logic for types with the
`Py_TPFLAGS_INLINE_VALUES` flag in
[`PyObject_ClearManagedDict`](https://github.com/python/cpython/blob/main/Objects/dictobject.c#L7803)
is not run. The condition to add this flag has changed in
[3.14](https://github.com/python/cpython/blob/3.14/Objects/typeobject.c#L8877)
vs
[3.13](https://github.com/python/cpython/blob/3.13/Objects/typeobject.c#L8171)
so generated types with `Py_TPFLAGS_MANAGED_DICT` get it in 3.14.
To fix, generate `tp_clear`, `tp_traverse`, and `tp_dealloc` for types
with managed dicts. Also add a special case in these functions for
classes with built-in bases to call the pointer of the base class.
0 commit comments