Exemplo n.º 1
0
def _embed(outer, inner, use_varargs=True, use_varkwargs=True):
    o_posargs, o_pokargs, o_varargs, o_kwoargs, o_varkwargs = outer

    i_posargs, i_pokargs, i_varargs, i_kwoargs, i_varkwargs = _merge(
        inner,
        ([], [], use_varargs and o_varargs, {}, use_varkwargs and o_varkwargs))

    names = set()

    e_posargs = []
    e_pokargs = []
    e_kwoargs = _util.OrderedDict()

    e_posargs.extend(o_posargs)
    _check_no_dupes(names, o_posargs)
    if i_posargs:
        _check_no_dupes(names, o_pokargs)
        e_posargs.extend(
            arg.replace(kind=arg.POSITIONAL_ONLY) for arg in o_pokargs)
        _check_no_dupes(names, i_posargs)
        e_posargs.extend(i_posargs)
    else:
        _check_no_dupes(names, o_pokargs)
        e_pokargs.extend(o_pokargs)
    _check_no_dupes(names, i_pokargs)
    e_pokargs.extend(i_pokargs)

    _check_no_dupes(names, o_kwoargs.values())
    e_kwoargs.update(o_kwoargs)
    _check_no_dupes(names, i_kwoargs.values())
    e_kwoargs.update(i_kwoargs)

    return (e_posargs, e_pokargs, i_varargs if use_varargs else o_varargs,
            e_kwoargs, i_varkwargs if use_varkwargs else o_varkwargs)
Exemplo n.º 2
0
def _embed(outer, inner, use_varargs=True, use_varkwargs=True, depth=1):
    o_posargs, o_pokargs, o_varargs, o_kwoargs, o_varkwargs, o_src = outer

    stars_sig = SortedParameters([], [], use_varargs and o_varargs, {},
                                 use_varkwargs and o_varkwargs, {})

    i_posargs, i_pokargs, i_varargs, i_kwoargs, i_varkwargs, i_src = \
        _Merger(inner, stars_sig)

    names = set()

    e_posargs = []
    e_pokargs = []
    e_kwoargs = _util.OrderedDict()

    e_posargs.extend(o_posargs)
    _check_no_dupes(names, o_posargs)
    if i_posargs:
        _check_no_dupes(names, o_pokargs)
        e_posargs.extend(
            arg.replace(kind=arg.POSITIONAL_ONLY) for arg in o_pokargs)
        if i_posargs[0].default is i_posargs[0].empty:
            e_posargs = list(_clear_defaults(e_posargs))
        _check_no_dupes(names, i_posargs)
        e_posargs.extend(i_posargs)
    else:
        _check_no_dupes(names, o_pokargs)
        if i_pokargs and i_pokargs[0].default == i_pokargs[0].empty:
            e_posargs = list(_clear_defaults(e_posargs))
            e_pokargs.extend(_clear_defaults(o_pokargs))
        else:
            e_pokargs.extend(o_pokargs)
    _check_no_dupes(names, i_pokargs)
    e_pokargs.extend(i_pokargs)

    _check_no_dupes(names, o_kwoargs.values())
    e_kwoargs.update(o_kwoargs)
    _check_no_dupes(names, i_kwoargs.values())
    e_kwoargs.update(i_kwoargs)

    src = dict(i_src, **o_src)
    if o_varargs and use_varargs:
        src.pop(o_varargs.name, None)
    if o_varkwargs and use_varkwargs:
        src.pop(o_varkwargs.name, None)

    src['+depths'] = merge_depths(
        o_src.get('+depths', {}),
        dict((f, v + depth) for f, v in i_src.get('+depths', {}).items()))

    return (e_posargs, e_pokargs, i_varargs if use_varargs else o_varargs,
            e_kwoargs, i_varkwargs if use_varkwargs else o_varkwargs, src)
