@@ -6599,6 +6599,12 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
65996599 }
66006600
66016601 PyTypeObject * oldto = Py_TYPE (self );
6602+ #ifdef Py_GIL_DISABLED
6603+ PyInterpreterState * interp = PyInterpreterState_Get ();
6604+ // The real Py_TYPE(self) (`oldto`) may have changed from
6605+ // underneath us in another thread, so we re-fetch it here.
6606+ _PyEval_StopTheWorld (interp );
6607+ #endif
66026608
66036609 /* In versions of CPython prior to 3.5, the code in
66046610 compatible_for_assignment was not set up to correctly check for memory
@@ -6656,7 +6662,7 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
66566662 PyErr_Format (PyExc_TypeError ,
66576663 "__class__ assignment only supported for mutable types "
66586664 "or ModuleType subclasses" );
6659- return -1 ;
6665+ goto err ;
66606666 }
66616667
66626668 if (compatible_for_assignment (oldto , newto , "__class__" )) {
@@ -6665,47 +6671,48 @@ object_set_class(PyObject *self, PyObject *value, void *closure)
66656671 if (oldto -> tp_flags & Py_TPFLAGS_INLINE_VALUES ) {
66666672 PyDictObject * dict = _PyObject_MaterializeManagedDict (self );
66676673 if (dict == NULL ) {
6668- return -1 ;
6674+ goto err ;
66696675 }
66706676
6671- bool error = false;
6672-
6673- Py_BEGIN_CRITICAL_SECTION2 (self , dict );
6674-
66756677 // If we raced after materialization and replaced the dict
66766678 // then the materialized dict should no longer have the
66776679 // inline values in which case detach is a nop.
6680+ // Note: we don't need to lock here because the world should be stopped.
6681+
66786682 assert (_PyObject_GetManagedDict (self ) == dict ||
66796683 dict -> ma_values != _PyObject_InlineValues (self ));
66806684
66816685 if (_PyDict_DetachFromObject (dict , self ) < 0 ) {
6682- error = true ;
6686+ goto err ;
66836687 }
66846688
6685- Py_END_CRITICAL_SECTION2 ();
6686- if (error ) {
6687- return -1 ;
6688- }
66896689 }
66906690 if (newto -> tp_flags & Py_TPFLAGS_HEAPTYPE ) {
66916691 Py_INCREF (newto );
66926692 }
6693- Py_BEGIN_CRITICAL_SECTION (self );
6694- // The real Py_TYPE(self) (`oldto`) may have changed from
6695- // underneath us in another thread, so we re-fetch it here.
6693+
66966694 oldto = Py_TYPE (self );
66976695 Py_SET_TYPE (self , newto );
6698- Py_END_CRITICAL_SECTION ();
6696+
66996697 if (oldto -> tp_flags & Py_TPFLAGS_HEAPTYPE ) {
67006698 Py_DECREF (oldto );
67016699 }
67026700
67036701 RARE_EVENT_INC (set_class );
6702+
6703+ #ifdef Py_GIL_DISABLED
6704+ _PyEval_StartTheWorld (interp );
6705+ #endif
67046706 return 0 ;
67056707 }
67066708 else {
6707- return -1 ;
6709+ goto err ;
67086710 }
6711+ err :
6712+ #ifdef Py_GIL_DISABLED
6713+ _PyEval_StartTheWorld (interp );
6714+ #endif
6715+ return -1 ;
67096716}
67106717
67116718static PyGetSetDef object_getsets [] = {
0 commit comments