-
Notifications
You must be signed in to change notification settings - Fork 1
/
catcher.py
46 lines (43 loc) · 940 Bytes
/
catcher.py
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 inspect
import logging
import sys
def GetWrapper(obj, f):
def wrapper(*args, **kwargs):
try:
ret=f(obj, *args, **kwargs)
return ret
except:
(E, e, trace) = sys.exc_info()
logging.warning('exception caught: '+str(e))
logging.warning(str(trace))
return wrapper
def AttachCatcher(obj, functions=None):
cdict=obj.__class__.__dict__
if functions==None:
functions=cdict
if isinstance(functions, basestring):
functions=(functions,)
for func in functions:
if func.startswith('__'):
continue
f=cdict[func]
if not inspect.isfunction(f):
continue
logging.debug("Wrapping "+func)
wrapper=GetWrapper(obj, f)
obj.__dict__[func]=wrapper
return obj
if __name__=="__main__":
class kkkk:
def abcd(a,b):
print "a,b:", a,b
raise IOError
def asdf(a,s):
print a+s
k=kkkk()
kc=AttachCatcher(k, ["abcd", "asdf"])
print(kc.__dict__)
kc.abcd(2)
# kc.abcd(2)
kc.asdf("asdf")
print ("OK")