| 1 | n/a | doctests = """ |
|---|
| 2 | n/a | ########### Tests borrowed from or inspired by test_genexps.py ############ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Test simple loop with conditional |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | >>> sum([i*i for i in range(100) if i&1 == 1]) |
|---|
| 7 | n/a | 166650 |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | Test simple nesting |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | >>> [(i,j) for i in range(3) for j in range(4)] |
|---|
| 12 | n/a | [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3)] |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | Test nesting with the inner expression dependent on the outer |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | >>> [(i,j) for i in range(4) for j in range(i)] |
|---|
| 17 | n/a | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | Make sure the induction variable is not exposed |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | >>> i = 20 |
|---|
| 22 | n/a | >>> sum([i*i for i in range(100)]) |
|---|
| 23 | n/a | 328350 |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | >>> i |
|---|
| 26 | n/a | 20 |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | Verify that syntax error's are raised for listcomps used as lvalues |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | >>> [y for y in (1,2)] = 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
|---|
| 31 | n/a | Traceback (most recent call last): |
|---|
| 32 | n/a | ... |
|---|
| 33 | n/a | SyntaxError: ... |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | >>> [y for y in (1,2)] += 10 # doctest: +IGNORE_EXCEPTION_DETAIL |
|---|
| 36 | n/a | Traceback (most recent call last): |
|---|
| 37 | n/a | ... |
|---|
| 38 | n/a | SyntaxError: ... |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | ########### Tests borrowed from or inspired by test_generators.py ############ |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | Make a nested list comprehension that acts like range() |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | >>> def frange(n): |
|---|
| 46 | n/a | ... return [i for i in range(n)] |
|---|
| 47 | n/a | >>> frange(10) |
|---|
| 48 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | Same again, only as a lambda expression instead of a function definition |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | >>> lrange = lambda n: [i for i in range(n)] |
|---|
| 53 | n/a | >>> lrange(10) |
|---|
| 54 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | Generators can call other generators: |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | >>> def grange(n): |
|---|
| 59 | n/a | ... for x in [i for i in range(n)]: |
|---|
| 60 | n/a | ... yield x |
|---|
| 61 | n/a | >>> list(grange(5)) |
|---|
| 62 | n/a | [0, 1, 2, 3, 4] |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | Make sure that None is a valid return value |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | >>> [None for i in range(10)] |
|---|
| 68 | n/a | [None, None, None, None, None, None, None, None, None, None] |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | ########### Tests for various scoping corner cases ############ |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | Return lambdas that use the iteration variable as a default argument |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | >>> items = [(lambda i=i: i) for i in range(5)] |
|---|
| 75 | n/a | >>> [x() for x in items] |
|---|
| 76 | n/a | [0, 1, 2, 3, 4] |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | Same again, only this time as a closure variable |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | >>> items = [(lambda: i) for i in range(5)] |
|---|
| 81 | n/a | >>> [x() for x in items] |
|---|
| 82 | n/a | [4, 4, 4, 4, 4] |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | Another way to test that the iteration variable is local to the list comp |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | >>> items = [(lambda: i) for i in range(5)] |
|---|
| 87 | n/a | >>> i = 20 |
|---|
| 88 | n/a | >>> [x() for x in items] |
|---|
| 89 | n/a | [4, 4, 4, 4, 4] |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | And confirm that a closure can jump over the list comp scope |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | >>> items = [(lambda: y) for i in range(5)] |
|---|
| 94 | n/a | >>> y = 2 |
|---|
| 95 | n/a | >>> [x() for x in items] |
|---|
| 96 | n/a | [2, 2, 2, 2, 2] |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | We also repeat each of the above scoping tests inside a function |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | >>> def test_func(): |
|---|
| 101 | n/a | ... items = [(lambda i=i: i) for i in range(5)] |
|---|
| 102 | n/a | ... return [x() for x in items] |
|---|
| 103 | n/a | >>> test_func() |
|---|
| 104 | n/a | [0, 1, 2, 3, 4] |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | >>> def test_func(): |
|---|
| 107 | n/a | ... items = [(lambda: i) for i in range(5)] |
|---|
| 108 | n/a | ... return [x() for x in items] |
|---|
| 109 | n/a | >>> test_func() |
|---|
| 110 | n/a | [4, 4, 4, 4, 4] |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | >>> def test_func(): |
|---|
| 113 | n/a | ... items = [(lambda: i) for i in range(5)] |
|---|
| 114 | n/a | ... i = 20 |
|---|
| 115 | n/a | ... return [x() for x in items] |
|---|
| 116 | n/a | >>> test_func() |
|---|
| 117 | n/a | [4, 4, 4, 4, 4] |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | >>> def test_func(): |
|---|
| 120 | n/a | ... items = [(lambda: y) for i in range(5)] |
|---|
| 121 | n/a | ... y = 2 |
|---|
| 122 | n/a | ... return [x() for x in items] |
|---|
| 123 | n/a | >>> test_func() |
|---|
| 124 | n/a | [2, 2, 2, 2, 2] |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | """ |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | __test__ = {'doctests' : doctests} |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def test_main(verbose=None): |
|---|
| 132 | n/a | import sys |
|---|
| 133 | n/a | from test import support |
|---|
| 134 | n/a | from test import test_listcomps |
|---|
| 135 | n/a | support.run_doctest(test_listcomps, verbose) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | # verify reference counting |
|---|
| 138 | n/a | if verbose and hasattr(sys, "gettotalrefcount"): |
|---|
| 139 | n/a | import gc |
|---|
| 140 | n/a | counts = [None] * 5 |
|---|
| 141 | n/a | for i in range(len(counts)): |
|---|
| 142 | n/a | support.run_doctest(test_genexps, verbose) |
|---|
| 143 | n/a | gc.collect() |
|---|
| 144 | n/a | counts[i] = sys.gettotalrefcount() |
|---|
| 145 | n/a | print(counts) |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | if __name__ == "__main__": |
|---|
| 148 | n/a | test_main(verbose=True) |
|---|