-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdebughook.py
More file actions
65 lines (51 loc) · 1.86 KB
/
Copy pathdebughook.py
File metadata and controls
65 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Breakpoint hook that prefers a rich debugger but always yields one.
Wired in through ``PYTHONBREAKPOINT=debughook.set_trace``. The module sits on
``PYTHONPATH`` via ``~/.config/python``, so ``import debughook`` succeeds in
every interpreter, including project virtualenvs. It tries pdbp, then ipdb,
then the standard library pdb. A missing optional debugger downgrades to the
next one instead of leaving ``breakpoint()`` dead, which is what a bare
``PYTHONBREAKPOINT=pdbp.set_trace`` would do in any venv without pdbp.
"""
from __future__ import annotations
import sys
def _enter(frame, header):
"""Open the best available debugger stopped at ``frame``."""
if header is not None:
print(header)
# pdbp: maintained pdb++ fork. Drop-in pdb with sticky mode, syntax
# highlighting and tab completion.
try:
import pdbp
except ImportError:
pass
else:
pdbp.Pdb().set_trace(frame)
return
# ipdb: IPython-powered prompt with completion and richer tracebacks.
try:
import ipdb
except ImportError:
pass
else:
ipdb.set_trace(frame)
return
# Standard library fallback. Always present.
import pdb
pdb.Pdb().set_trace(frame)
def set_trace(*args, **kwargs):
"""``sys.breakpointhook`` entry point. Stops in the caller's frame.
``breakpoint()`` forwards its arguments here. Only ``header`` is meaningful
across backends, so it is honored and the rest are ignored.
"""
header = kwargs.pop("header", None)
_enter(sys._getframe(1), header)
def post_mortem(traceback=None):
"""Post-mortem debugging with the same backend preference as set_trace."""
try:
import pdbp as backend
except ImportError:
try:
import ipdb as backend
except ImportError:
import pdb as backend
backend.post_mortem(traceback)