- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinject.py
More file actions
Latest commit
33 lines (26 loc) · 936 Bytes
/
Copy pathinject.py
File metadata and controls
33 lines (26 loc) · 936 Bytes
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
importsys
classinject(object):
def__init__(self, **kwargs):
self.kwargs=kwargs
def__call__(self, fn):
fromfunctoolsimportwraps
@wraps(fn)
defwrapped(*args, **kwargs):
old_trace=sys.gettrace()
deftracefn(frame, event, arg):
# define a new tracefn each time, since it needs to
# call the *current* tracefn if they're in debug mode
frame.f_locals.update(self.kwargs)
ifold_trace:
returnold_trace(frame, event, arg)
else:
returnNone
sys.settrace(tracefn)
try:
retval=fn(*args, **kwargs)
finally:
sys.settrace(old_trace)
returnretval
returnwrapped
definto(self, fn, *args, **kwargs):
returnself(fn)(*args, **kwargs)