Skip to content

Commit 2e9b423

Browse files
pythongh-111789: Use PyDict_GetItemRef()
1 parent 836e0a7 commit 2e9b423

26 files changed

Lines changed: 264 additions & 469 deletions

‎Modules/_asynciomodule.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,15 +3514,11 @@ _asyncio_current_task_impl(PyObject *module, PyObject *loop)
35143514
Py_INCREF(loop);
35153515
}
35163516

3517-
ret = PyDict_GetItemWithError(state->current_tasks, loop);
3517+
int rc = PyDict_GetItemRef(state->current_tasks, loop, &ret);
35183518
Py_DECREF(loop);
3519-
if (ret == NULL && PyErr_Occurred()) {
3520-
return NULL;
3521-
}
3522-
else if (ret == NULL) {
3519+
if (rc == 0) {
35233520
Py_RETURN_NONE;
35243521
}
3525-
Py_INCREF(ret);
35263522
return ret;
35273523
}
35283524

‎Modules/_csv.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,9 @@ static PyObject *
160160
get_dialect_from_registry(PyObject *name_obj, _csvstate *module_state)
161161
{
162162
PyObject *dialect_obj;
163-
164-
dialect_obj = PyDict_GetItemWithError(module_state->dialects, name_obj);
165-
if (dialect_obj == NULL) {
166-
if (!PyErr_Occurred())
167-
PyErr_Format(module_state->error_obj, "unknown dialect");
163+
if (PyDict_GetItemRef(module_state->dialects, name_obj, &dialect_obj) == 0) {
164+
PyErr_SetString(module_state->error_obj, "unknown dialect");
168165
}
169-
else
170-
Py_INCREF(dialect_obj);
171-
172166
return dialect_obj;
173167
}
174168

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,19 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
243243
static 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
}

‎Modules/_ctypes/callproc.c‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,18 @@ _ctypes_get_errobj(int **pspace)
168168
if (error_object_name == NULL)
169169
return NULL;
170170
}
171-
errobj = PyDict_GetItemWithError(dict, error_object_name);
171+
if (PyDict_GetItemRef(dict, error_object_name, &errobj) < 0) {
172+
return NULL;
173+
}
172174
if (errobj) {
173175
if (!PyCapsule_IsValid(errobj, CTYPES_CAPSULE_NAME_PYMEM)) {
174176
PyErr_SetString(PyExc_RuntimeError,
175177
"ctypes.error_object is an invalid capsule");
178+
Py_DECREF(errobj);
176179
return NULL;
177180
}
178-
Py_INCREF(errobj);
179181
}
180-
else if (!PyErr_Occurred()) {
182+
else {
181183
void *space = PyMem_Calloc(2, sizeof(int));
182184
if (space == NULL)
183185
return NULL;
@@ -192,9 +194,6 @@ _ctypes_get_errobj(int **pspace)
192194
return NULL;
193195
}
194196
}
195-
else {
196-
return NULL;
197-
}
198197
*pspace = (int *)PyCapsule_GetPointer(errobj, CTYPES_CAPSULE_NAME_PYMEM);
199198
return errobj;
200199
}
@@ -1922,12 +1921,8 @@ create_pointer_type(PyObject *module, PyObject *cls)
19221921
PyTypeObject *typ;
19231922
PyObject *key;
19241923

