Skip to content

Instantly share code, notes, and snippets.

@dobo90
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dobo90/b50badb5ed7d543c9947 to your computer and use it in GitHub Desktop.
Save dobo90/b50badb5ed7d543c9947 to your computer and use it in GitHub Desktop.
bottle.py func_has_arg proposal
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