Skip to content

Commit 03e405f

Browse files
Return new reference from get_path_importer().
1 parent 3e89124 commit 03e405f

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

Python/import.c

+13-19
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,7 @@ is_builtin(PyObject *name)
912912
that can handle the path item. Return None if no hook could;
913913
this tells our caller that the path based finder could not find
914914
a finder for this path item. Cache the result in
915-
path_importer_cache.
916-
Returns a borrowed reference. */
915+
path_importer_cache. */
917916

918917
static PyObject *
919918
get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
@@ -931,8 +930,10 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
931930
return NULL; /* Shouldn't happen */
932931

933932
importer = PyDict_GetItemWithError(path_importer_cache, p);
934-
if (importer != NULL || _PyErr_Occurred(tstate))
933+
if (importer != NULL || _PyErr_Occurred(tstate)) {
934+
Py_XINCREF(importer);
935935
return importer;
936+
}
936937

937938
/* set path_importer_cache[p] to None to avoid recursion */
938939
if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
@@ -952,13 +953,11 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
952953
_PyErr_Clear(tstate);
953954
}
954955
if (importer == NULL) {
955-
return Py_None;
956+
Py_RETURN_NONE;
956957
}
957-
if (importer != NULL) {
958-
int err = PyDict_SetItem(path_importer_cache, p, importer);
958+
if (PyDict_SetItem(path_importer_cache, p, importer) < 0) {
959959
Py_DECREF(importer);
960-
if (err != 0)
961-
return NULL;
960+
return NULL;
962961
}
963962
return importer;
964963
}
@@ -967,16 +966,12 @@ PyObject *
967966
PyImport_GetImporter(PyObject *path)
968967
{
969968
PyThreadState *tstate = _PyThreadState_GET();
970-
PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
971-
972-
path_importer_cache = PySys_GetObject("path_importer_cache");
973-
path_hooks = PySys_GetObject("path_hooks");
974-
if (path_importer_cache != NULL && path_hooks != NULL) {
975-
importer = get_path_importer(tstate, path_importer_cache,
976-
path_hooks, path);
969+
PyObject *path_importer_cache = PySys_GetObject("path_importer_cache");
970+
PyObject *path_hooks = PySys_GetObject("path_hooks");
971+
if (path_importer_cache == NULL || path_hooks == NULL) {
972+
return NULL;
977973
}
978-
Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
979-
return importer;
974+
return get_path_importer(tstate, path_importer_cache, path_hooks, path);
980975
}
981976

982977
static PyObject*
@@ -1178,13 +1173,12 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
11781173
goto err_return;
11791174
}
11801175
m = exec_code_in_module(tstate, name, d, co);
1176+
Py_DECREF(d);
11811177
if (m == NULL) {
1182-
Py_DECREF(d);
11831178
goto err_return;
11841179
}
11851180
Py_DECREF(co);
11861181
Py_DECREF(m);
1187-
Py_DECREF(d);
11881182
return 1;
11891183

11901184
err_return:

0 commit comments

Comments
 (0)