summaryrefslogtreecommitdiffstats
path: root/Python/pythonrun.c
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-14 01:36:07 (GMT)
committerGitHub <noreply@github.com>2021-04-14 01:36:07 (GMT)
commit37494b441aced0362d7edd2956ab3ea7801e60c8 (patch)
tree45d9355cde5f7c71924e8df0f16ea2e0ad63eea8 /Python/pythonrun.c
parent3bc694d5f3d4eb2e5d2f0b83e498b19662845d4e (diff)
downloadcpython-37494b441aced0362d7edd2956ab3ea7801e60c8.zip
cpython-37494b441aced0362d7edd2956ab3ea7801e60c8.tar.gz
cpython-37494b441aced0362d7edd2956ab3ea7801e60c8.tar.bz2
bpo-38530: Offer suggestions on AttributeError (#16856)
When printing AttributeError, PyErr_Display will offer suggestions of similar attribute names in the object that the exception was raised from: >>> collections.namedtoplo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?
Diffstat (limited to 'Python/pythonrun.c')
-rw-r--r--Python/pythonrun.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 99be629..321b04e 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -15,7 +15,7 @@
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
#include "pycore_parser.h" // _PyParser_ASTFromString()
-#include "pycore_pyerrors.h" // _PyErr_Fetch
+#include "pycore_pyerrors.h" // _PyErr_Fetch, _Py_Offer_Suggestions
#include "pycore_pylifecycle.h" // _Py_UnhandledKeyboardInterrupt
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_sysmodule.h" // _PySys_Audit()
@@ -953,6 +953,16 @@ print_exception(PyObject *f, PyObject *value)
if (err < 0) {
PyErr_Clear();
}
+ PyObject* suggestions = _Py_Offer_Suggestions(value);
+ if (suggestions) {
+ // Add a trailer ". Did you mean: (...)?"
+ err = PyFile_WriteString(". Did you mean: ", f);
+ if (err == 0) {
+ err = PyFile_WriteObject(suggestions, f, Py_PRINT_RAW);
+ err += PyFile_WriteString("?", f);
+ }
+ Py_DECREF(suggestions);
+ }
err += PyFile_WriteString("\n", f);
Py_XDECREF(tb);
Py_DECREF(value);