|
22 | 22 | StrType = str
|
23 | 23 |
|
24 | 24 |
|
25 |
| -class DebugFrame: |
26 |
| - __slots__ = 'function', 'path', 'lineno' |
27 |
| - |
28 |
| - @staticmethod |
29 |
| - def from_call_frame(call_frame: 'FrameType') -> 'DebugFrame': |
30 |
| - from pathlib import Path |
31 |
| - |
32 |
| - function = call_frame.f_code.co_name |
33 |
| - |
34 |
| - path = Path(call_frame.f_code.co_filename) |
35 |
| - if path.is_absolute(): |
36 |
| - # make the path relative |
37 |
| - cwd = Path('.').resolve() |
38 |
| - try: |
39 |
| - path = path.relative_to(cwd) |
40 |
| - except ValueError: |
41 |
| - # happens if filename path is not within CWD |
42 |
| - pass |
43 |
| - |
44 |
| - lineno = call_frame.f_lineno |
45 |
| - |
46 |
| - return DebugFrame(function, str(path), lineno) |
47 |
| - |
48 |
| - def __init__(self, function: str, path: str, lineno: int): |
49 |
| - self.function = function |
50 |
| - self.path = path |
51 |
| - self.lineno = lineno |
52 |
| - |
53 |
| - def __str__(self) -> StrType: |
54 |
| - return self.str() |
55 |
| - |
56 |
| - def str(self, highlight: bool = False) -> StrType: |
57 |
| - if highlight: |
58 |
| - return ( |
59 |
| - f'{sformat(self.path, sformat.magenta)}:{sformat(self.lineno, sformat.green)} ' |
60 |
| - f'{sformat(self.function, sformat.green, sformat.italic)}' |
61 |
| - ) |
62 |
| - else: |
63 |
| - return f'{self.path}:{self.lineno} {self.function}' |
64 |
| - |
65 |
| - |
66 | 25 | class DebugArgument:
|
67 | 26 | __slots__ = 'value', 'name', 'extra'
|
68 | 27 |
|
@@ -129,7 +88,7 @@ def str(self, highlight: bool = False) -> StrType:
|
129 | 88 | else:
|
130 | 89 | prefix += f' ({self.warning})'
|
131 | 90 |
|
132 |
| - return prefix + '\n ' + '\n '.join(a.str(highlight) for a in self.arguments) |
| 91 | + return f'{prefix}\n ' + '\n '.join(a.str(highlight) for a in self.arguments) |
133 | 92 |
|
134 | 93 | def __str__(self) -> StrType:
|
135 | 94 | return self.str()
|
@@ -202,8 +161,7 @@ def _process(self, args: 'Any', kwargs: 'Any', frame_depth: int, trace: bool) ->
|
202 | 161 | try:
|
203 | 162 | call_frame: 'FrameType' = sys._getframe(frame_depth)
|
204 | 163 | except ValueError:
|
205 |
| - # "If [the given frame depth] is deeper than the call stack, |
206 |
| - # ValueError is raised" |
| 164 | + # "If [ValueError] is deeper than the call stack, ValueError is raised" |
207 | 165 | return self.output_class(
|
208 | 166 | call_context=[DebugFrame(function='', path='<unknown>', lineno=0)],
|
209 | 167 | arguments=list(self._args_inspection_failed(args, kwargs)),
|
@@ -262,6 +220,47 @@ def _process_args(self, ex: 'Any', args: 'Any', kwargs: 'Any') -> 'Generator[Deb
|
262 | 220 | yield self.output_class.arg_class(value, name=name, variable=kw_arg_names.get(name))
|
263 | 221 |
|
264 | 222 |
|
| 223 | +class DebugFrame: |
| 224 | + __slots__ = 'function', 'path', 'lineno' |
| 225 | + |
| 226 | + def __init__(self, function: str, path: str, lineno: int): |
| 227 | + self.function = function |
| 228 | + self.path = path |
| 229 | + self.lineno = lineno |
| 230 | + |
| 231 | + @staticmethod |
| 232 | + def from_call_frame(call_frame: 'FrameType') -> 'DebugFrame': |
| 233 | + from pathlib import Path |
| 234 | + |
| 235 | + function = call_frame.f_code.co_name |
| 236 | + |
| 237 | + path = Path(call_frame.f_code.co_filename) |
| 238 | + if path.is_absolute(): |
| 239 | + # make the path relative |
| 240 | + cwd = Path('.').resolve() |
| 241 | + try: |
| 242 | + path = path.relative_to(cwd) |
| 243 | + except ValueError: |
| 244 | + # happens if filename path is not within CWD |
| 245 | + pass |
| 246 | + |
| 247 | + lineno = call_frame.f_lineno |
| 248 | + |
| 249 | + return DebugFrame(function, str(path), lineno) |
| 250 | + |
| 251 | + def __str__(self) -> StrType: |
| 252 | + return self.str() |
| 253 | + |
| 254 | + def str(self, highlight: bool = False) -> StrType: |
| 255 | + if highlight: |
| 256 | + return ( |
| 257 | + f'{sformat(self.path, sformat.magenta)}:{sformat(self.lineno, sformat.green)} ' |
| 258 | + f'{sformat(self.function, sformat.green, sformat.italic)}' |
| 259 | + ) |
| 260 | + else: |
| 261 | + return f'{self.path}:{self.lineno} {self.function}' |
| 262 | + |
| 263 | + |
265 | 264 | def _make_call_context(call_frame: 'Optional[FrameType]', trace: bool) -> 'List[DebugFrame]':
|
266 | 265 | call_context: 'List[DebugFrame]' = []
|
267 | 266 |
|
|
0 commit comments