Skip to content

Commit 876f0d8

Browse files
committed
refactor logic and make it cleaner
1 parent eb3f798 commit 876f0d8

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

Modules/_collectionsmodule.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,16 +2233,9 @@ defdict_missing(PyObject *op, PyObject *key)
22332233
return value;
22342234
PyObject* result;
22352235
int res = PyDict_SetDefaultRef(op, key, value, &result);
2236-
if (res < 0) {
2237-
// set default ref with error
2238-
PyObject *tup;
2239-
tup = PyTuple_Pack(1, key);
2240-
if (!tup) return NULL;
2241-
PyErr_SetObject(PyExc_KeyError, tup);
2242-
Py_DECREF(tup);
2243-
Py_DECREF(value);
2244-
} else if (res > 0) {
2245-
// Key was already in the dict
2236+
if (res != 0) {
2237+
// when res < 0, result will be NULL
2238+
// when res > 0, result is a new reference to the existing value
22462239
Py_DECREF(value);
22472240
}
22482241
return result;

0 commit comments

Comments
 (0)