Skip to content

Commit

Permalink
gh-111789: Simplify import.c by using PyDict_GetItemRef() (GH-111979)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Nov 14, 2023
1 parent c98600b commit 9536562
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2372,11 +2372,11 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
if (nhooks < 0)
return NULL; /* Shouldn't happen */

importer = PyDict_GetItemWithError(path_importer_cache, p);
if (importer != NULL || _PyErr_Occurred(tstate)) {
return Py_XNewRef(importer);
if (PyDict_GetItemRef(path_importer_cache, p, &importer) != 0) {
// found or error
return importer;
}

// not found
/* set path_importer_cache[p] to None to avoid recursion */
if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
return NULL;
Expand Down Expand Up @@ -2565,7 +2565,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
{
PyObject *abs_name;
PyObject *package = NULL;
PyObject *spec;
PyObject *spec = NULL;
Py_ssize_t last_dot;
PyObject *base;
int level_up;
Expand All @@ -2578,20 +2578,18 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
_PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
goto error;
}
package = PyDict_GetItemWithError(globals, &_Py_ID(__package__));
if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
goto error;
}
if (package == Py_None) {
Py_DECREF(package);
package = NULL;
}
else if (package == NULL && _PyErr_Occurred(tstate)) {
goto error;
}
spec = PyDict_GetItemWithError(globals, &_Py_ID(__spec__));
if (spec == NULL && _PyErr_Occurred(tstate)) {
if (PyDict_GetItemRef(globals, &_Py_ID(__spec__), &spec) < 0) {
goto error;
}

if (package != NULL) {
Py_INCREF(package);
if (!PyUnicode_Check(package)) {
_PyErr_SetString(tstate, PyExc_TypeError,
"package must be a string");
Expand Down Expand Up @@ -2635,16 +2633,15 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
goto error;
}

package = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &package) < 0) {
goto error;
}
if (package == NULL) {
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetString(tstate, PyExc_KeyError,
"'__name__' not in globals");
}
_PyErr_SetString(tstate, PyExc_KeyError,
"'__name__' not in globals");
goto error;
}

Py_INCREF(package);
if (!PyUnicode_Check(package)) {
_PyErr_SetString(tstate, PyExc_TypeError,
"__name__ must be a string");
Expand Down Expand Up @@ -2692,6 +2689,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
}
}

Py_XDECREF(spec);
base = PyUnicode_Substring(package, 0, last_dot);
Py_DECREF(package);
if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) {
Expand All @@ -2708,6 +2706,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
"with no known parent package");

error:
Py_XDECREF(spec);
Py_XDECREF(package);
return NULL;
}
Expand Down

0 comments on commit 9536562

Please sign in to comment.