Mailing List Archive

[issue45266] subtype_clear can not be called from derived types
New submission from Victor Milovanov <lostfreeman@gmail.com>:

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

----------
components: C API
messages: 402466
nosy: Victor Milovanov
priority: normal
severity: normal
status: open
title: subtype_clear can not be called from derived types
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45266>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue45266] subtype_clear can not be called from derived types [ In reply to ]
Victor Milovanov <lostfreeman@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue45266>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com