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

Function install_threading_hook

Lib/_pyrepl/_threading_handler.py:30–74  ·  view source on GitHub ↗
(reader: Reader)

Source from the content-addressed store, hash-verified

28
29
30def install_threading_hook(reader: Reader) -> None:
31 import threading
32
33 @dataclass
34 class ExceptHookHandler:
35 lock: threading.Lock = field(default_factory=threading.Lock)
36 messages: list[str] = field(default_factory=list)
37
38 def show(self) -> int:
39 count = 0
40 with self.lock:
41 if not self.messages:
42 return 0
43 reader.restore()
44 for tb in self.messages:
45 count += 1
46 if tb:
47 print(tb)
48 self.messages.clear()
49 reader.scheduled_commands.append("ctrl-c")
50 reader.prepare()
51 return count
52
53 def add(self, s: str) -> None:
54 with self.lock:
55 self.messages.append(s)
56
57 def exception(self, args: ExceptHookArgs) -> None:
58 lines = traceback.format_exception(
59 args.exc_type,
60 args.exc_value,
61 args.exc_traceback,
62 colorize=reader.can_colorize,
63 ) # type: ignore[call-overload]
64 pre = f"\nException in {args.thread.name}:\n" if args.thread else "\n"
65 tb = pre + "".join(lines)
66 self.add(tb)
67
68 def __call__(self) -> int:
69 return self.show()
70
71
72 handler = ExceptHookHandler()
73 reader.threading_hook = handler
74 threading.excepthook = handler.exception

Callers 1

run_hooksMethod · 0.85

Calls 1

ExceptHookHandlerClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…