Is there any way to wrap function call in Python?
I've tried the following:
but it doesn't work since assignment "f.func_cod e = w.func_code" requires that code objects contain same sets of free vars (exactly, error message was "ValueError: test_f() requires a code object with 0 free vars, not 2" for call wrap(test_f, test_wrapper))
I've tried the following:
Code:
def wrap(f, wrapper):
def h(*args, **opts): pass
h.func_code = f.func_code
def w(*args, **opts):
wrapper(args, opts)
return h(args, opts)
f.func_code = w.func_code
return w
def test_f():
print 'test_f()'
def test_wrapper(*args):
print 'test_wrapper()'
wrap(test_f, test_wrapper)
Comment