Skip to content

Commit 3743f92

Browse files
committed
make sure abc.register update type_version
1 parent e0b56f0 commit 3743f92

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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):

Objects/typeobject.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6419,9 +6419,23 @@ set_flags_recursive(PyTypeObject *self, unsigned long mask, unsigned long flags)
64196419
void
64206420
_PyType_SetFlagsRecursive(PyTypeObject *self, unsigned long mask, unsigned long flags)
64216421
{
6422+
BEGIN_TYPE_LOCK();
6423+
/* Invalidate the old version before changing flags. This must happen
6424+
before types_stop_world(); immutable/static-builtin types are
6425+
skipped because set_flags_recursive() does not touch them. */
6426+
if (!PyType_HasFeature(self, Py_TPFLAGS_IMMUTABLETYPE) &&
6427+
(self->tp_flags & mask) != flags)
6428+
{
6429+
type_modified_unlocked(self);
6430+
}
6431+
/* Keep TYPE_LOCK held while waiting for stop-the-world so no thread
6432+
can reassign a version tag before the flag update. */
6433+
type_lock_prevent_release();
64226434
types_stop_world();
64236435
set_flags_recursive(self, mask, flags);
64246436
types_start_world();
6437+
type_lock_allow_release();
6438+
END_TYPE_LOCK();
64256439
}
64266440

64276441
/* This is similar to PyObject_GenericGetAttr(),

0 commit comments

Comments
 (0)