| 319 | self._print_formatted_text(page) |
| 320 | |
| 321 | def _format_exception_output( |
| 322 | self, e: BaseException, highlight: bool |
| 323 | ) -> Generator[OneStyleAndTextTuple, None, None]: |
| 324 | if e.__cause__: |
| 325 | yield from self._format_exception_output(e.__cause__, highlight=highlight) |
| 326 | yield ( |
| 327 | "", |
| 328 | "\nThe above exception was the direct cause of the following exception:\n\n", |
| 329 | ) |
| 330 | elif e.__context__: |
| 331 | yield from self._format_exception_output(e.__context__, highlight=highlight) |
| 332 | yield ( |
| 333 | "", |
| 334 | "\nDuring handling of the above exception, another exception occurred:\n\n", |
| 335 | ) |
| 336 | |
| 337 | tblist = list(traceback.extract_tb(e.__traceback__)) |
| 338 | |
| 339 | for line_nr, tb_tuple in enumerate(tblist): |
| 340 | if tb_tuple[0] == "<stdin>": |
| 341 | tblist = tblist[line_nr:] |
| 342 | break |
| 343 | |
| 344 | tb_list = traceback.format_list(tblist) |
| 345 | if tb_list: |
| 346 | tb_list.insert(0, "Traceback (most recent call last):\n") |
| 347 | tb_list.extend(traceback.format_exception_only(type(e), e)) |
| 348 | |
| 349 | tb_str = "".join(tb_list) |
| 350 | |
| 351 | # Format exception and write to output. |
| 352 | # (We use the default style. Most other styles result |
| 353 | # in unreadable colors for the traceback.) |
| 354 | if highlight: |
| 355 | for index, tokentype, text in PythonTracebackLexer().get_tokens_unprocessed( |
| 356 | tb_str |
| 357 | ): |
| 358 | yield ("class:" + pygments_token_to_classname(tokentype), text) |
| 359 | else: |
| 360 | yield ("", tb_str) |
| 361 | |
| 362 | |
| 363 | class PagerResult(Enum): |