MCPcopy
hub / github.com/Textualize/rich / test_handler

Function test_handler

tests/test_traceback.py:13–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11
12
13def test_handler():
14 console = Console(file=io.StringIO(), width=100, color_system=None)
15 expected_old_handler = sys.excepthook
16
17 def level1():
18 level2()
19
20 def level2():
21 return 1 / 0
22
23 try:
24 old_handler = install(console=console)
25 try:
26 level1()
27 except Exception:
28 exc_type, exc_value, traceback = sys.exc_info()
29 sys.excepthook(exc_type, exc_value, traceback)
30 rendered_exception = console.file.getvalue()
31 print(repr(rendered_exception))
32 assert "Traceback" in rendered_exception
33 assert "ZeroDivisionError" in rendered_exception
34
35 frame_blank_line_possible_preambles = (
36 # Start of the stack rendering:
37 "╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮",
38 # Each subsequent frame (starting with the file name) should then be preceded with a blank line:
39 "│" + (" " * 98) + "│",
40 )
41 for frame_start in re.finditer(
42 "^│ .+rich/tests/test_traceback.py:",
43 rendered_exception,
44 flags=re.MULTILINE,
45 ):
46 frame_start_index = frame_start.start()
47 for preamble in frame_blank_line_possible_preambles:
48 preamble_start, preamble_end = (
49 frame_start_index - len(preamble) - 1,
50 frame_start_index - 1,
51 )
52 if rendered_exception[preamble_start:preamble_end] == preamble:
53 break
54 else:
55 pytest.fail(
56 f"Frame {frame_start[0]} doesn't have the expected preamble"
57 )
58 finally:
59 sys.excepthook = old_handler
60 assert old_handler == expected_old_handler
61
62
63def test_capture():

Callers

nothing calls this directly

Calls 5

ConsoleClass · 0.90
installFunction · 0.90
level1Function · 0.85
printFunction · 0.50
startMethod · 0.45

Tested by

no test coverage detected