diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2020-10-26 12:47:57 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-26 12:47:57 +0200 |
| commit | b510e101f8b5b31276bf97b921ca9247162881d2 (patch) | |
| tree | 582a988d8259c0d92d67fbe739852ea0ec46e4fa /Modules/selectmodule.c | |
| parent | fb5db7ec58624cab0797b4050735be865d380823 (diff) | |
| download | cpython-git-b510e101f8b5b31276bf97b921ca9247162881d2.tar.gz | |
bpo-42152: Use PyDict_Contains and PyDict_SetDefault if appropriate. (GH-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.
Diffstat (limited to 'Modules/selectmodule.c')
| -rw-r--r-- | Modules/selectmodule.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index fe852f93c3..d02e3905f5 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -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; } |
