# Python code coverage for Lib/lib2to3/tests/data/py3_test_grammar.py

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

1 | n/a | # Python test set -- part 1, grammar. |

2 | n/a | # This just tests whether the parser accepts them all. |

3 | n/a | |

4 | n/a | # NOTE: When you run this test as a script from the command line, you |

5 | n/a | # get warnings about certain hex/oct constants. Since those are |

6 | n/a | # issued by the parser, you can't suppress them by adding a |

7 | n/a | # filterwarnings() call to this module. Therefore, to shut up the |

8 | n/a | # regression test, the filterwarnings() call has been added to |

9 | n/a | # regrtest.py. |

10 | n/a | |

11 | n/a | from test.support import run_unittest, check_syntax_error |

12 | n/a | import unittest |

13 | n/a | import sys |

14 | n/a | # testing import * |

15 | n/a | from sys import * |

16 | n/a | |

17 | n/a | class TokenTests(unittest.TestCase): |

18 | n/a | |

19 | n/a | def testBackslash(self): |

20 | n/a | # Backslash means line continuation: |

21 | n/a | x = 1 \ |

22 | n/a | + 1 |

23 | n/a | self.assertEquals(x, 2, 'backslash for line continuation') |

24 | n/a | |

25 | n/a | # Backslash does not means continuation in comments :\ |

26 | n/a | x = 0 |

27 | n/a | self.assertEquals(x, 0, 'backslash ending comment') |

28 | n/a | |

29 | n/a | def testPlainIntegers(self): |

30 | n/a | self.assertEquals(type(000), type(0)) |

31 | n/a | self.assertEquals(0xff, 255) |

32 | n/a | self.assertEquals(0o377, 255) |

33 | n/a | self.assertEquals(2147483647, 0o17777777777) |

34 | n/a | self.assertEquals(0b1001, 9) |

35 | n/a | # "0x" is not a valid literal |

36 | n/a | self.assertRaises(SyntaxError, eval, "0x") |

37 | n/a | from sys import maxsize |

38 | n/a | if maxsize == 2147483647: |

39 | n/a | self.assertEquals(-2147483647-1, -0o20000000000) |

40 | n/a | # XXX -2147483648 |

41 | n/a | self.assert_(0o37777777777 > 0) |

42 | n/a | self.assert_(0xffffffff > 0) |

43 | n/a | self.assert_(0b1111111111111111111111111111111 > 0) |

44 | n/a | for s in ('2147483648', '0o40000000000', '0x100000000', |

45 | n/a | '0b10000000000000000000000000000000'): |

46 | n/a | try: |

47 | n/a | x = eval(s) |

48 | n/a | except OverflowError: |

49 | n/a | self.fail("OverflowError on huge integer literal %r" % s) |

50 | n/a | elif maxsize == 9223372036854775807: |

51 | n/a | self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000) |

52 | n/a | self.assert_(0o1777777777777777777777 > 0) |

53 | n/a | self.assert_(0xffffffffffffffff > 0) |

54 | n/a | self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |

55 | n/a | for s in '9223372036854775808', '0o2000000000000000000000', \ |

56 | n/a | '0x10000000000000000', \ |

57 | n/a | '0b100000000000000000000000000000000000000000000000000000000000000': |

58 | n/a | try: |

59 | n/a | x = eval(s) |

60 | n/a | except OverflowError: |

61 | n/a | self.fail("OverflowError on huge integer literal %r" % s) |

62 | n/a | else: |

63 | n/a | self.fail('Weird maxsize value %r' % maxsize) |

64 | n/a | |

65 | n/a | def testLongIntegers(self): |

66 | n/a | x = 0 |

67 | n/a | x = 0xffffffffffffffff |

68 | n/a | x = 0Xffffffffffffffff |

69 | n/a | x = 0o77777777777777777 |

70 | n/a | x = 0O77777777777777777 |

71 | n/a | x = 123456789012345678901234567890 |

72 | n/a | x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |

73 | n/a | x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |

74 | n/a | |

75 | n/a | def testFloats(self): |

76 | n/a | x = 3.14 |

77 | n/a | x = 314. |

78 | n/a | x = 0.314 |

79 | n/a | # XXX x = 000.314 |

80 | n/a | x = .314 |

81 | n/a | x = 3e14 |

82 | n/a | x = 3E14 |

83 | n/a | x = 3e-14 |

84 | n/a | x = 3e+14 |

85 | n/a | x = 3.e14 |

86 | n/a | x = .3e14 |

87 | n/a | x = 3.1e4 |

88 | n/a | |

89 | n/a | def testStringLiterals(self): |

90 | n/a | x = ''; y = ""; self.assert_(len(x) == 0 and x == y) |

91 | n/a | x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39) |

92 | n/a | x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34) |

93 | n/a | x = "doesn't \"shrink\" does it" |

94 | n/a | y = 'doesn\'t "shrink" does it' |

95 | n/a | self.assert_(len(x) == 24 and x == y) |

96 | n/a | x = "does \"shrink\" doesn't it" |

97 | n/a | y = 'does "shrink" doesn\'t it' |

98 | n/a | self.assert_(len(x) == 24 and x == y) |

99 | n/a | x = """ |

100 | n/a | The "quick" |

101 | n/a | brown fox |

102 | n/a | jumps over |

103 | n/a | the 'lazy' dog. |

104 | n/a | """ |

