Message402589
In 3.10+, end_lineno and end_offset fields were added to SyntaxError objects and the args tuple.
>>> try: compile('1 1', '', 'single')
... except SyntaxError as e: print(e.args)
...
('invalid syntax. Perhaps you forgot a comma?', ('', 1, 1, '1 1', 1, 4))
Here, line 1, offset 4 is the first 1-based column not part of the error.
The builtin default sys.excepthook was modified to read and use this new information and mark (end_offset - offset) columns with '^'s. This default prints what it does to sys.stderr.
The syntax error formatting in the traceback module was not altered. However, a new method, TracebackException._format_syntax_error, was extracted from TracebackException.format_exception_only so that the former could be overridden by software that simulates interaction.
The printed traceback does not come from line 1348. That *executes* the user code, but all Exceptions, including SyntaxError, are caught. If the exception is not expected and the run is not quiet, the exception is output by report_unexpected_exception(), as seen above as 'OUTPUT' and the lines that follows.
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/doctest.py#L1264
This calls _exception_traceback(exc_info).
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/doctest.py#L244
This calls traceback.print_exception, which I believe, for syntax errors, ultimately calls TracebackException._format_syntax_error.
I believe that the options for a fix are either
1. Call default sys.excepthook while capturing its output into a StringIO instance.
2. Assuming I am correct above about _format_syntax_error being called, monkeypatch it. In line 779,
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/traceback.py#L779
replace '^' with a field with a calculated number of ^s.
I need to do one of these two for IDLE, and may try both. |
|
| Date |
User |
Action |
Args |
| 2021-09-24 22:13:12 | terry.reedy | set | recipients:
+ terry.reedy, pablogsal, kj, andrei.avk |
| 2021-09-24 22:13:12 | terry.reedy | set | messageid: <[email protected]> |
| 2021-09-24 22:13:12 | terry.reedy | link | issue45249 messages |
| 2021-09-24 22:13:12 | terry.reedy | create | |
|