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

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

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

2 | n/a | |

3 | n/a | Test simple loop with conditional |

4 | n/a | |

5 | n/a | >>> sum(i*i for i in range(100) if i&1 == 1) |

6 | n/a | 166650 |

7 | n/a | |

8 | n/a | Test simple nesting |

9 | n/a | |

10 | n/a | >>> list((i,j) for i in range(3) for j in range(4) ) |

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

12 | n/a | |

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

14 | n/a | |

15 | n/a | >>> list((i,j) for i in range(4) for j in range(i) ) |

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

17 | n/a | |

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

19 | n/a | |

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

21 | n/a | >>> sum(i*i for i in range(100)) |

22 | n/a | 328350 |

23 | n/a | >>> i |

24 | n/a | 20 |

25 | n/a | |

26 | n/a | Test first class |

27 | n/a | |

28 | n/a | >>> g = (i*i for i in range(4)) |

29 | n/a | >>> type(g) |

30 | n/a | <class 'generator'> |

31 | n/a | >>> list(g) |

32 | n/a | [0, 1, 4, 9] |

33 | n/a | |

34 | n/a | Test direct calls to next() |

35 | n/a | |

36 | n/a | >>> g = (i*i for i in range(3)) |

37 | n/a | >>> next(g) |

38 | n/a | 0 |

39 | n/a | >>> next(g) |

40 | n/a | 1 |

41 | n/a | >>> next(g) |

42 | n/a | 4 |

43 | n/a | >>> next(g) |

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

45 | n/a | File "<pyshell#21>", line 1, in -toplevel- |

46 | n/a | next(g) |

47 | n/a | StopIteration |

48 | n/a | |

49 | n/a | Does it stay stopped? |

50 | n/a | |

51 | n/a | >>> next(g) |

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

53 | n/a | File "<pyshell#21>", line 1, in -toplevel- |

54 | n/a | next(g) |

55 | n/a | StopIteration |

56 | n/a | >>> list(g) |

57 | n/a | [] |

58 | n/a | |

59 | n/a | Test running gen when defining function is out of scope |

60 | n/a | |

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

62 | n/a | ... return (i*i for i in range(n)) |

63 | n/a | >>> list(f(10)) |

64 | n/a | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |

65 | n/a | |

66 | n/a | >>> def f(n): |

67 | n/a | ... return ((i,j) for i in range(3) for j in range(n)) |

68 | n/a | >>> list(f(4)) |

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

70 | n/a | >>> def f(n): |

71 | n/a | ... return ((i,j) for i in range(3) for j in range(4) if j in range(n)) |

72 | n/a | >>> list(f(4)) |

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

74 | n/a | >>> list(f(2)) |

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

76 | n/a | |

77 | n/a | Verify that parenthesis are required in a statement |

78 | n/a | |

79 | n/a | >>> def f(n): |

80 | n/a | ... return i*i for i in range(n) |

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

82 | n/a | ... |

83 | n/a | SyntaxError: invalid syntax |

84 | n/a | |

85 | n/a | Verify that parenthesis are required when used as a keyword argument value |

86 | n/a | |

87 | n/a | >>> dict(a = i for i in range(10)) |

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

89 | n/a | ... |

90 | n/a | SyntaxError: invalid syntax |

91 | n/a | |

92 | n/a | Verify that parenthesis are required when used as a keyword argument value |

93 | n/a | |

94 | n/a | >>> dict(a = (i for i in range(10))) #doctest: +ELLIPSIS |

95 | n/a | {'a': <generator object <genexpr> at ...>} |

96 | n/a | |

97 | n/a | Verify early binding for the outermost for-expression |

98 | n/a | |

99 | n/a | >>> x=10 |

100 | n/a | >>> g = (i*i for i in range(x)) |

101 | n/a | >>> x = 5 |

102 | n/a | >>> list(g) |

103 | n/a | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] |

104 | n/a | |

105 | n/a | Verify that the outermost for-expression makes an immediate check |

106 | n/a | for iterability |

107 | n/a | |

108 | n/a | >>> (i for i in 6) |

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

110 | n/a | File "<pyshell#4>", line 1, in -toplevel- |

111 | n/a | (i for i in 6) |

112 | n/a | TypeError: 'int' object is not iterable |

113 | n/a | |

114 | n/a | Verify late binding for the outermost if-expression |

115 | n/a | |

116 | n/a | >>> include = (2,4,6,8) |

117 | n/a | >>> g = (i*i for i in range(10) if i in include) |

118 | n/a | >>> include = (1,3,5,7,9) |

119 | n/a | >>> list(g) |

120 | n/a | [1, 9, 25, 49, 81] |

121 | n/a | |

122 | n/a | Verify late binding for the innermost for-expression |

123 | n/a | |

124 | n/a | >>> g = ((i,j) for i in range(3) for j in range(x)) |

125 | n/a | >>> x = 4 |

126 | n/a | >>> list(g) |

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

128 | n/a | |

129 | n/a | Verify re-use of tuples (a side benefit of using genexps over listcomps) |

130 | n/a | |

131 | n/a | >>> tupleids = list(map(id, ((i,i) for i in range(10)))) |

132 | n/a | >>> int(max(tupleids) - min(tupleids)) |

133 | n/a | 0 |

134 | n/a | |

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

136 | n/a | |

137 | n/a | >>> (y for y in (1,2)) = 10 |

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

139 | n/a | ... |

140 | n/a | SyntaxError: can't assign to generator expression |

141 | n/a | |

142 | n/a | >>> (y for y in (1,2)) += 10 |

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

144 | n/a | ... |

