Commit | Line | Data |
---|---|---|
86530b38 AT |
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, 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 | verify(f2.func_code.co_varnames == ('one_argument',)) | |
161 | verify(f3.func_code.co_varnames == ('two', 'arguments')) | |
162 | if sys.platform.startswith('java'): | |
163 | verify(f4.func_code.co_varnames == | |
164 | ('two', '(compound, (argument, list))', 'compound', 'argument', | |
165 | 'list',)) | |
166 | verify(f5.func_code.co_varnames == | |
167 | ('(compound, first)', 'two', 'compound', 'first')) | |
168 | else: | |
169 | verify(f4.func_code.co_varnames == ('two', '.2', 'compound', | |
170 | 'argument', 'list')) | |
171 | verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first')) | |
172 | def a1(one_arg,): pass | |
173 | def a2(two, args,): pass | |
174 | def v0(*rest): pass | |
175 | def v1(a, *rest): pass | |
176 | def v2(a, b, *rest): pass | |
177 | def v3(a, (b, c), *rest): return a, b, c, rest | |
178 | if sys.platform.startswith('java'): | |
179 | verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c')) | |
180 | else: | |
181 | verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c')) | |
182 | verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,))) | |
183 | def d01(a=1): pass | |
184 | d01() | |
185 | d01(1) | |
186 | d01(*(1,)) | |
187 | d01(**{'a':2}) | |
188 | def d11(a, b=1): pass | |
189 | d11(1) | |
190 | d11(1, 2) | |
191 | d11(1, **{'b':2}) | |
192 | def d21(a, b, c=1): pass | |
193 | d21(1, 2) | |
194 | d21(1, 2, 3) | |
195 | d21(*(1, 2, 3)) | |
196 | d21(1, *(2, 3)) | |
197 | d21(1, 2, *(3,)) | |
198 | d21(1, 2, **{'c':3}) | |
199 | def d02(a=1, b=2): pass | |
200 | d02() | |
201 | d02(1) | |
202 | d02(1, 2) | |
203 | d02(*(1, 2)) | |
204 | d02(1, *(2,)) | |
205 | d02(1, **{'b':2}) | |
206 | d02(**{'a': 1, 'b': 2}) | |
207 | def d12(a, b=1, c=2): pass | |
208 | d12(1) | |
209 | d12(1, 2) | |
210 | d12(1, 2, 3) | |
211 | def d22(a, b, c=1, d=2): pass | |
212 | d22(1, 2) | |
213 | d22(1, 2, 3) | |
214 | d22(1, 2, 3, 4) | |
215 | def d01v(a=1, *rest): pass | |
216 | d01v() | |
217 | d01v(1) | |
218 | d01v(1, 2) | |
219 | d01v(*(1, 2, 3, 4)) | |
220 | d01v(*(1,)) | |
221 | d01v(**{'a':2}) | |
222 | def d11v(a, b=1, *rest): pass | |
223 | d11v(1) | |
224 | d11v(1, 2) | |
225 | d11v(1, 2, 3) | |
226 | def d21v(a, b, c=1, *rest): pass | |
227 | d21v(1, 2) | |
228 | d21v(1, 2, 3) | |
229 | d21v(1, 2, 3, 4) | |
230 | d21v(*(1, 2, 3, 4)) | |
231 | d21v(1, 2, **{'c': 3}) | |
232 | def d02v(a=1, b=2, *rest): pass | |
233 | d02v() | |
234 | d02v(1) | |
235 | d02v(1, 2) | |
236 | d02v(1, 2, 3) | |
237 | d02v(1, *(2, 3, 4)) | |
238 | d02v(**{'a': 1, 'b': 2}) | |
239 | def d12v(a, b=1, c=2, *rest): pass | |
240 | d12v(1) | |
241 | d12v(1, 2) | |
242 | d12v(1, 2, 3) | |
243 | d12v(1, 2, 3, 4) | |
244 | d12v(*(1, 2, 3, 4)) | |
245 | d12v(1, 2, *(3, 4, 5)) | |
246 | d12v(1, *(2,), **{'c': 3}) | |
247 | def d22v(a, b, c=1, d=2, *rest): pass | |
248 | d22v(1, 2) | |
249 | d22v(1, 2, 3) | |
250 | d22v(1, 2, 3, 4) | |
251 | d22v(1, 2, 3, 4, 5) | |
252 | d22v(*(1, 2, 3, 4)) | |
253 | d22v(1, 2, *(3, 4, 5)) | |
254 | d22v(1, *(2, 3), **{'d': 4}) | |
255 | ||
256 | ### lambdef: 'lambda' [varargslist] ':' test | |
257 | print 'lambdef' | |
258 | l1 = lambda : 0 | |
259 | verify(l1() == 0) | |
260 | l2 = lambda : a[d] # XXX just testing the expression | |
261 | l3 = lambda : [2 < x for x in [-1, 3, 0L]] | |
262 | verify(l3() == [0, 1, 0]) | |
263 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() | |
264 | verify(l4() == 1) | |
265 | l5 = lambda x, y, z=2: x + y + z | |
266 | verify(l5(1, 2) == 5) | |
267 | verify(l5(1, 2, 3) == 6) | |
268 | check_syntax("lambda x: x = 2") | |
269 | ||
270 | ### stmt: simple_stmt | compound_stmt | |
271 | # Tested below | |
272 | ||
273 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] | |
274 | print 'simple_stmt' | |
275 | x = 1; pass; del x | |
276 | ||
277 | ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt | |
278 | # Tested below | |
279 | ||
280 | print 'expr_stmt' # (exprlist '=')* exprlist | |
281 | 1 | |
282 | 1, 2, 3 | |
283 | x = 1 | |
284 | x = 1, 2, 3 | |
285 | x = y = z = 1, 2, 3 | |
286 | x, y, z = 1, 2, 3 | |
287 | abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) | |
288 | # NB these variables are deleted below | |
289 | ||
290 | check_syntax("x + 1 = 1") | |
291 | check_syntax("a + 1 = b + 2") | |
292 | ||
293 | print 'print_stmt' # 'print' (test ',')* [test] | |
294 | print 1, 2, 3 | |
295 | print 1, 2, 3, | |
296 | ||
297 | print 0 or 1, 0 or 1, | |
298 | print 0 or 1 | |
299 | ||
300 | print 'extended print_stmt' # 'print' '>>' test ',' | |
301 | import sys | |
302 | print >> sys.stdout, 1, 2, 3 | |
303 | print >> sys.stdout, 1, 2, 3, | |
304 | print >> sys.stdout | |
305 | print >> sys.stdout, 0 or 1, 0 or 1, | |
306 | print >> sys.stdout, 0 or 1 | |
307 | ||
308 | # test printing to an instance | |
309 | class Gulp: | |
310 | def write(self, msg): pass | |
311 | ||
312 | gulp = Gulp() | |
313 | print >> gulp, 1, 2, 3 | |
314 | print >> gulp, 1, 2, 3, | |
315 | print >> gulp | |
316 | print >> gulp, 0 or 1, 0 or 1, | |
317 | print >> gulp, 0 or 1 | |
318 | ||
319 | # test print >> None | |
320 | def driver(): | |
321 | oldstdout = sys.stdout | |
322 | sys.stdout = Gulp() | |
323 | try: | |
324 | tellme(Gulp()) | |
325 | tellme() | |
326 | finally: | |
327 | sys.stdout = oldstdout | |
328 | ||
329 | # we should see this once | |
330 | def tellme(file=sys.stdout): | |
331 | print >> file, 'hello world' | |
332 | ||
333 | driver() | |
334 | ||
335 | # we should not see this at all | |
336 | def tellme(file=None): | |
337 | print >> file, 'goodbye universe' | |
338 | ||
339 | driver() | |
340 | ||
341 | # syntax errors | |
342 | check_syntax('print ,') | |
343 | check_syntax('print >> x,') | |
344 | ||
345 | print 'del_stmt' # 'del' exprlist | |
346 | del abc | |
347 | del x, y, (z, xyz) | |
348 | ||
349 | print 'pass_stmt' # 'pass' | |
350 | pass | |
351 | ||
352 | print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt | |
353 | # Tested below | |
354 | ||
355 | print 'break_stmt' # 'break' | |
356 | while 1: break | |
357 | ||
358 | print 'continue_stmt' # 'continue' | |
359 | i = 1 | |
360 | while i: i = 0; continue | |
361 | ||
362 | msg = "" | |
363 | while not msg: | |
364 | msg = "continue + try/except ok" | |
365 | try: | |
366 | continue | |
367 | msg = "continue failed to continue inside try" | |
368 | except: | |
369 | msg = "continue inside try called except block" | |
370 | print msg | |
371 | ||
372 | msg = "" | |
373 | while not msg: | |
374 | msg = "finally block not called" | |
375 | try: | |
376 | continue | |
377 | finally: | |
378 | msg = "continue + try/finally ok" | |
379 | print msg | |
380 | ||
381 | ||
382 | # This test warrants an explanation. It is a test specifically for SF bugs | |
383 | # #463359 and #462937. The bug is that a 'break' statement executed or | |
384 | # exception raised inside a try/except inside a loop, *after* a continue | |
385 | # statement has been executed in that loop, will cause the wrong number of | |
386 | # arguments to be popped off the stack and the instruction pointer reset to | |
387 | # a very small number (usually 0.) Because of this, the following test | |
388 | # *must* written as a function, and the tracking vars *must* be function | |
389 | # arguments with default values. Otherwise, the test will loop and loop. | |
390 | ||
391 | print "testing continue and break in try/except in loop" | |
392 | def test_break_continue_loop(extra_burning_oil = 1, count=0): | |
393 | big_hippo = 2 | |
394 | while big_hippo: | |
395 | count += 1 | |
396 | try: | |
397 | if extra_burning_oil and big_hippo == 1: | |
398 | extra_burning_oil -= 1 | |
399 | break | |
400 | big_hippo -= 1 | |
401 | continue | |
402 | except: | |
403 | raise | |
404 | if count > 2 or big_hippo <> 1: | |
405 | print "continue then break in try/except in loop broken!" | |
406 | test_break_continue_loop() | |
407 | ||
408 | print 'return_stmt' # 'return' [testlist] | |
409 | def g1(): return | |
410 | def g2(): return 1 | |
411 | g1() | |
412 | x = g2() | |
413 | ||
414 | print 'raise_stmt' # 'raise' test [',' test] | |
415 | try: raise RuntimeError, 'just testing' | |
416 | except RuntimeError: pass | |
417 | try: raise KeyboardInterrupt | |
418 | except KeyboardInterrupt: pass | |
419 | ||
420 | print 'import_name' # 'import' dotted_as_names | |
421 | import sys | |
422 | import time, sys | |
423 | print 'import_from' # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) | |
424 | from time import time | |
425 | from time import (time) | |
426 | from sys import * | |
427 | from sys import path, argv | |
428 | from sys import (path, argv) | |
429 | from sys import (path, argv,) | |
430 | ||
431 | print 'global_stmt' # 'global' NAME (',' NAME)* | |
432 | def f(): | |
433 | global a | |
434 | global a, b | |
435 | global one, two, three, four, five, six, seven, eight, nine, ten | |
436 | ||
437 | print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]] | |
438 | def f(): | |
439 | z = None | |
440 | del z | |
441 | exec 'z=1+1\n' | |
442 | if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n' | |
443 | del z | |
444 | exec 'z=1+1' | |
445 | if z != 2: raise TestFailed, 'exec \'z=1+1\'' | |
446 | z = None | |
447 | del z | |
448 | import types | |
449 | if hasattr(types, "UnicodeType"): | |
450 | exec r"""if 1: | |
451 | exec u'z=1+1\n' | |
452 | if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n' | |
453 | del z | |
454 | exec u'z=1+1' | |
455 | if z != 2: raise TestFailed, 'exec u\'z=1+1\'' | |
456 | """ | |
457 | f() | |
458 | g = {} | |
459 | exec 'z = 1' in g | |
460 | if g.has_key('__builtins__'): del g['__builtins__'] | |
461 | if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g' | |
462 | g = {} | |
463 | l = {} | |
464 | ||
465 | import warnings | |
466 | warnings.filterwarnings("ignore", "global statement", module="<string>") | |
467 | exec 'global a; a = 1; b = 2' in g, l | |
468 | if g.has_key('__builtins__'): del g['__builtins__'] | |
469 | if l.has_key('__builtins__'): del l['__builtins__'] | |
470 | if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l) | |
471 | ||
472 | ||
473 | print "assert_stmt" # assert_stmt: 'assert' test [',' test] | |
474 | assert 1 | |
475 | assert 1, 1 | |
476 | assert lambda x:x | |
477 | assert 1, lambda x:x+1 | |
478 | ||
479 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | |
480 | # Tested below | |
481 | ||
482 | print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] | |
483 | if 1: pass | |
484 | if 1: pass | |
485 | else: pass | |
486 | if 0: pass | |
487 | elif 0: pass | |
488 | if 0: pass | |
489 | elif 0: pass | |
490 | elif 0: pass | |
491 | elif 0: pass | |
492 | else: pass | |
493 | ||
494 | print 'while_stmt' # 'while' test ':' suite ['else' ':' suite] | |
495 | while 0: pass | |
496 | while 0: pass | |
497 | else: pass | |
498 | ||
499 | print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] | |
500 | for i in 1, 2, 3: pass | |
501 | for i, j, k in (): pass | |
502 | else: pass | |
503 | class Squares: | |
504 | def __init__(self, max): | |
505 | self.max = max | |
506 | self.sofar = [] | |
507 | def __len__(self): return len(self.sofar) | |
508 | def __getitem__(self, i): | |
509 | if not 0 <= i < self.max: raise IndexError | |
510 | n = len(self.sofar) | |
511 | while n <= i: | |
512 | self.sofar.append(n*n) | |
513 | n = n+1 | |
514 | return self.sofar[i] | |
515 | n = 0 | |
516 | for x in Squares(10): n = n+x | |
517 | if n != 285: raise TestFailed, 'for over growing sequence' | |
518 | ||
519 | print 'try_stmt' | |
520 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] | |
521 | ### | 'try' ':' suite 'finally' ':' suite | |
522 | ### except_clause: 'except' [expr [',' expr]] | |
523 | try: | |
524 | 1/0 | |
525 | except ZeroDivisionError: | |
526 | pass | |
527 | else: | |
528 | pass | |
529 | try: 1/0 | |
530 | except EOFError: pass | |
531 | except TypeError, msg: pass | |
532 | except RuntimeError, msg: pass | |
533 | except: pass | |
534 | else: pass | |
535 | try: 1/0 | |
536 | except (EOFError, TypeError, ZeroDivisionError): pass | |
537 | try: 1/0 | |
538 | except (EOFError, TypeError, ZeroDivisionError), msg: pass | |
539 | try: pass | |
540 | finally: pass | |
541 | ||
542 | print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT | |
543 | if 1: pass | |
544 | if 1: | |
545 | pass | |
546 | if 1: | |
547 | # | |
548 | # | |
549 | # | |
550 | pass | |
551 | pass | |
552 | # | |
553 | pass | |
554 | # | |
555 | ||
556 | print 'test' | |
557 | ### and_test ('or' and_test)* | |
558 | ### and_test: not_test ('and' not_test)* | |
559 | ### not_test: 'not' not_test | comparison | |
560 | if not 1: pass | |
561 | if 1 and 1: pass | |
562 | if 1 or 1: pass | |
563 | if not not not 1: pass | |
564 | if not 1 and 1 and 1: pass | |
565 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass | |
566 | ||
567 | print 'comparison' | |
568 | ### comparison: expr (comp_op expr)* | |
569 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' | |
570 | if 1: pass | |
571 | x = (1 == 1) | |
572 | if 1 == 1: pass | |
573 | if 1 != 1: pass | |
574 | if 1 <> 1: pass | |
575 | if 1 < 1: pass | |
576 | if 1 > 1: pass | |
577 | if 1 <= 1: pass | |
578 | if 1 >= 1: pass | |
579 | if 1 is 1: pass | |
580 | if 1 is not 1: pass | |
581 | if 1 in (): pass | |
582 | if 1 not in (): pass | |
583 | if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass | |
584 | ||
585 | print 'binary mask ops' | |
586 | x = 1 & 1 | |
587 | x = 1 ^ 1 | |
588 | x = 1 | 1 | |
589 | ||
590 | print 'shift ops' | |
591 | x = 1 << 1 | |
592 | x = 1 >> 1 | |
593 | x = 1 << 1 >> 1 | |
594 | ||
595 | print 'additive ops' | |
596 | x = 1 | |
597 | x = 1 + 1 | |
598 | x = 1 - 1 - 1 | |
599 | x = 1 - 1 + 1 - 1 + 1 | |
600 | ||
601 | print 'multiplicative ops' | |
602 | x = 1 * 1 | |
603 | x = 1 / 1 | |
604 | x = 1 % 1 | |
605 | x = 1 / 1 * 1 % 1 | |
606 | ||
607 | print 'unary ops' | |
608 | x = +1 | |
609 | x = -1 | |
610 | x = ~1 | |
611 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 | |
612 | x = -1*1/1 + 1*1 - ---1*1 | |
613 | ||
614 | print 'selectors' | |
615 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME | |
616 | ### subscript: expr | [expr] ':' [expr] | |
617 | f1() | |
618 | f2(1) | |
619 | f2(1,) | |
620 | f3(1, 2) | |
621 | f3(1, 2,) | |
622 | f4(1, (2, (3, 4))) | |
623 | v0() | |
624 | v0(1) | |
625 | v0(1,) | |
626 | v0(1,2) | |
627 | v0(1,2,3,4,5,6,7,8,9,0) | |
628 | v1(1) | |
629 | v1(1,) | |
630 | v1(1,2) | |
631 | v1(1,2,3) | |
632 | v1(1,2,3,4,5,6,7,8,9,0) | |
633 | v2(1,2) | |
634 | v2(1,2,3) | |
635 | v2(1,2,3,4) | |
636 | v2(1,2,3,4,5,6,7,8,9,0) | |
637 | v3(1,(2,3)) | |
638 | v3(1,(2,3),4) | |
639 | v3(1,(2,3),4,5,6,7,8,9,0) | |
640 | ||
641 | import sys, time | |
642 | c = sys.path[0] | |
643 | x = time.time() | |
644 | x = sys.modules['time'].time() | |
645 | a = '01234' | |
646 | c = a[0] | |
647 | c = a[-1] | |
648 | s = a[0:5] | |
649 | s = a[:5] | |
650 | s = a[0:] | |
651 | s = a[:] | |
652 | s = a[-5:] | |
653 | s = a[:-1] | |
654 | s = a[-4:-3] | |
655 | ||
656 | print 'atoms' | |
657 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING | |
658 | ### dictmaker: test ':' test (',' test ':' test)* [','] | |
659 | ||
660 | x = (1) | |
661 | x = (1 or 2 or 3) | |
662 | x = (1 or 2 or 3, 2, 3) | |
663 | ||
664 | x = [] | |
665 | x = [1] | |
666 | x = [1 or 2 or 3] | |
667 | x = [1 or 2 or 3, 2, 3] | |
668 | x = [] | |
669 | ||
670 | x = {} | |
671 | x = {'one': 1} | |
672 | x = {'one': 1,} | |
673 | x = {'one' or 'two': 1 or 2} | |
674 | x = {'one': 1, 'two': 2} | |
675 | x = {'one': 1, 'two': 2,} | |
676 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} | |
677 | ||
678 | x = `x` | |
679 | x = `1 or 2 or 3` | |
680 | x = x | |
681 | x = 'x' | |
682 | x = 123 | |
683 | ||
684 | ### exprlist: expr (',' expr)* [','] | |
685 | ### testlist: test (',' test)* [','] | |
686 | # These have been exercised enough above | |
687 | ||
688 | print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite | |
689 | class B: pass | |
690 | class C1(B): pass | |
691 | class C2(B): pass | |
692 | class D(C1, C2, B): pass | |
693 | class C: | |
694 | def meth1(self): pass | |
695 | def meth2(self, arg): pass | |
696 | def meth3(self, a1, a2): pass | |
697 | ||
698 | # list comprehension tests | |
699 | nums = [1, 2, 3, 4, 5] | |
700 | strs = ["Apple", "Banana", "Coconut"] | |
701 | spcs = [" Apple", " Banana ", "Coco nut "] | |
702 | ||
703 | print [s.strip() for s in spcs] | |
704 | print [3 * x for x in nums] | |
705 | print [x for x in nums if x > 2] | |
706 | print [(i, s) for i in nums for s in strs] | |
707 | print [(i, s) for i in nums for s in [f for f in strs if "n" in f]] | |
708 | print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)] | |
709 | ||
710 | def test_in_func(l): | |
711 | return [None < x < 3 for x in l if x > 2] | |
712 | ||
713 | print test_in_func(nums) | |
714 | ||
715 | def test_nested_front(): | |
716 | print [[y for y in [x, x + 1]] for x in [1,3,5]] | |
717 | ||
718 | test_nested_front() | |
719 | ||
720 | check_syntax("[i, s for i in nums for s in strs]") | |
721 | check_syntax("[x if y]") | |
722 | ||
723 | suppliers = [ | |
724 | (1, "Boeing"), | |
725 | (2, "Ford"), | |
726 | (3, "Macdonalds") | |
727 | ] | |
728 | ||
729 | parts = [ | |
730 | (10, "Airliner"), | |
731 | (20, "Engine"), | |
732 | (30, "Cheeseburger") | |
733 | ] | |
734 | ||
735 | suppart = [ | |
736 | (1, 10), (1, 20), (2, 20), (3, 30) | |
737 | ] | |
738 | ||
739 | print [ | |
740 | (sname, pname) | |
741 | for (sno, sname) in suppliers | |
742 | for (pno, pname) in parts | |
743 | for (sp_sno, sp_pno) in suppart | |
744 | if sno == sp_sno and pno == sp_pno | |
745 | ] | |
746 | ||
747 | # generator expression tests | |
748 | g = ([x for x in range(10)] for x in range(1)) | |
749 | verify(g.next() == [x for x in range(10)]) | |
750 | try: | |
751 | g.next() | |
752 | raise TestFailed, 'should produce StopIteration exception' | |
753 | except StopIteration: | |
754 | pass | |
755 | ||
756 | a = 1 | |
757 | try: | |
758 | g = (a for d in a) | |
759 | g.next() | |
760 | raise TestFailed, 'should produce TypeError' | |
761 | except TypeError: | |
762 | pass | |
763 | ||
764 | verify(list((x, y) for x in 'abcd' for y in 'abcd') == [(x, y) for x in 'abcd' for y in 'abcd']) | |
765 | verify(list((x, y) for x in 'ab' for y in 'xy') == [(x, y) for x in 'ab' for y in 'xy']) | |
766 | ||
767 | a = [x for x in range(10)] | |
768 | b = (x for x in (y for y in a)) | |
769 | verify(sum(b) == sum([x for x in range(10)])) | |
770 | ||
771 | verify(sum(x**2 for x in range(10)) == sum([x**2 for x in range(10)])) | |
772 | verify(sum(x*x for x in range(10) if x%2) == sum([x*x for x in range(10) if x%2])) | |
773 | verify(sum(x for x in (y for y in range(10))) == sum([x for x in range(10)])) | |
774 | verify(sum(x for x in (y for y in (z for z in range(10)))) == sum([x for x in range(10)])) | |
775 | verify(sum(x for x in [y for y in (z for z in range(10))]) == sum([x for x in range(10)])) | |
776 | 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)])) | |
777 | verify(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True) == 0) | |
778 | check_syntax("foo(x for x in range(10), 100)") | |
779 | check_syntax("foo(100, x for x in range(10))") | |
780 | ||
781 | # test for outmost iterable precomputation | |
782 | x = 10; g = (i for i in range(x)); x = 5 | |
783 | verify(len(list(g)) == 10) | |
784 | ||
785 | # This should hold, since we're only precomputing outmost iterable. | |
786 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) | |
787 | x = 5; t = True; | |
788 | verify([(i,j) for i in range(10) for j in range(5)] == list(g)) |