示例#1
0
def getargcount(iteratee, maxargs):
    """Return argument count of iteratee function."""
    if hasattr(iteratee, "_argcount"):
        # Optimization feature where argcount of iteratee is known and properly
        # set by initator.
        return iteratee._argcount

    if isinstance(iteratee, type) or pyd.is_builtin(iteratee):
        # Only pass single argument to type iteratees or builtins.
        argcount = 1
    else:
        argcount = 1

        try:
            argcount = _getfullargspec(iteratee, maxargs)
        except TypeError:  # pragma: no cover
            # PY2: Python2.7 throws a TypeError on classes that have __call__() defined but Python3
            # doesn't. So if we fail with TypeError here, try iteratee as iteratee.__call__.
            if PY2 and hasattr(iteratee, "__call__"):  # noqa: B004
                try:
                    argcount = _getfullargspec(iteratee.__call__, maxargs)
                except TypeError:
                    pass

    return argcount
示例#2
0
def getargcount(iteratee, maxargs):
    """Return argument count of iteratee function."""
    if hasattr(iteratee, '_argcount'):
        # Optimization feature where argcount of iteratee is known and properly
        # set by initator.
        return iteratee._argcount

    argspec = None

    if isinstance(iteratee, type) or pyd.is_builtin(iteratee):
        # Only pass single argument to type iteratees or builtins.
        argcount = 1
    else:
        try:
            argspec = getfullargspec(iteratee)

            if argspec and not argspec.varargs:
                # Use inspected arg count.
                argcount = len(argspec.args)
            else:
                # Assume all args are handleable
                argcount = maxargs
        except TypeError:  # pragma: no cover
            argcount = 1

    return argcount
示例#3
0
def getargcount(iteratee, maxargs):
    """Return argument count of iteratee function."""
    if hasattr(iteratee, '_argcount'):
        # Optimization feature where argcount of iteratee is known and properly
        # set by initator.
        return iteratee._argcount

    if isinstance(iteratee, type) or pyd.is_builtin(iteratee):
        # Only pass single argument to type iteratees or builtins.
        argcount = 1
    else:
        argcount = 1

        try:
            argcount = _getfullargspec(iteratee, maxargs)
        except TypeError:  # pragma: no cover
            # PY2: Python2.7 throws a TypeError on classes that have __call__()
            # defined but Python3 doesn't. So if we fail with TypeError here,
            # try iteratee as iteratee.__call__.
            if PY2 and hasattr(iteratee, '__call__'):
                try:
                    argcount = _getfullargspec(iteratee.__call__, maxargs)
                except TypeError:
                    pass

    return argcount
示例#4
0
def call_callback(callback, *args):
    """Inspect argspec of `callback` function and only pass the supported
    arguments when calling it.
    """
    maxargs = len(args)
    argspec = None

    try:
        argspec = inspect.getargspec(callback)
    except TypeError:
        try:
            argspec = inspect.getargspec(getattr(callback, "__call__", None))
        except TypeError:  # pragma: no cover
            pass
    finally:
        if isinstance(callback, type):
            # Only pass single argument to type callbacks. This is for things
            # like int(), float(), str(), etc.
            argcount = 1
        elif pyd.is_builtin(callback):
            argcount = guess_builtin_argcount(callback) or maxargs
        elif argspec and argspec.varargs:
            # Callback supports variable arguments.
            argcount = maxargs
        elif argspec:
            # Use inspected arg count.
            argcount = len(argspec.args)
        else:  # pragma: no cover
            argcount = maxargs

    argstop = min([maxargs, argcount])

    return callback(*args[:argstop])
示例#5
0
def call_callback(callback, *args):
    """Inspect argspec of `callback` function and only pass the supported
    arguments when calling it.
    """
    maxargs = len(args)
    argspec = None

    try:
        argspec = inspect.getargspec(callback)
    except TypeError:
        try:
            argspec = inspect.getargspec(getattr(callback, '__call__', None))
        except TypeError:  # pragma: no cover
            pass
    finally:
        if isinstance(callback, type):
            # Only pass single argument to type callbacks. This is for things
            # like int(), float(), str(), etc.
            argcount = 1
        elif pyd.is_builtin(callback):
            argcount = guess_builtin_argcount(callback) or maxargs
        elif argspec and argspec.varargs:
            # Callback supports variable arguments.
            argcount = maxargs
        elif argspec:
            # Use inspected arg count.
            argcount = len(argspec.args)
        else:  # pragma: no cover
            argcount = maxargs

    argstop = min([maxargs, argcount])

    return callback(*args[:argstop])
示例#6
0
def getargcount(callback, maxargs):
    """Return argument count of callback function."""
    if hasattr(callback, '_argcount'):
        # Optimization feature where argcount of callback is known and properly
        # set by initator.
        return callback._argcount

    argspec = None

    if isinstance(callback, type) or pyd.is_builtin(callback):
        # Only pass single argument to type callbacks or builtins.
        argcount = 1
    else:
        try:
            argspec = inspect.getargspec(callback)

            if argspec and not argspec.varargs:
                # Use inspected arg count.
                argcount = len(argspec.args)
            else:
                # Assume all args are handleable
                argcount = maxargs
        except TypeError:  # pragma: no cover
            argcount = 1

    return argcount
示例#7
0
def getargcount(iteratee, maxargs):
    """Return argument count of iteratee function."""
    if hasattr(iteratee, "_argcount"):
        # Optimization feature where argcount of iteratee is known and properly
        # set by initator.
        return iteratee._argcount

    if isinstance(iteratee, type) or pyd.is_builtin(iteratee):
        # Only pass single argument to type iteratees or builtins.
        argcount = 1
    else:
        argcount = 1

        try:
            argcount = _getargcount(iteratee, maxargs)
        except TypeError:  # pragma: no cover
            pass

    return argcount
示例#8
0
def test_is_builtin(case, expected):
    assert _.is_builtin(case) == expected