https://github.com/python/cpython/commit/fdac87ae39ae0672be22837592f1fc4101e8432a
commit: fdac87ae39ae0672be22837592f1fc4101e8432a
branch: 3.13
author: Petr Viktorin <[email protected]>
committer: encukou <[email protected]>
date: 2026-01-26T16:31:52+01:00
summary:

[3.13] gh-127773: Disable attribute cache on incompatible MRO entries 
(GH-127924) (GH-143729)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-12-13-15-21-45.gh-issue-127773.E-DZR4.rst
M Include/cpython/object.h
M Lib/test/test_metaclass.py
M Objects/typeobject.c

diff --git a/Include/cpython/object.h b/Include/cpython/object.h
index 6cb2c40fe2eb71..5b3b890dcf3723 100644
--- a/Include/cpython/object.h
+++ b/Include/cpython/object.h
@@ -221,7 +221,9 @@ struct _typeobject {
     PyObject *tp_weaklist; /* not used for static builtin types */
     destructor tp_del;
 
-    /* Type attribute cache version tag. Added in version 2.6 */
+    /* Type attribute cache version tag. Added in version 2.6.
+     * If zero, the cache is invalid and must be initialized.
+     */
     unsigned int tp_version_tag;
 
     destructor tp_finalize;
@@ -229,9 +231,17 @@ struct _typeobject {
 
     /* bitset of which type-watchers care about this type */
     unsigned char tp_watched;
+
+    /* Number of tp_version_tag values used.
+     * Set to _Py_ATTR_CACHE_UNUSED if the attribute cache is
+     * disabled for this type (e.g. due to custom MRO entries).
+     * Otherwise, limited to MAX_VERSIONS_PER_CLASS (defined elsewhere).
+     */
     uint16_t tp_versions_used;
 };
 
+#define _Py_ATTR_CACHE_UNUSED (30000)  // (see tp_versions_used)
+
 /* This struct is used by the specializer
  * It should be treated as an opaque blob
  * by code other than the specializer and interpreter. */
diff --git a/Lib/test/test_metaclass.py b/Lib/test/test_metaclass.py
index b37b7defe84d1c..07a333f98fa0a9 100644
--- a/Lib/test/test_metaclass.py
+++ b/Lib/test/test_metaclass.py
@@ -254,6 +254,33 @@
     [...]
     test.test_metaclass.ObscureException
 
+Test setting attributes with a non-base type in mro() (gh-127773).
+
+    >>> class Base:
+    ...     value = 1
+    ...
+    >>> class Meta(type):
+    ...     def mro(cls):
+    ...         return (cls, Base, object)
+    ...
+    >>> class WeirdClass(metaclass=Meta):
+    ...     pass
+    ...
+    >>> Base.value
+    1
+    >>> WeirdClass.value
+    1
+    >>> Base.value = 2
+    >>> Base.value
+    2
+    >>> WeirdClass.value
+    2
+    >>> Base.value = 3
+    >>> Base.value
+    3
+    >>> WeirdClass.value
+    3
+
 """
 
 import sys
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-12-13-15-21-45.gh-issue-127773.E-DZR4.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-12-13-15-21-45.gh-issue-127773.E-DZR4.rst
new file mode 100644
index 00000000000000..7e68b3fecd1c08
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-12-13-15-21-45.gh-issue-127773.E-DZR4.rst 
@@ -0,0 +1 @@
+Do not use the type attribute cache for types with incompatible :term:`MRO`.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index ded0f22b4e10c8..0c16377034fbb7 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -967,6 +967,7 @@ static void
 set_version_unlocked(PyTypeObject *tp, unsigned int version)
 {
     ASSERT_TYPE_LOCK_HELD();
+    assert(version == 0 || (tp->tp_versions_used != _Py_ATTR_CACHE_UNUSED));
 #ifndef Py_GIL_DISABLED
     if (version) {
         tp->tp_versions_used++;
@@ -1109,6 +1110,10 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
         PyObject *b = PyTuple_GET_ITEM(bases, i);
         PyTypeObject *cls = _PyType_CAST(b);
 
+        if (cls->tp_versions_used >= _Py_ATTR_CACHE_UNUSED) {
+            goto clear;
+        }
+
         if (!is_subtype_with_mro(lookup_tp_mro(type), type, cls)) {
             goto clear;
         }
@@ -1117,7 +1122,8 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
 
  clear:
     assert(!(type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
-    set_version_unlocked(type, 0); /* 0 is not a valid version tag */
+    set_version_unlocked(type, 0);  /* 0 is not a valid version tag */
+    type->tp_versions_used = _Py_ATTR_CACHE_UNUSED;
     if (PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
         // This field *must* be invalidated if the type is modified (see the
         // comment on struct _specialization_cache):
@@ -1127,6 +1133,9 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
 }
 
 #define MAX_VERSIONS_PER_CLASS 1000
+#if _Py_ATTR_CACHE_UNUSED < MAX_VERSIONS_PER_CLASS
+#error "_Py_ATTR_CACHE_UNUSED must be bigger than max"
+#endif
 
 static int
 assign_version_tag(PyInterpreterState *interp, PyTypeObject *type)
@@ -1144,6 +1153,7 @@ assign_version_tag(PyInterpreterState *interp, 
PyTypeObject *type)
         return 0;
     }
     if (type->tp_versions_used >= MAX_VERSIONS_PER_CLASS) {
+        /* (this includes `tp_versions_used == _Py_ATTR_CACHE_UNUSED`) */
         return 0;
     }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to