Jonathan Fine <[email protected]> added the comment:
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.
----------
Added file: https://bugs.python.org/file50656/funny_break_error_fix.py
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46910>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com