diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-11-14 09:29:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-14 09:29:49 (GMT) |
commit | 16055c160412544e2a49794aaf3aa70c584f843a (patch) | |
tree | 1a279f39d718457cff8a742e12c01fe75cb09b02 /Python/ceval.c | |
parent | 95365625f4aa8615c94fbfa10f6a0264ca3054db (diff) | |
download | cpython-16055c160412544e2a49794aaf3aa70c584f843a.zip cpython-16055c160412544e2a49794aaf3aa70c584f843a.tar.gz cpython-16055c160412544e2a49794aaf3aa70c584f843a.tar.bz2 |
gh-111789: Simplify ceval.c by using PyDict_GetItemRef() (GH-111980)
Diffstat (limited to 'Python/ceval.c')
-rw-r--r-- | Python/ceval.c | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 584b64a..fe1bf31 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1592,14 +1592,14 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func, continue; PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i); if (func->func_kwdefaults != NULL) { - PyObject *def = PyDict_GetItemWithError(func->func_kwdefaults, varname); + PyObject *def; + if (PyDict_GetItemRef(func->func_kwdefaults, varname, &def) < 0) { + goto fail_post_args; + } if (def) { - localsplus[i] = Py_NewRef(def); + localsplus[i] = def; continue; } - else if (_PyErr_Occurred(tstate)) { - goto fail_post_args; - } } missing++; } @@ -2401,13 +2401,9 @@ PyEval_GetBuiltins(void) PyObject * _PyEval_GetBuiltin(PyObject *name) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *attr = PyDict_GetItemWithError(PyEval_GetBuiltins(), name); - if (attr) { - Py_INCREF(attr); - } - else if (!_PyErr_Occurred(tstate)) { - _PyErr_SetObject(tstate, PyExc_AttributeError, name); + PyObject *attr; + if (PyDict_GetItemRef(PyEval_GetBuiltins(), name, &attr) == 0) { + PyErr_SetObject(PyExc_AttributeError, name); } return attr; } @@ -2558,12 +2554,12 @@ static PyObject * import_name(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject *name, PyObject *fromlist, PyObject *level) { - PyObject *import_func = _PyDict_GetItemWithError(frame->f_builtins, - &_Py_ID(__import__)); + PyObject *import_func; + if (PyDict_GetItemRef(frame->f_builtins, &_Py_ID(__import__), &import_func) < 0) { + return NULL; + } if (import_func == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); - } + _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found"); return NULL; } @@ -2574,6 +2570,7 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame, /* Fast path for not overloaded __import__. */ if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) { + Py_DECREF(import_func); int ilevel = PyLong_AsInt(level); if (ilevel == -1 && _PyErr_Occurred(tstate)) { return NULL; @@ -2587,7 +2584,6 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame, } PyObject* args[5] = {name, frame->f_globals, locals, fromlist, level}; - Py_INCREF(import_func); PyObject *res = PyObject_Vectorcall(import_func, args, 5, NULL); Py_DECREF(import_func); return res; |