Repository navigation
Stale entries in MRO cache if MRO contains non-base classes #127773
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes
on Dec 9, 2024 - addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Dec 9, 2024 cc @encukou - this broke in a4760cc (GH-27260), which removed
Py_TPFLAGS_HAVE_VERSION_TAG.The reason for removing
Py_TPFLAGS_HAVE_VERSION_TAGwas because "the field is always present in the type struct", but the flag indicates whether a type supports the method cache (i.e, supports having a valid version tag) and types with MRO entries that aren't superclasses don't support the method cache.Great (if very annoying) find, @colesbury! The bug even affects pypy, which has an entirely different architecture for its caches 😳
- added a commit that references this issue
on Dec 20, 2024 I think this was fixed by @encukou
In the main branch. I don't know if I'll get to older branches in time.
@encukou do you want to backport it? Otherwise, this seems complete now.
Oh, thanks for the ping!
See #143729 for the backport.- added a commit that references this issue
on Jan 26, 2026 I've merged the backport to 3.13.
Bug report
For example the following program fails with either
assert WeirdClass.value == 2orassert WeirdClass.value == 3in recent Python versions:Adding intervening calls to
sys _clear_internal_caches()makes the test pass.AssertionError: assert WeirdClass.value == 2AssertionError: assert WeirdClass.value == 2AssertionError: assert WeirdClass.value == 2AssertionError: assert WeirdClass.value == 3AssertionError: assert WeirdClass.value == 3We have code that checks for this case, but it hasn't worked properly in Python 3.10+:
cpython/Objects/typeobject.c
Lines 1107 to 1112 in 5c89adf
We also have a test that partly covers this case, but doesn't detect the bug:
cpython/Lib/test/test_capi/test_type.py
Lines 40 to 66 in 5c89adf
Linked PRs