MCPcopy Index your code
hub / github.com/python/cpython / _format_syntax_error

Method _format_syntax_error

Lib/traceback.py:1444–1535  ·  view source on GitHub ↗

Format SyntaxError exceptions (internal helper).

(self, stype, **kwargs)

Source from the content-addressed store, hash-verified

1442
1443
1444 def _format_syntax_error(self, stype, **kwargs):
1445 """Format SyntaxError exceptions (internal helper)."""
1446 # Show exactly where the problem was found.
1447 colorize = kwargs.get("colorize", False)
1448 if colorize:
1449 theme = _colorize.get_theme(force_color=True).traceback
1450 else:
1451 theme = _colorize.get_theme(force_no_color=True).traceback
1452 filename_suffix = ''
1453 if self.lineno is not None:
1454 yield ' File {}"{}"{}, line {}{}{}\n'.format(
1455 theme.filename,
1456 self.filename or "<string>",
1457 theme.reset,
1458 theme.line_no,
1459 self.lineno,
1460 theme.reset,
1461 )
1462 elif self.filename is not None:
1463 filename_suffix = ' ({})'.format(self.filename)
1464
1465 text = self.text
1466 if isinstance(text, str):
1467 # text = " foo\n"
1468 # rtext = " foo"
1469 # ltext = "foo"
1470 with suppress(Exception):
1471 self._find_keyword_typos()
1472 text = self.text
1473 rtext = text.rstrip('\n')
1474 ltext = rtext.lstrip(' \n\f')
1475 spaces = len(rtext) - len(ltext)
1476 if self.offset is None:
1477 yield ' {}\n'.format(ltext)
1478 elif isinstance(self.offset, int):
1479 offset = self.offset
1480 if self.lineno == self.end_lineno:
1481 end_offset = (
1482 self.end_offset
1483 if (
1484 isinstance(self.end_offset, int)
1485 and self.end_offset != 0
1486 )
1487 else offset
1488 )
1489 else:
1490 end_offset = len(rtext) + 1
1491
1492 if self.text and offset > len(self.text):
1493 offset = len(rtext) + 1
1494 if self.text and end_offset > len(self.text):
1495 end_offset = len(rtext) + 1
1496 if offset >= end_offset or end_offset < 0:
1497 end_offset = offset + 1
1498
1499 # Convert 1-based column offset to 0-based index into stripped text
1500 colno = offset - 1 - spaces
1501 end_colno = end_offset - 1 - spaces

Callers 1

format_exception_onlyMethod · 0.95

Calls 7

_find_keyword_typosMethod · 0.95
suppressClass · 0.90
_display_widthFunction · 0.85
getMethod · 0.45
formatMethod · 0.45
rstripMethod · 0.45
lstripMethod · 0.45

Tested by

no test coverage detected