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.
This commit is contained in:
Serhiy Storchaka 2020-10-26 12:47:57 +02:00 committed by GitHub
parent fb5db7ec58
commit b510e101f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 137 additions and 165 deletions

View file

@ -1098,10 +1098,11 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
goto failure;
}
if (_PyDict_GetItemIdWithError(dict, &PyId___module__) == NULL) {
if (_PyErr_Occurred(tstate)) {
goto failure;
}
int r = _PyDict_ContainsId(dict, &PyId___module__);
if (r < 0) {
goto failure;
}
if (r == 0) {
modulename = PyUnicode_FromStringAndSize(name,
(Py_ssize_t)(dot-name));
if (modulename == NULL)