Get the mapping of arguments to values.

    Generates a dict, with keys being the function argument names
    (including the names of the * and ** arguments, if any), and
    values the respective bound values from 'positional' and 'named'.
    A parameter for the request is injected.  Returns a tuple of the
    dict, the object the method is being called on, and the name of
    the injected request argument.

def _getcallargs(func, positional, named):
    """Get the mapping of arguments to values.

    Generates a dict, with keys being the function argument names
    (including the names of the * and ** arguments, if any), and
    values the respective bound values from 'positional' and 'named'.
    A parameter for the request is injected.  Returns a tuple of the
    dict, the object the method is being called on, and the name of
    the injected request argument.
    """

    args, varargs, varkw, defaults = inspect.getargspec(func)
    f_name = func.__name__
    arg2value = {}

    # The following closures are basically because of tuple parameter
    # unpacking.
    assigned_tuple_params = []

    def assign(arg, value):
        if isinstance(arg, str):
            arg2value[arg] = value
        else:
            assigned_tuple_params.append(arg)
            value = iter(value)
            for i, subarg in enumerate(arg):
                try:
                    subvalue = next(value)
                except StopIteration:
                    raise ValueError('need more than %d %s to unpack' %
                                     (i, 'values' if i > 1 else 'value'))
                assign(subarg, subvalue)
            try:
                next(value)
            except StopIteration:
                pass
            else:
                raise ValueError('too many values to unpack')

    def is_assigned(arg):
        if isinstance(arg, str):
            return arg in arg2value
        return arg in assigned_tuple_params

    # Inject a place-holder for the request and get the self and the
    # req_name
    positional = positional[:1] + (None,) + positional[1:]
    theSelf = positional[0]
    req_name = args[1]

    num_pos = len(positional)
    num_total = num_pos + len(named)
    num_args = len(args)
    num_defaults = len(defaults) if defaults else 0

    # Start with our positional parameters...
    for arg, value in zip(args, positional):
        assign(arg, value)

    # Deal with the variable argument list...
    if varargs:
        if num_pos > num_args:
            assign(varargs, positional[-(num_pos-num_args):])
        else:
            assign(varargs, ())
    elif 0 < num_args < num_pos:
        raise TypeError('%s() takes %s %d %s (%d given)' % (
            f_name, 'at most' if defaults else 'exactly', num_args,
            'arguments' if num_args > 1 else 'argument', num_total))
    elif num_args == 0 and num_total:
        raise TypeError('%s() takes no arguments (%d given)' %
                        (f_name, num_total))

    # Exclusion rules on keyword arguments
    for arg in args:
        if isinstance(arg, str) and arg in named:
            if is_assigned(arg):
                raise TypeError("%s() got multiple values for keyword "
                                "argument '%s'" % (f_name, arg))
            else:
                assign(arg, named.pop(arg))

    # Fill in any missing values with the defaults
    if defaults:
        for arg, value in zip(args[-num_defaults:], defaults):
            if not is_assigned(arg):
                assign(arg, value)

    # Handle the **names
    if varkw:
        assign(varkw, named)
    elif named:
        unexpected = next(iter(named))
        if isinstance(unexpected, unicode):
            unexpected = unexpected.encode(sys.getdefaultencoding(), 'replace')
        raise TypeError("%s() got an unexpected keyword argument '%s'" %
                        (f_name, unexpected))

    # Anything left over?
    unassigned = num_args - len([arg for arg in args if is_assigned(arg)])
    if unassigned:
        num_required = num_args - num_defaults
        raise TypeError('%s() takes %s %d %s (%d given)' % (
            f_name, 'at least' if defaults else 'exactly', num_required,
            'arguments' if num_required > 1 else 'argument', num_total))

    # Return the mapping and the name of the request argument
    return arg2value, theSelf, req_name