1925-
result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
1926-
if (result) {
1927-
return Py_NewRef(result);
1928-
}
1929-
else if (PyErr_Occurred()) {
1930-
return NULL;
1924+
if (PyDict_GetItemRef(_ctypes_ptrtype_cache, cls, &result) != 0) {
1925+
return result;
19311926
}
19321927
if (PyUnicode_CheckExact(cls)) {
19331928
PyObject *name = PyUnicode_FromFormat("LP_%U", cls);
@@ -1986,16 +1981,14 @@ create_pointer_inst(PyObject *module, PyObject *arg)
19861981
PyObject *result;
19871982
PyObject *typ;
19881983

1989-
typ = PyDict_GetItemWithError(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg));
1990-
if (typ) {
1991-
return PyObject_CallOneArg(typ, arg);
1992-
}
1993-
else if (PyErr_Occurred()) {
1984+
if (PyDict_GetItemRef(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg), &typ) < 0) {
19941985
return NULL;
19951986
}
1996-
typ = create_pointer_type(NULL, (PyObject *)Py_TYPE(arg));
1997-
if (typ == NULL)
1998-
return NULL;
1987+
if (typ == NULL) {
1988+
typ = create_pointer_type(NULL, (PyObject *)Py_TYPE(arg));
1989+
if (typ == NULL)
1990+
return NULL;
1991+
}
19991992
result = PyObject_CallOneArg(typ, arg);
20001993
Py_DECREF(typ);
20011994
return result;

‎Modules/_elementtree.c‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -372,26 +372,26 @@ get_attrib_from_keywords(PyObject *kwds)
372372
if (attrib_str == NULL) {
373373
return NULL;
374374
}
375-
PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
376-
377-
if (attrib) {
375+
PyObject *attrib;
376+
if (PyDict_GetItemRef(kwds, attrib_str, &attrib) == 0) {
377+
attrib = PyDict_New();
378+
}
379+
else if (attrib) {
378380
/* If attrib was found in kwds, copy its value and remove it from
379381
* kwds
380382
*/
381383
if (!PyDict_Check(attrib)) {
382384
Py_DECREF(attrib_str);
383385
PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s",
384386
Py_TYPE(attrib)->tp_name);
387+
Py_DECREF(attrib);
385388
return NULL;
386389
}
387-
attrib = PyDict_Copy(attrib);
390+
Py_SETREF(attrib, PyDict_Copy(attrib));
388391
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
389392
Py_SETREF(attrib, NULL);
390393
}
391394
}
392-
else if (!PyErr_Occurred()) {
393-
attrib = PyDict_New();
394-
}
395395

396396
Py_DECREF(attrib_str);
397397

@@ -1421,11 +1421,12 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
14211421
{
14221422
if (self->extra && self->extra->attrib) {
14231423
PyObject *attrib = Py_NewRef(self->extra->attrib);
1424-
PyObject *value = Py_XNewRef(PyDict_GetItemWithError(attrib, key));
1425-
Py_DECREF(attrib);
1426-
if (value != NULL || PyErr_Occurred()) {
1424+
PyObject *value;
1425+
if (PyDict_GetItemRef(attrib, key, &value) != 0) {
1426+
Py_DECREF(attrib);
14271427
return value;
14281428
}
1429+
Py_DECREF(attrib);
14291430
}
14301431

14311432
return Py_NewRef(default_value);
@@ -3085,9 +3086,7 @@ makeuniversal(XMLParserObject* self, const char* string)
30853086
if (!key)
30863087
return NULL;
30873088

3088-
value = Py_XNewRef(PyDict_GetItemWithError(self->names, key));
3089-
3090-
if (value == NULL && !PyErr_Occurred()) {
3089+
if (PyDict_GetItemRef(self->names, key, &value) == 0) {
30913090
/* new name. convert to universal name, and decode as
30923091
necessary */
30933092

‎Modules/_sqlite/cursor.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,12 +737,8 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
737737
if (!binding_name_obj) {
738738
return;
739739
}
740-
if (PyDict_CheckExact(parameters)) {
741-
PyObject *item = PyDict_GetItemWithError(parameters, binding_name_obj);
742-
current_param = Py_XNewRef(item);
743-
} else {
744-
current_param = PyObject_GetItem(parameters, binding_name_obj);
745-
}
740+
PyObject *current_param;
741+
(void)PyMapping_GetOptionalItem(parameters, binding_name_obj, &current_param);
746742
Py_DECREF(binding_name_obj);
747743
if (!current_param) {
748744
if (!PyErr_Occurred() || PyErr_ExceptionMatches(PyExc_LookupError)) {

0 commit comments

Comments
 (0)