@@ -243,26 +243,19 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
243243static int
244244_PyDict_GetItemProxy (PyObject * dict , PyObject * key , PyObject * * presult )
245245{
246- PyObject * item = PyDict_GetItemWithError (dict , key );
247- if (item == NULL ) {
248- if (PyErr_Occurred ()) {
249- return -1 ;
250- }
246+ int rc = PyDict_GetItemRef (dict , key , presult );
247+ if (rc <= 0 ) { // error or not found
251248 * presult = NULL ;
252- return 0 ;
249+ return rc ;
253250 }
254251
252+ PyObject * item = * presult ;
255253 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 ;
254+ return 1 ;
262255 }
263- // ref is NULL if the referenced object was destroyed
264- * presult = ref ;
265- return 0 ;
256+ rc = PyWeakref_GetRef ( item , presult );
257+ Py_DECREF ( item ) ;
258+ return rc ;
266259}
267260
268261/******************************************************************/
@@ -565,18 +558,19 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
565558
566559 dict -> paramfunc = StructUnionType_paramfunc ;
567560
568- fields = PyDict_GetItemWithError ((PyObject * )dict , & _Py_ID (_fields_ ));
561+ if (PyDict_GetItemRef ((PyObject * )dict , & _Py_ID (_fields_ ), & fields ) < 0 ) {
562+ Py_DECREF (result );
563+ return NULL ;
564+ }
569565 if (fields ) {
570566 if (PyObject_SetAttr ((PyObject * )result , & _Py_ID (_fields_ ), fields ) < 0 ) {
571567 Py_DECREF (result );
568+ Py_DECREF (fields );
572569 return NULL ;
573570 }
571+ Py_DECREF (fields );
574572 return (PyObject * )result ;
575573 }
576- else if (PyErr_Occurred ()) {
577- Py_DECREF (result );
578- return NULL ;
579- }
580574 else {
581575 StgDictObject * basedict = PyType_stgdict ((PyObject * )result -> tp_base );
582576
@@ -1110,11 +1104,15 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11101104 stgdict -> paramfunc = PyCPointerType_paramfunc ;
11111105 stgdict -> flags |= TYPEFLAG_ISPOINTER ;
11121106
1113- proto = PyDict_GetItemWithError (typedict , & _Py_ID (_type_ )); /* Borrowed ref */
1107+ if (PyDict_GetItemRef (typedict , & _Py_ID (_type_ ), & proto ) < 0 ) {
1108+ Py_DECREF ((PyObject * )stgdict );
1109+ return NULL ;
1110+ }
11141111 if (proto ) {
11151112 StgDictObject * itemdict ;
11161113 const char * current_format ;
11171114 if (-1 == PyCPointerType_SetProto (stgdict , proto )) {
1115+ Py_DECREF (proto );
11181116 Py_DECREF ((PyObject * )stgdict );
11191117 return NULL ;
11201118 }
@@ -1134,15 +1132,12 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11341132 } else {
11351133 stgdict -> format = _ctypes_alloc_format_string ("&" , current_format );
11361134 }
1135+ Py_DECREF (proto );
11371136 if (stgdict -> format == NULL ) {
11381137 Py_DECREF ((PyObject * )stgdict );
11391138 return NULL ;
11401139 }
11411140 }
1142- else if (PyErr_Occurred ()) {
1143- Py_DECREF ((PyObject * )stgdict );
1144- return NULL ;
1145- }
11461141
11471142 /* create the new instance (which is a class,
11481143 since we are a metatype!) */
@@ -2461,58 +2456,61 @@ make_funcptrtype_dict(StgDictObject *stgdict)
24612456 stgdict -> getfunc = NULL ;
24622457 stgdict -> ffi_type_pointer = ffi_type_pointer ;
24632458
2464- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_flags_ ));
2459+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_flags_ ), & ob ) < 0 ) {
2460+ return -1 ;
2461+ }
24652462 if (!ob || !PyLong_Check (ob )) {
2466- if (!PyErr_Occurred ()) {
2467- PyErr_SetString (PyExc_TypeError ,
2463+ PyErr_SetString (PyExc_TypeError ,
24682464 "class must define _flags_ which must be an integer" );
2469- }
2465+ Py_XDECREF ( ob );
24702466 return -1 ;
24712467 }
24722468 stgdict -> flags = PyLong_AsUnsignedLongMask (ob ) | TYPEFLAG_ISPOINTER ;
2469+ Py_DECREF (ob );
24732470
24742471 /* _argtypes_ is optional... */
2475- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_argtypes_ ));
2472+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_argtypes_ ), & ob ) < 0 ) {
2473+ return -1 ;
2474+ }
24762475 if (ob ) {
24772476 converters = converters_from_argtypes (ob );
2478- if (!converters )
2477+ if (!converters ) {
2478+ Py_DECREF (ob );
24792479 return -1 ;
2480- stgdict -> argtypes = Py_NewRef (ob );
2480+ }
2481+ stgdict -> argtypes = ob ;
24812482 stgdict -> converters = converters ;
24822483 }
2483- else if (PyErr_Occurred ()) {
2484+
2485+ if (PyDict_GetItemRef ((PyObject * )stgdict , & _Py_ID (_restype_ ), & ob ) < 0 ) {
24842486 return -1 ;
24852487 }
2486-
2487- ob = PyDict_GetItemWithError ((PyObject * )stgdict , & _Py_ID (_restype_ ));
24882488 if (ob ) {
24892489 if (ob != Py_None && !PyType_stgdict (ob ) && !PyCallable_Check (ob )) {
24902490 PyErr_SetString (PyExc_TypeError ,
24912491 "_restype_ must be a type, a callable, or None" );
2492+ Py_DECREF (ob );
24922493 return -1 ;
24932494 }
2494- stgdict -> restype = Py_NewRef ( ob ) ;
2495+ stgdict -> restype = ob ;
24952496 if (PyObject_GetOptionalAttr (ob , & _Py_ID (_check_retval_ ),
24962497 & stgdict -> checker ) < 0 )
24972498 {
24982499 return -1 ;
24992500 }
25002501 }
2501- else if (PyErr_Occurred ()) {
2502+ /* XXX later, maybe.
2503+ if (PyDict_GetItemRef((PyObject *)stgdict, &_Py _ID(_errcheck_), &ob) < 0) {
25022504 return -1;
25032505 }
2504- /* XXX later, maybe.
2505- ob = _PyDict_GetItemIdWithError((PyObject *)stgdict, &PyId__errcheck_);
25062506 if (ob) {
25072507 if (!PyCallable_Check(ob)) {
25082508 PyErr_SetString(PyExc_TypeError,
25092509 "_errcheck_ must be callable");
2510+ Py_DECREF(ob);
25102511 return -1;
25112512 }
2512- stgdict->errcheck = Py_NewRef(ob);
2513- }
2514- else if (PyErr_Occurred()) {
2515- return -1;
2513+ stgdict->errcheck = ob;
25162514 }
25172515*/
25182516 return 0 ;
@@ -3812,13 +3810,12 @@ _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje
38123810 return Py_NewRef (v );
38133811 }
38143812 if (kwds && name ) {
3815- v = PyDict_GetItemWithError (kwds , name );
3813+ if (PyDict_GetItemRef (kwds , name , & v ) < 0 ) {
3814+ return NULL ;
3815+ }
38163816 if (v ) {
38173817 ++ * pindex ;
3818- return Py_NewRef (v );
3819- }
3820- else if (PyErr_Occurred ()) {
3821- return NULL ;
3818+ return v ;
38223819 }
38233820 }
38243821 if (defval ) {
@@ -4870,11 +4867,8 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
48704867 return NULL ;
48714868
48724869 PyObject * result ;
4873- if (_PyDict_GetItemProxy (cache , key , & result ) < 0 ) {
4874- Py_DECREF (key );
4875- return NULL ;
4876- }
4877- if (result ) {
4870+ if (_PyDict_GetItemProxy (cache , key , & result ) != 0 ) {
4871+ // error or found
48784872 Py_DECREF (key );
48794873 return result ;
48804874 }
0 commit comments