forked from uweschmitt/emzed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_utils.py
38 lines (31 loc) · 1.21 KB
/
patch_utils.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 00:04:22 2013
@author: uwe
"""
import inspect, sys
def replace( orig_func, target=None, verbose=False):
""" monkey pathchin decorator: replaces function """
def decorator(new_func, target=target):
def wrapper(*a, **kw):
return new_func(*a, **kw)
wrapper.isPatched = True
if inspect.ismethod(orig_func):
if target is None:
target = orig_func.im_class
setattr(target, orig_func.__name__, wrapper)
setattr(target, "_orig_%s" % orig_func.__name__, orig_func)
elif inspect.isfunction(orig_func):
if target is None:
target = sys.modules[orig_func.__module__]
setattr(target, orig_func.func_name, wrapper)
setattr(target, "_orig_%s" % orig_func.__name__, orig_func)
else:
raise Exception("can not wrap %s " % orig_func)
return wrapper # not needed as new_func is not modified at all
return decorator
def add(target, verbose=False):
""" monkey pathchin decorator: adds function """
def decorator(new_func, target=target):
setattr(target, new_func.__name__, new_func)
return decorator