145 | n/a | SyntaxError: can't assign to generator expression |

146 | n/a | |

147 | n/a | |

148 | n/a | ########### Tests borrowed from or inspired by test_generators.py ############ |

149 | n/a | |

150 | n/a | Make a generator that acts like range() |

151 | n/a | |

152 | n/a | >>> yrange = lambda n: (i for i in range(n)) |

153 | n/a | >>> list(yrange(10)) |

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

155 | n/a | |

156 | n/a | Generators always return to the most recent caller: |

157 | n/a | |

158 | n/a | >>> def creator(): |

159 | n/a | ... r = yrange(5) |

160 | n/a | ... print("creator", next(r)) |

161 | n/a | ... return r |

162 | n/a | >>> def caller(): |

163 | n/a | ... r = creator() |

164 | n/a | ... for i in r: |

165 | n/a | ... print("caller", i) |

166 | n/a | >>> caller() |

167 | n/a | creator 0 |

168 | n/a | caller 1 |

169 | n/a | caller 2 |

170 | n/a | caller 3 |

171 | n/a | caller 4 |

172 | n/a | |

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

174 | n/a | |

175 | n/a | >>> def zrange(n): |

176 | n/a | ... for i in yrange(n): |

177 | n/a | ... yield i |

178 | n/a | >>> list(zrange(5)) |

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

180 | n/a | |

181 | n/a | |

182 | n/a | Verify that a gen exp cannot be resumed while it is actively running: |

183 | n/a | |

184 | n/a | >>> g = (next(me) for i in range(10)) |

185 | n/a | >>> me = g |

186 | n/a | >>> next(me) |

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

188 | n/a | File "<pyshell#30>", line 1, in -toplevel- |

189 | n/a | next(me) |

190 | n/a | File "<pyshell#28>", line 1, in <generator expression> |

191 | n/a | g = (next(me) for i in range(10)) |

192 | n/a | ValueError: generator already executing |

193 | n/a | |

194 | n/a | Verify exception propagation |

195 | n/a | |

196 | n/a | >>> g = (10 // i for i in (5, 0, 2)) |

197 | n/a | >>> next(g) |

198 | n/a | 2 |

199 | n/a | >>> next(g) |

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

201 | n/a | File "<pyshell#37>", line 1, in -toplevel- |

202 | n/a | next(g) |

203 | n/a | File "<pyshell#35>", line 1, in <generator expression> |

204 | n/a | g = (10 // i for i in (5, 0, 2)) |

205 | n/a | ZeroDivisionError: integer division or modulo by zero |

206 | n/a | >>> next(g) |

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

208 | n/a | File "<pyshell#38>", line 1, in -toplevel- |

209 | n/a | next(g) |

210 | n/a | StopIteration |

211 | n/a | |

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

213 | n/a | |

214 | n/a | >>> list(None for i in range(10)) |

215 | n/a | [None, None, None, None, None, None, None, None, None, None] |

216 | n/a | |

217 | n/a | Check that generator attributes are present |

218 | n/a | |

219 | n/a | >>> g = (i*i for i in range(3)) |

220 | n/a | >>> expected = set(['gi_frame', 'gi_running']) |

221 | n/a | >>> set(attr for attr in dir(g) if not attr.startswith('__')) >= expected |

222 | n/a | True |

223 | n/a | |

224 | n/a | >>> from test.support import HAVE_DOCSTRINGS |

225 | n/a | >>> print(g.__next__.__doc__ if HAVE_DOCSTRINGS else 'Implement next(self).') |

226 | n/a | Implement next(self). |

227 | n/a | >>> import types |

228 | n/a | >>> isinstance(g, types.GeneratorType) |

229 | n/a | True |

230 | n/a | |

231 | n/a | Check the __iter__ slot is defined to return self |

232 | n/a | |

233 | n/a | >>> iter(g) is g |

234 | n/a | True |

235 | n/a | |

236 | n/a | Verify that the running flag is set properly |

237 | n/a | |

238 | n/a | >>> g = (me.gi_running for i in (0,1)) |

239 | n/a | >>> me = g |

240 | n/a | >>> me.gi_running |

241 | n/a | 0 |

242 | n/a | >>> next(me) |

243 | n/a | 1 |

244 | n/a | >>> me.gi_running |

245 | n/a | 0 |

246 | n/a | |

247 | n/a | Verify that genexps are weakly referencable |

248 | n/a | |

249 | n/a | >>> import weakref |

250 | n/a | >>> g = (i*i for i in range(4)) |

251 | n/a | >>> wr = weakref.ref(g) |

252 | n/a | >>> wr() is g |

253 | n/a | True |

254 | n/a | >>> p = weakref.proxy(g) |

255 | n/a | >>> list(p) |

256 | n/a | [0, 1, 4, 9] |

257 | n/a | |

258 | n/a | |

259 | n/a | """ |

260 | n/a | |

261 | n/a | import sys |

262 | n/a | |

263 | n/a | # Trace function can throw off the tuple reuse test. |

264 | n/a | if hasattr(sys, 'gettrace') and sys.gettrace(): |

265 | n/a | __test__ = {} |

266 | n/a | else: |

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

268 | n/a | |

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

270 | n/a | from test import support |

271 | n/a | from test import test_genexps |

272 | n/a | support.run_doctest(test_genexps, verbose) |

273 | n/a | |

274 | n/a | # verify reference counting |

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

276 | n/a | import gc |

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

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

279 | n/a | support.run_doctest(test_genexps, verbose) |

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

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

282 | n/a | print(counts) |

283 | n/a | |

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

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