← 返回首页
gh-99139: Improve NameError error suggestion for instances by pablogsal · Pull Request #99140 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-99139: Improve NameError error suggestion for instances#99140

Merged
pablogsal merged 7 commits into
python:mainfrom
pablogsal:better_attr
Nov 6, 2022
Merged

gh-99139: Improve NameError error suggestion for instances#99140
pablogsal merged 7 commits into
python:mainfrom
pablogsal:better_attr

Conversation

pablogsal commented Nov 5, 2022
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

ammaraskar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Looks great :)

Comment thread Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Comment thread Python/suggestions.c Outdated Show resolved Hide resolved
Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
Comment thread Python/suggestions.c Outdated Show resolved Hide resolved
Comment thread Python/suggestions.c Show resolved Hide resolved

isidentical left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Looks really helpful, thanks a lot @pablogsal!

pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 5, 2022

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit b1546e5 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 5, 2022
pablogsal and others added 3 commits November 6, 2022 01:54
Co-authored-by: Ammar Askar <ammar_askar@hotmail.com>
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 6, 2022

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit 4925719 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Nov 6, 2022
Comment thread Python/suggestions.c Outdated Show resolved Hide resolved
Comment thread Python/suggestions.c Outdated Show resolved Hide resolved
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Comment thread Python/suggestions.c Outdated Show resolved Hide resolved
Comment thread Python/suggestions.c Show resolved Hide resolved
pablogsal merged commit 99e2e60 into python:main Nov 6, 2022
pablogsal deleted the better_attr branch November 6, 2022 13:52
Comment thread Python/suggestions.c
if (!locals) {
goto error;
}
PyObject* self = PyDict_GetItem(locals, &_Py_ID(self)); /* borrowed */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Do not use PyDict_GetItem. It is broken by design.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

What should we use instead for both?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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.

Comment thread Python/suggestions.c
goto error;
}

if (PyObject_HasAttr(self, name)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Do not use PyObject_HasAttr. It is broken by design.

Copy link
Copy Markdown

I'm surprised this uses hasattr under the covers. A simple benign name error could cause side effects at run time.

>>> class Test: ... def __getattr__(self, name): ... if name == "foo": ... raise SystemExit("The end.") ... def bar(self): ... foo ... ... Test().bar() ... The end.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

Footer

© 2026 GitHub, Inc.