This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jfine2358
Recipients jfine2358, lys.nikolaou, pablogsal
Date 2022-03-04.11:45:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1646394318.28.0.583958425503.issue46910@roundup.psfhosted.org>
In-reply-to
Content
Many thanks Pablo for the clear explanation. I'd prefer that the issue remain open, as there's an important user experience issue here. I suspect there are other similar examples of how the compiler error messages could be improved.

Here's a change that doesn't seem to be too hard, that could fix the problem at hand.

The IndentationError occurred at a known location in the input string. So as part of error reporting truncate the input string and try to compile that. In other words, make a good faith attempt to find an earlier error.

I've attached a funny_break_error_fix.py which is a first draft implementation of this idea. 

Here's the output:
===
$ python3 funny_break_error_fix.py funny_break_error.py
unexpected indent (funny_break_error.py, line 6)
Traceback (most recent call last):
  File "funny_break_error_fix.py", line 3, in compile_fix
    compile(source, filename, 'exec')
  File "funny_break_error.py", line 6
    else:
    ^
IndentationError: unexpected indent

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "funny_break_error_fix.py", line 18, in <module>
    compile_fix(src.read(), filename, 'exec')
  File "funny_break_error_fix.py", line 9, in compile_fix
    compile(new_source, filename, 'exec')
  File "funny_break_error.py", line 5
    break
    ^
SyntaxError: 'break' outside loop
===

And in this case we've got hold of the first error (at the cost of compiling part of the source file twice). Many thanks again for the clear explanation, which I found most helpful when formulating the above fix.
History
Date User Action Args
2022-03-04 11:45:18jfine2358setrecipients: + jfine2358, lys.nikolaou, pablogsal
2022-03-04 11:45:18jfine2358setmessageid: <1646394318.28.0.583958425503.issue46910@roundup.psfhosted.org>
2022-03-04 11:45:18jfine2358linkissue46910 messages
2022-03-04 11:45:18jfine2358create