Skip to content

Stale entries in MRO cache if MRO contains non-base classes#127773

@colesbury

Description

@colesbury

Bug report

For example the following program fails with either assert WeirdClass.value == 2 or assert WeirdClass.value == 3 in recent Python versions:

importsysclassBase: value=1classMeta(type): defmro(cls): return (cls, Base, object) classWeirdClass(metaclass=Meta): passassertBase.value==1assertWeirdClass.value==1Base.value=2assertBase.value==2assertWeirdClass.value==2Base.value=3assertBase.value==3assertWeirdClass.value==3

Adding intervening calls to sys _clear_internal_caches() makes the test pass.

VersionResult
3.7OK
3.8OK
3.9OK
3.10AssertionError: assert WeirdClass.value == 2
3.11AssertionError: assert WeirdClass.value == 2
3.12AssertionError: assert WeirdClass.value == 2
3.13AssertionError: assert WeirdClass.value == 3
mainAssertionError: assert WeirdClass.value == 3

We have code that checks for this case, but it hasn't worked properly in Python 3.10+:

staticvoid
type_mro_modified(PyTypeObject*type, PyObject*bases){
/*
Check that all base classes or elements of the MRO of type are
able to be cached. This function is called after the base
classes or mro of the type are altered.

We also have a test that partly covers this case, but doesn't detect the bug:

deftest_freeze_meta(self):
"""test PyType_Freeze() with overridden MRO"""
type_freeze=_testcapi.type_freeze
classBase:
value=1
classMeta(type):
defmro(cls):
return (cls, Base, object)
classFreezeThis(metaclass=Meta):
"""This has `Base` in the MRO, but not tp_bases"""
self.assertEqual(FreezeThis.value, 1)
withself.assertRaises(TypeError):
type_freeze(FreezeThis)
Base.value=2
self.assertEqual(FreezeThis.value, 2)
type_freeze(Base)
withself.assertRaises(TypeError):
Base.value=3
type_freeze(FreezeThis)
self.assertEqual(FreezeThis.value, 2)

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions