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)
| 1224 | return self._str |
| 1225 | |
| 1226 | def format_exception_only(self, *, show_group=False, _depth=0, **kwargs): |
| 1227 | """Format the exception part of the traceback. |
| 1228 | |
| 1229 | The return value is a generator of strings, each ending in a newline. |
| 1230 | |
| 1231 | Generator yields the exception message. |
| 1232 | For :exc:`SyntaxError` exceptions, it |
| 1233 | also yields (before the exception message) |
| 1234 | several lines that (when printed) |
| 1235 | display detailed information about where the syntax error occurred. |
| 1236 | Following the message, generator also yields |
| 1237 | all the exception's ``__notes__``. |
| 1238 | |
| 1239 | When *show_group* is ``True``, and the exception is an instance of |
| 1240 | :exc:`BaseExceptionGroup`, the nested exceptions are included as |
| 1241 | well, recursively, with indentation relative to their nesting depth. |
| 1242 | """ |
| 1243 | colorize = kwargs.get("colorize", False) |
| 1244 | |
| 1245 | indent = 3 * _depth * ' ' |
| 1246 | if not self._have_exc_type: |
| 1247 | yield indent + _format_final_exc_line(None, self._str, colorize=colorize) |
| 1248 | return |
| 1249 | |
| 1250 | stype = self.exc_type_str |
| 1251 | if not self._is_syntax_error: |
| 1252 | if _depth > 0: |
| 1253 | # Nested exceptions needs correct handling of multiline messages. |
| 1254 | formatted = _format_final_exc_line( |
| 1255 | stype, self._str, insert_final_newline=False, colorize=colorize |
| 1256 | ).split('\n') |
| 1257 | yield from [ |
| 1258 | indent + l + '\n' |
| 1259 | for l in formatted |
| 1260 | ] |
| 1261 | else: |
| 1262 | yield _format_final_exc_line(stype, self._str, colorize=colorize) |
| 1263 | else: |
| 1264 | yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)] |
| 1265 | |
| 1266 | if ( |
| 1267 | isinstance(self.__notes__, collections.abc.Sequence) |
| 1268 | and not isinstance(self.__notes__, (str, bytes)) |
| 1269 | ): |
| 1270 | for note in self.__notes__: |
| 1271 | note = _safe_string(note, 'note') |
| 1272 | yield from [indent + l + '\n' for l in note.split('\n')] |
| 1273 | elif self.__notes__ is not None: |
| 1274 | yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr)) |
| 1275 | |
| 1276 | if self.exceptions and show_group: |
| 1277 | for ex in self.exceptions: |
| 1278 | yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1, colorize=colorize) |
| 1279 | |
| 1280 | def _find_keyword_typos(self): |
| 1281 | assert self._is_syntax_error |