@@ -243,26 +243,13 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
243
243
static int
244
244
_PyDict_GetItemProxy (PyObject * dict , PyObject * key , PyObject * * presult )
245
245
{
246
- PyObject * item = PyDict_GetItemWithError (dict , key );
247
- if (item == NULL ) {
248
- if (PyErr_Occurred ()) {
249
- return -1 ;
250
- }
251
- * presult = NULL ;
252
- return 0 ;
246
+ int rc = PyDict_GetItemRef (dict , key , presult );
247
+ PyObject * item = * presult ;
248
+ if (item && PyWeakref_CheckProxy (item )) {
249
+ rc = PyWeakref_GetRef (item , presult );
250
+ Py_DECREF (item );
253
251
}
254
-
255
- if (!PyWeakref_CheckProxy (item )) {
256
- * presult = Py_NewRef (item );
257
- return 0 ;
258
- }
259
- PyObject * ref ;
260
- if (PyWeakref_GetRef (item , & ref ) < 0 ) {
261
- return -1 ;
262
- }
263
- // ref is NULL if the referenced object was destroyed
264
- * presult = ref ;
265
- return 0 ;
252
+ return rc ;
266
253
}
267
254
268
255
/******************************************************************/
@@ -565,18 +552,19 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
565
552
566
553
dict -> paramfunc = StructUnionType_paramfunc ;
567
554
568
- fields = PyDict_GetItemWithError ((PyObject * )dict , & _Py_ID (_fields_ ));
555
+ if (PyDict_GetItemRef ((PyObject * )dict , & _Py_ID (_fields_ ), & fields ) < 0 ) {
556
+ Py_DECREF (result );
557
+ return NULL ;
558
+ }
569
559
if (fields ) {
570
560
if (PyObject_SetAttr ((PyObject * )result , & _Py_ID (_fields_ ), fields ) < 0 ) {
571
561
Py_DECREF (result );
562
+ Py_DECREF (fields );
572
563
return NULL ;
573
564
}
565
+ Py_DECREF (fields );
574
566
return (PyObject * )result ;
575
567
}
576
- else if (PyErr_Occurred ()) {
577
- Py_DECREF (result );
578
- return NULL ;
579
- }
580
568
else {
581
569
StgDictObject * basedict = PyType_stgdict ((PyObject * )result -> tp_base );
582
570
@@ -1110,11 +1098,15 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1110
1098
stgdict -> paramfunc = PyCPointerType_paramfunc ;
1111
1099
stgdict -> flags |= TYPEFLAG_ISPOINTER ;
1112
1100
1113
- proto = PyDict_GetItemWithError (typedict , & _Py_ID (_type_ )); /* Borrowed ref */
1101
+ if (PyDict_GetItemRef (typedict , & _Py_ID (_type_ ), & proto ) < 0 ) {
1102
+ Py_DECREF ((PyObject * )stgdict );
1103
+ return NULL ;
1104
+ }
1114
1105
if (proto ) {
1115
1106
StgDictObject * itemdict ;
1116
1107
const char * current_format ;
1117
1108
if (-1 == PyCPointerType_SetProto (stgdict , proto )) {
1109
+ Py_DECREF (proto );
1118
1110
Py_DECREF ((PyObject * )stgdict );
1119
1111
return NULL ;
1120
1112
}
@@ -1134,15 +1126,12 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1134
1126
} else {
1135
1127
stgdict -> format = _ctypes_alloc_format_string ("&" , current_format );
1136
1128
}
1129
+ Py_DECREF (proto );
1137
1130
if (stgdict -> format == NULL ) {
1138
1131
Py_DECREF ((PyObject * )stgdict );
1139
1132
return NULL ;
1140
1133
}
1141
1134
}
1142
- else if (PyErr_Occurred ()) {
1143
- Py_DECREF ((PyObject * )stgdict );
1144
- return NULL ;
1145
- }
1146
1135
1147
1136
/* create the new instance (which is a class,
1148
1137
since we are a metatype!) */
@@ -2461,58 +2450,61 @@ make_funcptrtype_dict(StgDictObject *stgdict)
2461
2450
stgdict -> getfunc = NULL ;
2462
2451
stgdict -> ffi_type_pointer = ffi_type_pointer ;
2463
2452
2464
- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_flags_ ));
2453
+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_flags_ ), & ob ) < 0 ) {
2454
+ return -1 ;
2455
+ }
2465
2456
if (!ob || !PyLong_Check (ob )) {
2466
- if (!PyErr_Occurred ()) {
2467
- PyErr_SetString (PyExc_TypeError ,
2457
+ PyErr_SetString (PyExc_TypeError ,
2468
2458
"class must define _flags_ which must be an integer" );
2469
- }
2459
+ Py_XDECREF ( ob );
2470
2460
return -1 ;
2471
2461
}
2472
2462
stgdict -> flags = PyLong_AsUnsignedLongMask (ob ) | TYPEFLAG_ISPOINTER ;
2463
+ Py_DECREF (ob );
2473
2464
2474
2465
/* _argtypes_ is optional... */
2475
- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_argtypes_ ));
2466
+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_argtypes_ ), & ob ) < 0 ) {
2467
+ return -1 ;
2468
+ }
2476
2469
if (ob ) {
2477
2470
converters = converters_from_argtypes (ob );
2478
- if (!converters )
2471
+ if (!converters ) {
2472
+ Py_DECREF (ob );
2479
2473
return -1 ;
2480
- stgdict -> argtypes = Py_NewRef (ob );
2474
+ }
2475
+ stgdict -> argtypes = ob ;
2481
2476
stgdict -> converters = converters ;
2482
2477
}
2483
- else if (PyErr_Occurred ()) {
2478
+
2479
+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_restype_ ), & ob ) < 0 ) {
2484
2480
return -1 ;
2485
2481
}
2486
-
2487
- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_restype_ ));
2488
2482
if (ob ) {
2489
2483
if (ob != Py_None && !PyType_stgdict (ob ) && !PyCallable_Check (ob )) {
2490
2484
PyErr_SetString (PyExc_TypeError ,
2491
2485
"_restype_ must be a type, a callable, or None" );
2486
+ Py_DECREF (ob );
2492
2487
return -1 ;
2493
2488
}
2494
- stgdict -> restype = Py_NewRef ( ob ) ;
2489
+ stgdict -> restype = ob ;
2495
2490
if (PyObject_GetOptionalAttr (ob , & _Py_ID (_check_retval_ ),
2496
2491
& stgdict -> checker ) < 0 )
2497
2492
{
2498
2493
return -1 ;
2499
2494
}
2500
2495
}
2501
- else if (PyErr_Occurred ()) {
2496
+ /* XXX later, maybe.
2497
+ if (PyDict_GetItemRef((PyObject *)stgdict, &_Py _ID(_errcheck_), &ob) < 0) {
2502
2498
return -1;
2503
2499
}
2504
- /* XXX later, maybe.
2505
- ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__errcheck_);
2506
2500
if (ob) {
2507
2501
if (!PyCallable_Check(ob)) {
2508
2502
PyErr_SetString(PyExc_TypeError,
2509
2503
"_errcheck_ must be callable");
2504
+ Py_DECREF(ob);
2510
2505
return -1;
2511
2506
}
2512
- stgdict->errcheck = Py_NewRef(ob);
2513
- }
2514
- else if (PyErr_Occurred()) {
2515
- return -1;
2507
+ stgdict->errcheck = ob;
2516
2508
}
2517
2509
*/
2518
2510
return 0 ;
@@ -3812,13 +3804,12 @@ _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje
3812
3804
return Py_NewRef (v );
3813
3805
}
3814
3806
if (kwds && name ) {
3815
- v = PyDict_GetItemWithError (kwds , name );
3807
+ if (PyDict_GetItemRef (kwds , name , & v ) < 0 ) {
3808
+ return NULL ;
3809
+ }
3816
3810
if (v ) {
3817
3811
++ * pindex ;
3818
- return Py_NewRef (v );
3819
- }
3820
- else if (PyErr_Occurred ()) {
3821
- return NULL ;
3812
+ return v ;
3822
3813
}
3823
3814
}
3824
3815
if (defval ) {
@@ -4870,15 +4861,12 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
4870
4861
return NULL ;
4871
4862
4872
4863
PyObject * result ;
4873
- if (_PyDict_GetItemProxy (cache , key , & result ) < 0 ) {
4874
- Py_DECREF (key );
4875
- return NULL ;
4876
- }
4877
- if (result ) {
4864
+ if (_PyDict_GetItemProxy (cache , key , & result ) != 0 ) {
4865
+ // found or error
4878
4866
Py_DECREF (key );
4879
4867
return result ;
4880
4868
}
4881
-
4869
+ // not found
4882
4870
if (!PyType_Check (itemtype )) {
4883
4871
PyErr_SetString (PyExc_TypeError ,
4884
4872
"Expected a type object" );
0 commit comments