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

Method format_exception_only

Lib/traceback.py:1280–1337  ·  view source on GitHub ↗

Format the exception part of the traceback. The return value is a generator of strings, each ending in a newline. Generator yields the exception message. For :exc:`SyntaxError` exceptions, it also yields (before the exception message) several lines that (whe

(self, *, show_group=False, _depth=0, **kwargs)

Source from the content-addressed store, hash-verified

1278 return self._str
1279
1280 def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
1281 """Format the exception part of the traceback.
1282
1283 The return value is a generator of strings, each ending in a newline.
1284
1285 Generator yields the exception message.
1286 For :exc:`SyntaxError` exceptions, it
1287 also yields (before the exception message)
1288 several lines that (when printed)
1289 display detailed information about where the syntax error occurred.
1290 Following the message, generator also yields
1291 all the exception's ``__notes__``.
1292
1293 When *show_group* is ``True``, and the exception is an instance of
1294 :exc:`BaseExceptionGroup`, the nested exceptions are included as
1295 well, recursively, with indentation relative to their nesting depth.
1296 """
1297 colorize = kwargs.get("colorize", False)
1298 if colorize:
1299 theme = _colorize.get_theme(force_color=True).traceback
1300 else:
1301 theme = _colorize.get_theme(force_no_color=True).traceback
1302
1303 indent = 3 * _depth * ' '
1304 if not self._have_exc_type:
1305 yield indent + _format_final_exc_line(None, self._str, colorize=colorize)
1306 return
1307
1308 stype = self.exc_type_str
1309 if not self._is_syntax_error:
1310 if _depth > 0:
1311 # Nested exceptions needs correct handling of multiline messages.
1312 formatted = _format_final_exc_line(
1313 stype, self._str, insert_final_newline=False, colorize=colorize
1314 ).split('\n')
1315 yield from [
1316 indent + l + '\n'
1317 for l in formatted
1318 ]
1319 else:
1320 yield _format_final_exc_line(stype, self._str, colorize=colorize)
1321 else:
1322 yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)]
1323
1324 if (
1325 isinstance(self.__notes__, collections.abc.Sequence)
1326 and not isinstance(self.__notes__, (str, bytes))
1327 ):
1328 for note in self.__notes__:
1329 note = _safe_string(note, 'note')
1330 yield from _format_note(note, indent, theme)
1331 elif self.__notes__ is not None:
1332 note = _safe_string(self.__notes__, '__notes__', func=repr)
1333 yield from _format_note(note, indent, theme)
1334
1335 if self.exceptions and show_group:
1336 for ex in self.exceptions:
1337 yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1, colorize=colorize)

Callers 15

format_exception_onlyFunction · 0.95
formatMethod · 0.80
__runMethod · 0.80
__init__Method · 0.80
_format_excMethod · 0.80
prompt_for_replyMethod · 0.80
_task_print_stackFunction · 0.80
get_exception_formatMethod · 0.80
test_nocaretMethod · 0.80
test_base_exceptionMethod · 0.80

Calls 6

_format_syntax_errorMethod · 0.95
_format_final_exc_lineFunction · 0.85
_safe_stringFunction · 0.85
_format_noteFunction · 0.85
getMethod · 0.45
splitMethod · 0.45