105 | n/a | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |

106 | n/a | self.assertEquals(x, y) |

107 | n/a | y = ''' |

108 | n/a | The "quick" |

109 | n/a | brown fox |

110 | n/a | jumps over |

111 | n/a | the 'lazy' dog. |

112 | n/a | ''' |

113 | n/a | self.assertEquals(x, y) |

114 | n/a | y = "\n\ |

115 | n/a | The \"quick\"\n\ |

116 | n/a | brown fox\n\ |

117 | n/a | jumps over\n\ |

118 | n/a | the 'lazy' dog.\n\ |

119 | n/a | " |

120 | n/a | self.assertEquals(x, y) |

121 | n/a | y = '\n\ |

122 | n/a | The \"quick\"\n\ |

123 | n/a | brown fox\n\ |

124 | n/a | jumps over\n\ |

125 | n/a | the \'lazy\' dog.\n\ |

126 | n/a | ' |

127 | n/a | self.assertEquals(x, y) |

128 | n/a | |

129 | n/a | def testEllipsis(self): |

130 | n/a | x = ... |

131 | n/a | self.assert_(x is Ellipsis) |

132 | n/a | self.assertRaises(SyntaxError, eval, ".. .") |

133 | n/a | |

134 | n/a | class GrammarTests(unittest.TestCase): |

135 | n/a | |

136 | n/a | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |

137 | n/a | # XXX can't test in a script -- this rule is only used when interactive |

138 | n/a | |

139 | n/a | # file_input: (NEWLINE | stmt)* ENDMARKER |

140 | n/a | # Being tested as this very moment this very module |

141 | n/a | |

142 | n/a | # expr_input: testlist NEWLINE |

143 | n/a | # XXX Hard to test -- used only in calls to input() |

144 | n/a | |

145 | n/a | def testEvalInput(self): |

146 | n/a | # testlist ENDMARKER |

147 | n/a | x = eval('1, 0 or 1') |

148 | n/a | |

149 | n/a | def testFuncdef(self): |

150 | n/a | ### [decorators] 'def' NAME parameters ['->' test] ':' suite |

151 | n/a | ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |

152 | n/a | ### decorators: decorator+ |

153 | n/a | ### parameters: '(' [typedargslist] ')' |

154 | n/a | ### typedargslist: ((tfpdef ['=' test] ',')* |

155 | n/a | ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |

156 | n/a | ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |

157 | n/a | ### tfpdef: NAME [':' test] |

158 | n/a | ### varargslist: ((vfpdef ['=' test] ',')* |

159 | n/a | ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |

160 | n/a | ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |

161 | n/a | ### vfpdef: NAME |

162 | n/a | def f1(): pass |

163 | n/a | f1() |

164 | n/a | f1(*()) |

165 | n/a | f1(*(), **{}) |

166 | n/a | def f2(one_argument): pass |

167 | n/a | def f3(two, arguments): pass |

168 | n/a | self.assertEquals(f2.__code__.co_varnames, ('one_argument',)) |

169 | n/a | self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments')) |

170 | n/a | def a1(one_arg,): pass |

171 | n/a | def a2(two, args,): pass |

172 | n/a | def v0(*rest): pass |

173 | n/a | def v1(a, *rest): pass |

174 | n/a | def v2(a, b, *rest): pass |

175 | n/a | |

176 | n/a | f1() |

177 | n/a | f2(1) |

178 | n/a | f2(1,) |

179 | n/a | f3(1, 2) |

180 | n/a | f3(1, 2,) |

181 | n/a | v0() |

182 | n/a | v0(1) |

183 | n/a | v0(1,) |

184 | n/a | v0(1,2) |

185 | n/a | v0(1,2,3,4,5,6,7,8,9,0) |

186 | n/a | v1(1) |

187 | n/a | v1(1,) |

188 | n/a | v1(1,2) |

189 | n/a | v1(1,2,3) |

190 | n/a | v1(1,2,3,4,5,6,7,8,9,0) |

191 | n/a | v2(1,2) |

192 | n/a | v2(1,2,3) |

193 | n/a | v2(1,2,3,4) |

194 | n/a | v2(1,2,3,4,5,6,7,8,9,0) |

195 | n/a | |

196 | n/a | def d01(a=1): pass |

197 | n/a | d01() |

198 | n/a | d01(1) |

199 | n/a | d01(*(1,)) |

200 | n/a | d01(**{'a':2}) |

201 | n/a | def d11(a, b=1): pass |

202 | n/a | d11(1) |

203 | n/a | d11(1, 2) |

204 | n/a | d11(1, **{'b':2}) |

205 | n/a | def d21(a, b, c=1): pass |

206 | n/a | d21(1, 2) |

207 | n/a | d21(1, 2, 3) |

208 | n/a | d21(*(1, 2, 3)) |

209 | n/a | d21(1, *(2, 3)) |

210 | n/a | d21(1, 2, *(3,)) |

211 | n/a | d21(1, 2, **{'c':3}) |

212 | n/a | def d02(a=1, b=2): pass |

213 | n/a | d02() |

214 | n/a | d02(1) |

215 | n/a | d02(1, 2) |

216 | n/a | d02(*(1, 2)) |

217 | n/a | d02(1, *(2,)) |

