Skip to content

Commit

Permalink
pythongh-127316: fix incorrect assertion in setting __class__ in fr…
Browse files Browse the repository at this point in the history
…ee-threading (pythonGH-127399)

(cherry picked from commit 45c5cba)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
  • Loading branch information
kumaraditya303 authored and miss-islington committed Nov 29, 2024
1 parent c4a359d commit 5388279
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Lib/test/test_free_threading/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ def work():
for thread in threads:
thread.join()

def test_object_class_change(self):
class Base:
def __init__(self):
self.attr = 123
class ClassA(Base):
pass
class ClassB(Base):
pass

obj = ClassA()
# keep reference to __dict__
d = obj.__dict__
obj.__class__ = ClassB


def run_one(self, writer_func, reader_func):
writer = Thread(target=writer_func)
readers = []
Expand Down
2 changes: 1 addition & 1 deletion Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -7190,7 +7190,7 @@ _PyDict_DetachFromObject(PyDictObject *mp, PyObject *obj)

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

0 comments on commit 5388279

Please sign in to comment.