Exemplo n.º 3
0
def sort_params(sig, sources=False):
    """Classifies the parameters from sig.

    :param inspect.Signature sig: The signature to operate on

    :returns: A tuple ``(posargs, pokargs, varargs, kwoargs, varkwas)``
    :rtype: ``(list, list, Parameter or None, dict, Parameter or None)``

    ::

        >>> from sigtools import signatures, support
        >>> from pprint import pprint
        >>> pprint(signatures.sort_params(support.s('a, /, b, *args, c, d')))
        ([<Parameter at 0x7fdda4e89418 'a'>],
         [<Parameter at 0x7fdda4e89470 'b'>],
         <Parameter at 0x7fdda4e89c58 'args'>,
         {'c': <Parameter at 0x7fdda4e89c00 'c'>,
          'd': <Parameter at 0x7fdda4e89db8 'd'>},
         None)

    """
    posargs = []
    pokargs = []
    varargs = None
    kwoargs = _util.OrderedDict()
    varkwas = None
    for param in sig.parameters.values():
        if param.kind == param.POSITIONAL_ONLY:
            posargs.append(param)
        elif param.kind == param.POSITIONAL_OR_KEYWORD:
            pokargs.append(param)
        elif param.kind == param.VAR_POSITIONAL:
            varargs = param
        elif param.kind == param.KEYWORD_ONLY:
            kwoargs[param.name] = param
        elif param.kind == param.VAR_KEYWORD:
            varkwas = param
        else:
            raise AssertionError('Unknown param kind {0}'.format(param.kind))
    if sources:
        src = getattr(sig, 'sources', {})
        return SortedParameters(posargs, pokargs, varargs, kwoargs, varkwas,
                                copy_sources(src))
    else:
        return posargs, pokargs, varargs, kwoargs, varkwas
Exemplo n.º 4
0
    def test_multiple_method_calls(self):
        class A(object):
            def wrapped_1(self, x, y):
                pass

            def wrapped_2(self, x, y):
                pass

            def method(self, a, *args, **kwargs):
                self.wrapped_1(a, *args, **kwargs)
                self.wrapped_2(a, *args, **kwargs)

        self._test(
            A().method, 'a, y',
            _util.OrderedDict([(0, 'a'), ('method', 'a'), ('wrapped_1', 'y'),
                               ('wrapped_2', 'y'),
                               ('+depths', {
                                   'method': 0,
                                   'wrapped_1': 1,
                                   'wrapped_2': 1
                               })]))
Exemplo n.º 5
0
def _merge(left, right):
    l_posargs, l_pokargs, l_varargs, l_kwoargs, l_varkwargs = left
    r_posargs, r_pokargs, r_varargs, r_kwoargs, r_varkwargs = right

    posargs = []
    pokargs = []
    varargs = r_varargs and l_varargs
    kwoargs = _util.OrderedDict()
    varkwargs = r_varkwargs and l_varkwargs

    l_kwoargs_limbo = _util.OrderedDict()
    for l_kwoarg in l_kwoargs.values():
        if l_kwoarg.name in r_kwoargs:
            kwoargs[l_kwoarg.name] = _concile_meta(l_kwoarg,
                                                   r_kwoargs[l_kwoarg.name])
        else:
            l_kwoargs_limbo[l_kwoarg.name] = l_kwoarg

    r_kwoargs_limbo = _util.OrderedDict()
    for r_kwoarg in r_kwoargs.values():
        if r_kwoarg.name not in l_kwoargs:
            r_kwoargs_limbo[r_kwoarg.name] = r_kwoarg

    il_pokargs = iter(l_pokargs)
    ir_pokargs = iter(r_pokargs)

    for l_posarg, r_posarg in zip_longest(l_posargs, r_posargs):
        if l_posarg and r_posarg:
            posargs.append(_concile_meta(l_posarg, r_posarg))
        else:
            if l_posarg:
                _merge_unbalanced_pos(l_posarg, ir_pokargs, r_varargs, posargs)
            else:
                _merge_unbalanced_pos(r_posarg,
                                      il_pokargs,
                                      l_varargs,
                                      posargs,
                                      prefer_o=True)

    for l_pokarg, r_pokarg in zip_longest(il_pokargs, ir_pokargs):
        if l_pokarg and r_pokarg:
            if l_pokarg.name == r_pokarg.name:
                pokargs.append(_concile_meta(l_pokarg, r_pokarg))
            else:
                for i, pokarg in enumerate(pokargs):
                    pokargs[i] = pokarg.replace(kind=pokarg.POSITIONAL_ONLY)
                pokargs.append(
                    _concile_meta(
                        l_pokarg,
                        r_pokarg).replace(kind=l_pokarg.POSITIONAL_ONLY))
        else:
            if l_pokarg:
                _merge_unbalanced_pok(l_pokarg, r_varargs, r_varkwargs,
                                      r_kwoargs_limbo, posargs, pokargs,
                                      kwoargs)
            else:
                _merge_unbalanced_pok(r_pokarg, l_varargs, l_varkwargs,
                                      l_kwoargs_limbo, posargs, pokargs,
                                      kwoargs)

    if l_kwoargs_limbo:
        _merge_kwoargs_limbo(l_kwoargs_limbo, r_varkwargs, kwoargs)
    if r_kwoargs_limbo:
        _merge_kwoargs_limbo(r_kwoargs_limbo, l_varkwargs, kwoargs)

    return posargs, pokargs, varargs, kwoargs, varkwargs
Exemplo n.º 6
0
    def _merge(self):
        self.posargs = []
        self.pokargs = []
        self.varargs_src = [self.l.varargs, self.r.varargs]
        self.kwoargs = _util.OrderedDict()
        self.varkwargs_src = [self.l.varkwargs, self.r.varkwargs]
        self.src = {'+depths': self._merge_depths()}

        self.l_unmatched_kwoargs = _util.OrderedDict()
        for param in self.l.kwoargs.values():
            name = param.name
            if name in self.r.kwoargs:
                self.kwoargs[name] = self._concile_meta(
                    param, self.r.kwoargs[name])
                self.src[name] = list(
                    itertools.chain(self.l.sources.get(name, ()),
                                    self.r.sources.get(name, ())))
            else:
                self.l_unmatched_kwoargs[param.name] = param

        self.r_unmatched_kwoargs = _util.OrderedDict()
        for param in self.r.kwoargs.values():
            if param.name not in self.l.kwoargs:
                self.r_unmatched_kwoargs[param.name] = param

        il_pokargs = iter(self.l.pokargs)
        ir_pokargs = iter(self.r.pokargs)

        for l_param, r_param in zip_longest(self.l.posargs, self.r.posargs):
            if l_param and r_param:
                p = self._concile_meta(l_param, r_param)
                self.posargs.append(p)
                if l_param.name == r_param.name:
                    _add_sources(self.src, l_param.name, self.l.sources,
                                 self.r.sources)
                else:
                    _add_sources(self.src, l_param.name, self.l.sources)
            else:
                if l_param:
                    self._merge_unbalanced_pos(l_param, self.l.sources,
                                               ir_pokargs, self.r.varargs,
                                               self.r.sources)
                else:
                    self._merge_unbalanced_pos(r_param, self.r.sources,
                                               il_pokargs, self.l.varargs,
                                               self.l.sources)

        for l_param, r_param in zip_longest(il_pokargs, ir_pokargs):
            if l_param and r_param:
                if l_param.name == r_param.name:
                    self.pokargs.append(self._concile_meta(l_param, r_param))
                    _add_sources(self.src, l_param.name, self.l.sources,
                                 self.r.sources)
                else:
                    for i, pokarg in enumerate(self.pokargs):
                        self.pokargs[i] = pokarg.replace(
                            kind=pokarg.POSITIONAL_ONLY)
                    self.pokargs.append(
                        self._concile_meta(
                            l_param,
                            r_param).replace(kind=l_param.POSITIONAL_ONLY))
                    _add_sources(self.src, l_param.name, self.l.sources)
            else:
                if l_param:
                    self._merge_unbalanced_pok(l_param, self.l.sources,
                                               self.r.varargs,
                                               self.r.varkwargs,
                                               self.r_unmatched_kwoargs,
                                               self.r.sources)
                else:
                    self._merge_unbalanced_pok(r_param, self.r.sources,
                                               self.l.varargs,
                                               self.l.varkwargs,
                                               self.l_unmatched_kwoargs,
                                               self.l.sources)

        if self.l_unmatched_kwoargs:
            self._merge_unmatched_kwoargs(self.l_unmatched_kwoargs,
                                          self.r.varkwargs, self.l.sources)
        if self.r_unmatched_kwoargs:
            self._merge_unmatched_kwoargs(self.r_unmatched_kwoargs,
                                          self.l.varkwargs, self.r.sources)

        self.varargs = self._add_starargs(self.varargs_src, self.l.varargs,
                                          self.r.varargs)
        self.varkwargs = self._add_starargs(self.varkwargs_src,
                                            self.l.varkwargs, self.r.varkwargs)