Last active
August 29, 2015 14:04
-
-
Save korc/5acdf23f24a8e0f4c498 to your computer and use it in GitHub Desktop.
Test getting of wrapped function arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import functools | |
import inspect | |
def orig_func(db): | |
print("this func has db argument", db) | |
def wrap(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
ret=func(*args, **kwargs) | |
return ret | |
return wrapper | |
@wrap | |
def wrapped_func(db): | |
print("this func has db argument, too", db) | |
@wrap | |
def other_func(): | |
print("this func has no db argument") | |
def _get_closure(func): | |
return func.func_closure if hasattr(func, "func_closure") else func.__closure__ | |
def func_has_arg(func, argname): | |
if argname in inspect.getargspec(func)[0]: | |
return True | |
closure=_get_closure(func) | |
if closure and any(map(lambda cell: argname in inspect.getargspec(cell.cell_contents)[0], closure)): | |
return True | |
return False | |
def debug_func(name, func): | |
print(name,"name:", func.__name__) | |
print("\targs:", inspect.getargspec(func)) | |
closure=_get_closure(func) | |
print("\tclosure:", closure) | |
if closure: | |
for cell in closure: | |
print("\tcell:", cell.cell_contents) | |
print("\targs:", inspect.getargspec(cell.cell_contents)) | |
print("\thas db arg:", func_has_arg(func, "db")) | |
if __name__ == '__main__': | |
debug_func("original", orig_func) | |
debug_func("wrapped", wrapped_func) | |
debug_func("other_func", other_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment