summaryrefslogtreecommitdiffstats
path: root/Objects/exceptions.c
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-10-25 22:56:59 (GMT)
committerGitHub <noreply@github.com>2022-10-25 22:56:59 (GMT)
commit7cfbb49fcd4c85f9bab3797302eadf93df490344 (patch)
treea20c316c8296dbe3e327e7fcf19925e8090ee267 /Objects/exceptions.c
parent1f737edb67e702095feb97118a911afb569f5705 (diff)
downloadcpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.zip
cpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.tar.gz
cpython-7cfbb49fcd4c85f9bab3797302eadf93df490344.tar.bz2
gh-91058: Add error suggestions to 'import from' import errors (#98305)
Diffstat (limited to 'Objects/exceptions.c')
-rw-r--r--Objects/exceptions.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 80e98bb..4b4f31a 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -1464,11 +1464,12 @@ SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
static int
ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
{
- static char *kwlist[] = {"name", "path", 0};
+ static char *kwlist[] = {"name", "path", "name_from", 0};
PyObject *empty_tuple;
PyObject *msg = NULL;
PyObject *name = NULL;
PyObject *path = NULL;
+ PyObject *name_from = NULL;
if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
return -1;
@@ -1476,8 +1477,8 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
empty_tuple = PyTuple_New(0);
if (!empty_tuple)
return -1;
- if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
- &name, &path)) {
+ if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OOO:ImportError", kwlist,
+ &name, &path, &name_from)) {
Py_DECREF(empty_tuple);
return -1;
}
@@ -1489,6 +1490,9 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
Py_XINCREF(path);
Py_XSETREF(self->path, path);
+ Py_XINCREF(name_from);
+ Py_XSETREF(self->name_from, name_from);
+
if (PyTuple_GET_SIZE(args) == 1) {
msg = PyTuple_GET_ITEM(args, 0);
Py_INCREF(msg);
@@ -1504,6 +1508,7 @@ ImportError_clear(PyImportErrorObject *self)
Py_CLEAR(self->msg);
Py_CLEAR(self->name);
Py_CLEAR(self->path);
+ Py_CLEAR(self->name_from);
return BaseException_clear((PyBaseExceptionObject *)self);
}
@@ -1521,6 +1526,7 @@ ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
Py_VISIT(self->msg);
Py_VISIT(self->name);
Py_VISIT(self->path);
+ Py_VISIT(self->name_from);
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
}
@@ -1540,7 +1546,7 @@ static PyObject *
ImportError_getstate(PyImportErrorObject *self)
{
PyObject *dict = ((PyBaseExceptionObject *)self)->dict;
- if (self->name || self->path) {
+ if (self->name || self->path || self->name_from) {
dict = dict ? PyDict_Copy(dict) : PyDict_New();
if (dict == NULL)
return NULL;
@@ -1552,6 +1558,10 @@ ImportError_getstate(PyImportErrorObject *self)
Py_DECREF(dict);
return NULL;
}
+ if (self->name_from && PyDict_SetItem(dict, &_Py_ID(name_from), self->name_from) < 0) {
+ Py_DECREF(dict);
+ return NULL;
+ }
return dict;
}
else if (dict) {
@@ -1588,6 +1598,8 @@ static PyMemberDef ImportError_members[] = {
PyDoc_STR("module name")},
{"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0,
PyDoc_STR("module path")},
+ {"name_from", T_OBJECT, offsetof(PyImportErrorObject, name_from), 0,
+ PyDoc_STR("name imported from module")},
{NULL} /* Sentinel */
};