218 | n/a | d02(1, **{'b':2}) |

219 | n/a | d02(**{'a': 1, 'b': 2}) |

220 | n/a | def d12(a, b=1, c=2): pass |

221 | n/a | d12(1) |

222 | n/a | d12(1, 2) |

223 | n/a | d12(1, 2, 3) |

224 | n/a | def d22(a, b, c=1, d=2): pass |

225 | n/a | d22(1, 2) |

226 | n/a | d22(1, 2, 3) |

227 | n/a | d22(1, 2, 3, 4) |

228 | n/a | def d01v(a=1, *rest): pass |

229 | n/a | d01v() |

230 | n/a | d01v(1) |

231 | n/a | d01v(1, 2) |

232 | n/a | d01v(*(1, 2, 3, 4)) |

233 | n/a | d01v(*(1,)) |

234 | n/a | d01v(**{'a':2}) |

235 | n/a | def d11v(a, b=1, *rest): pass |

236 | n/a | d11v(1) |

237 | n/a | d11v(1, 2) |

238 | n/a | d11v(1, 2, 3) |

239 | n/a | def d21v(a, b, c=1, *rest): pass |

240 | n/a | d21v(1, 2) |

241 | n/a | d21v(1, 2, 3) |

242 | n/a | d21v(1, 2, 3, 4) |

243 | n/a | d21v(*(1, 2, 3, 4)) |

244 | n/a | d21v(1, 2, **{'c': 3}) |

245 | n/a | def d02v(a=1, b=2, *rest): pass |

246 | n/a | d02v() |

247 | n/a | d02v(1) |

248 | n/a | d02v(1, 2) |

249 | n/a | d02v(1, 2, 3) |

250 | n/a | d02v(1, *(2, 3, 4)) |

251 | n/a | d02v(**{'a': 1, 'b': 2}) |

252 | n/a | def d12v(a, b=1, c=2, *rest): pass |

253 | n/a | d12v(1) |

254 | n/a | d12v(1, 2) |

255 | n/a | d12v(1, 2, 3) |

256 | n/a | d12v(1, 2, 3, 4) |

257 | n/a | d12v(*(1, 2, 3, 4)) |

258 | n/a | d12v(1, 2, *(3, 4, 5)) |

259 | n/a | d12v(1, *(2,), **{'c': 3}) |

260 | n/a | def d22v(a, b, c=1, d=2, *rest): pass |

261 | n/a | d22v(1, 2) |

262 | n/a | d22v(1, 2, 3) |

263 | n/a | d22v(1, 2, 3, 4) |

264 | n/a | d22v(1, 2, 3, 4, 5) |

265 | n/a | d22v(*(1, 2, 3, 4)) |

266 | n/a | d22v(1, 2, *(3, 4, 5)) |

267 | n/a | d22v(1, *(2, 3), **{'d': 4}) |

268 | n/a | |

269 | n/a | # keyword argument type tests |

270 | n/a | try: |

271 | n/a | str('x', **{b'foo':1 }) |

272 | n/a | except TypeError: |

273 | n/a | pass |

274 | n/a | else: |

275 | n/a | self.fail('Bytes should not work as keyword argument names') |

276 | n/a | # keyword only argument tests |

277 | n/a | def pos0key1(*, key): return key |

278 | n/a | pos0key1(key=100) |

279 | n/a | def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |

280 | n/a | pos2key2(1, 2, k1=100) |

281 | n/a | pos2key2(1, 2, k1=100, k2=200) |

282 | n/a | pos2key2(1, 2, k2=100, k1=200) |

283 | n/a | def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |

284 | n/a | pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |

285 | n/a | pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |

286 | n/a | |

287 | n/a | # keyword arguments after *arglist |

288 | n/a | def f(*args, **kwargs): |

289 | n/a | return args, kwargs |

290 | n/a | self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |

291 | n/a | {'x':2, 'y':5})) |

292 | n/a | self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") |

293 | n/a | self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |

294 | n/a | |

295 | n/a | # argument annotation tests |

296 | n/a | def f(x) -> list: pass |

297 | n/a | self.assertEquals(f.__annotations__, {'return': list}) |

298 | n/a | def f(x:int): pass |

299 | n/a | self.assertEquals(f.__annotations__, {'x': int}) |

300 | n/a | def f(*x:str): pass |

301 | n/a | self.assertEquals(f.__annotations__, {'x': str}) |

302 | n/a | def f(**x:float): pass |

303 | n/a | self.assertEquals(f.__annotations__, {'x': float}) |

304 | n/a | def f(x, y:1+2): pass |

305 | n/a | self.assertEquals(f.__annotations__, {'y': 3}) |

306 | n/a | def f(a, b:1, c:2, d): pass |

307 | n/a | self.assertEquals(f.__annotations__, {'b': 1, 'c': 2}) |

308 | n/a | def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass |

309 | n/a | self.assertEquals(f.__annotations__, |

310 | n/a | {'b': 1, 'c': 2, 'e': 3, 'g': 6}) |

311 | n/a | def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10, |

312 | n/a | **k:11) -> 12: pass |

313 | n/a | self.assertEquals(f.__annotations__, |

314 | n/a | {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |

315 | n/a | 'k': 11, 'return': 12}) |

