mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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?
This commit is contained in:
parent
c4073a24f9
commit
5bf8bf2267
8 changed files with 287 additions and 10 deletions
|
@ -6319,6 +6319,20 @@ format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
|
|||
return;
|
||||
|
||||
_PyErr_Format(tstate, exc, format_str, obj_str);
|
||||
|
||||
if (exc == PyExc_NameError) {
|
||||
// Include the name in the NameError exceptions to offer suggestions later.
|
||||
_Py_IDENTIFIER(name);
|
||||
PyObject *type, *value, *traceback;
|
||||
PyErr_Fetch(&type, &value, &traceback);
|
||||
PyErr_NormalizeException(&type, &value, &traceback);
|
||||
if (PyErr_GivenExceptionMatches(value, PyExc_NameError)) {
|
||||
// We do not care if this fails because we are going to restore the
|
||||
// NameError anyway.
|
||||
(void)_PyObject_SetAttrId(value, &PyId_name, obj);
|
||||
}
|
||||
PyErr_Restore(type, value, traceback);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue