Skip to content

Commit

Permalink
bpo-42152: Use PyDict_Contains and PyDict_SetDefault if appropriate. (G…
Browse files Browse the repository at this point in the history
…H-22986)

If PyDict_GetItemWithError is only used to check whether the key is in dict,
it is better to use PyDict_Contains instead.

And if it is used in combination with PyDict_SetItem, PyDict_SetDefault can
replace the combination.
  • Loading branch information
serhiy-storchaka authored Oct 26, 2020
1 parent fb5db7e commit b510e10
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 163 deletions.
21 changes: 10 additions & 11 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,10 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
return NULL;

/* keep this for bw compatibility */
if (_PyDict_GetItemIdWithError(result->tp_dict, &PyId__abstract_))
int r = _PyDict_ContainsId(result->tp_dict, &PyId__abstract_);
if (r > 0)
return (PyObject *)result;
if (PyErr_Occurred()) {
if (r < 0) {
Py_DECREF(result);
return NULL;
}
Expand Down Expand Up @@ -4397,15 +4398,13 @@ _init_pos_args(PyObject *self, PyTypeObject *type,
}
val = PyTuple_GET_ITEM(args, i + index);
if (kwds) {
if (PyDict_GetItemWithError(kwds, name)) {
PyErr_Format(PyExc_TypeError,
"duplicate values for field %R",
name);
Py_DECREF(pair);
Py_DECREF(name);
return -1;
}
else if (PyErr_Occurred()) {
res = PyDict_Contains(kwds, name);
if (res != 0) {
if (res > 0) {
PyErr_Format(PyExc_TypeError,
"duplicate values for field %R",
name);
}
Py_DECREF(pair);
Py_DECREF(name);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ TryAddRef(StgDictObject *dict, CDataObject *obj)
IUnknown *punk;
_Py_IDENTIFIER(_needs_com_addref_);

if (!_PyDict_GetItemIdWithError((PyObject *)dict, &PyId__needs_com_addref_)) {
if (PyErr_Occurred()) {
int r = _PyDict_ContainsId((PyObject *)dict, &PyId__needs_com_addref_);
if (r <= 0) {
if (r < 0) {
PrintError("getting _needs_com_addref_");
}
return;
Expand Down
17 changes: 6 additions & 11 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,26 +2004,21 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
self->fast_nesting = -1;
return 0;
}
if (PyDict_GetItemWithError(self->fast_memo, key)) {
Py_DECREF(key);
int r = PyDict_Contains(self->fast_memo, key);
if (r > 0) {
PyErr_Format(PyExc_ValueError,
"fast mode: can't pickle cyclic objects "
"including object type %.200s at %p",
Py_TYPE(obj)->tp_name, obj);
self->fast_nesting = -1;
return 0;
}
if (PyErr_Occurred()) {
Py_DECREF(key);
self->fast_nesting = -1;
return 0;
else if (r == 0) {
r = PyDict_SetItem(self->fast_memo, key, Py_None);
}
if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Py_DECREF(key);
Py_DECREF(key);
if (r != 0) {
self->fast_nesting = -1;
return 0;
}
Py_DECREF(key);
}
return 1;
}
Expand Down
12 changes: 5 additions & 7 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,13 +1526,11 @@ convertenviron(void)
Py_DECREF(d);
return NULL;
}
if (PyDict_GetItemWithError(d, k) == NULL) {
if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
Py_DECREF(v);
Py_DECREF(k);
Py_DECREF(d);
return NULL;
}
if (PyDict_SetDefault(d, k, v) == NULL) {
Py_DECREF(v);
Py_DECREF(k);
Py_DECREF(d);
return NULL;
}
Py_DECREF(k);
Py_DECREF(v);
Expand Down
10 changes: 1 addition & 9 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,15 +1614,7 @@ static int init_handler_descrs(void)
if (descr == NULL)
return -1;

if (PyDict_GetItemWithError(Xmlparsetype.tp_dict, PyDescr_NAME(descr))) {
Py_DECREF(descr);
continue;
}
else if (PyErr_Occurred()) {
Py_DECREF(descr);
return -1;
}
if (PyDict_SetItem(Xmlparsetype.tp_dict, PyDescr_NAME(descr), descr) < 0) {
if (PyDict_SetDefault(Xmlparsetype.tp_dict, PyDescr_NAME(descr), descr) == NULL) {
Py_DECREF(descr);
return -1;
}
Expand Down
13 changes: 8 additions & 5 deletions Modules/selectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,14 @@ select_poll_modify_impl(pollObject *self, int fd, unsigned short eventmask)
key = PyLong_FromLong(fd);
if (key == NULL)
return NULL;
if (PyDict_GetItemWithError(self->dict, key) == NULL) {
if (!PyErr_Occurred()) {
errno = ENOENT;
PyErr_SetFromErrno(PyExc_OSError);
}
err = PyDict_Contains(self->dict, key);
if (err < 0) {
Py_DECREF(key);
return NULL;
}
if (err == 0) {
errno = ENOENT;
PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(key);
return NULL;
}
Expand Down
45 changes: 22 additions & 23 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2544,8 +2544,8 @@ PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
goto Fail;
}
}
else if (PyDict_GetItemWithError(d, key) == NULL) {
if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
else {
if (PyDict_SetDefault(d, key, value) == NULL) {
Py_DECREF(key);
Py_DECREF(value);
goto Fail;
Expand Down Expand Up @@ -2660,19 +2660,20 @@ dict_merge(PyObject *a, PyObject *b, int override)
Py_INCREF(value);
if (override == 1)
err = insertdict(mp, key, hash, value);
else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
if (PyErr_Occurred()) {
Py_DECREF(value);
Py_DECREF(key);
return -1;
else {
err = _PyDict_Contains_KnownHash(a, key, hash);
if (err == 0) {
err = insertdict(mp, key, hash, value);
}
else if (err > 0) {
if (override != 0) {
_PyErr_SetKeyError(key);
Py_DECREF(value);
Py_DECREF(key);
return -1;
}
err = 0;
}
err = insertdict(mp, key, hash, value);
}
else if (override != 0) {
_PyErr_SetKeyError(key);
Py_DECREF(value);
Py_DECREF(key);
return -1;
}
Py_DECREF(value);
Py_DECREF(key);
Expand Down Expand Up @@ -2709,17 +2710,15 @@ dict_merge(PyObject *a, PyObject *b, int override)

for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
if (override != 1) {
if (PyDict_GetItemWithError(a, key) != NULL) {
if (override != 0) {
status = PyDict_Contains(a, key);
if (status != 0) {
if (status > 0) {
if (override == 0) {
Py_DECREF(key);
continue;
}
_PyErr_SetKeyError(key);
Py_DECREF(key);
Py_DECREF(iter);
return -1;
}
Py_DECREF(key);
continue;
}
else if (PyErr_Occurred()) {
Py_DECREF(key);
Py_DECREF(iter);
return -1;
Expand Down
Loading

0 comments on commit b510e10

Please sign in to comment.