https://github.com/python/cpython/commit/8a162b2e270bc371f0c229e8511e7c19b3742f52
commit: 8a162b2e270bc371f0c229e8511e7c19b3742f52
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-05-23T08:53:12Z
summary:

[3.15] gh-148450: `abc.register` needs to update `type_version` when `tp_flags` 
is changed (GH-148623) (#150300)

gh-148450: `abc.register` needs to update `type_version` when `tp_flags` is 
changed (GH-148623)
(cherry picked from commit e7eaed56149aa08f7fd5012784cc1deef8e483de)

Co-authored-by: Hai Zhu <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-15-48-04.gh-issue-148450.2MEVqH.rst
M Lib/test/test_type_cache.py
M Objects/typeobject.c

diff --git a/Lib/test/test_type_cache.py b/Lib/test/test_type_cache.py
index 22ad9f6243eda91..849a2afd8ed7986 100644
--- a/Lib/test/test_type_cache.py
+++ b/Lib/test/test_type_cache.py
@@ -1,4 +1,5 @@
 """ Tests for the internal type cache in CPython. """
+import collections.abc
 import dis
 import unittest
 import warnings
@@ -114,6 +115,25 @@ class HolderSub(Holder):
             Holder.set_value()
             HolderSub.value
 
+    def test_abc_register_invalidates_subclass_versions(self):
+        class Parent:
+            pass
+
+        class Child(Parent):
+            pass
+
+        type_assign_version(Parent)
+        type_assign_version(Child)
+        parent_version = type_get_version(Parent)
+        child_version = type_get_version(Child)
+        if parent_version == 0 or child_version == 0:
+            self.skipTest("Could not assign valid type versions")
+
+        collections.abc.Mapping.register(Parent)
+
+        self.assertEqual(type_get_version(Parent), 0)
+        self.assertEqual(type_get_version(Child), 0)
+
 @support.cpython_only
 class TypeCacheWithSpecializationTests(unittest.TestCase):
     def tearDown(self):
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-15-48-04.gh-issue-148450.2MEVqH.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-15-48-04.gh-issue-148450.2MEVqH.rst
new file mode 100644
index 000000000000000..2a7d0d9bb3a7f7e
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-15-48-04.gh-issue-148450.2MEVqH.rst
@@ -0,0 +1 @@
+Fix ``abc.register()`` so it invalidates type version tags for registered 
classes.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 7cca137f74be58f..fc679ef747e8567 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -6490,9 +6490,25 @@ set_flags_recursive(PyTypeObject *self, unsigned long 
mask, unsigned long flags)
 void
 _PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask, unsigned 
long flags)
 {
+    BEGIN_TYPE_LOCK();
+    /* Ideally, changing flags and invalidating the old version tag would
+       happen in one step. But type_modified_unlocked() is re-entrant and
+       cannot run with the world stopped, so we must invalidate first.
+       Immutable/static-builtin types are skipped because
+       set_flags_recursive() does not modify them. */
+    if (!PyType_HasFeature(self, Py_TPFLAGS_IMMUTABLETYPE) &&
+        (self->tp_flags & mask) != flags)
+    {
+        type_modified_unlocked(self);
+    }
+    /* Keep TYPE_LOCK held while waiting for stop-the-world so no thread
+       can reassign a version tag before the flag update. */
+    type_lock_prevent_release();
     types_stop_world();
     set_flags_recursive(self, mask, flags);
     types_start_world();
+    type_lock_allow_release();
+    END_TYPE_LOCK();
 }
 
 /* This is similar to PyObject_GenericGetAttr(),

_______________________________________________
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