10000 gh-99139: Improve NameError error suggestion for instances by pablogsal · Pull Request #99140 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 7 commits into from
Nov 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Python/suggestions.c
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
  • Loading branch information
pablogsal and kumaraditya303 authored Nov 6, 2022
commit ab2874a6e50bcd99f44b5c73a284fafc03cdc74b
6 changes: 2 additions & 4 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,12 @@ get_suggestions_for_name_error(PyObject* name, PyFrameObject* frame)
}

// Are we inside a method and the instance has an attribute called 'name'?
_Py_IDENTIFIER(self);
PyObject* self_str = _PyUnicode_FromId(&PyId_self); /* borrowed */
if (PySequence_Contains(dir, self_str) > 0) {
if (PySequence_Contains(dir, &_Py_ID(self)) > 0) {
PyObject* locals = PyFrame_GetLocals(frame);
if (!locals) {
goto error;
}
PyObject* self = PyDict_GetItem(locals, self_str); /* borrowed */
PyObject* self = PyDict_GetItem(locals, &_Py_ID(self)); /* borrowed */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use PyDict_GetItem. It is broken by design.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we use instead for both?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyDict_GetItemWithError and PyObject_GetAttr if limited by public API. It is suggested in the documentation. But _PyObject_LookupAttr is more convenient in the latter case.

Py_DECREF(locals);
if (!self) {
goto error;
Expand Down
0