diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-10-26 10:47:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-26 10:47:57 (GMT) |
commit | b510e101f8b5b31276bf97b921ca9247162881d2 (patch) | |
tree | 582a988d8259c0d92d67fbe739852ea0ec46e4fa /Python/bltinmodule.c | |
parent | fb5db7ec58624cab0797b4050735be865d380823 (diff) | |
download | cpython-b510e101f8b5b31276bf97b921ca9247162881d2.zip cpython-b510e101f8b5b31276bf97b921ca9247162881d2.tar.gz cpython-b510e101f8b5b31276bf97b921ca9247162881d2.tar.bz2 |
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 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 89b7fce..1ce55b6 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -924,12 +924,12 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals, return NULL; } - if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) { - if (_PyDict_SetItemId(globals, &PyId___builtins__, - PyEval_GetBuiltins()) != 0) - return NULL; + int r = _PyDict_ContainsId(globals, &PyId___builtins__); + if (r == 0) { + r = _PyDict_SetItemId(globals, &PyId___builtins__, + PyEval_GetBuiltins()); } - else if (PyErr_Occurred()) { + if (r < 0) { return NULL; } @@ -1012,12 +1012,12 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals, Py_TYPE(locals)->tp_name); return NULL; } - if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) { - if (_PyDict_SetItemId(globals, &PyId___builtins__, - PyEval_GetBuiltins()) != 0) - return NULL; + int r = _PyDict_ContainsId(globals, &PyId___builtins__); + if (r == 0) { + r = _PyDict_SetItemId(globals, &PyId___builtins__, + PyEval_GetBuiltins()); } - else if (PyErr_Occurred()) { + if (r < 0) { return NULL; } |