Skip to content

Commit 9536562

Browse files
gh-111789: Simplify import.c by using PyDict_GetItemRef() (GH-111979)
1 parent c98600b commit 9536562

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

Python/import.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,11 +2372,11 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
23722372
if (nhooks < 0)
23732373
return NULL; /* Shouldn't happen */
23742374

2375-
importer = PyDict_GetItemWithError(path_importer_cache, p);
2376-
if (importer != NULL || _PyErr_Occurred(tstate)) {
2377-
return Py_XNewRef(importer);
2375+
if (PyDict_GetItemRef(path_importer_cache, p, &importer) != 0) {
2376+
// found or error
2377+
return importer;
23782378
}
2379-
2379+
// not found
23802380
/* set path_importer_cache[p] to None to avoid recursion */
23812381
if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
23822382
return NULL;
@@ -2565,7 +2565,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
25652565
{
25662566
PyObject *abs_name;
25672567
PyObject *package = NULL;
2568-
PyObject *spec;
2568+
PyObject *spec = NULL;
25692569
Py_ssize_t last_dot;
25702570
PyObject *base;
25712571
int level_up;
@@ -2578,20 +2578,18 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
25782578
_PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
25792579
goto error;
25802580
}
2581-
package = PyDict_GetItemWithError(globals, &_Py_ID(__package__));
2581+
if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
2582+
goto error;
2583+
}
25822584
if (package == Py_None) {
2585+
Py_DECREF(package);
25832586
package = NULL;
25842587
}
2585-
else if (package == NULL && _PyErr_Occurred(tstate)) {
2586-
goto error;
2587-
}
2588-
spec = PyDict_GetItemWithError(globals, &_Py_ID(__spec__));
2589-
if (spec == NULL && _PyErr_Occurred(tstate)) {
2588+
if (PyDict_GetItemRef(globals, &_Py_ID(__spec__), &spec) < 0) {
25902589
goto error;
25912590
}
25922591

25932592
if (package != NULL) {
2594-
Py_INCREF(package);
25952593
if (!PyUnicode_Check(package)) {
25962594
_PyErr_SetString(tstate, PyExc_TypeError,
25972595
"package must be a string");
@@ -2635,16 +2633,15 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
26352633
goto error;
26362634
}
26372635

2638-
package = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
2636+
if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &package) < 0) {
2637+
goto error;
2638+
}
26392639
if (package == NULL) {
2640-
if (!_PyErr_Occurred(tstate)) {
2641-
_PyErr_SetString(tstate, PyExc_KeyError,
2642-
"'__name__' not in globals");
2643-
}
2640+
_PyErr_SetString(tstate, PyExc_KeyError,
2641+
"'__name__' not in globals");
26442642
goto error;
26452643
}
26462644

2647-
Py_INCREF(package);
26482645
if (!PyUnicode_Check(package)) {
26492646
_PyErr_SetString(tstate, PyExc_TypeError,
26502647
"__name__ must be a string");
@@ -2692,6 +2689,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
26922689
}
26932690
}
26942691

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

27102708
error:
2709+
Py_XDECREF(spec);
27112710
Py_XDECREF(package);
27122711
return NULL;
27132712
}

0 commit comments

Comments
 (0)