Skip to content

Commit 45c5cba

Browse files
gh-127316: fix incorrect assertion in setting __class__ in free-threading (#127399)
1 parent b14fdad commit 45c5cba

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_free_threading/test_type.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ def work():
124124
for thread in threads:
125125
thread.join()
126126

127+
def test_object_class_change(self):
128+
class Base:
129+
def __init__(self):
130+
self.attr = 123
131+
class ClassA(Base):
132+
pass
133+
class ClassB(Base):
134+
pass
135+
136+
obj = ClassA()
137+
# keep reference to __dict__
138+
d = obj.__dict__
139+
obj.__class__ = ClassB
140+
141+
127142
def run_one(self, writer_func, reader_func):
128143
writer = Thread(target=writer_func)
129144
readers = []

Objects/dictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7300,7 +7300,7 @@ _PyDict_DetachFromObject(PyDictObject *mp, PyObject *obj)
73007300

73017301
// We could be called with an unlocked dict when the caller knows the
73027302
// values are already detached, so we assert after inline values check.
7303-
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(mp);
7303+
ASSERT_WORLD_STOPPED_OR_OBJ_LOCKED(mp);
73047304
assert(mp->ma_values->embedded == 1);
73057305
assert(mp->ma_values->valid == 1);
73067306
assert(Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES);

0 commit comments

Comments
 (0)