316 | n/a | # Check for SF Bug #1697248 - mixing decorators and a return annotation |

317 | n/a | def null(x): return x |

318 | n/a | @null |

319 | n/a | def f(x) -> list: pass |

320 | n/a | self.assertEquals(f.__annotations__, {'return': list}) |

321 | n/a | |

322 | n/a | # test closures with a variety of oparg's |

323 | n/a | closure = 1 |

324 | n/a | def f(): return closure |

325 | n/a | def f(x=1): return closure |

326 | n/a | def f(*, k=1): return closure |

327 | n/a | def f() -> int: return closure |

328 | n/a | |

329 | n/a | # Check ast errors in *args and *kwargs |

330 | n/a | check_syntax_error(self, "f(*g(1=2))") |

331 | n/a | check_syntax_error(self, "f(**g(1=2))") |

332 | n/a | |

333 | n/a | def testLambdef(self): |

334 | n/a | ### lambdef: 'lambda' [varargslist] ':' test |

335 | n/a | l1 = lambda : 0 |

336 | n/a | self.assertEquals(l1(), 0) |

337 | n/a | l2 = lambda : a[d] # XXX just testing the expression |

338 | n/a | l3 = lambda : [2 < x for x in [-1, 3, 0]] |

339 | n/a | self.assertEquals(l3(), [0, 1, 0]) |

340 | n/a | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |

341 | n/a | self.assertEquals(l4(), 1) |

342 | n/a | l5 = lambda x, y, z=2: x + y + z |

343 | n/a | self.assertEquals(l5(1, 2), 5) |

344 | n/a | self.assertEquals(l5(1, 2, 3), 6) |

345 | n/a | check_syntax_error(self, "lambda x: x = 2") |

346 | n/a | check_syntax_error(self, "lambda (None,): None") |

347 | n/a | l6 = lambda x, y, *, k=20: x+y+k |

348 | n/a | self.assertEquals(l6(1,2), 1+2+20) |

349 | n/a | self.assertEquals(l6(1,2,k=10), 1+2+10) |

350 | n/a | |

351 | n/a | |

352 | n/a | ### stmt: simple_stmt | compound_stmt |

353 | n/a | # Tested below |

354 | n/a | |

355 | n/a | def testSimpleStmt(self): |

356 | n/a | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |

357 | n/a | x = 1; pass; del x |

358 | n/a | def foo(): |

359 | n/a | # verify statements that end with semi-colons |

360 | n/a | x = 1; pass; del x; |

361 | n/a | foo() |

362 | n/a | |

363 | n/a | ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt |

364 | n/a | # Tested below |

365 | n/a | |

366 | n/a | def testExprStmt(self): |

367 | n/a | # (exprlist '=')* exprlist |

368 | n/a | 1 |

369 | n/a | 1, 2, 3 |

370 | n/a | x = 1 |

371 | n/a | x = 1, 2, 3 |

372 | n/a | x = y = z = 1, 2, 3 |

373 | n/a | x, y, z = 1, 2, 3 |

374 | n/a | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |

375 | n/a | |

376 | n/a | check_syntax_error(self, "x + 1 = 1") |

377 | n/a | check_syntax_error(self, "a + 1 = b + 2") |

378 | n/a | |

379 | n/a | def testDelStmt(self): |

380 | n/a | # 'del' exprlist |

381 | n/a | abc = [1,2,3] |

382 | n/a | x, y, z = abc |

383 | n/a | xyz = x, y, z |

384 | n/a | |

385 | n/a | del abc |

386 | n/a | del x, y, (z, xyz) |

387 | n/a | |

388 | n/a | def testPassStmt(self): |

389 | n/a | # 'pass' |

390 | n/a | pass |

391 | n/a | |

392 | n/a | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |

393 | n/a | # Tested below |

394 | n/a | |

395 | n/a | def testBreakStmt(self): |

396 | n/a | # 'break' |

397 | n/a | while 1: break |

398 | n/a | |

399 | n/a | def testContinueStmt(self): |

400 | n/a | # 'continue' |

401 | n/a | i = 1 |

402 | n/a | while i: i = 0; continue |

403 | n/a | |

404 | n/a | msg = "" |

405 | n/a | while not msg: |

406 | n/a | msg = "ok" |

407 | n/a | try: |

408 | n/a | continue |

409 | n/a | msg = "continue failed to continue inside try" |

410 | n/a | except: |

411 | n/a | msg = "continue inside try called except block" |

412 | n/a | if msg != "ok": |

413 | n/a | self.fail(msg) |

414 | n/a | |

415 | n/a | msg = "" |

416 | n/a | while not msg: |

417 | n/a | msg = "finally block not called" |

418 | n/a | try: |

419 | n/a | continue |

420 | n/a | finally: |

421 | n/a | msg = "ok" |

422 | n/a | if msg != "ok": |

423 | n/a | self.fail(msg) |

424 | n/a | |

425 | n/a | def test_break_continue_loop(self): |

426 | n/a | # This test warrants an explanation. It is a test specifically for SF bugs |

427 | n/a | # #463359 and #462937. The bug is that a 'break' statement executed or |

428 | n/a | # exception raised inside a try/except inside a loop, *after* a continue |

429 | n/a | # statement has been executed in that loop, will cause the wrong number of |

430 | n/a | # arguments to be popped off the stack and the instruction pointer reset to |

