コード例 #1
0
ファイル: functional.py プロジェクト: alekibango/celery
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
コード例 #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)))
コード例 #4
0
ファイル: sphinx.py プロジェクト: CheeseL0ver/celery
 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 ''
コード例 #5
0
ファイル: sphinx.py プロジェクト: xpxu/celery
 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 ''
コード例 #6
0
ファイル: sphinx.py プロジェクト: Charles-Yurun/celery
 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 ""
コード例 #7
0
ファイル: base.py プロジェクト: jdufresne/celery
 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))
         )
コード例 #8
0
ファイル: functional.py プロジェクト: Puneet-Shivanand/celery
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
コード例 #9
0
ファイル: functional.py プロジェクト: yangkf1985/celery
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
コード例 #10
0
ファイル: functional.py プロジェクト: Changgyujin/celery
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
コード例 #11
0
ファイル: functional.py プロジェクト: Puneet-Shivanand/celery
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)
    )
コード例 #12
0
ファイル: functional.py プロジェクト: alekibango/celery
def arity_greater(fun, n):
    argspec = getfullargspec(fun)
    return argspec.varargs or len(argspec.args) > n
コード例 #13
0
ファイル: functional.py プロジェクト: xpxu/celery
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)
    )
コード例 #14
0
ファイル: functional.py プロジェクト: xpxu/celery
def arity_greater(fun, n):
    argspec = getfullargspec(fun)
    return argspec.varargs or len(argspec.args) > n