-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.py
More file actions
47 lines (37 loc) · 1.21 KB
/
Copy pathtrace.py
File metadata and controls
47 lines (37 loc) · 1.21 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
import pdb
def trace(errors=None, on_input=None, on_output=None):
"""
trace() returns a decorator that will drop the file into a pdb session if
the appropriate condition is met. Samle usage
>>> @trace(errors=Exception)
>>> def foo():
>>> ...
>>> foo() # opens pdb if foo() throws an error
>>> @trace(on_input=lambda arg: arg == 1)
>>> def foo(arg):
>>> ...
>>> foo(1) # opens pdb if foo() gets an input of 1
>>> @trace(on_output=lambda ret: ret == 1)
>>> def foo():
>>> ...
>>> foo() # opens pdb if foo() returns 1
on_input: (*any) => bool
errors: (Exception,)
on_output: (*any) => bool
"""
def decorator(f):
def g(*args, **kwargs):
if on_input is not None and on_input(*args, **kwargs):
pdb.set_trace()
if errors is not None:
try:
retval = f(*args, **kwargs)
except errors as e:
pdb.set_trace()
else:
retval = f(*args, **kwargs)
if on_output is not None and on_output(retval):
pdb.set_trace()
return retval
return g
return decorator