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
{{ message }}
Repository navigation
subtype_clear can not be called from derived types #89429
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-09-22.19:16:19.650>labels= ['expert-C-API', 'type-bug', '3.11']
title='subtype_clear can not be called from derived types'updated_at=<Date2022-01-15.16:35:53.826>user='https://bugs.python.org/VictorMilovanov'
I am trying to define a type in C, that derives from PyTypeObject.
I want to override tp_clear. To do so properly, I should call base type's tp_clear and have it perform its cleanup steps. PyTypeObject has a tp_clear implementation: subtype_clear. Problem is, it assumes the instance it gets is of a type, that does not override PyTypeObject's tp_clear, and behaves incorrectly in 2 ways:
it does not perform the usual cleanup, because in this code
base = type;
while ((baseclear = base->tp_clear) == subtype_clear)
the loop condition is immediately false, as my types overrode tp_clear
later on it calls baseclear on the same object. But because of the loop above baseclear actually points to my type's custom tp_clear implementation, which leads to reentry to that function (basically a stack overflow, unless there's a guard against it).
Victor, it's not clear to me whether this is a bug report or a "how do I do this" question, but in either case if you posted your code along with "I expected X but got Y", it would be easier to understand the issue.
Activity
VictorMilovanov commentedon Sep 22, 2021
I am trying to define a type in C, that derives from PyTypeObject.
I want to override tp_clear. To do so properly, I should call base type's tp_clear and have it perform its cleanup steps. PyTypeObject has a tp_clear implementation: subtype_clear. Problem is, it assumes the instance it gets is of a type, that does not override PyTypeObject's tp_clear, and behaves incorrectly in 2 ways:
base = type;
while ((baseclear = base->tp_clear) == subtype_clear)
the loop condition is immediately false, as my types overrode tp_clear
VictorMilovanov commentedon Sep 22, 2021
To put it differently, if you think in terms of MRO, my custom type's MRO is
my_type_clear (from my type), subtype_clear (from PyTypeObject), etc
And subtype_clear incorrectly assumes that it is the first entry in the object's MRO list for tp_clear.
iritkatriel commentedon Jan 15, 2022
Victor, it's not clear to me whether this is a bug report or a "how do I do this" question, but in either case if you posted your code along with "I expected X but got Y", it would be easier to understand the issue.