Skip to content

Commit

Permalink
pythongh-111789: Simplify ceval.c by using PyDict_GetItemRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 11, 2023
1 parent fa84e5f commit 030482d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1596,14 +1596,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++;
}
Expand Down Expand Up @@ -2405,13 +2405,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;
}
Expand Down Expand Up @@ -2562,12 +2558,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;
}

Expand All @@ -2578,6 +2574,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;
Expand All @@ -2591,7 +2588,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;
Expand Down

0 comments on commit 030482d

Please sign in to comment.