Skip to content

Commit 16055c1

Browse files
gh-111789: Simplify ceval.c by using PyDict_GetItemRef() (GH-111980)
1 parent 9536562 commit 16055c1

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

Python/ceval.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,14 +1592,14 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
15921592
continue;
15931593
PyObject *varname = PyTuple_GET_ITEM(co->co_localsplusnames, i);
15941594
if (func->func_kwdefaults != NULL) {
1595-
PyObject *def = PyDict_GetItemWithError(func->func_kwdefaults, varname);
1595+
PyObject *def;
1596+
if (PyDict_GetItemRef(func->func_kwdefaults, varname, &def) < 0) {
1597+
goto fail_post_args;
1598+
}
15961599
if (def) {
1597-
localsplus[i] = Py_NewRef(def);
1600+
localsplus[i] = def;
15981601
continue;
15991602
}
1600-
else if (_PyErr_Occurred(tstate)) {
1601-
goto fail_post_args;
1602-
}
16031603
}
16041604
missing++;
16051605
}
@@ -2401,13 +2401,9 @@ PyEval_GetBuiltins(void)
24012401
PyObject *
24022402
_PyEval_GetBuiltin(PyObject *name)
24032403
{
2404-
PyThreadState *tstate = _PyThreadState_GET();
2405-
PyObject *attr = PyDict_GetItemWithError(PyEval_GetBuiltins(), name);
2406-
if (attr) {
2407-
Py_INCREF(attr);
2408-
}
2409-
else if (!_PyErr_Occurred(tstate)) {
2410-
_PyErr_SetObject(tstate, PyExc_AttributeError, name);
2404+
PyObject *attr;
2405+
if (PyDict_GetItemRef(PyEval_GetBuiltins(), name, &attr) == 0) {
2406+
PyErr_SetObject(PyExc_AttributeError, name);
24112407
}
24122408
return attr;
24132409
}
@@ -2558,12 +2554,12 @@ static PyObject *
25582554
import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
25592555
PyObject *name, PyObject *fromlist, PyObject *level)
25602556
{
2561-
PyObject *import_func = _PyDict_GetItemWithError(frame->f_builtins,
2562-
&_Py_ID(__import__));
2557+
PyObject *import_func;
2558+
if (PyDict_GetItemRef(frame->f_builtins, &_Py_ID(__import__), &import_func) < 0) {
2559+
return NULL;
2560+
}
25632561
if (import_func == NULL) {
2564-
if (!_PyErr_Occurred(tstate)) {
2565-
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
2566-
}
2562+
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
25672563
return NULL;
25682564
}
25692565

@@ -2574,6 +2570,7 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
25742570

25752571
/* Fast path for not overloaded __import__. */
25762572
if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) {
2573+
Py_DECREF(import_func);
25772574
int ilevel = PyLong_AsInt(level);
25782575
if (ilevel == -1 && _PyErr_Occurred(tstate)) {
25792576
return NULL;
@@ -2587,7 +2584,6 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
25872584
}
25882585

25892586
PyObject* args[5] = {name, frame->f_globals, locals, fromlist, level};
2590-
Py_INCREF(import_func);
25912587
PyObject *res = PyObject_Vectorcall(import_func, args, 5, NULL);
25922588
Py_DECREF(import_func);
25932589
return res;

0 commit comments

Comments
 (0)