Last active
August 29, 2015 14:04
-
-
Save dobo90/b50badb5ed7d543c9947 to your computer and use it in GitHub Desktop.
bottle.py func_has_arg proposal
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
import sys | |
PY3 = sys.version_info[0] == 3 | |
PY33 = sys.version_info[0] == 3 and sys.version_info[1] >= 3 | |
if PY33: | |
from inspect import signature | |
def func_has_arg(func, arg): | |
return arg in signature(func).parameters | |
elif PY3: | |
from inspect import getfullargspec | |
def func_has_arg(func, arg): | |
if arg in getfullargspec(func)[0]: | |
return True | |
closure = func.__closure__ | |
if closure: | |
for cell in (cell for cell in closure if hasattr(cell.cell_contents, '__call__')): | |
cell_contents = cell.cell_contents | |
if cell_contents.__closure__: | |
if func_has_arg(cell_contents, arg): | |
return True | |
if arg in getfullargspec(cell_contents)[0]: | |
return True | |
return False | |
else: | |
from inspect import getargspec | |
def func_has_arg(func, arg): | |
if arg in getargspec(func)[0]: | |
return True | |
closure = func.func_closure | |
if closure: | |
for cell in (cell for cell in closure if callable(cell.cell_contents)): | |
cell_contents = cell.cell_contents | |
if cell_contents.func_closure: | |
if func_has_arg(cell_contents, arg): | |
return True | |
if arg in getargspec(cell_contents)[0]: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment