|
1 # Python test set -- part 1, grammar. |
|
2 # This just tests whether the parser accepts them all. |
|
3 |
|
4 # NOTE: When you run this test as a script from the command line, you |
|
5 # get warnings about certain hex/oct constants. Since those are |
|
6 # issued by the parser, you can't suppress them by adding a |
|
7 # filterwarnings() call to this module. Therefore, to shut up the |
|
8 # regression test, the filterwarnings() call has been added to |
|
9 # regrtest.py. |
|
10 |
|
11 from test.test_support import TestFailed, verify, vereq, check_syntax |
|
12 import sys |
|
13 |
|
14 print '1. Parser' |
|
15 |
|
16 print '1.1 Tokens' |
|
17 |
|
18 print '1.1.1 Backslashes' |
|
19 |
|
20 # Backslash means line continuation: |
|
21 x = 1 \ |
|
22 + 1 |
|
23 if x != 2: raise TestFailed, 'backslash for line continuation' |
|
24 |
|
25 # Backslash does not means continuation in comments :\ |
|
26 x = 0 |
|
27 if x != 0: raise TestFailed, 'backslash ending comment' |
|
28 |
|
29 print '1.1.2 Numeric literals' |
|
30 |
|
31 print '1.1.2.1 Plain integers' |
|
32 if 0xff != 255: raise TestFailed, 'hex int' |
|
33 if 0377 != 255: raise TestFailed, 'octal int' |
|
34 if 2147483647 != 017777777777: raise TestFailed, 'large positive int' |
|
35 try: |
|
36 from sys import maxint |
|
37 except ImportError: |
|
38 maxint = 2147483647 |
|
39 if maxint == 2147483647: |
|
40 # The following test will start to fail in Python 2.4; |
|
41 # change the 020000000000 to -020000000000 |
|
42 if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int' |
|
43 # XXX -2147483648 |
|
44 if 037777777777 < 0: raise TestFailed, 'large oct' |
|
45 if 0xffffffff < 0: raise TestFailed, 'large hex' |
|
46 for s in '2147483648', '040000000000', '0x100000000': |
|
47 try: |
|
48 x = eval(s) |
|
49 except OverflowError: |
|
50 print "OverflowError on huge integer literal " + repr(s) |
|
51 elif eval('maxint == 9223372036854775807'): |
|
52 if eval('-9223372036854775807-1 != -01000000000000000000000'): |
|
53 raise TestFailed, 'max negative int' |
|
54 if eval('01777777777777777777777') < 0: raise TestFailed, 'large oct' |
|
55 if eval('0xffffffffffffffff') < 0: raise TestFailed, 'large hex' |
|
56 for s in '9223372036854775808', '02000000000000000000000', \ |
|
57 '0x10000000000000000': |
|
58 try: |
|
59 x = eval(s) |
|
60 except OverflowError: |
|
61 print "OverflowError on huge integer literal " + repr(s) |
|
62 else: |
|
63 print 'Weird maxint value', maxint |
|
64 |
|
65 print '1.1.2.2 Long integers' |
|
66 x = 0L |
|
67 x = 0l |
|
68 x = 0xffffffffffffffffL |
|
69 x = 0xffffffffffffffffl |
|
70 x = 077777777777777777L |
|
71 x = 077777777777777777l |
|
72 x = 123456789012345678901234567890L |
|
73 x = 123456789012345678901234567890l |
|
74 |
|
75 print '1.1.2.3 Floating point' |
|
76 x = 3.14 |
|
77 x = 314. |
|
78 x = 0.314 |
|
79 # XXX x = 000.314 |
|
80 x = .314 |
|
81 x = 3e14 |
|
82 x = 3E14 |
|
83 x = 3e-14 |
|
84 x = 3e+14 |
|
85 x = 3.e14 |
|
86 x = .3e14 |
|
87 x = 3.1e4 |
|
88 |
|
89 print '1.1.3 String literals' |
|
90 |
|
91 x = ''; y = ""; verify(len(x) == 0 and x == y) |
|
92 x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39) |
|
93 x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34) |
|
94 x = "doesn't \"shrink\" does it" |
|
95 y = 'doesn\'t "shrink" does it' |
|
96 verify(len(x) == 24 and x == y) |
|
97 x = "does \"shrink\" doesn't it" |
|
98 y = 'does "shrink" doesn\'t it' |
|
99 verify(len(x) == 24 and x == y) |
|
100 x = """ |
|
101 The "quick" |
|
102 brown fox |
|
103 jumps over |
|
104 the 'lazy' dog. |
|
105 """ |
|
106 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
|
107 verify(x == y) |
|
108 y = ''' |
|
109 The "quick" |
|
110 brown fox |
|
111 jumps over |
|
112 the 'lazy' dog. |
|
113 '''; verify(x == y) |
|
114 y = "\n\ |
|
115 The \"quick\"\n\ |
|
116 brown fox\n\ |
|
117 jumps over\n\ |
|
118 the 'lazy' dog.\n\ |
|
119 "; verify(x == y) |
|
120 y = '\n\ |
|
121 The \"quick\"\n\ |
|
122 brown fox\n\ |
|
123 jumps over\n\ |
|
124 the \'lazy\' dog.\n\ |
|
125 '; verify(x == y) |
|
126 |
|
127 |
|
128 print '1.2 Grammar' |
|
129 |
|
130 print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE |
|
131 # XXX can't test in a script -- this rule is only used when interactive |
|
132 |
|
133 print 'file_input' # (NEWLINE | stmt)* ENDMARKER |
|
134 # Being tested as this very moment this very module |
|
135 |
|
136 print 'expr_input' # testlist NEWLINE |
|
137 # XXX Hard to test -- used only in calls to input() |
|
138 |
|
139 print 'eval_input' # testlist ENDMARKER |
|
140 x = eval('1, 0 or 1') |
|
141 |
|
142 print 'funcdef' |
|
143 ### 'def' NAME parameters ':' suite |
|
144 ### parameters: '(' [varargslist] ')' |
|
145 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] |
|
146 ### | ('**'|'*' '*') NAME) |
|
147 ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
|
148 ### fpdef: NAME | '(' fplist ')' |
|
149 ### fplist: fpdef (',' fpdef)* [','] |
|
150 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) |
|
151 ### argument: [test '='] test # Really [keyword '='] test |
|
152 def f1(): pass |
|
153 f1() |
|
154 f1(*()) |
|
155 f1(*(), **{}) |
|
156 def f2(one_argument): pass |
|
157 def f3(two, arguments): pass |
|
158 def f4(two, (compound, (argument, list))): pass |
|
159 def f5((compound, first), two): pass |
|
160 vereq(f2.func_code.co_varnames, ('one_argument',)) |
|
161 vereq(f3.func_code.co_varnames, ('two', 'arguments')) |
|
162 if sys.platform.startswith('java'): |
|
163 vereq(f4.func_code.co_varnames, |
|
164 ('two', '(compound, (argument, list))', 'compound', 'argument', |
|
165 'list',)) |
|
166 vereq(f5.func_code.co_varnames, |
|
167 ('(compound, first)', 'two', 'compound', 'first')) |
|
168 else: |
|
169 vereq(f4.func_code.co_varnames, |
|
170 ('two', '.1', 'compound', 'argument', 'list')) |
|
171 vereq(f5.func_code.co_varnames, |
|
172 ('.0', 'two', 'compound', 'first')) |
|
173 def a1(one_arg,): pass |
|
174 def a2(two, args,): pass |
|
175 def v0(*rest): pass |
|
176 def v1(a, *rest): pass |
|
177 def v2(a, b, *rest): pass |
|
178 def v3(a, (b, c), *rest): return a, b, c, rest |
|
179 # ceval unpacks the formal arguments into the first argcount names; |
|
180 # thus, the names nested inside tuples must appear after these names. |
|
181 if sys.platform.startswith('java'): |
|
182 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c')) |
|
183 else: |
|
184 vereq(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) |
|
185 verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,))) |
|
186 def d01(a=1): pass |
|
187 d01() |
|
188 d01(1) |
|
189 d01(*(1,)) |
|
190 d01(**{'a':2}) |
|
191 def d11(a, b=1): pass |
|
192 d11(1) |
|
193 d11(1, 2) |
|
194 d11(1, **{'b':2}) |
|
195 def d21(a, b, c=1): pass |
|
196 d21(1, 2) |
|
197 d21(1, 2, 3) |
|
198 d21(*(1, 2, 3)) |
|
199 d21(1, *(2, 3)) |
|
200 d21(1, 2, *(3,)) |
|
201 d21(1, 2, **{'c':3}) |
|
202 def d02(a=1, b=2): pass |
|
203 d02() |
|
204 d02(1) |
|
205 d02(1, 2) |
|
206 d02(*(1, 2)) |
|
207 d02(1, *(2,)) |
|
208 d02(1, **{'b':2}) |
|
209 d02(**{'a': 1, 'b': 2}) |
|
210 def d12(a, b=1, c=2): pass |
|
211 d12(1) |
|
212 d12(1, 2) |
|
213 d12(1, 2, 3) |
|
214 def d22(a, b, c=1, d=2): pass |
|
215 d22(1, 2) |
|
216 d22(1, 2, 3) |
|
217 d22(1, 2, 3, 4) |
|
218 def d01v(a=1, *rest): pass |
|
219 d01v() |
|
220 d01v(1) |
|
221 d01v(1, 2) |
|
222 d01v(*(1, 2, 3, 4)) |
|
223 d01v(*(1,)) |
|
224 d01v(**{'a':2}) |
|
225 def d11v(a, b=1, *rest): pass |
|
226 d11v(1) |
|
227 d11v(1, 2) |
|
228 d11v(1, 2, 3) |
|
229 def d21v(a, b, c=1, *rest): pass |
|
230 d21v(1, 2) |
|
231 d21v(1, 2, 3) |
|
232 d21v(1, 2, 3, 4) |
|
233 d21v(*(1, 2, 3, 4)) |
|
234 d21v(1, 2, **{'c': 3}) |
|
235 def d02v(a=1, b=2, *rest): pass |
|
236 d02v() |
|
237 d02v(1) |
|
238 d02v(1, 2) |
|
239 d02v(1, 2, 3) |
|
240 d02v(1, *(2, 3, 4)) |
|
241 d02v(**{'a': 1, 'b': 2}) |
|
242 def d12v(a, b=1, c=2, *rest): pass |
|
243 d12v(1) |
|
244 d12v(1, 2) |
|
245 d12v(1, 2, 3) |
|
246 d12v(1, 2, 3, 4) |
|
247 d12v(*(1, 2, 3, 4)) |
|
248 d12v(1, 2, *(3, 4, 5)) |
|
249 d12v(1, *(2,), **{'c': 3}) |
|
250 def d22v(a, b, c=1, d=2, *rest): pass |
|
251 d22v(1, 2) |
|
252 d22v(1, 2, 3) |
|
253 d22v(1, 2, 3, 4) |
|
254 d22v(1, 2, 3, 4, 5) |
|
255 d22v(*(1, 2, 3, 4)) |
|
256 d22v(1, 2, *(3, 4, 5)) |
|
257 d22v(1, *(2, 3), **{'d': 4}) |
|
258 def d31v((x)): pass |
|
259 d31v(1) |
|
260 def d32v((x,)): pass |
|
261 d32v((1,)) |
|
262 |
|
263 ### lambdef: 'lambda' [varargslist] ':' test |
|
264 print 'lambdef' |
|
265 l1 = lambda : 0 |
|
266 verify(l1() == 0) |
|
267 l2 = lambda : a[d] # XXX just testing the expression |
|
268 l3 = lambda : [2 < x for x in [-1, 3, 0L]] |
|
269 verify(l3() == [0, 1, 0]) |
|
270 l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
|
271 verify(l4() == 1) |
|
272 l5 = lambda x, y, z=2: x + y + z |
|
273 verify(l5(1, 2) == 5) |
|
274 verify(l5(1, 2, 3) == 6) |
|
275 check_syntax("lambda x: x = 2") |
|
276 |
|
277 ### stmt: simple_stmt | compound_stmt |
|
278 # Tested below |
|
279 |
|
280 ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
|
281 print 'simple_stmt' |
|
282 x = 1; pass; del x |
|
283 def foo(): |
|
284 # verify statments that end with semi-colons |
|
285 x = 1; pass; del x; |
|
286 foo() |
|
287 |
|
288 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt |
|
289 # Tested below |
|
290 |
|
291 print 'expr_stmt' # (exprlist '=')* exprlist |
|
292 1 |
|
293 1, 2, 3 |
|
294 x = 1 |
|
295 x = 1, 2, 3 |
|
296 x = y = z = 1, 2, 3 |
|
297 x, y, z = 1, 2, 3 |
|
298 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) |
|
299 # NB these variables are deleted below |
|
300 |
|
301 check_syntax("x + 1 = 1") |
|
302 check_syntax("a + 1 = b + 2") |
|
303 |
|
304 print 'print_stmt' # 'print' (test ',')* [test] |
|
305 print 1, 2, 3 |
|
306 print 1, 2, 3, |
|
307 print |
|
308 print 0 or 1, 0 or 1, |
|
309 print 0 or 1 |
|
310 |
|
311 print 'extended print_stmt' # 'print' '>>' test ',' |
|
312 import sys |
|
313 print >> sys.stdout, 1, 2, 3 |
|
314 print >> sys.stdout, 1, 2, 3, |
|
315 print >> sys.stdout |
|
316 print >> sys.stdout, 0 or 1, 0 or 1, |
|
317 print >> sys.stdout, 0 or 1 |
|
318 |
|
319 # test printing to an instance |
|
320 class Gulp: |
|
321 def write(self, msg): pass |
|
322 |
|
323 gulp = Gulp() |
|
324 print >> gulp, 1, 2, 3 |
|
325 print >> gulp, 1, 2, 3, |
|
326 print >> gulp |
|
327 print >> gulp, 0 or 1, 0 or 1, |
|
328 print >> gulp, 0 or 1 |
|
329 |
|
330 # test print >> None |
|
331 def driver(): |
|
332 oldstdout = sys.stdout |
|
333 sys.stdout = Gulp() |
|
334 try: |
|
335 tellme(Gulp()) |
|
336 tellme() |
|
337 finally: |
|
338 sys.stdout = oldstdout |
|
339 |
|
340 # we should see this once |
|
341 def tellme(file=sys.stdout): |
|
342 print >> file, 'hello world' |
|
343 |
|
344 driver() |
|
345 |
|
346 # we should not see this at all |
|
347 def tellme(file=None): |
|
348 print >> file, 'goodbye universe' |
|
349 |
|
350 driver() |
|
351 |
|
352 # syntax errors |
|
353 check_syntax('print ,') |
|
354 check_syntax('print >> x,') |
|
355 |
|
356 print 'del_stmt' # 'del' exprlist |
|
357 del abc |
|
358 del x, y, (z, xyz) |
|
359 |
|
360 print 'pass_stmt' # 'pass' |
|
361 pass |
|
362 |
|
363 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt |
|
364 # Tested below |
|
365 |
|
366 print 'break_stmt' # 'break' |
|
367 while 1: break |
|
368 |
|
369 print 'continue_stmt' # 'continue' |
|
370 i = 1 |
|
371 while i: i = 0; continue |
|
372 |
|
373 msg = "" |
|
374 while not msg: |
|
375 msg = "continue + try/except ok" |
|
376 try: |
|
377 continue |
|
378 msg = "continue failed to continue inside try" |
|
379 except: |
|
380 msg = "continue inside try called except block" |
|
381 print msg |
|
382 |
|
383 msg = "" |
|
384 while not msg: |
|
385 msg = "finally block not called" |
|
386 try: |
|
387 continue |
|
388 finally: |
|
389 msg = "continue + try/finally ok" |
|
390 print msg |
|
391 |
|
392 |
|
393 # This test warrants an explanation. It is a test specifically for SF bugs |
|
394 # #463359 and #462937. The bug is that a 'break' statement executed or |
|
395 # exception raised inside a try/except inside a loop, *after* a continue |
|
396 # statement has been executed in that loop, will cause the wrong number of |
|
397 # arguments to be popped off the stack and the instruction pointer reset to |
|
398 # a very small number (usually 0.) Because of this, the following test |
|
399 # *must* written as a function, and the tracking vars *must* be function |
|
400 # arguments with default values. Otherwise, the test will loop and loop. |
|
401 |
|
402 print "testing continue and break in try/except in loop" |
|
403 def test_break_continue_loop(extra_burning_oil = 1, count=0): |
|
404 big_hippo = 2 |
|
405 while big_hippo: |
|
406 count += 1 |
|
407 try: |
|
408 if extra_burning_oil and big_hippo == 1: |
|
409 extra_burning_oil -= 1 |
|
410 break |
|
411 big_hippo -= 1 |
|
412 continue |
|
413 except: |
|
414 raise |
|
415 if count > 2 or big_hippo <> 1: |
|
416 print "continue then break in try/except in loop broken!" |
|
417 test_break_continue_loop() |
|
418 |
|
419 print 'return_stmt' # 'return' [testlist] |
|
420 def g1(): return |
|
421 def g2(): return 1 |
|
422 g1() |
|
423 x = g2() |
|
424 check_syntax("class foo:return 1") |
|
425 |
|
426 print 'yield_stmt' |
|
427 check_syntax("class foo:yield 1") |
|
428 |
|
429 print 'raise_stmt' # 'raise' test [',' test] |
|
430 try: raise RuntimeError, 'just testing' |
|
431 except RuntimeError: pass |
|
432 try: raise KeyboardInterrupt |
|
433 except KeyboardInterrupt: pass |
|
434 |
|
435 print 'import_name' # 'import' dotted_as_names |
|
436 import sys |
|
437 import time, sys |
|
438 print 'import_from' # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
|
439 from time import time |
|
440 from time import (time) |
|
441 from sys import * |
|
442 from sys import path, argv |
|
443 from sys import (path, argv) |
|
444 from sys import (path, argv,) |
|
445 |
|
446 print 'global_stmt' # 'global' NAME (',' NAME)* |
|
447 def f(): |
|
448 global a |
|
449 global a, b |
|
450 global one, two, three, four, five, six, seven, eight, nine, ten |
|
451 |
|
452 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]] |
|
453 def f(): |
|
454 z = None |
|
455 del z |
|
456 exec 'z=1+1\n' |
|
457 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n' |
|
458 del z |
|
459 exec 'z=1+1' |
|
460 if z != 2: raise TestFailed, 'exec \'z=1+1\'' |
|
461 z = None |
|
462 del z |
|
463 import types |
|
464 if hasattr(types, "UnicodeType"): |
|
465 exec r"""if 1: |
|
466 exec u'z=1+1\n' |
|
467 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n' |
|
468 del z |
|
469 exec u'z=1+1' |
|
470 if z != 2: raise TestFailed, 'exec u\'z=1+1\'' |
|
471 """ |
|
472 f() |
|
473 g = {} |
|
474 exec 'z = 1' in g |
|
475 if g.has_key('__builtins__'): del g['__builtins__'] |
|
476 if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' |
|
477 g = {} |
|
478 l = {} |
|
479 |
|
480 import warnings |
|
481 warnings.filterwarnings("ignore", "global statement", module="<string>") |
|
482 exec 'global a; a = 1; b = 2' in g, l |
|
483 if g.has_key('__builtins__'): del g['__builtins__'] |
|
484 if l.has_key('__builtins__'): del l['__builtins__'] |
|
485 if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l) |
|
486 |
|
487 |
|
488 print "assert_stmt" # assert_stmt: 'assert' test [',' test] |
|
489 assert 1 |
|
490 assert 1, 1 |
|
491 assert lambda x:x |
|
492 assert 1, lambda x:x+1 |
|
493 |
|
494 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
|
495 # Tested below |
|
496 |
|
497 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
|
498 if 1: pass |
|
499 if 1: pass |
|
500 else: pass |
|
501 if 0: pass |
|
502 elif 0: pass |
|
503 if 0: pass |
|
504 elif 0: pass |
|
505 elif 0: pass |
|
506 elif 0: pass |
|
507 else: pass |
|
508 |
|
509 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] |
|
510 while 0: pass |
|
511 while 0: pass |
|
512 else: pass |
|
513 |
|
514 # Issue1920: "while 0" is optimized away, |
|
515 # ensure that the "else" clause is still present. |
|
516 x = 0 |
|
517 while 0: |
|
518 x = 1 |
|
519 else: |
|
520 x = 2 |
|
521 assert x == 2 |
|
522 |
|
523 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
|
524 for i in 1, 2, 3: pass |
|
525 for i, j, k in (): pass |
|
526 else: pass |
|
527 class Squares: |
|
528 def __init__(self, max): |
|
529 self.max = max |
|
530 self.sofar = [] |
|
531 def __len__(self): return len(self.sofar) |
|
532 def __getitem__(self, i): |
|
533 if not 0 <= i < self.max: raise IndexError |
|
534 n = len(self.sofar) |
|
535 while n <= i: |
|
536 self.sofar.append(n*n) |
|
537 n = n+1 |
|
538 return self.sofar[i] |
|
539 n = 0 |
|
540 for x in Squares(10): n = n+x |
|
541 if n != 285: raise TestFailed, 'for over growing sequence' |
|
542 |
|
543 result = [] |
|
544 for x, in [(1,), (2,), (3,)]: |
|
545 result.append(x) |
|
546 vereq(result, [1, 2, 3]) |
|
547 |
|
548 print 'try_stmt' |
|
549 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
|
550 ### | 'try' ':' suite 'finally' ':' suite |
|
551 ### except_clause: 'except' [expr [',' expr]] |
|
552 try: |
|
553 1/0 |
|
554 except ZeroDivisionError: |
|
555 pass |
|
556 else: |
|
557 pass |
|
558 try: 1/0 |
|
559 except EOFError: pass |
|
560 except TypeError, msg: pass |
|
561 except RuntimeError, msg: pass |
|
562 except: pass |
|
563 else: pass |
|
564 try: 1/0 |
|
565 except (EOFError, TypeError, ZeroDivisionError): pass |
|
566 try: 1/0 |
|
567 except (EOFError, TypeError, ZeroDivisionError), msg: pass |
|
568 try: pass |
|
569 finally: pass |
|
570 |
|
571 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
|
572 if 1: pass |
|
573 if 1: |
|
574 pass |
|
575 if 1: |
|
576 # |
|
577 # |
|
578 # |
|
579 pass |
|
580 pass |
|
581 # |
|
582 pass |
|
583 # |
|
584 |
|
585 print 'test' |
|
586 ### and_test ('or' and_test)* |
|
587 ### and_test: not_test ('and' not_test)* |
|
588 ### not_test: 'not' not_test | comparison |
|
589 if not 1: pass |
|
590 if 1 and 1: pass |
|
591 if 1 or 1: pass |
|
592 if not not not 1: pass |
|
593 if not 1 and 1 and 1: pass |
|
594 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
|
595 |
|
596 print 'comparison' |
|
597 ### comparison: expr (comp_op expr)* |
|
598 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
|
599 if 1: pass |
|
600 x = (1 == 1) |
|
601 if 1 == 1: pass |
|
602 if 1 != 1: pass |
|
603 if 1 <> 1: pass |
|
604 if 1 < 1: pass |
|
605 if 1 > 1: pass |
|
606 if 1 <= 1: pass |
|
607 if 1 >= 1: pass |
|
608 if 1 is 1: pass |
|
609 if 1 is not 1: pass |
|
610 if 1 in (): pass |
|
611 if 1 not in (): pass |
|
612 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass |
|
613 |
|
614 print 'binary mask ops' |
|
615 x = 1 & 1 |
|
616 x = 1 ^ 1 |
|
617 x = 1 | 1 |
|
618 |
|
619 print 'shift ops' |
|
620 x = 1 << 1 |
|
621 x = 1 >> 1 |
|
622 x = 1 << 1 >> 1 |
|
623 |
|
624 print 'additive ops' |
|
625 x = 1 |
|
626 x = 1 + 1 |
|
627 x = 1 - 1 - 1 |
|
628 x = 1 - 1 + 1 - 1 + 1 |
|
629 |
|
630 print 'multiplicative ops' |
|
631 x = 1 * 1 |
|
632 x = 1 / 1 |
|
633 x = 1 % 1 |
|
634 x = 1 / 1 * 1 % 1 |
|
635 |
|
636 print 'unary ops' |
|
637 x = +1 |
|
638 x = -1 |
|
639 x = ~1 |
|
640 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
|
641 x = -1*1/1 + 1*1 - ---1*1 |
|
642 |
|
643 print 'selectors' |
|
644 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
|
645 ### subscript: expr | [expr] ':' [expr] |
|
646 f1() |
|
647 f2(1) |
|
648 f2(1,) |
|
649 f3(1, 2) |
|
650 f3(1, 2,) |
|
651 f4(1, (2, (3, 4))) |
|
652 v0() |
|
653 v0(1) |
|
654 v0(1,) |
|
655 v0(1,2) |
|
656 v0(1,2,3,4,5,6,7,8,9,0) |
|
657 v1(1) |
|
658 v1(1,) |
|
659 v1(1,2) |
|
660 v1(1,2,3) |
|
661 v1(1,2,3,4,5,6,7,8,9,0) |
|
662 v2(1,2) |
|
663 v2(1,2,3) |
|
664 v2(1,2,3,4) |
|
665 v2(1,2,3,4,5,6,7,8,9,0) |
|
666 v3(1,(2,3)) |
|
667 v3(1,(2,3),4) |
|
668 v3(1,(2,3),4,5,6,7,8,9,0) |
|
669 print |
|
670 import sys, time |
|
671 c = sys.path[0] |
|
672 x = time.time() |
|
673 x = sys.modules['time'].time() |
|
674 a = '01234' |
|
675 c = a[0] |
|
676 c = a[-1] |
|
677 s = a[0:5] |
|
678 s = a[:5] |
|
679 s = a[0:] |
|
680 s = a[:] |
|
681 s = a[-5:] |
|
682 s = a[:-1] |
|
683 s = a[-4:-3] |
|
684 # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
|
685 # The testing here is fairly incomplete. |
|
686 # Test cases should include: commas with 1 and 2 colons |
|
687 d = {} |
|
688 d[1] = 1 |
|
689 d[1,] = 2 |
|
690 d[1,2] = 3 |
|
691 d[1,2,3] = 4 |
|
692 L = list(d) |
|
693 L.sort() |
|
694 print L |
|
695 |
|
696 |
|
697 print 'atoms' |
|
698 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING |
|
699 ### dictmaker: test ':' test (',' test ':' test)* [','] |
|
700 |
|
701 x = (1) |
|
702 x = (1 or 2 or 3) |
|
703 x = (1 or 2 or 3, 2, 3) |
|
704 |
|
705 x = [] |
|
706 x = [1] |
|
707 x = [1 or 2 or 3] |
|
708 x = [1 or 2 or 3, 2, 3] |
|
709 x = [] |
|
710 |
|
711 x = {} |
|
712 x = {'one': 1} |
|
713 x = {'one': 1,} |
|
714 x = {'one' or 'two': 1 or 2} |
|
715 x = {'one': 1, 'two': 2} |
|
716 x = {'one': 1, 'two': 2,} |
|
717 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
|
718 |
|
719 x = `x` |
|
720 x = `1 or 2 or 3` |
|
721 x = `1,2` |
|
722 x = x |
|
723 x = 'x' |
|
724 x = 123 |
|
725 |
|
726 ### exprlist: expr (',' expr)* [','] |
|
727 ### testlist: test (',' test)* [','] |
|
728 # These have been exercised enough above |
|
729 |
|
730 print 'classdef' # 'class' NAME ['(' [testlist] ')'] ':' suite |
|
731 class B: pass |
|
732 class B2(): pass |
|
733 class C1(B): pass |
|
734 class C2(B): pass |
|
735 class D(C1, C2, B): pass |
|
736 class C: |
|
737 def meth1(self): pass |
|
738 def meth2(self, arg): pass |
|
739 def meth3(self, a1, a2): pass |
|
740 |
|
741 # list comprehension tests |
|
742 nums = [1, 2, 3, 4, 5] |
|
743 strs = ["Apple", "Banana", "Coconut"] |
|
744 spcs = [" Apple", " Banana ", "Coco nut "] |
|
745 |
|
746 print [s.strip() for s in spcs] |
|
747 print [3 * x for x in nums] |
|
748 print [x for x in nums if x > 2] |
|
749 print [(i, s) for i in nums for s in strs] |
|
750 print [(i, s) for i in nums for s in [f for f in strs if "n" in f]] |
|
751 print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)] |
|
752 |
|
753 def test_in_func(l): |
|
754 return [None < x < 3 for x in l if x > 2] |
|
755 |
|
756 print test_in_func(nums) |
|
757 |
|
758 def test_nested_front(): |
|
759 print [[y for y in [x, x + 1]] for x in [1,3,5]] |
|
760 |
|
761 test_nested_front() |
|
762 |
|
763 check_syntax("[i, s for i in nums for s in strs]") |
|
764 check_syntax("[x if y]") |
|
765 |
|
766 suppliers = [ |
|
767 (1, "Boeing"), |
|
768 (2, "Ford"), |
|
769 (3, "Macdonalds") |
|
770 ] |
|
771 |
|
772 parts = [ |
|
773 (10, "Airliner"), |
|
774 (20, "Engine"), |
|
775 (30, "Cheeseburger") |
|
776 ] |
|
777 |
|
778 suppart = [ |
|
779 (1, 10), (1, 20), (2, 20), (3, 30) |
|
780 ] |
|
781 |
|
782 print [ |
|
783 (sname, pname) |
|
784 for (sno, sname) in suppliers |
|
785 for (pno, pname) in parts |
|
786 for (sp_sno, sp_pno) in suppart |
|
787 if sno == sp_sno and pno == sp_pno |
|
788 ] |
|
789 |
|
790 # generator expression tests |
|
791 g = ([x for x in range(10)] for x in range(1)) |
|
792 verify(g.next() == [x for x in range(10)]) |
|
793 try: |
|
794 g.next() |
|
795 raise TestFailed, 'should produce StopIteration exception' |
|
796 except StopIteration: |
|
797 pass |
|
798 |
|
799 a = 1 |
|
800 try: |
|
801 g = (a for d in a) |
|
802 g.next() |
|
803 raise TestFailed, 'should produce TypeError' |
|
804 except TypeError: |
|
805 pass |
|
806 |
|
807 verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd']) |
|
808 verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy']) |
|
809 |
|
810 a = [x for x in range(10)] |
|
811 b = (x for x in (y for y in a)) |
|
812 verify(sum(b) == sum([x for x in range(10)])) |
|
813 |
|
814 verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)])) |
|
815 verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2])) |
|
816 verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)])) |
|
817 verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)])) |
|
818 verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)])) |
|
819 verify(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)])) |
|
820 verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0) |
|
821 check_syntax("foo(x for x in range(10), 100)") |
|
822 check_syntax("foo(100, x for x in range(10))") |
|
823 |
|
824 # test for outmost iterable precomputation |
|
825 x = 10; g = (i for i in range(x)); x = 5 |
|
826 verify(len(list(g)) == 10) |
|
827 |
|
828 # This should hold, since we're only precomputing outmost iterable. |
|
829 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
|
830 x = 5; t = True; |
|
831 verify([(i,j) for i in range(10) for j in range(5)] == list(g)) |
|
832 |
|
833 # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
|
834 # even though it's silly. Make sure it works (ifelse broke this.) |
|
835 verify([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
|
836 verify((x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
|
837 |
|
838 # Verify unpacking single element tuples in listcomp/genexp. |
|
839 vereq([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
|
840 vereq(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
|
841 |
|
842 # Test ifelse expressions in various cases |
|
843 def _checkeval(msg, ret): |
|
844 "helper to check that evaluation of expressions is done correctly" |
|
845 print x |
|
846 return ret |
|
847 |
|
848 verify([ x() for x in lambda: True, lambda: False if x() ] == [True]) |
|
849 verify([ x() for x in (lambda: True, lambda: False) if x() ] == [True]) |
|
850 verify([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ] == [True]) |
|
851 verify((5 if 1 else _checkeval("check 1", 0)) == 5) |
|
852 verify((_checkeval("check 2", 0) if 0 else 5) == 5) |
|
853 verify((5 and 6 if 0 else 1) == 1) |
|
854 verify(((5 and 6) if 0 else 1) == 1) |
|
855 verify((5 and (6 if 1 else 1)) == 6) |
|
856 verify((0 or _checkeval("check 3", 2) if 0 else 3) == 3) |
|
857 verify((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)) == 1) |
|
858 verify((0 or 5 if 1 else _checkeval("check 6", 3)) == 5) |
|
859 verify((not 5 if 1 else 1) == False) |
|
860 verify((not 5 if 0 else 1) == 1) |
|
861 verify((6 + 1 if 1 else 2) == 7) |
|
862 verify((6 - 1 if 1 else 2) == 5) |
|
863 verify((6 * 2 if 1 else 4) == 12) |
|
864 verify((6 / 2 if 1 else 3) == 3) |
|
865 verify((6 < 4 if 0 else 2) == 2) |