-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathdebug.py
More file actions
228 lines (189 loc) · 7.65 KB
/
debug.py
File metadata and controls
228 lines (189 loc) · 7.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import sys
from .ansi import sformat
from .prettier import PrettyFormat
from .timer import Timer
from .utils import env_bool, env_true, is_literal, use_highlight
__all__ = 'Debug', 'debug'
MYPY = False
if MYPY:
from types import FrameType
from typing import Any, Generator, List, Optional, Union
pformat = PrettyFormat(
indent_step=int(os.getenv('PY_DEVTOOLS_INDENT', 4)),
simple_cutoff=int(os.getenv('PY_DEVTOOLS_SIMPLE_CUTOFF', 10)),
width=int(os.getenv('PY_DEVTOOLS_WIDTH', 120)),
yield_from_generators=env_true('PY_DEVTOOLS_YIELD_FROM_GEN', True),
)
# required for type hinting because I (stupidly) added methods called `str`
StrType = str
class DebugArgument:
__slots__ = 'value', 'name', 'extra'
def __init__(self, value: 'Any', *, name: 'Optional[str]' = None, **extra: 'Any') -> None:
self.value = value
self.name = name
self.extra = []
try:
length = len(value)
except TypeError:
pass
else:
self.extra.append(('len', length))
self.extra += [(k, v) for k, v in extra.items() if v is not None]
def str(self, highlight: bool = False) -> StrType:
s = ''
if self.name and not is_literal(self.name):
s = f'{sformat(self.name, sformat.blue, apply=highlight)}: '
suffix = sformat(
f" ({self.value.__class__.__name__}){''.join(f' {k}={v}' for k, v in self.extra)}",
sformat.dim,
apply=highlight,
)
try:
s += pformat(self.value, indent=4, highlight=highlight)
except Exception as exc:
v = sformat(f'!!! error pretty printing value: {exc!r}', sformat.yellow, apply=highlight)
s += f'{self.value!r}{suffix}\n {v}'
else:
s += suffix
return s
def __str__(self) -> StrType:
return self.str()
class DebugOutput:
"""
Represents the output of a debug command.
"""
arg_class = DebugArgument
__slots__ = 'filename', 'lineno', 'frame', 'arguments', 'warning'
def __init__(
self,
*,
filename: str,
lineno: int,
frame: str,
arguments: 'List[DebugArgument]',
warning: 'Union[None, str, bool]' = None,
) -> None:
self.filename = filename
self.lineno = lineno
self.frame = frame
self.arguments = arguments
self.warning = warning
def str(self, highlight: bool = False) -> StrType:
if highlight:
prefix = (
f'{sformat(self.filename, sformat.magenta)}:{sformat(self.lineno, sformat.green)} '
f'{sformat(self.frame, sformat.green, sformat.italic)}'
)
if self.warning:
prefix += sformat(f' ({self.warning})', sformat.dim)
else:
prefix = f'{self.filename}:{self.lineno} {self.frame}'
if self.warning:
prefix += f' ({self.warning})'
return f'{prefix}\n ' + '\n '.join(a.str(highlight) for a in self.arguments)
def __str__(self) -> StrType:
return self.str()
def __repr__(self) -> StrType:
arguments = ' '.join(str(a) for a in self.arguments)
return f'<DebugOutput {self.filename}:{self.lineno} {self.frame} arguments: {arguments}>'
class Debug:
output_class = DebugOutput
def __init__(self, *, warnings: 'Optional[bool]' = None, highlight: 'Optional[bool]' = None):
self._show_warnings = env_bool(warnings, 'PY_DEVTOOLS_WARNINGS', True)
self._highlight = highlight
def __call__(
self,
*args: 'Any',
file_: 'Any' = None,
flush_: bool = True,
frame_depth_: int = 2,
**kwargs: 'Any',
) -> 'Any':
d_out = self._process(args, kwargs, frame_depth_)
s = d_out.str(use_highlight(self._highlight, file_))
print(s, file=file_, flush=flush_)
if kwargs:
return (*args, kwargs)
elif len(args) == 1:
return args[0]
else:
return args
def format(self, *args: 'Any', frame_depth_: int = 2, **kwargs: 'Any') -> DebugOutput:
return self._process(args, kwargs, frame_depth_)
def breakpoint(self) -> None:
import pdb
pdb.Pdb(skip=['devtools.*']).set_trace()
def timer(self, name: 'Optional[str]' = None, *, verbose: bool = True, file: 'Any' = None, dp: int = 3) -> Timer:
return Timer(name=name, verbose=verbose, file=file, dp=dp)
def _process(self, args: 'Any', kwargs: 'Any', frame_depth: int) -> DebugOutput:
"""
BEWARE: this must be called from a function exactly `frame_depth` levels below the top of the stack.
"""
# HELP: any errors other than ValueError from _getframe? If so please submit an issue
try:
call_frame: 'FrameType' = sys._getframe(frame_depth)
except ValueError:
# "If [ValueError] is deeper than the call stack, ValueError is raised"
return self.output_class(
filename='<unknown>',
lineno=0,
frame='',
arguments=list(self._args_inspection_failed(args, kwargs)),
warning=self._show_warnings and 'error parsing code, call stack too shallow',
)
function = call_frame.f_code.co_name
from pathlib import Path
path = Path(call_frame.f_code.co_filename)
if path.is_absolute():
# make the path relative
cwd = Path('.').resolve()
try:
path = path.relative_to(cwd)
except ValueError:
# happens if filename path is not within CWD
pass
lineno = call_frame.f_lineno
warning = None
import executing
source = executing.Source.for_frame(call_frame)
if not source.text:
warning = 'no code context for debug call, code inspection impossible'
arguments = list(self._args_inspection_failed(args, kwargs))
else:
ex = source.executing(call_frame)
function = ex.code_qualname()
if not ex.node:
warning = 'executing failed to find the calling node'
arguments = list(self._args_inspection_failed(args, kwargs))
else:
arguments = list(self._process_args(ex, args, kwargs))
return self.output_class(
filename=str(path),
lineno=lineno,
frame=function,
arguments=arguments,
warning=self._show_warnings and warning,
)
def _args_inspection_failed(self, args: 'Any', kwargs: 'Any') -> 'Generator[DebugArgument, None, None]':
for arg in args:
yield self.output_class.arg_class(arg)
for name, value in kwargs.items():
yield self.output_class.arg_class(value, name=name)
def _process_args(self, ex: 'Any', args: 'Any', kwargs: 'Any') -> 'Generator[DebugArgument, None, None]':
import ast
func_ast = ex.node
atok = ex.source.asttokens()
for arg, ast_arg in zip(args, func_ast.args):
if isinstance(ast_arg, ast.Name):
yield self.output_class.arg_class(arg, name=ast_arg.id)
else:
name = ' '.join(map(str.strip, atok.get_text(ast_arg).splitlines()))
yield self.output_class.arg_class(arg, name=name)
kw_arg_names = {}
for kw in func_ast.keywords:
if isinstance(kw.value, ast.Name):
kw_arg_names[kw.arg] = kw.value.id
for name, value in kwargs.items():
yield self.output_class.arg_class(value, name=name, variable=kw_arg_names.get(name))
debug = Debug()