431 | n/a | # a very small number (usually 0.) Because of this, the following test |

432 | n/a | # *must* written as a function, and the tracking vars *must* be function |

433 | n/a | # arguments with default values. Otherwise, the test will loop and loop. |

434 | n/a | |

435 | n/a | def test_inner(extra_burning_oil = 1, count=0): |

436 | n/a | big_hippo = 2 |

437 | n/a | while big_hippo: |

438 | n/a | count += 1 |

439 | n/a | try: |

440 | n/a | if extra_burning_oil and big_hippo == 1: |

441 | n/a | extra_burning_oil -= 1 |

442 | n/a | break |

443 | n/a | big_hippo -= 1 |

444 | n/a | continue |

445 | n/a | except: |

446 | n/a | raise |

447 | n/a | if count > 2 or big_hippo != 1: |

448 | n/a | self.fail("continue then break in try/except in loop broken!") |

449 | n/a | test_inner() |

450 | n/a | |

451 | n/a | def testReturn(self): |

452 | n/a | # 'return' [testlist] |

453 | n/a | def g1(): return |

454 | n/a | def g2(): return 1 |

455 | n/a | g1() |

456 | n/a | x = g2() |

457 | n/a | check_syntax_error(self, "class foo:return 1") |

458 | n/a | |

459 | n/a | def testYield(self): |

460 | n/a | check_syntax_error(self, "class foo:yield 1") |

461 | n/a | |

462 | n/a | def testRaise(self): |

463 | n/a | # 'raise' test [',' test] |

464 | n/a | try: raise RuntimeError('just testing') |

465 | n/a | except RuntimeError: pass |

466 | n/a | try: raise KeyboardInterrupt |

467 | n/a | except KeyboardInterrupt: pass |

468 | n/a | |

469 | n/a | def testImport(self): |

470 | n/a | # 'import' dotted_as_names |

471 | n/a | import sys |

472 | n/a | import time, sys |

473 | n/a | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |

474 | n/a | from time import time |

475 | n/a | from time import (time) |

476 | n/a | # not testable inside a function, but already done at top of the module |

477 | n/a | # from sys import * |

478 | n/a | from sys import path, argv |

479 | n/a | from sys import (path, argv) |

480 | n/a | from sys import (path, argv,) |

481 | n/a | |

482 | n/a | def testGlobal(self): |

483 | n/a | # 'global' NAME (',' NAME)* |

484 | n/a | global a |

485 | n/a | global a, b |

486 | n/a | global one, two, three, four, five, six, seven, eight, nine, ten |

487 | n/a | |

488 | n/a | def testNonlocal(self): |

489 | n/a | # 'nonlocal' NAME (',' NAME)* |

490 | n/a | x = 0 |

491 | n/a | y = 0 |

492 | n/a | def f(): |

493 | n/a | nonlocal x |

494 | n/a | nonlocal x, y |

495 | n/a | |

496 | n/a | def testAssert(self): |

497 | n/a | # assert_stmt: 'assert' test [',' test] |

498 | n/a | assert 1 |

499 | n/a | assert 1, 1 |

500 | n/a | assert lambda x:x |

501 | n/a | assert 1, lambda x:x+1 |

502 | n/a | try: |

503 | n/a | assert 0, "msg" |

504 | n/a | except AssertionError as e: |

505 | n/a | self.assertEquals(e.args[0], "msg") |

506 | n/a | else: |

507 | n/a | if __debug__: |

508 | n/a | self.fail("AssertionError not raised by assert 0") |

509 | n/a | |

510 | n/a | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |

511 | n/a | # Tested below |

512 | n/a | |

513 | n/a | def testIf(self): |

514 | n/a | # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |

515 | n/a | if 1: pass |

516 | n/a | if 1: pass |

517 | n/a | else: pass |

518 | n/a | if 0: pass |

519 | n/a | elif 0: pass |

520 | n/a | if 0: pass |

521 | n/a | elif 0: pass |

522 | n/a | elif 0: pass |

523 | n/a | elif 0: pass |

524 | n/a | else: pass |

525 | n/a | |

526 | n/a | def testWhile(self): |

527 | n/a | # 'while' test ':' suite ['else' ':' suite] |

528 | n/a | while 0: pass |

529 | n/a | while 0: pass |

530 | n/a | else: pass |

531 | n/a | |

532 | n/a | # Issue1920: "while 0" is optimized away, |

533 | n/a | # ensure that the "else" clause is still present. |

534 | n/a | x = 0 |

535 | n/a | while 0: |

536 | n/a | x = 1 |

537 | n/a | else: |

538 | n/a | x = 2 |

539 | n/a | self.assertEquals(x, 2) |

540 | n/a | |

541 | n/a | def testFor(self): |

542 | n/a | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |

543 | n/a | for i in 1, 2, 3: pass |

544 | n/a | for i, j, k in (): pass |

545 | n/a | else: pass |

546 | n/a | class Squares: |

547 | n/a | def __init__(self, max): |

548 | n/a | self.max = max |

549 | n/a | self.sofar = [] |

550 | n/a | def __len__(self): return len(self.sofar) |

551 | n/a | def __getitem__(self, i): |

552 | n/a | if not 0 <= i < self.max: raise IndexError |

553 | n/a | n = len(self.sofar) |

