Files
deb-python-pecan/pecan/util.py
Ryan Petrello 9358e59af8 Improve argspec detection and leniency for wrapped controllers.
Pecan makes abundant use of `inspect.getargspec`, but unless you're very
meticulous in the decorators you wrap your controllers with, the original
argspec is not persisted (and pecan functionality can break in various ways).
When a controller is decorated in a way that breaks argspec, we should instead
attempt to locate the *actual* argspec for the method (not the wrapped
function) and use it.

Additionally, when controllers are missing **kwargs in the method signature to
map optional GET and POST arguments, we shouldn't consider that
a non-routable offense (an HTTP 400); instead, we should just *not* pass
extraneous arguments to the function.

Change-Id: I47fe0496ff6aa105359ee8e5b99f6c80476cc2e9
2014-09-24 18:00:08 -04:00

48 lines
931 B
Python

import inspect
import sys
import six
def iscontroller(obj):
return getattr(obj, 'exposed', False)
def getargspec(method):
"""
Drill through layers of decorators attempting to locate the actual argspec
for a method.
"""
argspec = inspect.getargspec(method)
args = argspec[0]
if args and args[0] == 'self':
return argspec
if hasattr(method, '__func__'):
method = method.__func__
func_closure = six.get_function_closure(method)
closure = next(
(
c for c in func_closure if six.callable(c.cell_contents)
),
None
)
method = closure.cell_contents
return getargspec(method)
def _cfg(f):
if not hasattr(f, '_pecan'):
f._pecan = {}
return f._pecan
if sys.version_info >= (2, 6, 5):
def encode_if_needed(s):
return s
else:
def encode_if_needed(s): # noqa
return s.encode('utf-8')