diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-04-14 14:10:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-14 14:10:33 (GMT) |
commit | 5bf8bf2267cd109970b2d946d43b2e9f71379ba2 (patch) | |
tree | a29b493cace0ba9cf1d5af516750ff9348ce6c49 /Objects | |
parent | c4073a24f95b54705416138dc1f20141ad76dd37 (diff) | |
download | cpython-5bf8bf2267cd109970b2d946d43b2e9f71379ba2.zip cpython-5bf8bf2267cd109970b2d946d43b2e9f71379ba2.tar.gz cpython-5bf8bf2267cd109970b2d946d43b2e9f71379ba2.tar.bz2 |
bpo-38530: Offer suggestions on NameError (GH-25397)
When printing NameError raised by the interpreter, PyErr_Display
will offer suggestions of simmilar variable names in the function that the exception
was raised from:
>>> schwarzschild_black_hole = None
>>> schwarschild_black_hole
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/exceptions.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 4bb4153..9916ce8 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1326,8 +1326,69 @@ SimpleExtendsException(PyExc_RuntimeError, NotImplementedError, /* * NameError extends Exception */ -SimpleExtendsException(PyExc_Exception, NameError, - "Name not found globally."); + +static int +NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"name", NULL}; + PyObject *name = NULL; + + if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1) { + return -1; + } + + PyObject *empty_tuple = PyTuple_New(0); + if (!empty_tuple) { + return -1; + } + if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$O:NameError", kwlist, + &name)) { + Py_DECREF(empty_tuple); + return -1; + } + Py_DECREF(empty_tuple); + + Py_XINCREF(name); + Py_XSETREF(self->name, name); + + return 0; +} + +static int +NameError_clear(PyNameErrorObject *self) +{ + Py_CLEAR(self->name); + return BaseException_clear((PyBaseExceptionObject *)self); +} + +static void +NameError_dealloc(PyNameErrorObject *self) +{ + _PyObject_GC_UNTRACK(self); + NameError_clear(self); + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static int +NameError_traverse(PyNameErrorObject *self, visitproc visit, void *arg) +{ + Py_VISIT(self->name); + return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); +} + +static PyMemberDef NameError_members[] = { + {"name", T_OBJECT, offsetof(PyNameErrorObject, name), 0, PyDoc_STR("name")}, + {NULL} /* Sentinel */ +}; + +static PyMethodDef NameError_methods[] = { + {NULL} /* Sentinel */ +}; + +ComplexExtendsException(PyExc_Exception, NameError, + NameError, 0, + NameError_methods, NameError_members, + 0, BaseException_str, "Name not found globally."); /* * UnboundLocalError extends NameError |