This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subtype_clear can not be called from derived types
Type: behavior Stage:
Components: C API Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Victor Milovanov, iritkatriel
Priority: normal Keywords:

Created on 2021-09-22 19:16 by Victor Milovanov, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg402466 - (view) Author: Victor Milovanov (Victor Milovanov) Date: 2021-09-22 19:16
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:

1) 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

2) 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).
msg402467 - (view) Author: Victor Milovanov (Victor Milovanov) Date: 2021-09-22 19:18
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.
msg410650 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-15 16:35
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.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89429
2022-01-15 16:35:53iritkatrielsetnosy: + iritkatriel

messages: + msg410650
versions: - Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10
2021-09-22 19:18:47Victor Milovanovsetmessages: + msg402467
2021-09-22 19:16:19Victor Milovanovcreate