554 | n/a | while n <= i: |

555 | n/a | self.sofar.append(n*n) |

556 | n/a | n = n+1 |

557 | n/a | return self.sofar[i] |

558 | n/a | n = 0 |

559 | n/a | for x in Squares(10): n = n+x |

560 | n/a | if n != 285: |

561 | n/a | self.fail('for over growing sequence') |

562 | n/a | |

563 | n/a | result = [] |

564 | n/a | for x, in [(1,), (2,), (3,)]: |

565 | n/a | result.append(x) |

566 | n/a | self.assertEqual(result, [1, 2, 3]) |

567 | n/a | |

568 | n/a | def testTry(self): |

569 | n/a | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |

570 | n/a | ### | 'try' ':' suite 'finally' ':' suite |

571 | n/a | ### except_clause: 'except' [expr ['as' expr]] |

572 | n/a | try: |

573 | n/a | 1/0 |

574 | n/a | except ZeroDivisionError: |

575 | n/a | pass |

576 | n/a | else: |

577 | n/a | pass |

578 | n/a | try: 1/0 |

579 | n/a | except EOFError: pass |

580 | n/a | except TypeError as msg: pass |

581 | n/a | except RuntimeError as msg: pass |

582 | n/a | except: pass |

583 | n/a | else: pass |

584 | n/a | try: 1/0 |

585 | n/a | except (EOFError, TypeError, ZeroDivisionError): pass |

586 | n/a | try: 1/0 |

587 | n/a | except (EOFError, TypeError, ZeroDivisionError) as msg: pass |

588 | n/a | try: pass |

589 | n/a | finally: pass |

590 | n/a | |

591 | n/a | def testSuite(self): |

592 | n/a | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |

593 | n/a | if 1: pass |

594 | n/a | if 1: |

595 | n/a | pass |

596 | n/a | if 1: |

597 | n/a | # |

598 | n/a | # |

599 | n/a | # |

600 | n/a | pass |

601 | n/a | pass |

602 | n/a | # |

603 | n/a | pass |

604 | n/a | # |

605 | n/a | |

606 | n/a | def testTest(self): |

607 | n/a | ### and_test ('or' and_test)* |

608 | n/a | ### and_test: not_test ('and' not_test)* |

609 | n/a | ### not_test: 'not' not_test | comparison |

610 | n/a | if not 1: pass |

611 | n/a | if 1 and 1: pass |

612 | n/a | if 1 or 1: pass |

613 | n/a | if not not not 1: pass |

614 | n/a | if not 1 and 1 and 1: pass |

615 | n/a | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |

616 | n/a | |

617 | n/a | def testComparison(self): |

618 | n/a | ### comparison: expr (comp_op expr)* |

619 | n/a | ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |

620 | n/a | if 1: pass |

621 | n/a | x = (1 == 1) |

622 | n/a | if 1 == 1: pass |

623 | n/a | if 1 != 1: pass |

624 | n/a | if 1 < 1: pass |

625 | n/a | if 1 > 1: pass |

626 | n/a | if 1 <= 1: pass |

627 | n/a | if 1 >= 1: pass |

628 | n/a | if 1 is 1: pass |

629 | n/a | if 1 is not 1: pass |

630 | n/a | if 1 in (): pass |

631 | n/a | if 1 not in (): pass |

632 | n/a | if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass |

633 | n/a | |

634 | n/a | def testBinaryMaskOps(self): |

635 | n/a | x = 1 & 1 |

636 | n/a | x = 1 ^ 1 |

637 | n/a | x = 1 | 1 |

638 | n/a | |

639 | n/a | def testShiftOps(self): |

640 | n/a | x = 1 << 1 |

641 | n/a | x = 1 >> 1 |

642 | n/a | x = 1 << 1 >> 1 |

643 | n/a | |

644 | n/a | def testAdditiveOps(self): |

645 | n/a | x = 1 |

646 | n/a | x = 1 + 1 |

647 | n/a | x = 1 - 1 - 1 |

648 | n/a | x = 1 - 1 + 1 - 1 + 1 |

649 | n/a | |

650 | n/a | def testMultiplicativeOps(self): |

651 | n/a | x = 1 * 1 |

652 | n/a | x = 1 / 1 |

653 | n/a | x = 1 % 1 |

654 | n/a | x = 1 / 1 * 1 % 1 |

655 | n/a | |

656 | n/a | def testUnaryOps(self): |

657 | n/a | x = +1 |

658 | n/a | x = -1 |

659 | n/a | x = ~1 |

660 | n/a | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |

661 | n/a | x = -1*1/1 + 1*1 - ---1*1 |

662 | n/a | |

663 | n/a | def testSelectors(self): |

664 | n/a | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |

665 | n/a | ### subscript: expr | [expr] ':' [expr] |

666 | n/a | |

667 | n/a | import sys, time |

668 | n/a | c = sys.path[0] |

669 | n/a | x = time.time() |

670 | n/a | x = sys.modules['time'].time() |

671 | n/a | a = '01234' |

672 | n/a | c = a[0] |

673 | n/a | c = a[-1] |

674 | n/a | s = a[0:5] |

675 | n/a | s = a[:5] |

676 | n/a | s = a[0:] |

677 | n/a | s = a[:] |

678 | n/a | s = a[-5:] |

