Skip to content

Commit 3b635cd

Browse files
committed
Close #15766: Catch exceptions while raising the ImportError in imp.load_dynamic()
1 parent f25dabe commit 3b635cd

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Python/dynload_shlib.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,30 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
129129
handle = dlopen(pathname, dlopenflags);
130130

131131
if (handle == NULL) {
132-
PyObject *mod_name = NULL;
133-
PyObject *path = NULL;
134-
PyObject *error_ob = NULL;
132+
PyObject *mod_name;
133+
PyObject *path;
134+
PyObject *error_ob;
135135
const char *error = dlerror();
136136
if (error == NULL)
137137
error = "unknown dlopen() error";
138138
error_ob = PyUnicode_FromString(error);
139-
path = PyUnicode_FromString(pathname);
139+
if (error_ob == NULL)
140+
return NULL;
140141
mod_name = PyUnicode_FromString(shortname);
142+
if (mod_name == NULL) {
143+
Py_DECREF(error_ob);
144+
return NULL;
145+
}
146+
path = PyUnicode_FromString(pathname);
147+
if (path == NULL) {
148+
Py_DECREF(error_ob);
149+
Py_DECREF(mod_name);
150+
return NULL;
151+
}
141152
PyErr_SetImportError(error_ob, mod_name, path);
142-
Py_XDECREF(error_ob);
143-
Py_XDECREF(path);
144-
Py_XDECREF(mod_name);
153+
Py_DECREF(error_ob);
154+
Py_DECREF(mod_name);
155+
Py_DECREF(path);
145156
return NULL;
146157
}
147158
if (fp != NULL && nhandles < 128)

0 commit comments

Comments
 (0)