]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127316: fix incorrect assertion in setting `__class__` in free-threading (#127399)
authorKumar Aditya <kumaraditya@python.org>
Fri, 29 Nov 2024 16:14:20 +0000 (21:44 +0530)
committerGitHub <noreply@github.com>
Fri, 29 Nov 2024 16:14:20 +0000 (21:44 +0530)
Lib/test/test_free_threading/test_type.py
Objects/dictobject.c

index 51463b6bb8c1b413f45d7150c19986356b0e64af..53f6d778bbecbc263f699a39702a936eb74caf99 100644 (file)
@@ -124,6 +124,21 @@ class TestType(TestCase):
         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 = []
index 49b213eaa817e2ff995808a453234e972a2e5a6e..a13d8084d14d663cff0c066d8caf104736f83221 100644 (file)
@@ -7300,7 +7300,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);