679 | n/a | s = a[:-1] |

680 | n/a | s = a[-4:-3] |

681 | n/a | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |

682 | n/a | # The testing here is fairly incomplete. |

683 | n/a | # Test cases should include: commas with 1 and 2 colons |

684 | n/a | d = {} |

685 | n/a | d[1] = 1 |

686 | n/a | d[1,] = 2 |

687 | n/a | d[1,2] = 3 |

688 | n/a | d[1,2,3] = 4 |

689 | n/a | L = list(d) |

690 | n/a | L.sort(key=lambda x: x if isinstance(x, tuple) else ()) |

691 | n/a | self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |

692 | n/a | |

693 | n/a | def testAtoms(self): |

694 | n/a | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING |

695 | n/a | ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |

696 | n/a | |

697 | n/a | x = (1) |

698 | n/a | x = (1 or 2 or 3) |

699 | n/a | x = (1 or 2 or 3, 2, 3) |

700 | n/a | |

701 | n/a | x = [] |

702 | n/a | x = [1] |

703 | n/a | x = [1 or 2 or 3] |

704 | n/a | x = [1 or 2 or 3, 2, 3] |

705 | n/a | x = [] |

706 | n/a | |

707 | n/a | x = {} |

708 | n/a | x = {'one': 1} |

709 | n/a | x = {'one': 1,} |

710 | n/a | x = {'one' or 'two': 1 or 2} |

711 | n/a | x = {'one': 1, 'two': 2} |

712 | n/a | x = {'one': 1, 'two': 2,} |

713 | n/a | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |

714 | n/a | |

715 | n/a | x = {'one'} |

716 | n/a | x = {'one', 1,} |

717 | n/a | x = {'one', 'two', 'three'} |

718 | n/a | x = {2, 3, 4,} |

719 | n/a | |

720 | n/a | x = x |

721 | n/a | x = 'x' |

722 | n/a | x = 123 |

723 | n/a | |

724 | n/a | ### exprlist: expr (',' expr)* [','] |

725 | n/a | ### testlist: test (',' test)* [','] |

726 | n/a | # These have been exercised enough above |

727 | n/a | |

728 | n/a | def testClassdef(self): |

729 | n/a | # 'class' NAME ['(' [testlist] ')'] ':' suite |

730 | n/a | class B: pass |

731 | n/a | class B2(): pass |

732 | n/a | class C1(B): pass |

733 | n/a | class C2(B): pass |

734 | n/a | class D(C1, C2, B): pass |

735 | n/a | class C: |

736 | n/a | def meth1(self): pass |

737 | n/a | def meth2(self, arg): pass |

738 | n/a | def meth3(self, a1, a2): pass |

739 | n/a | |

740 | n/a | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |

741 | n/a | # decorators: decorator+ |

742 | n/a | # decorated: decorators (classdef | funcdef) |

743 | n/a | def class_decorator(x): return x |

744 | n/a | @class_decorator |

745 | n/a | class G: pass |

746 | n/a | |

747 | n/a | def testDictcomps(self): |

748 | n/a | # dictorsetmaker: ( (test ':' test (comp_for | |

749 | n/a | # (',' test ':' test)* [','])) | |

750 | n/a | # (test (comp_for | (',' test)* [','])) ) |

751 | n/a | nums = [1, 2, 3] |

752 | n/a | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |

753 | n/a | |

754 | n/a | def testListcomps(self): |

755 | n/a | # list comprehension tests |

756 | n/a | nums = [1, 2, 3, 4, 5] |

757 | n/a | strs = ["Apple", "Banana", "Coconut"] |

758 | n/a | spcs = [" Apple", " Banana ", "Coco nut "] |

759 | n/a | |

760 | n/a | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |

761 | n/a | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |

762 | n/a | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |

763 | n/a | self.assertEqual([(i, s) for i in nums for s in strs], |

764 | n/a | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |

765 | n/a | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |

766 | n/a | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |

767 | n/a | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |

768 | n/a | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |

769 | n/a | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |

770 | n/a | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |

771 | n/a | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |

772 | n/a | (5, 'Banana'), (5, 'Coconut')]) |

773 | n/a | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |

774 | n/a | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |

775 | n/a | |

776 | n/a | def test_in_func(l): |

777 | n/a | return [0 < x < 3 for x in l if x > 2] |

778 | n/a | |

779 | n/a | self.assertEqual(test_in_func(nums), [False, False, False]) |

780 | n/a | |

781 | n/a | def test_nested_front(): |

782 | n/a | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |

783 | n/a | [[1, 2], [3, 4], [5, 6]]) |

784 | n/a | |

785 | n/a | test_nested_front() |

786 | n/a | |

787 | n/a | check_syntax_error(self, "[i, s for i in nums for s in strs]") |

788 | n/a | check_syntax_error(self, "[x if y]") |

789 | n/a | |

790 | n/a | suppliers = [ |

791 | n/a | (1, "Boeing"), |

792 | n/a | (2, "Ford"), |

793 | n/a | (3, "Macdonalds") |

794 | n/a | ] |

795 | n/a | |

796 | n/a | parts = [ |

797 | n/a | (10, "Airliner"), |

798 | n/a | (20, "Engine"), |

799 | n/a | (30, "Cheeseburger") |

800 | n/a | ] |

