@@ -1220,45 +1220,62 @@ functools_wraps(PyObject *wrapper, PyObject *wrapped)
1220
1220
// Used for wrapping __annotations__ and __annotate__ on classmethod
1221
1221
// and staticmethod objects.
1222
1222
static PyObject *
1223
- descriptor_get_wrapped_attribute (PyObject * wrapped , PyObject * dict , PyObject * name )
1223
+ descriptor_get_wrapped_attribute (PyObject * wrapped , PyObject * obj , PyObject * name )
1224
1224
{
1225
+ PyObject * dict = PyObject_GenericGetDict (obj , NULL );
1226
+ if (dict == NULL ) {
1227
+ return NULL ;
1228
+ }
1225
1229
PyObject * res ;
1226
1230
if (PyDict_GetItemRef (dict , name , & res ) < 0 ) {
1231
+ Py_DECREF (dict );
1227
1232
return NULL ;
1228
1233
}
1229
1234
if (res != NULL ) {
1235
+ Py_DECREF (dict );
1230
1236
return res ;
1231
1237
}
1232
1238
res = PyObject_GetAttr (wrapped , name );
1233
1239
if (res == NULL ) {
1240
+ Py_DECREF (dict );
1234
1241
return NULL ;
1235
1242
}
1236
1243
if (PyDict_SetItem (dict , name , res ) < 0 ) {
1244
+ Py_DECREF (dict );
1237
1245
Py_DECREF (res );
1238
1246
return NULL ;
1239
1247
}
1248
+ Py_DECREF (dict );
1240
1249
return res ;
1241
1250
}
1242
1251
1243
1252
static int
1244
- descriptor_set_wrapped_attribute (PyObject * dict , PyObject * name , PyObject * value ,
1253
+ descriptor_set_wrapped_attribute (PyObject * oobj , PyObject * name , PyObject * value ,
1245
1254
char * type_name )
1246
1255
{
1256
+ PyObject * dict = PyObject_GenericGetDict (oobj , NULL );
1257
+ if (dict == NULL ) {
1258
+ return -1 ;
1259
+ }
1247
1260
if (value == NULL ) {
1248
1261
if (PyDict_DelItem (dict , name ) < 0 ) {
1249
1262
if (PyErr_ExceptionMatches (PyExc_KeyError )) {
1250
1263
PyErr_Clear ();
1251
1264
PyErr_Format (PyExc_AttributeError ,
1252
1265
"'%.200s' object has no attribute '%U'" ,
1253
1266
type_name , name );
1267
+ return -1 ;
1254
1268
}
1255
1269
else {
1270
+ Py_DECREF (dict );
1256
1271
return -1 ;
1257
1272
}
1258
1273
}
1274
+ Py_DECREF (dict );
1259
1275
return 0 ;
1260
1276
}
1261
1277
else {
1278
+ Py_DECREF (dict );
1262
1279
return PyDict_SetItem (dict , name , value );
1263
1280
}
1264
1281
}
@@ -1380,28 +1397,26 @@ static PyObject *
1380
1397
cm_get___annotations__ (PyObject * self , void * closure )
1381
1398
{
1382
1399
classmethod * cm = _PyClassMethod_CAST (self );
1383
- return descriptor_get_wrapped_attribute (cm -> cm_callable , cm -> cm_dict , & _Py_ID (__annotations__ ));
1400
+ return descriptor_get_wrapped_attribute (cm -> cm_callable , self , & _Py_ID (__annotations__ ));
1384
1401
}
1385
1402
1386
1403
static int
1387
1404
cm_set___annotations__ (PyObject * self , PyObject * value , void * closure )
1388
1405
{
1389
- classmethod * cm = _PyClassMethod_CAST (self );
1390
- return descriptor_set_wrapped_attribute (cm -> cm_dict , & _Py_ID (__annotations__ ), value , "classmethod" );
1406
+ return descriptor_set_wrapped_attribute (self , & _Py_ID (__annotations__ ), value , "classmethod" );
1391
1407
}
1392
1408
1393
1409
static PyObject *
1394
1410
cm_get___annotate__ (PyObject * self , void * closure )
1395
1411
{
1396
1412
classmethod * cm = _PyClassMethod_CAST (self );
1397
- return descriptor_get_wrapped_attribute (cm -> cm_callable , cm -> cm_dict , & _Py_ID (__annotate__ ));
1413
+ return descriptor_get_wrapped_attribute (cm -> cm_callable , self , & _Py_ID (__annotate__ ));
1398
1414
}
1399
1415
1400
1416
static int
1401
1417
cm_set___annotate__ (PyObject * self , PyObject * value , void * closure )
1402
1418
{
1403
- classmethod * cm = _PyClassMethod_CAST (self );
1404
- return descriptor_set_wrapped_attribute (cm -> cm_dict , & _Py_ID (__annotate__ ), value , "classmethod" );
1419
+ return descriptor_set_wrapped_attribute (self , & _Py_ID (__annotate__ ), value , "classmethod" );
1405
1420
}
1406
1421
1407
1422
@@ -1615,28 +1630,26 @@ static PyObject *
1615
1630
sm_get___annotations__ (PyObject * self , void * closure )
1616
1631
{
1617
1632
staticmethod * sm = _PyStaticMethod_CAST (self );
1618
- return descriptor_get_wrapped_attribute (sm -> sm_callable , sm -> sm_dict , & _Py_ID (__annotations__ ));
1633
+ return descriptor_get_wrapped_attribute (sm -> sm_callable , self , & _Py_ID (__annotations__ ));
1619
1634
}
1620
1635
1621
1636
static int
1622
1637
sm_set___annotations__ (PyObject * self , PyObject * value , void * closure )
1623
1638
{
1624
- staticmethod * sm = _PyStaticMethod_CAST (self );
1625
- return descriptor_set_wrapped_attribute (sm -> sm_dict , & _Py_ID (__annotations__ ), value , "staticmethod" );
1639
+ return descriptor_set_wrapped_attribute (self , & _Py_ID (__annotations__ ), value , "staticmethod" );
1626
1640
}
1627
1641
1628
1642
static PyObject *
1629
1643
sm_get___annotate__ (PyObject * self , void * closure )
1630
1644
{
1631
1645
staticmethod * sm = _PyStaticMethod_CAST (self );
1632
- return descriptor_get_wrapped_attribute (sm -> sm_callable , sm -> sm_dict , & _Py_ID (__annotate__ ));
1646
+ return descriptor_get_wrapped_attribute (sm -> sm_callable , self , & _Py_ID (__annotate__ ));
1633
1647
}
1634
1648
1635
1649
static int
1636
1650
sm_set___annotate__ (PyObject * self , PyObject * value , void * closure )
1637
1651
{
1638
- staticmethod * sm = _PyStaticMethod_CAST (self );
1639
- return descriptor_set_wrapped_attribute (sm -> sm_dict , & _Py_ID (__annotate__ ), value , "staticmethod" );
1652
+ return descriptor_set_wrapped_attribute (self , & _Py_ID (__annotate__ ), value , "staticmethod" );
1640
1653
}
1641
1654
1642
1655
static PyGetSetDef sm_getsetlist [] = {
0 commit comments