@@ -440,8 +440,11 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
440440 return NULL ;
441441
442442 /* keep this for bw compatibility */
443- if (PyDict_GetItemString (result -> tp_dict , "_abstract_" ))
443+ PyObject * abstract = PyDict_GetItemRefString (result -> tp_dict , "_abstract_" );
444+ if (abstract ) {
445+ Py_DECREF (abstract );
444446 return (PyObject * )result ;
447+ }
445448
446449 dict = (StgDictObject * )_PyObject_CallNoArg ((PyObject * )& PyCStgDict_Type );
447450 if (!dict ) {
@@ -464,7 +467,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
464467
465468 dict -> paramfunc = StructUnionType_paramfunc ;
466469
467- fields = PyDict_GetItemString ((PyObject * )dict , "_fields_" );
470+ fields = PyDict_GetItemRefString ((PyObject * )dict , "_fields_" );
468471 if (!fields ) {
469472 StgDictObject * basedict = PyType_stgdict ((PyObject * )result -> tp_base );
470473
@@ -482,8 +485,10 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
482485
483486 if (-1 == PyObject_SetAttrString ((PyObject * )result , "_fields_" , fields )) {
484487 Py_DECREF (result );
488+ Py_DECREF (fields );
485489 return NULL ;
486490 }
491+ Py_DECREF (fields );
487492 return (PyObject * )result ;
488493}
489494
@@ -987,10 +992,11 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
987992 stgdict -> paramfunc = PyCPointerType_paramfunc ;
988993 stgdict -> flags |= TYPEFLAG_ISPOINTER ;
989994
990- proto = PyDict_GetItemString (typedict , "_type_" ); /* Borrowed ref */
995+ proto = PyDict_GetItemRefString (typedict , "_type_" );
991996 Py_DECREF (typedict );
992997 if (proto && -1 == PyCPointerType_SetProto (stgdict , proto )) {
993998 Py_DECREF ((PyObject * )stgdict );
999+ Py_DECREF (proto );
9941000 return NULL ;
9951001 }
9961002
@@ -1014,8 +1020,10 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10141020 }
10151021 if (stgdict -> format == NULL ) {
10161022 Py_DECREF ((PyObject * )stgdict );
1023+ Py_DECREF (proto );
10171024 return NULL ;
10181025 }
1026+ Py_DECREF (proto );
10191027 }
10201028
10211029 /* create the new instance (which is a class,
@@ -2298,47 +2306,50 @@ make_funcptrtype_dict(StgDictObject *stgdict)
22982306 stgdict -> getfunc = NULL ;
22992307 stgdict -> ffi_type_pointer = ffi_type_pointer ;
23002308
2301- ob = PyDict_GetItemString ((PyObject * )stgdict , "_flags_" );
2309+ ob = PyDict_GetItemRefString ((PyObject * )stgdict , "_flags_" );
23022310 if (!ob || !PyLong_Check (ob )) {
2311+ Py_XDECREF (ob );
23032312 PyErr_SetString (PyExc_TypeError ,
23042313 "class must define _flags_ which must be an integer" );
23052314 return -1 ;
23062315 }
23072316 stgdict -> flags = PyLong_AS_LONG (ob ) | TYPEFLAG_ISPOINTER ;
2317+ Py_DECREF (ob );
23082318
23092319 /* _argtypes_ is optional... */
2310- ob = PyDict_GetItemString ((PyObject * )stgdict , "_argtypes_" );
2320+ ob = PyDict_GetItemRefString ((PyObject * )stgdict , "_argtypes_" );
23112321 if (ob ) {
23122322 converters = converters_from_argtypes (ob );
2313- if (!converters )
2323+ if (!converters ) {
2324+ Py_DECREF (ob );
23142325 goto error ;
2315- Py_INCREF ( ob );
2326+ }
23162327 stgdict -> argtypes = ob ;
23172328 stgdict -> converters = converters ;
23182329 }
23192330
2320- ob = PyDict_GetItemString ((PyObject * )stgdict , "_restype_" );
2331+ ob = PyDict_GetItemRefString ((PyObject * )stgdict , "_restype_" );
23212332 if (ob ) {
23222333 if (ob != Py_None && !PyType_stgdict (ob ) && !PyCallable_Check (ob )) {
2334+ Py_DECREF (ob );
23232335 PyErr_SetString (PyExc_TypeError ,
23242336 "_restype_ must be a type, a callable, or None" );
23252337 return -1 ;
23262338 }
2327- Py_INCREF (ob );
23282339 stgdict -> restype = ob ;
23292340 stgdict -> checker = PyObject_GetAttrString (ob , "_check_retval_" );
23302341 if (stgdict -> checker == NULL )
23312342 PyErr_Clear ();
23322343 }
23332344/* XXX later, maybe.
2334- ob = PyDict_GetItemString ((PyObject *)stgdict, "_errcheck_");
2345+ ob = PyDict_GetItemRefString ((PyObject *)stgdict, "_errcheck_");
23352346 if (ob) {
23362347 if (!PyCallable_Check(ob)) {
23372348 PyErr_SetString(PyExc_TypeError,
23382349 "_errcheck_ must be callable");
2350+ Py_DECREF(ob);
23392351 return -1;
23402352 }
2341- Py_INCREF(ob);
23422353 stgdict->errcheck = ob;
23432354 }
23442355*/
@@ -3546,7 +3557,11 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
35463557 like that.
35473558 */
35483559/*
3549- if (kwds && PyDict_GetItemString(kwds, "options")) {
3560+ if (kwds) {
3561+ PyObject *options = PyDict_GetItemRefString(kwds, "options");
3562+ if (options) {
3563+ Py_DECREF(options);
3564+ }
35503565 ...
35513566 }
35523567*/
@@ -4181,7 +4196,7 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
41814196 }
41824197
41834198 dict = PyType_stgdict ((PyObject * )type );
4184- fields = PyDict_GetItemString ((PyObject * )dict , "_fields_" );
4199+ fields = PyDict_GetItemRefString ((PyObject * )dict , "_fields_" );
41854200 if (fields == NULL )
41864201 return index ;
41874202
@@ -4192,14 +4207,13 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
41924207 PyObject * name , * val ;
41934208 int res ;
41944209 if (!pair )
4195- return -1 ;
4210+ goto error ;
41964211 name = PySequence_GetItem (pair , 0 );
41974212 if (!name ) {
41984213 Py_DECREF (pair );
4199- return -1 ;
4214+ goto error ;
42004215 }
42014216 val = PyTuple_GetItemRef (args , i + index );
4202- Py_DECREF (val );
42034217 if (kwds ) {
42044218 PyObject * existing = PyDict_GetItemRef (kwds , name );
42054219 if (existing ) {
@@ -4209,17 +4223,22 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
42094223 Py_DECREF (existing );
42104224 Py_DECREF (pair );
42114225 Py_DECREF (name );
4212- return -1 ;
4226+ Py_DECREF (val );
4227+ goto error ;
42134228 }
42144229 }
42154230
42164231 res = PyObject_SetAttr (self , name , val );
4232+ Py_DECREF (val );
42174233 Py_DECREF (pair );
42184234 Py_DECREF (name );
42194235 if (res == -1 )
4220- return -1 ;
4236+ goto error ;
42214237 }
42224238 return index + dict -> length ;
4239+ error :
4240+ Py_DECREF (fields );
4241+ return -1 ;
42234242}
42244243
42254244static int
0 commit comments