-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-99139: Improve NameError error suggestion for instances #99140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
01650df
b1546e5
8a244b9
d8933dd
4925719
ab2874a
00706e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Improve the error suggestion for :exc:`NameError` exceptions for instances. | ||
Now if a :exc:`NameError` is raised in a method and the instance has an | ||
attribute that's exactly equal to the name in the exception, the suggestion | ||
will include ``self.<NAME>`` instead of the closest match in the method | ||
scope. Patch by Pablo Galindo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include "Python.h" | ||
#include "pycore_frame.h" | ||
#include "pycore_runtime_init.h" // _Py_ID() | ||
|
||
#include "pycore_pyerrors.h" | ||
#include "pycore_code.h" // _PyCode_GetVarnames() | ||
|
@@ -226,6 +227,24 @@ get_suggestions_for_name_error(PyObject* name, PyFrameObject* frame) | |
return NULL; | ||
} | ||
|
||
// Are we inside a method and the instance has an attribute called 'name'? | ||
if (PySequence_Contains(dir, &_Py_ID(self)) > 0) { | ||
PyObject* locals = PyFrame_GetLocals(frame); | ||
if (!locals) { | ||
goto error; | ||
} | ||
PyObject* self = PyDict_GetItem(locals, &_Py_ID(self)); /* borrowed */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should we use instead for both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Py_DECREF(locals); | ||
if (!self) { | ||
goto error; | ||
} | ||
|
||
if (PyObject_HasAttr(self, name)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not use |
||
Py_DECREF(dir); | ||
return PyUnicode_FromFormat("self.%S", name); | ||
pablogsal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
PyObject *suggestions = calculate_suggestions(dir, name); | ||
Py_DECREF(dir); | ||
if (suggestions != NULL) { | ||
|
@@ -250,6 +269,10 @@ get_suggestions_for_name_error(PyObject* name, PyFrameObject* frame) | |
Py_DECREF(dir); | ||
|
||
return suggestions; | ||
|
||
error: | ||
Py_DECREF(dir); | ||
return NULL; | ||
} | ||
|
||
static bool | ||
|
Uh oh!
There was an error while loading. Please reload this page.