Skip to content

Commit 5d1926d

Browse files
committed
gh-162752: Test racing unchanged slot updates with real ones
Assigning a Python function over another one now decides without stopping the world whether any slot changes, by walking the subclasses with the type lock held. Race that walk against assignments that do change the slots of a subclass and against __bases__ assignments that remove the subclass and free its base's tp_subclasses, then check that the slots still agree with the MRO. Co-authored with Claude Fable 5.2
1 parent 24499ff commit 5d1926d

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

‎Lib/test/test_free_threading/test_type.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,58 @@ class Both(Override, Inherit):
534534
self.assertEqual(repr(obj), "new")
535535
self.assertEqual(repr(Both()), "override")
536536

537+
def test_slot_update_unchanged_race(self):
538+
# Replacing a Python function with another one does not stop the
539+
# world if no slot changes. That is decided by looking at the slots
540+
# of the subclasses, while other threads change those slots and the
541+
# subclasses. At the end, the slots must agree with the MRO.
542+
N = 100
543+
544+
class Base:
545+
def __repr__(self):
546+
return "base"
547+
548+
class Target(Base):
549+
pass
550+
551+
class Native(Base):
552+
__repr__ = object.__repr__
553+
554+
class Inherit(Target, Native):
555+
pass
556+
557+
def swapper():
558+
# No slot changes.
559+
for _ in range(N):
560+
Base.__repr__ = lambda self: "base"
561+
562+
def toggler():
563+
# Inherit's slot alternates between generic and Native's.
564+
for _ in range(N):
565+
Target.__repr__ = lambda self: "target"
566+
del Target.__repr__
567+
568+
def rebaser():
569+
# Target's only subclass comes and goes.
570+
for _ in range(N):
571+
Inherit.__bases__ = (Native,)
572+
Inherit.__bases__ = (Target, Native)
573+
574+
def reader():
575+
for _ in range(N):
576+
self.assertEqual(repr(Base()), "base")
577+
self.assertIn(repr(Target()), ("base", "target"))
578+
repr(Inherit())
579+
580+
threading_helper.run_concurrently(
581+
[swapper, swapper, toggler, rebaser, reader] + world_stoppers())
582+
583+
obj = Inherit()
584+
self.assertEqual(repr(Target()), "base")
585+
self.assertEqual(repr(obj), object.__repr__(obj))
586+
Target.__repr__ = lambda self: "new"
587+
self.assertEqual(repr(obj), "new")
588+
537589
def test_slot_update_skips_non_str_keys(self):
538590
# Slots are computed (with the world stopped, for an update) without
539591
# comparing the slot name with non-str keys of the type dicts, which

0 commit comments

Comments
 (0)