mirror of
https://github.com/python/cpython.git
synced 2025-07-16 15:55:18 +00:00
Fix #14600. Correct reference handling and naming of ImportError convenience function
This commit is contained in:
parent
fba807ac44
commit
09b86d1196
5 changed files with 37 additions and 59 deletions
|
@ -254,9 +254,9 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
|
|||
theLength));
|
||||
}
|
||||
if (message != NULL) {
|
||||
PyErr_SetFromImportErrorWithNameAndPath(message,
|
||||
PyUnicode_FromString(shortname),
|
||||
pathname);
|
||||
PyErr_SetImportError(message, PyUnicode_FromString(shortname),
|
||||
pathname);
|
||||
Py_DECREF(message);
|
||||
}
|
||||
return NULL;
|
||||
} else {
|
||||
|
|
|
@ -586,50 +586,43 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
|
|||
#endif /* MS_WINDOWS */
|
||||
|
||||
PyObject *
|
||||
PyErr_SetExcWithArgsKwargs(PyObject *exc, PyObject *args, PyObject *kwargs)
|
||||
PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
|
||||
{
|
||||
PyObject *val;
|
||||
PyObject *args, *kwargs, *error;
|
||||
|
||||
args = PyTuple_New(1);
|
||||
if (args == NULL)
|
||||
return NULL;
|
||||
|
||||
kwargs = PyDict_New();
|
||||
if (args == NULL)
|
||||
return NULL;
|
||||
|
||||
if (name == NULL)
|
||||
name = Py_None;
|
||||
|
||||
if (path == NULL)
|
||||
path = Py_None;
|
||||
|
||||
Py_INCREF(msg);
|
||||
PyTuple_SetItem(args, 0, msg);
|
||||
PyDict_SetItemString(kwargs, "name", name);
|
||||
PyDict_SetItemString(kwargs, "path", path);
|
||||
|
||||
/* args must at least be an empty tuple */
|
||||
if (args == NULL)
|
||||
args = PyTuple_New(0);
|
||||
|
||||
val = PyObject_Call(exc, args, kwargs);
|
||||
if (val != NULL) {
|
||||
PyErr_SetObject((PyObject *) Py_TYPE(val), val);
|
||||
Py_DECREF(val);
|
||||
error = PyObject_Call(PyExc_ImportError, args, kwargs);
|
||||
if (error!= NULL) {
|
||||
PyErr_SetObject((PyObject *) Py_TYPE(error), error);
|
||||
Py_DECREF(error);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg,
|
||||
PyObject *name, PyObject *path)
|
||||
{
|
||||
PyObject *args = PyTuple_New(1);
|
||||
PyObject *kwargs = PyDict_New();
|
||||
PyObject *result;
|
||||
|
||||
if (path == NULL)
|
||||
path = Py_None;
|
||||
|
||||
PyTuple_SetItem(args, 0, msg);
|
||||
PyDict_SetItemString(kwargs, "name", name);
|
||||
PyDict_SetItemString(kwargs, "path", path);
|
||||
|
||||
result = PyErr_SetExcWithArgsKwargs(PyExc_ImportError, args, kwargs);
|
||||
|
||||
Py_DECREF(args);
|
||||
Py_DECREF(kwargs);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name)
|
||||
{
|
||||
return PyErr_SetFromImportErrorWithNameAndPath(msg, name, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -2460,7 +2460,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
|
|||
PyObject *msg = PyUnicode_FromFormat("import of %R halted; "
|
||||
"None in sys.modules", abs_name);
|
||||
if (msg != NULL) {
|
||||
PyErr_SetFromImportErrorWithName(msg, abs_name);
|
||||
PyErr_SetImportError(msg, abs_name, NULL);
|
||||
Py_DECREF(msg);
|
||||
}
|
||||
mod = NULL;
|
||||
goto error_with_unlock;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue