@@ -1213,16 +1213,27 @@ PyObject_HasAttrString(PyObject *obj, const char *name)
12131213int
12141214PyObject_SetAttrString (PyObject * v , const char * name , PyObject * w )
12151215{
1216- PyObject * s ;
1217- int res ;
1216+ PyThreadState * tstate = _PyThreadState_GET ();
1217+ if (w == NULL && _PyErr_Occurred (tstate )) {
1218+ PyObject * exc = _PyErr_GetRaisedException (tstate );
1219+ _PyErr_SetString (tstate , PyExc_SystemError ,
1220+ "PyObject_SetAttrString() must not be called with NULL value "
1221+ "and an exception set" );
1222+ _PyErr_ChainExceptions1Tstate (tstate , exc );
1223+ return -1 ;
1224+ }
12181225
1219- if (Py_TYPE (v )-> tp_setattr != NULL )
1226+ if (Py_TYPE (v )-> tp_setattr != NULL ) {
12201227 return (* Py_TYPE (v )-> tp_setattr )(v , (char * )name , w );
1221- s = PyUnicode_InternFromString (name );
1222- if (s == NULL )
1228+ }
1229+
1230+ PyObject * s = PyUnicode_InternFromString (name );
1231+ if (s == NULL ) {
12231232 return -1 ;
1224- res = PyObject_SetAttr (v , s , w );
1225- Py_XDECREF (s );
1233+ }
1234+
1235+ int res = PyObject_SetAttr (v , s , w );
1236+ Py_DECREF (s );
12261237 return res ;
12271238}
12281239
@@ -1440,6 +1451,16 @@ PyObject_HasAttr(PyObject *obj, PyObject *name)
14401451int
14411452PyObject_SetAttr (PyObject * v , PyObject * name , PyObject * value )
14421453{
1454+ PyThreadState * tstate = _PyThreadState_GET ();
1455+ if (value == NULL && _PyErr_Occurred (tstate )) {
1456+ PyObject * exc = _PyErr_GetRaisedException (tstate );
1457+ _PyErr_SetString (tstate , PyExc_SystemError ,
1458+ "PyObject_SetAttr() must not be called with NULL value "
1459+ "and an exception set" );
1460+ _PyErr_ChainExceptions1Tstate (tstate , exc );
1461+ return -1 ;
1462+ }
1463+
14431464 PyTypeObject * tp = Py_TYPE (v );
14441465 int err ;
14451466
@@ -1451,8 +1472,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
14511472 }
14521473 Py_INCREF (name );
14531474
1454- PyInterpreterState * interp = _PyInterpreterState_GET ();
1455- _PyUnicode_InternMortal (interp , & name );
1475+ _PyUnicode_InternMortal (tstate -> interp , & name );
14561476 if (tp -> tp_setattro != NULL ) {
14571477 err = (* tp -> tp_setattro )(v , name , value );
14581478 Py_DECREF (name );
0 commit comments