mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-91058: Add error suggestions to 'import from' import errors (#98305)
This commit is contained in:
parent
1f737edb67
commit
7cfbb49fcd
11 changed files with 235 additions and 14 deletions
|
@ -975,9 +975,10 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(
|
|||
|
||||
#endif /* MS_WINDOWS */
|
||||
|
||||
PyObject *
|
||||
PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
|
||||
PyObject *name, PyObject *path)
|
||||
static PyObject *
|
||||
_PyErr_SetImportErrorSubclassWithNameFrom(
|
||||
PyObject *exception, PyObject *msg,
|
||||
PyObject *name, PyObject *path, PyObject* from_name)
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
int issubclass;
|
||||
|
@ -1005,6 +1006,10 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
|
|||
if (path == NULL) {
|
||||
path = Py_None;
|
||||
}
|
||||
if (from_name == NULL) {
|
||||
from_name = Py_None;
|
||||
}
|
||||
|
||||
|
||||
kwargs = PyDict_New();
|
||||
if (kwargs == NULL) {
|
||||
|
@ -1016,6 +1021,9 @@ PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
|
|||
if (PyDict_SetItemString(kwargs, "path", path) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (PyDict_SetItemString(kwargs, "name_from", from_name) < 0) {
|
||||
goto done;
|
||||
}
|
||||
|
||||
error = PyObject_VectorcallDict(exception, &msg, 1, kwargs);
|
||||
if (error != NULL) {
|
||||
|
@ -1028,6 +1036,20 @@ done:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
PyObject *
|
||||
PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
|
||||
PyObject *name, PyObject *path)
|
||||
{
|
||||
return _PyErr_SetImportErrorSubclassWithNameFrom(exception, msg, name, path, NULL);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyErr_SetImportErrorWithNameFrom(PyObject *msg, PyObject *name, PyObject *path, PyObject* from_name)
|
||||
{
|
||||
return _PyErr_SetImportErrorSubclassWithNameFrom(PyExc_ImportError, msg, name, path, from_name);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue