summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tools/test_c_analyzer/util.py
blob: ba73b0a4b5fc6b1b13607e4f2f7bf52774ed4e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import itertools


class PseudoStr(str):
    pass


class StrProxy:
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return self.value
    def __bool__(self):
        return bool(self.value)


class Object:
    def __repr__(self):
        return '<object>'


def wrapped_arg_combos(*args,
                       wrappers=(PseudoStr, StrProxy),
                       skip=(lambda w, i, v: not isinstance(v, str)),
                       ):
    """Yield every possible combination of wrapped items for the given args.

    Effectively, the wrappers are applied to the args according to the
    powerset of the args indicies.  So the result includes the args
    completely unwrapped.

    If "skip" is supplied (default is to skip all non-str values) and
    it returns True for a given arg index/value then that arg will
    remain unwrapped,

    Only unique results are returned.  If an arg was skipped for one
    of the combinations then it could end up matching one of the other
    combinations.  In that case only one of them will be yielded.
    """
    if not args:
        return
    indices = list(range(len(args)))
    # The powerset (from recipe in the itertools docs).
    combos = itertools.chain.from_iterable(itertools.combinations(indices, r)
                                           for r in range(len(indices)+1))
    seen = set()
    for combo in combos:
        for wrap in wrappers:
            indexes = []
            applied = list(args)
            for i in combo:
                arg = args[i]
                if skip and skip(wrap, i, arg):
                    continue
                indexes.append(i)
                applied[i] = wrap(arg)
            key = (wrap, tuple(indexes))
            if key not in seen:
                yield tuple(applied)
                seen.add(key)