@@ -1183,6 +1183,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
11831183 self .msg = exc_value .msg
11841184 self ._is_syntax_error = True
11851185 self ._exc_metadata = getattr (exc_value , "_metadata" , None )
1186+ self ._opening_offset = getattr (exc_value , "_opening_offset" , None )
11861187 elif exc_type and issubclass (exc_type , ImportError ) and \
11871188 getattr (exc_value , "name_from" , None ) is not None :
11881189 wrong_name = getattr (exc_value , "name_from" , None )
@@ -1592,21 +1593,41 @@ def _format_syntax_error(self, stype, **kwargs):
15921593 dp_colno = _display_width (ltext , colno )
15931594 highlighted = ltext [colno :end_colno ]
15941595 caret_count = _display_width (highlighted ) if highlighted else (end_colno - colno )
1596+ opening_colno = None
1597+ if (
1598+ isinstance (self ._opening_offset , int )
1599+ and self .lineno == self .end_lineno
1600+ ):
1601+ opening_colno = self ._opening_offset - 1 - spaces
1602+ if not 0 <= opening_colno < colno :
1603+ opening_colno = None
1604+ carets = '^' * caret_count
1605+ if opening_colno is not None :
1606+ dp_opening_colno = _display_width (ltext , opening_colno )
1607+ carets = '^' + ' ' * (dp_colno - dp_opening_colno - 1 ) + carets
1608+ dp_colno = dp_opening_colno
15951609 start_color = end_color = ""
15961610 if colorize :
1597- # colorize from colno to end_colno
1611+ # Colorize the later range first so the earlier
1612+ # opening column is unaffected by escape sequences.
15981613 ltext = (
15991614 ltext [:colno ] +
16001615 theme .error_highlight + ltext [colno :end_colno ] + theme .reset +
16011616 ltext [end_colno :]
16021617 )
1618+ if opening_colno is not None :
1619+ ltext = (
1620+ ltext [:opening_colno ] +
1621+ theme .error_highlight + ltext [opening_colno ] + theme .reset +
1622+ ltext [opening_colno + 1 :]
1623+ )
16031624 start_color = theme .error_highlight
16041625 end_color = theme .reset
16051626 yield ' {}\n ' .format (ltext )
16061627 yield ' {}{}{}{}\n ' .format (
16071628 ' ' * dp_colno ,
16081629 start_color ,
1609- '^' * caret_count ,
1630+ carets ,
16101631 end_color ,
16111632 )
16121633 else :
0 commit comments