Method: rich.traceback.install
Calls: 1, Exceptions: 1, Paths: 1Back
Path 1: 1 calls (1.0)
Console (1)
100 (1)
3 (1)
None (1)
False (1)
False (1)
True (1)
() (1)
100 (1)
builtin_function_or_method (1)
NameError (1)
1def install(
2 *,
3 console: Optional[Console] = None,
4 width: Optional[int] = 100,
5 extra_lines: int = 3,
6 theme: Optional[str] = None,
7 word_wrap: bool = False,
8 show_locals: bool = False,
9 indent_guides: bool = True,
10 suppress: Iterable[Union[str, ModuleType]] = (),
11 max_frames: int = 100,
12) -> Callable[[Type[BaseException], BaseException, Optional[TracebackType]], Any]:
13 """Install a rich traceback handler.
14
15 Once installed, any tracebacks will be printed with syntax highlighting and rich formatting.
16
17
18 Args:
19 console (Optional[Console], optional): Console to write exception to. Default uses internal Console instance.
20 width (Optional[int], optional): Width (in characters) of traceback. Defaults to 100.
21 extra_lines (int, optional): Extra lines of code. Defaults to 3.
22 theme (Optional[str], optional): Pygments theme to use in traceback. Defaults to ``None`` which will pick
23 a theme appropriate for the platform.
24 word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
25 show_locals (bool, optional): Enable display of local variables. Defaults to False.
26 indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
27 suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
28
29 Returns:
30 Callable: The previous exception handler that was replaced.
31
32 """
33 traceback_console = Console(file=sys.stderr) if console is None else console
34
35 def excepthook(
36 type_: Type[BaseException],
37 value: BaseException,
38 traceback: Optional[TracebackType],
39 ) -> None:
40 traceback_console.print(
41 Traceback.from_exception(
42 type_,
43 value,
44 traceback,
45 width=width,
46 extra_lines=extra_lines,
47 theme=theme,
48 word_wrap=word_wrap,
49 show_locals=show_locals,
50 indent_guides=indent_guides,
51 suppress=suppress,
52 max_frames=max_frames,
53 )
54 )
55
56 def ipy_excepthook_closure(ip: Any) -> None: # pragma: no cover
57 tb_data = {} # store information about showtraceback call
58 default_showtraceback = ip.showtraceback # keep reference of default traceback
59
60 def ipy_show_traceback(*args: Any, **kwargs: Any) -> None:
61 """wrap the default ip.showtraceback to store info for ip._showtraceback"""
62 nonlocal tb_data
63 tb_data = kwargs
64 default_showtraceback(*args, **kwargs)
65
66 def ipy_display_traceback(
67 *args: Any, is_syntax: bool = False, **kwargs: Any
68 ) -> None:
69 """Internally called traceback from ip._showtraceback"""
70 nonlocal tb_data
71 exc_tuple = ip._get_exc_info()
72
73 # do not display trace on syntax error
74 tb: Optional[TracebackType] = None if is_syntax else exc_tuple[2]
75
76 # determine correct tb_offset
77 compiled = tb_data.get("running_compiled_code", False)
78 tb_offset = tb_data.get("tb_offset", 1 if compiled else 0)
79 # remove ipython internal frames from trace with tb_offset
80 for _ in range(tb_offset):
81 if tb is None:
82 break
83 tb = tb.tb_next
84
85 excepthook(exc_tuple[0], exc_tuple[1], tb)
86 tb_data = {} # clear data upon usage
87
88 # replace _showtraceback instead of showtraceback to allow ipython features such as debugging to work
89 # this is also what the ipython docs recommends to modify when subclassing InteractiveShell
90 ip._showtraceback = ipy_display_traceback
91 # add wrapper to capture tb_data
92 ip.showtraceback = ipy_show_traceback
93 ip.showsyntaxerror = lambda *args, **kwargs: ipy_display_traceback(
94 *args, is_syntax=True, **kwargs
95 )
96
97 try: # pragma: no cover
98 # if within ipython, use customized traceback
99 ip = get_ipython() # type: ignore[name-defined]
100 ipy_excepthook_closure(ip)
101 return sys.excepthook
102 except Exception:
103 # otherwise use default system hook
104 old_excepthook = sys.excepthook
105 sys.excepthook = excepthook
106 return old_excepthook