aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/import.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-10-26 12:47:57 +0200
committerGitHub <noreply@github.com>2020-10-26 12:47:57 +0200
commitb510e101f8b5b31276bf97b921ca9247162881d2 (patch)
tree582a988d8259c0d92d67fbe739852ea0ec46e4fa /Python/import.c
parentfb5db7ec58624cab0797b4050735be865d380823 (diff)
downloadcpython-b510e101f8b5b31276bf97b921ca9247162881d2.tar.gz
cpython-b510e101f8b5b31276bf97b921ca9247162881d2.zip
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/import.c')
-rw-r--r--Python/import.c24
1 files changed, 14 insertions, 10 deletions
diff --git a/Python/import.c b/Python/import.c
index 26b80f320c3..b79bda058db 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1018,14 +1018,14 @@ module_dict_for_exec(PyThreadState *tstate, PyObject *name)
/* If the module is being reloaded, we get the old module back
and re-use its dict to exec the new code. */
d = PyModule_GetDict(m);
- if (_PyDict_GetItemIdWithError(d, &PyId___builtins__) == NULL) {
- if (_PyErr_Occurred(tstate) ||
- _PyDict_SetItemId(d, &PyId___builtins__,
- PyEval_GetBuiltins()) != 0)
- {
- remove_module(tstate, name);
- return NULL;
- }
+ int r = _PyDict_ContainsId(d, &PyId___builtins__);
+ if (r == 0) {
+ r = _PyDict_SetItemId(d, &PyId___builtins__,
+ PyEval_GetBuiltins());
+ }
+ if (r < 0) {
+ remove_module(tstate, name);
+ return NULL;
}
return d; /* Return a borrowed reference. */
@@ -1660,10 +1660,14 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
goto error;
}
- if (_PyDict_GetItemIdWithError(globals, &PyId___path__) == NULL) {
+ int haspath = _PyDict_ContainsId(globals, &PyId___path__);
+ if (haspath < 0) {
+ goto error;
+ }
+ if (!haspath) {
Py_ssize_t dot;
- if (_PyErr_Occurred(tstate) || PyUnicode_READY(package) < 0) {
+ if (PyUnicode_READY(package) < 0) {
goto error;
}