# Python code coverage for Lib/test/test_setcomps.py

# | count | content |
---|---|---|

1 | n/a | doctests = """ |

2 | n/a | ########### Tests mostly copied from test_listcomps.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 case |

10 | n/a | |

11 | n/a | >>> {2*y + x + 1 for x in (0,) for y in (1,)} |

12 | n/a | {3} |

13 | n/a | |

14 | n/a | Test simple nesting |

15 | n/a | |

16 | n/a | >>> list(sorted({(i,j) for i in range(3) for j in range(4)})) |

17 | 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)] |

18 | n/a | |

19 | n/a | Test nesting with the inner expression dependent on the outer |

20 | n/a | |

21 | n/a | >>> list(sorted({(i,j) for i in range(4) for j in range(i)})) |

22 | n/a | [(1, 0), (2, 0), (2, 1), (3, 0), (3, 1), (3, 2)] |

23 | n/a | |

24 | n/a | Make sure the induction variable is not exposed |

25 | n/a | |

26 | n/a | >>> i = 20 |

27 | n/a | >>> sum({i*i for i in range(100)}) |

28 | n/a | 328350 |

29 | n/a | |

30 | n/a | >>> i |

31 | n/a | 20 |

32 | n/a | |

33 | n/a | Verify that syntax error's are raised for setcomps used as lvalues |

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 | >>> {y for y in (1,2)} += 10 # doctest: +IGNORE_EXCEPTION_DETAIL |

41 | n/a | Traceback (most recent call last): |

42 | n/a | ... |

43 | n/a | SyntaxError: ... |

44 | n/a | |

45 | n/a | |

46 | n/a | Make a nested set comprehension that acts like set(range()) |

47 | n/a | |

48 | n/a | >>> def srange(n): |

49 | n/a | ... return {i for i in range(n)} |

50 | n/a | >>> list(sorted(srange(10))) |

51 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |

52 | n/a | |

53 | n/a | Same again, only as a lambda expression instead of a function definition |

54 | n/a | |

55 | n/a | >>> lrange = lambda n: {i for i in range(n)} |

56 | n/a | >>> list(sorted(lrange(10))) |

57 | n/a | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |

58 | n/a | |

59 | n/a | Generators can call other generators: |

60 | n/a | |

61 | n/a | >>> def grange(n): |

62 | n/a | ... for x in {i for i in range(n)}: |

63 | n/a | ... yield x |

64 | n/a | >>> list(sorted(grange(5))) |

65 | n/a | [0, 1, 2, 3, 4] |

66 | n/a | |

67 | n/a | |

68 | n/a | Make sure that None is a valid return value |

69 | n/a | |

70 | n/a | >>> {None for i in range(10)} |

71 | n/a | {None} |

72 | n/a | |

73 | n/a | ########### Tests for various scoping corner cases ############ |

74 | n/a | |

75 | n/a | Return lambdas that use the iteration variable as a default argument |

76 | n/a | |

77 | n/a | >>> items = {(lambda i=i: i) for i in range(5)} |

78 | n/a | >>> {x() for x in items} == set(range(5)) |

79 | n/a | True |

80 | n/a | |

81 | n/a | Same again, only this time as a closure variable |

82 | n/a | |

83 | n/a | >>> items = {(lambda: i) for i in range(5)} |

84 | n/a | >>> {x() for x in items} |

85 | n/a | {4} |

86 | n/a | |

87 | n/a | Another way to test that the iteration variable is local to the list comp |

88 | n/a | |

89 | n/a | >>> items = {(lambda: i) for i in range(5)} |

90 | n/a | >>> i = 20 |

91 | n/a | >>> {x() for x in items} |

92 | n/a | {4} |

93 | n/a | |

94 | n/a | And confirm that a closure can jump over the list comp scope |

95 | n/a | |

96 | n/a | >>> items = {(lambda: y) for i in range(5)} |

97 | n/a | >>> y = 2 |

98 | n/a | >>> {x() for x in items} |

99 | n/a | {2} |

100 | n/a | |

101 | n/a | We also repeat each of the above scoping tests inside a function |

102 | n/a | |

103 | n/a | >>> def test_func(): |

104 | n/a | ... items = {(lambda i=i: i) for i in range(5)} |

105 | n/a | ... return {x() for x in items} |

106 | n/a | >>> test_func() == set(range(5)) |

107 | n/a | True |

108 | n/a | |

109 | n/a | >>> def test_func(): |

110 | n/a | ... items = {(lambda: i) for i in range(5)} |

111 | n/a | ... return {x() for x in items} |

112 | n/a | >>> test_func() |

113 | n/a | {4} |

114 | n/a | |

115 | n/a | >>> def test_func(): |

116 | n/a | ... items = {(lambda: i) for i in range(5)} |

117 | n/a | ... i = 20 |

118 | n/a | ... return {x() for x in items} |

119 | n/a | >>> test_func() |

120 | n/a | {4} |

121 | n/a | |

122 | n/a | >>> def test_func(): |

123 | n/a | ... items = {(lambda: y) for i in range(5)} |

124 | n/a | ... y = 2 |

125 | n/a | ... return {x() for x in items} |

126 | n/a | >>> test_func() |

127 | n/a | {2} |

128 | n/a | |

129 | n/a | """ |

130 | n/a | |

131 | n/a | |

132 | n/a | __test__ = {'doctests' : doctests} |

133 | n/a | |

134 | n/a | def test_main(verbose=None): |

135 | n/a | import sys |

136 | n/a | from test import support |

137 | n/a | from test import test_setcomps |

138 | n/a | support.run_doctest(test_setcomps, verbose) |

139 | n/a | |

140 | n/a | # verify reference counting |

141 | n/a | if verbose and hasattr(sys, "gettotalrefcount"): |

142 | n/a | import gc |

143 | n/a | counts = [None] * 5 |

144 | n/a | for i in range(len(counts)): |

145 | n/a | support.run_doctest(test_setcomps, verbose) |

146 | n/a | gc.collect() |

147 | n/a | counts[i] = sys.gettotalrefcount() |

148 | n/a | print(counts) |

149 | n/a | |

150 | n/a | if __name__ == "__main__": |

151 | n/a | test_main(verbose=True) |