Format SyntaxError exceptions (internal helper).
(self, stype, **kwargs)
| 1374 | |
| 1375 | |
| 1376 | def _format_syntax_error(self, stype, **kwargs): |
| 1377 | """Format SyntaxError exceptions (internal helper).""" |
| 1378 | # Show exactly where the problem was found. |
| 1379 | colorize = kwargs.get("colorize", False) |
| 1380 | if colorize: |
| 1381 | theme = _colorize.get_theme(force_color=True).traceback |
| 1382 | else: |
| 1383 | theme = _colorize.get_theme(force_no_color=True).traceback |
| 1384 | filename_suffix = '' |
| 1385 | if self.lineno is not None: |
| 1386 | yield ' File {}"{}"{}, line {}{}{}\n'.format( |
| 1387 | theme.filename, |
| 1388 | self.filename or "<string>", |
| 1389 | theme.reset, |
| 1390 | theme.line_no, |
| 1391 | self.lineno, |
| 1392 | theme.reset, |
| 1393 | ) |
| 1394 | elif self.filename is not None: |
| 1395 | filename_suffix = ' ({})'.format(self.filename) |
| 1396 | |
| 1397 | text = self.text |
| 1398 | if isinstance(text, str): |
| 1399 | # text = " foo\n" |
| 1400 | # rtext = " foo" |
| 1401 | # ltext = "foo" |
| 1402 | with suppress(Exception): |
| 1403 | self._find_keyword_typos() |
| 1404 | text = self.text |
| 1405 | rtext = text.rstrip('\n') |
| 1406 | ltext = rtext.lstrip(' \n\f') |
| 1407 | spaces = len(rtext) - len(ltext) |
| 1408 | if self.offset is None: |
| 1409 | yield ' {}\n'.format(ltext) |
| 1410 | elif isinstance(self.offset, int): |
| 1411 | offset = self.offset |
| 1412 | if self.lineno == self.end_lineno: |
| 1413 | end_offset = ( |
| 1414 | self.end_offset |
| 1415 | if ( |
| 1416 | isinstance(self.end_offset, int) |
| 1417 | and self.end_offset != 0 |
| 1418 | ) |
| 1419 | else offset |
| 1420 | ) |
| 1421 | else: |
| 1422 | end_offset = len(rtext) + 1 |
| 1423 | |
| 1424 | if self.text and offset > len(self.text): |
| 1425 | offset = len(rtext) + 1 |
| 1426 | if self.text and end_offset > len(self.text): |
| 1427 | end_offset = len(rtext) + 1 |
| 1428 | if offset >= end_offset or end_offset < 0: |
| 1429 | end_offset = offset + 1 |
| 1430 | |
| 1431 | # Convert 1-based column offset to 0-based index into stripped text |
| 1432 | colno = offset - 1 - spaces |
| 1433 | end_colno = end_offset - 1 - spaces |
no test coverage detected