@@ -1092,14 +1092,9 @@ type_module(PyTypeObject *type, void *context)
1092
1092
1093
1093
if (type -> tp_flags & Py_TPFLAGS_HEAPTYPE ) {
1094
1094
PyObject * dict = lookup_tp_dict (type );
1095
- mod = PyDict_GetItemWithError (dict , & _Py_ID (__module__ ));
1096
- if (mod == NULL ) {
1097
- if (!PyErr_Occurred ()) {
1098
- PyErr_Format (PyExc_AttributeError , "__module__" );
1099
- }
1100
- return NULL ;
1095
+ if (PyDict_GetItemRef (dict , & _Py_ID (__module__ ), & mod ) == 0 ) {
1096
+ PyErr_Format (PyExc_AttributeError , "__module__" );
1101
1097
}
1102
- Py_INCREF (mod );
1103
1098
}
1104
1099
else {
1105
1100
const char * s = strrchr (type -> tp_name , '.' );
@@ -1134,17 +1129,16 @@ type_abstractmethods(PyTypeObject *type, void *context)
1134
1129
PyObject * mod = NULL ;
1135
1130
/* type itself has an __abstractmethods__ descriptor (this). Don't return
1136
1131
that. */
1137
- if (type != & PyType_Type ) {
1138
- PyObject * dict = lookup_tp_dict (type );
1139
- mod = PyDict_GetItemWithError (dict , & _Py_ID (__abstractmethods__ ));
1132
+ if (type == & PyType_Type ) {
1133
+ PyErr_SetObject (PyExc_AttributeError , & _Py_ID (__abstractmethods__ ));
1140
1134
}
1141
- if (!mod ) {
1142
- if (!PyErr_Occurred ()) {
1135
+ else {
1136
+ PyObject * dict = lookup_tp_dict (type );
1137
+ if (PyDict_GetItemRef (dict , & _Py_ID (__abstractmethods__ ), & mod ) == 0 ) {
1143
1138
PyErr_SetObject (PyExc_AttributeError , & _Py_ID (__abstractmethods__ ));
1144
1139
}
1145
- return NULL ;
1146
1140
}
1147
- return Py_NewRef ( mod ) ;
1141
+ return mod ;
1148
1142
}
1149
1143
1150
1144
static int
@@ -1435,18 +1429,12 @@ type_get_doc(PyTypeObject *type, void *context)
1435
1429
return _PyType_GetDocFromInternalDoc (type -> tp_name , type -> tp_doc );
1436
1430
}
1437
1431
PyObject * dict = lookup_tp_dict (type );
1438
- result = PyDict_GetItemWithError (dict , & _Py_ID (__doc__ ));
1439
- if (result == NULL ) {
1440
- if (!PyErr_Occurred ()) {
1441
- result = Py_NewRef (Py_None );
1442
- }
1443
- }
1444
- else if (Py_TYPE (result )-> tp_descr_get ) {
1445
- result = Py_TYPE (result )-> tp_descr_get (result , NULL ,
1446
- (PyObject * )type );
1432
+ if (PyDict_GetItemRef (dict , & _Py_ID (__doc__ ), & result ) == 0 ) {
1433
+ result = Py_NewRef (Py_None );
1447
1434
}
1448
- else {
1449
- Py_INCREF (result );
1435
+ else if (result && Py_TYPE (result )-> tp_descr_get ) {
1436
+ Py_SETREF (result , Py_TYPE (result )-> tp_descr_get (result , NULL ,
1437
+ (PyObject * )type ));
1450
1438
}
1451
1439
return result ;
1452
1440
}
@@ -1477,16 +1465,16 @@ type_get_annotations(PyTypeObject *type, void *context)
1477
1465
1478
1466
PyObject * annotations ;
1479
1467
PyObject * dict = lookup_tp_dict (type );
1480
- annotations = PyDict_GetItemWithError (dict , & _Py_ID (__annotations__ ));
1468
+ if (PyDict_GetItemRef (dict , & _Py_ID (__annotations__ ), & annotations ) < 0 ) {
1469
+ return NULL ;
1470
+ }
1481
1471
if (annotations ) {
1482
1472
if (Py_TYPE (annotations )-> tp_descr_get ) {
1483
- annotations = Py_TYPE (annotations )-> tp_descr_get (
1484
- annotations , NULL , (PyObject * )type );
1485
- } else {
1486
- Py_INCREF (annotations );
1473
+ Py_SETREF (annotations , Py_TYPE (annotations )-> tp_descr_get (
1474
+ annotations , NULL , (PyObject * )type ));
1487
1475
}
1488
1476
}
1489
- else if (! PyErr_Occurred ()) {
1477
+ else {
1490
1478
annotations = PyDict_New ();
1491
1479
if (annotations ) {
1492
1480
int result = PyDict_SetItem (
@@ -1533,16 +1521,11 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
1533
1521
static PyObject *
1534
1522
type_get_type_params (PyTypeObject * type , void * context )
1535
1523
{
1536
- PyObject * params = PyDict_GetItemWithError (lookup_tp_dict (type ), & _Py_ID (__type_params__ ));
1537
-
1538
- if (params ) {
1539
- return Py_NewRef (params );
1540
- }
1541
- if (PyErr_Occurred ()) {
1542
- return NULL ;
1524
+ PyObject * params ;
1525
+ if (PyDict_GetItemRef (lookup_tp_dict (type ), & _Py_ID (__type_params__ ), & params ) == 0 ) {
1526
+ params = PyTuple_New (0 );
1543
1527
}
1544
-
1545
- return PyTuple_New (0 );
1528
+ return params ;
1546
1529
}
1547
1530
1548
1531
static int
@@ -3436,18 +3419,13 @@ type_new_set_module(PyTypeObject *type)
3436
3419
return 0 ;
3437
3420
}
3438
3421
3439
- PyObject * module = PyDict_GetItemWithError (globals , & _Py_ID (__name__ ));
3440
- if (module == NULL ) {
3441
- if (PyErr_Occurred ()) {
3442
- return -1 ;
3443
- }
3444
- return 0 ;
3422
+ PyObject * module ;
3423
+ r = PyDict_GetItemRef (globals , & _Py_ID (__name__ ), & module );
3424
+ if (module ) {
3425
+ r = PyDict_SetItem (dict , & _Py_ID (__module__ ), module );
3426
+ Py_DECREF (module );
3445
3427
}
3446
-
3447
- if (PyDict_SetItem (dict , & _Py_ID (__module__ ), module ) < 0 ) {
3448
- return -1 ;
3449
- }
3450
- return 0 ;
3428
+ return r ;
3451
3429
}
3452
3430
3453
3431
@@ -3458,23 +3436,24 @@ type_new_set_ht_name(PyTypeObject *type)
3458
3436
{
3459
3437
PyHeapTypeObject * et = (PyHeapTypeObject * )type ;
3460
3438
PyObject * dict = lookup_tp_dict (type );
3461
- PyObject * qualname = PyDict_GetItemWithError (dict , & _Py_ID (__qualname__ ));
3439
+ PyObject * qualname ;
3440
+ if (PyDict_GetItemRef (dict , & _Py_ID (__qualname__ ), & qualname ) < 0 ) {
3441
+ return -1 ;
3442
+ }
3462
3443
if (qualname != NULL ) {
3463
3444
if (!PyUnicode_Check (qualname )) {
3464
3445
PyErr_Format (PyExc_TypeError ,
3465
3446
"type __qualname__ must be a str, not %s" ,
3466
3447
Py_TYPE (qualname )-> tp_name );
3448
+ Py_DECREF (qualname );
3467
3449
return -1 ;
3468
3450
}
3469
- et -> ht_qualname = Py_NewRef ( qualname ) ;
3451
+ et -> ht_qualname = qualname ;
3470
3452
if (PyDict_DelItem (dict , & _Py_ID (__qualname__ )) < 0 ) {
3471
3453
return -1 ;
3472
3454
}
3473
3455
}
3474
3456
else {
3475
- if (PyErr_Occurred ()) {
3476
- return -1 ;
3477
- }
3478
3457
et -> ht_qualname = Py_NewRef (et -> ht_name );
3479
3458
}
3480
3459
return 0 ;
@@ -5887,24 +5866,22 @@ _PyType_GetSlotNames(PyTypeObject *cls)
5887
5866
5888
5867
/* Get the slot names from the cache in the class if possible. */
5889
5868
PyObject * dict = lookup_tp_dict (cls );
5890
- slotnames = PyDict_GetItemWithError (dict , & _Py_ID (__slotnames__ ));
5869
+ if (PyDict_GetItemRef (dict , & _Py_ID (__slotnames__ ), & slotnames ) < 0 ) {
5870
+ return NULL ;
5871
+ }
5891
5872
if (slotnames != NULL ) {
5892
5873
if (slotnames != Py_None && !PyList_Check (slotnames )) {
5893
5874
PyErr_Format (PyExc_TypeError ,
5894
5875
"%.200s.__slotnames__ should be a list or None, "
5895
5876
"not %.200s" ,
5896
5877
cls -> tp_name , Py_TYPE (slotnames )-> tp_name );
5878
+ Py_DECREF (slotnames );
5897
5879
return NULL ;
5898
5880
}
5899
- return Py_NewRef (slotnames );
5900
- }
5901
- else {
5902
- if (PyErr_Occurred ()) {
5903
- return NULL ;
5904
- }
5905
- /* The class does not have the slot names cached yet. */
5881
+ return slotnames ;
5906
5882
}
5907
5883
5884
+ /* The class does not have the slot names cached yet. */
5908
5885
copyreg = import_copyreg ();
5909
5886
if (copyreg == NULL )
5910
5887
return NULL ;
@@ -10263,23 +10240,18 @@ _super_lookup_descr(PyTypeObject *su_type, PyTypeObject *su_obj_type, PyObject *
10263
10240
return NULL ;
10264
10241
10265
10242
/* keep a strong reference to mro because su_obj_type->tp_mro can be
10266
- replaced during PyDict_GetItemWithError (dict, name) */
10243
+ replaced during PyDict_GetItemRef (dict, name, &res ) */
10267
10244
Py_INCREF (mro );
10268
10245
do {
10269
10246
PyObject * obj = PyTuple_GET_ITEM (mro , i );
10270
10247
PyObject * dict = lookup_tp_dict (_PyType_CAST (obj ));
10271
10248
assert (dict != NULL && PyDict_Check (dict ));
10272
10249
10273
- res = PyDict_GetItemWithError (dict , name );
10274
- if (res != NULL ) {
10275
- Py_INCREF (res );
10250
+ if (PyDict_GetItemRef (dict , name , & res ) != 0 ) {
10251
+ // error or found
10276
10252
Py_DECREF (mro );
10277
10253
return res ;
10278
10254
}
10279
- else if (PyErr_Occurred ()) {
10280
- Py_DECREF (mro );
10281
- return NULL ;
10282
- }
10283
10255
10284
10256
i ++ ;
10285
10257
} while (i < n );
0 commit comments