Michael Merickel
2017-07-08 417320a5391e7f3b551f8f10e28aae9cd00ae27e
pyramid/util.py
@@ -17,6 +17,8 @@
    )
from pyramid.compat import (
    getargspec,
    im_func,
    is_nonstr_iter,
    integer_types,
    string_types,
@@ -645,3 +647,53 @@
    return (pattern[0] == "." and
            (host.endswith(pattern) or host == pattern[1:]) or
            pattern == host)
def takes_one_arg(callee, attr=None, argname=None):
    ismethod = False
    if attr is None:
        attr = '__call__'
    if inspect.isroutine(callee):
        fn = callee
    elif inspect.isclass(callee):
        try:
            fn = callee.__init__
        except AttributeError:
            return False
        ismethod = hasattr(fn, '__call__')
    else:
        try:
            fn = getattr(callee, attr)
        except AttributeError:
            return False
    try:
        argspec = getargspec(fn)
    except TypeError:
        return False
    args = argspec[0]
    if hasattr(fn, im_func) or ismethod:
        # it's an instance method (or unbound method on py2)
        if not args:
            return False
        args = args[1:]
    if not args:
        return False
    if len(args) == 1:
        return True
    if argname:
        defaults = argspec[3]
        if defaults is None:
            defaults = ()
        if args[0] == argname:
            if len(args) - len(defaults) == 1:
                return True
    return False