diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-10-25 22:56:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-25 22:56:59 (GMT) |
commit | 7cfbb49fcd4c85f9bab3797302eadf93df490344 (patch) | |
tree | a20c316c8296dbe3e327e7fcf19925e8090ee267 /Python/errors.c | |
parent | 1f737edb67e702095feb97118a911afb569f5705 (diff) | |
download | cpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.zip cpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.tar.gz cpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.tar.bz2 |
gh-91058: Add error suggestions to 'import from' import errors (#98305)
Diffstat (limited to 'Python/errors.c')
-rw-r--r-- | Python/errors.c | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/Python/errors.c b/Python/errors.c index 2aa748c..fc3e468 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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) { |