801 | n/a | |

802 | n/a | suppart = [ |

803 | n/a | (1, 10), (1, 20), (2, 20), (3, 30) |

804 | n/a | ] |

805 | n/a | |

806 | n/a | x = [ |

807 | n/a | (sname, pname) |

808 | n/a | for (sno, sname) in suppliers |

809 | n/a | for (pno, pname) in parts |

810 | n/a | for (sp_sno, sp_pno) in suppart |

811 | n/a | if sno == sp_sno and pno == sp_pno |

812 | n/a | ] |

813 | n/a | |

814 | n/a | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |

815 | n/a | ('Macdonalds', 'Cheeseburger')]) |

816 | n/a | |

817 | n/a | def testGenexps(self): |

818 | n/a | # generator expression tests |

819 | n/a | g = ([x for x in range(10)] for x in range(1)) |

820 | n/a | self.assertEqual(next(g), [x for x in range(10)]) |

821 | n/a | try: |

822 | n/a | next(g) |

823 | n/a | self.fail('should produce StopIteration exception') |

824 | n/a | except StopIteration: |

825 | n/a | pass |

826 | n/a | |

827 | n/a | a = 1 |

828 | n/a | try: |

829 | n/a | g = (a for d in a) |

830 | n/a | next(g) |

831 | n/a | self.fail('should produce TypeError') |

832 | n/a | except TypeError: |

833 | n/a | pass |

834 | n/a | |

835 | n/a | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |

836 | n/a | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |

837 | n/a | |

838 | n/a | a = [x for x in range(10)] |

839 | n/a | b = (x for x in (y for y in a)) |

840 | n/a | self.assertEqual(sum(b), sum([x for x in range(10)])) |

841 | n/a | |

842 | n/a | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |

843 | n/a | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |

844 | n/a | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |

845 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |

846 | n/a | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |

847 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)])) |

848 | n/a | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |

849 | n/a | check_syntax_error(self, "foo(x for x in range(10), 100)") |

850 | n/a | check_syntax_error(self, "foo(100, x for x in range(10))") |

851 | n/a | |

852 | n/a | def testComprehensionSpecials(self): |

853 | n/a | # test for outmost iterable precomputation |

854 | n/a | x = 10; g = (i for i in range(x)); x = 5 |

855 | n/a | self.assertEqual(len(list(g)), 10) |

856 | n/a | |

857 | n/a | # This should hold, since we're only precomputing outmost iterable. |

858 | n/a | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |

859 | n/a | x = 5; t = True; |

860 | n/a | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |

861 | n/a | |

862 | n/a | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |

863 | n/a | # even though it's silly. Make sure it works (ifelse broke this.) |

864 | n/a | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |

865 | n/a | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |

866 | n/a | |

867 | n/a | # verify unpacking single element tuples in listcomp/genexp. |

868 | n/a | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |

869 | n/a | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |

870 | n/a | |

871 | n/a | def test_with_statement(self): |

872 | n/a | class manager(object): |

873 | n/a | def __enter__(self): |

874 | n/a | return (1, 2) |

875 | n/a | def __exit__(self, *args): |

876 | n/a | pass |

877 | n/a | |

878 | n/a | with manager(): |

879 | n/a | pass |

880 | n/a | with manager() as x: |

881 | n/a | pass |

882 | n/a | with manager() as (x, y): |

883 | n/a | pass |

884 | n/a | with manager(), manager(): |

885 | n/a | pass |

886 | n/a | with manager() as x, manager() as y: |

887 | n/a | pass |

888 | n/a | with manager() as x, manager(): |

889 | n/a | pass |

890 | n/a | |

891 | n/a | def testIfElseExpr(self): |

892 | n/a | # Test ifelse expressions in various cases |

893 | n/a | def _checkeval(msg, ret): |

894 | n/a | "helper to check that evaluation of expressions is done correctly" |

895 | n/a | print(x) |

896 | n/a | return ret |

897 | n/a | |

898 | n/a | # the next line is not allowed anymore |

899 | n/a | #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |

900 | n/a | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |

901 | n/a | self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True]) |

902 | n/a | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |

903 | n/a | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |

904 | n/a | self.assertEqual((5 and 6 if 0 else 1), 1) |

905 | n/a | self.assertEqual(((5 and 6) if 0 else 1), 1) |

906 | n/a | self.assertEqual((5 and (6 if 1 else 1)), 6) |

907 | n/a | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |

908 | n/a | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |

909 | n/a | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |

910 | n/a | self.assertEqual((not 5 if 1 else 1), False) |

911 | n/a | self.assertEqual((not 5 if 0 else 1), 1) |

912 | n/a | self.assertEqual((6 + 1 if 1 else 2), 7) |

913 | n/a | self.assertEqual((6 - 1 if 1 else 2), 5) |

914 | n/a | self.assertEqual((6 * 2 if 1 else 4), 12) |

915 | n/a | self.assertEqual((6 / 2 if 1 else 3), 3) |

916 | n/a | self.assertEqual((6 < 4 if 0 else 2), 2) |

917 | n/a | |

918 | n/a | |

919 | n/a | def test_main(): |

920 | n/a | run_unittest(TokenTests, GrammarTests) |

921 | n/a | |

922 | n/a | if __name__ == '__main__': |

923 | n/a | test_main() |