Terry J. Reedy <tjre...@udel.edu> added the comment:

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.

----------
components: +Library (Lib) -Interpreter Core, Tests
nosy: +terry.reedy
stage:  -> needs patch
title: SyntaxError location range indicator does not work in doctests -> Update 
doctect SyntaxErrors for location range

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45249>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to