Exemplo n.º 1
0
def head_from_fun(fun, bound=False, debug=False):
    """Generate signature function from actual function."""
    # we could use inspect.Signature here, but that implementation
    # is very slow since it implements the argument checking
    # in pure-Python.  Instead we use exec to create a new function
    # with an empty body, meaning it has the same performance as
    # as just calling a function.
    is_function = inspect.isfunction(fun)
    is_callable = hasattr(fun, '__call__')
    is_method = inspect.ismethod(fun)

    if not is_function and is_callable and not is_method:
        name, fun = fun.__class__.__name__, fun.__call__
    else:
        name = fun.__name__
    definition = FUNHEAD_TEMPLATE.format(
        fun_name=name,
        fun_args=_argsfromspec(getfullargspec(fun)),
        fun_value=1,
    )
    if debug:  # pragma: no cover
        print(definition, file=sys.stderr)
    namespace = {'__name__': fun.__module__}
    # pylint: disable=exec-used
    # Tasks are rarely, if ever, created at runtime - exec here is fine.
    exec(definition, namespace)
    result = namespace[name]
    result._source = definition
    if bound:
        return partial(result, object())
    return result
def head_from_fun(fun, bound=False, debug=False):
    """Generate signature function from actual function."""
    # we could use inspect.Signature here, but that implementation
    # is very slow since it implements the argument checking
    # in pure-Python.  Instead we use exec to create a new function
    # with an empty body, meaning it has the same performance as
    # as just calling a function.
    if not isfunction(fun) and hasattr(fun, '__call__'):
        name, fun = fun.__class__.__name__, fun.__call__
    else:
        name = fun.__name__
    definition = FUNHEAD_TEMPLATE.format(
        fun_name=name,
        fun_args=_argsfromspec(getfullargspec(fun)),
        fun_value=1,
    )
    if debug:  # pragma: no cover
        print(definition, file=sys.stderr)
    namespace = {'__name__': fun.__module__}
    # pylint: disable=exec-used
    # Tasks are rarely, if ever, created at runtime - exec here is fine.
    exec(definition, namespace)
    result = namespace[name]
    result._source = definition
    if bound:
        return partial(result, object())
    return result
Exemplo n.º 3
0
 def verify_args(self, given, _index=0):
     S = getfullargspec(self.run)
     _index = 1 if S.args and S.args[0] == 'self' else _index
     required = S.args[_index:-len(S.defaults) if S.defaults else None]
     missing = required[len(given):]
     if missing:
         raise self.UsageError('Missing required {0}: {1}'.format(
             text.pluralize(len(missing), 'argument'), ', '.join(missing)))
Exemplo n.º 4
0
 def format_args(self):
     wrapped = getattr(self.object, '__wrapped__')
     if wrapped is not None:
         argspec = getfullargspec(wrapped)
         fmt = formatargspec(*argspec)
         fmt = fmt.replace('\\', '\\\\')
         return fmt
     return ''
Exemplo n.º 5
0
 def format_args(self):
     wrapped = getattr(self.object, '__wrapped__', None)
     if wrapped is not None:
         argspec = getfullargspec(wrapped)
         fmt = formatargspec(*argspec)
         fmt = fmt.replace('\\', '\\\\')
         return fmt
     return ''
Exemplo n.º 6
0
 def format_args(self):
     wrapped = getattr(self.object, "__wrapped__", None)
     if wrapped is not None:
         argspec = getfullargspec(wrapped)
         fmt = formatargspec(*argspec)
         fmt = fmt.replace("\\", "\\\\")
         return fmt
     return ""
Exemplo n.º 7
0
 def verify_args(self, given, _index=0):
     S = getfullargspec(self.run)
     _index = 1 if S.args and S.args[0] == "self" else _index
     required = S.args[_index : -len(S.defaults) if S.defaults else None]
     missing = required[len(given) :]
     if missing:
         raise self.UsageError(
             "Missing required {0}: {1}".format(text.pluralize(len(missing), "argument"), ", ".join(missing))
         )
Exemplo n.º 8
0
def head_from_fun(fun, bound=False, debug=False):
    if not isfunction(fun) and hasattr(fun, '__call__'):
        name, fun = fun.__class__.__name__, fun.__call__
    else:
        name = fun.__name__
    definition = FUNHEAD_TEMPLATE.format(
        fun_name=name,
        fun_args=_argsfromspec(getfullargspec(fun)),
        fun_value=1,
    )
    if debug:  # pragma: no cover
        print(definition, file=sys.stderr)
    namespace = {'__name__': 'headof_{0}'.format(name)}
    exec(definition, namespace)
    result = namespace[name]
    result._source = definition
    if bound:
        return partial(result, object())
    return result
Exemplo n.º 9
0
def head_from_fun(fun, bound=False, debug=False):
    if not isfunction(fun) and hasattr(fun, '__call__'):
        name, fun = fun.__class__.__name__, fun.__call__
    else:
        name = fun.__name__
    definition = FUNHEAD_TEMPLATE.format(
        fun_name=name,
        fun_args=_argsfromspec(getfullargspec(fun)),
        fun_value=1,
    )
    if debug:  # pragma: no cover
        print(definition, file=sys.stderr)
    namespace = {'__name__': 'headof_{0}'.format(name)}
    exec(definition, namespace)
    result = namespace[name]
    result._source = definition
    if bound:
        return partial(result, object())
    return result
Exemplo n.º 10
0
def head_from_fun(fun, bound=False, debug=False):
    # we could use inspect.Signature here, but that implementation
    # is very slow since it implements the argument checking
    # in pure-Python.  Instead we use exec to create a new function
    # with an empty body, meaning it has the same performance as
    # as just calling a function.
    if not isfunction(fun) and hasattr(fun, '__call__'):
        name, fun = fun.__class__.__name__, fun.__call__
    else:
        name = fun.__name__
    definition = FUNHEAD_TEMPLATE.format(
        fun_name=name,
        fun_args=_argsfromspec(getfullargspec(fun)),
        fun_value=1,
    )
    if debug:  # pragma: no cover
        print(definition, file=sys.stderr)
    namespace = {'__name__': fun.__module__}
    exec(definition, namespace)
    result = namespace[name]
    result._source = definition
    if bound:
        return partial(result, object())
    return result
Exemplo n.º 11
0
def fun_takes_argument(name, fun, position=None):
    spec = getfullargspec(fun)
    return (
        spec.varkw or spec.varargs or
        (len(spec.args) >= position if position else name in spec.args)
    )
Exemplo n.º 12
0
def arity_greater(fun, n):
    argspec = getfullargspec(fun)
    return argspec.varargs or len(argspec.args) > n
Exemplo n.º 13
0
def fun_takes_argument(name, fun, position=None):
    spec = getfullargspec(fun)
    return (
        spec.varkw or spec.varargs or
        (len(spec.args) >= position if position else name in spec.args)
    )
Exemplo n.º 14
0
def arity_greater(fun, n):
    argspec = getfullargspec(fun)
    return argspec.varargs or len(argspec.args) > n