Skip to content

Commit e2dd5ad

Browse files
committed
backup 3.14
1 parent 2d94a6e commit e2dd5ad

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lib/test/test_type_cache.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" Tests for the internal type cache in CPython. """
2+
import collections.abc
23
import dis
34
import unittest
45
import warnings
@@ -114,6 +115,25 @@ class HolderSub(Holder):
114115
Holder.set_value()
115116
HolderSub.value
116117

118+
def test_abc_register_invalidates_subclass_versions(self):
119+
class Parent:
120+
pass
121+
122+
class Child(Parent):
123+
pass
124+
125+
type_assign_version(Parent)
126+
type_assign_version(Child)
127+
parent_version = type_get_version(Parent)
128+
child_version = type_get_version(Child)
129+
if parent_version == 0 or child_version == 0:
130+
self.skipTest("Could not assign valid type versions")
131+
132+
collections.abc.Mapping.register(Parent)
133+
134+
self.assertEqual(type_get_version(Parent), 0)
135+
self.assertEqual(type_get_version(Child), 0)
136+
117137
@support.cpython_only
118138
class TypeCacheWithSpecializationTests(unittest.TestCase):
119139
def tearDown(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``abc.register()`` so it invalidates type version tags for registered classes.

Objects/typeobject.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6066,7 +6066,23 @@ void
60666066
_PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask, unsigned long flags)
60676067
{
60686068
BEGIN_TYPE_LOCK();
6069+
/* Ideally, changing flags and invalidating the old version tag would
6070+
happen in one step. But type_modified_unlocked() is re-entrant and
6071+
cannot run with the world stopped, so we must invalidate first.
6072+
Immutable/static-builtin types are skipped because
6073+
set_flags_recursive() does not modify them. */
6074+
if (!PyType_HasFeature(self, Py_TPFLAGS_IMMUTABLETYPE) &&
6075+
(self->tp_flags & mask) != flags)
6076+
{
6077+
type_modified_unlocked(self);
6078+
}
6079+
/* Keep TYPE_LOCK held while waiting for stop-the-world so no thread
6080+
can reassign a version tag before the flag update. */
6081+
type_lock_prevent_release();
6082+
types_stop_world();
60696083
set_flags_recursive(self, mask, flags);
6084+
types_start_world();
6085+
type_lock_allow_release();
60706086
END_TYPE_LOCK();
60716087
}
60726088

0 commit comments

Comments
 (0)