mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
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:
parent
fb5db7ec58
commit
b510e101f8
11 changed files with 137 additions and 165 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue