Skip to content

Conversation

@E-Paine
Copy link
Contributor

@E-Paine E-Paine commented May 5, 2021

Based on the test_exception code included on bpo by @sweeneyde. If the exception is an AttributeError or NameError, we remove the last line from the normal traceback and replace it with one captured from sys.__excepthook__. This implementation utilises test.support.captured_stderr, like the original.

https://bugs.python.org/issue44026

@E-Paine E-Paine requested a review from terryjreedy as a code owner May 5, 2021 08:49
@E-Paine E-Paine changed the title Idle: get Python's 'did you mean' bpo-44026: Idle get Python's 'did you mean' May 5, 2021
@E-Paine
Copy link
Contributor Author

E-Paine commented May 5, 2021

Pipelines failure is an unrelated test_concurrent_futures issue

@shreyanavigyan
Copy link
Contributor

AFAICT this PR needs a news entry

@E-Paine
Copy link
Contributor Author

E-Paine commented May 5, 2021

The pipelines test is a different test_concurrent_futures failure. I cannot see how these failures could be linked to this PR so am inclined to ignore them.

@sweeneyde
Copy link
Member

Perhaps it would be better to use contextlib.redirect_stderr(to), since, from the docs:

Note test.support is not a public module. It is documented here to help Python developers write tests. The API of this module is subject to change without backwards compatibility concerns between releases. 
@E-Paine
Copy link
Contributor Author

E-Paine commented May 5, 2021

Perhaps it would be better to use contextlib.redirect_stderr(to)

I did not know that existed; thanks for pointing me to it!

@E-Paine
Copy link
Contributor Author

E-Paine commented May 5, 2021

OK, that's the third consecutive pipelines Windows failure of test_concurrent_futures. Is this an issue with other PRs, or could this (somehow) be to do with my changes? I'll try a merge and see if that fixes it.

@shreyanavigyan
Copy link
Contributor

The test you're talking about has failed in lot of PRs recently.

@terryjreedy
Copy link
Member

Paine, I am currently reviewing and revising, so please no more changes.

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

Functions should only be nested if they need to be. They cannot be tested properly if they are. I expanded the scope slightly to make it more 'complete'. I then reduced the scope of the new tests to testing the new unit, and used subTest to combine and add a 'normal' case.

print_exc should be checked to see if it should be nested. get_message_lines should become get_message. Currently, format_exception_only only returns multiple lines. Combining them could be done within the function. I left that for another issue.

I think this is ready to merge, but am leaving for others and myself to look at again (and, I hope, for someone else to fix the test failures if not done yet).

Current coverage is 54% on my machine. Should update if any other edits.

@E-Paine
Copy link
Contributor Author

E-Paine commented May 6, 2021

Sorry Terry, I know you asked for no more changes, but I need to patch it to correctly deal with multiple errors in a traceback (I will also add a test for this).

>>> try: ... abc ... except NameError: ... bcd ... Traceback (most recent call last): File "<pyshell#8>", line 2, in <module> abc NameError: name 'bcd' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<pyshell#8>", line 4, in <module> bcd NameError: name 'bcd' is not defined 
@E-Paine
Copy link
Contributor Author

E-Paine commented May 6, 2021

MacOS failure is an unrelated issue with test_ssl.

@terryjreedy
Copy link
Member

Sorry, I meant no more changes until I posted my edits, to avoid any merge conflicts. Once I did, I did not merge immediately to allow for further review and possible change. Good thing.

In your bad example above, should not the first bad name have been 'abc'? Was 'bcd' a result of fetching sys.exec info again, in get_message_lines, during processing? In any case, with the last patch:

try: abc except NameError: ing Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> try: abc NameError: name 'abc' is not defined. Did you mean: 'abs'? During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<pyshell#2>", line 2, in <module> except NameError: ing NameError: name 'ing' is not defined. Did you mean: 'int'? 

Both bad names are listed and corrected. Great.

I am impressed by and like the new test, but want to try to improve it and the previous one further. So no changes while I do ;-). (I am assuming that you are not editing right now.)

Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

I did edits in comprehensible chunks, testing after each. I will again wait a bit for you to check before merging.

@E-Paine
Copy link
Contributor Author

E-Paine commented May 7, 2021

[from the news] Include parser's typo fix suggestions

Just checking: is it the parser?

@terryjreedy terryjreedy merged commit 092f9dd into python:main May 7, 2021
@miss-islington
Copy link
Contributor

Thanks @E-Paine for the PR, and @terryjreedy for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.9.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @E-Paine and @terryjreedy, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 092f9ddb5e85665552c8207972cd393d492f764e 3.9

@bedevere-bot
Copy link

GH-25973 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label May 7, 2021
terryjreedy added a commit to terryjreedy/cpython that referenced this pull request May 8, 2021
…H-25912) A C function accessible by the default exception handler, but not by python code, finds the existing name closest to the name causing a name or attribute error. For such errors, call the default handler after capturing stderr and retrieve its message line. Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> (cherry picked from commit 092f9dd)
terryjreedy added a commit to terryjreedy/cpython that referenced this pull request May 8, 2021
…ythonGH-25912) A C function accessible by the default exception handler, but not by python code, finds the existing name closest to the name causing a name or attribute error. For such errors, call the default handler after capturing stderr and retrieve its message line. Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>. (cherry picked from commit 092f9dd) Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
@bedevere-bot
Copy link

GH-25975 is a backport of this pull request to the 3.9 branch.

terryjreedy added a commit that referenced this pull request May 8, 2021
A C function accessible by the default exception handler, but not by python code, finds the existing name closest to the name causing a name or attribute error. For such errors, call the default handler after capturing stderr and retrieve its message line. Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu> (cherry picked from commit 092f9dd) Co-authored-by: E-Paine <63801254+E-Paine@users.noreply.github.com>
@terryjreedy
Copy link
Member

@E-Paine The conflict was in the idlelib NEWS file. I fixed it and then remembered that this was not be be backported to 3.9 because it does not have the hints.

@python python deleted a comment from bedevere-bot May 8, 2021
@terryjreedy
Copy link
Member

Buildbot failure due to test asyncio deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

7 participants