- Got rid of newmodule.c
[python/dscho.git] / Lib / test / test_grammar.py
blobbb843fb6246136492dbcfa2394d6f040bc16b1f5
1 # Python test set -- part 1, grammar.
2 # This just tests whether the parser accepts them all.
4 from test_support import *
5 import sys
7 print '1. Parser'
9 print '1.1 Tokens'
11 print '1.1.1 Backslashes'
13 # Backslash means line continuation:
14 x = 1 \
15 + 1
16 if x != 2: raise TestFailed, 'backslash for line continuation'
18 # Backslash does not means continuation in comments :\
19 x = 0
20 if x != 0: raise TestFailed, 'backslash ending comment'
22 print '1.1.2 Numeric literals'
24 print '1.1.2.1 Plain integers'
25 if 0xff != 255: raise TestFailed, 'hex int'
26 if 0377 != 255: raise TestFailed, 'octal int'
27 if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
28 try:
29 from sys import maxint
30 except ImportError:
31 maxint = 2147483647
32 if maxint == 2147483647:
33 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
34 # XXX -2147483648
35 if 037777777777 != -1: raise TestFailed, 'oct -1'
36 if 0xffffffff != -1: raise TestFailed, 'hex -1'
37 for s in '2147483648', '040000000000', '0x100000000':
38 try:
39 x = eval(s)
40 except OverflowError:
41 print "OverflowError on huge integer literal " + `s`
42 elif eval('maxint == 9223372036854775807'):
43 if eval('-9223372036854775807-1 != 01000000000000000000000'):
44 raise TestFailed, 'max negative int'
45 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
46 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
47 for s in '9223372036854775808', '02000000000000000000000', \
48 '0x10000000000000000':
49 try:
50 x = eval(s)
51 except OverflowError:
52 print "OverflowError on huge integer literal " + `s`
53 else:
54 print 'Weird maxint value', maxint
56 print '1.1.2.2 Long integers'
57 x = 0L
58 x = 0l
59 x = 0xffffffffffffffffL
60 x = 0xffffffffffffffffl
61 x = 077777777777777777L
62 x = 077777777777777777l
63 x = 123456789012345678901234567890L
64 x = 123456789012345678901234567890l
66 print '1.1.2.3 Floating point'
67 x = 3.14
68 x = 314.
69 x = 0.314
70 # XXX x = 000.314
71 x = .314
72 x = 3e14
73 x = 3E14
74 x = 3e-14
75 x = 3e+14
76 x = 3.e14
77 x = .3e14
78 x = 3.1e4
80 print '1.1.3 String literals'
82 x = ''; y = ""; verify(len(x) == 0 and x == y)
83 x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
84 x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
85 x = "doesn't \"shrink\" does it"
86 y = 'doesn\'t "shrink" does it'
87 verify(len(x) == 24 and x == y)
88 x = "does \"shrink\" doesn't it"
89 y = 'does "shrink" doesn\'t it'
90 verify(len(x) == 24 and x == y)
91 x = """
92 The "quick"
93 brown fox
94 jumps over
95 the 'lazy' dog.
96 """
97 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
98 verify(x == y)
99 y = '''
100 The "quick"
101 brown fox
102 jumps over
103 the 'lazy' dog.
104 '''; verify(x == y)
105 y = "\n\
106 The \"quick\"\n\
107 brown fox\n\
108 jumps over\n\
109 the 'lazy' dog.\n\
110 "; verify(x == y)
111 y = '\n\
112 The \"quick\"\n\
113 brown fox\n\
114 jumps over\n\
115 the \'lazy\' dog.\n\
116 '; verify(x == y)
119 print '1.2 Grammar'
121 print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
122 # XXX can't test in a script -- this rule is only used when interactive
124 print 'file_input' # (NEWLINE | stmt)* ENDMARKER
125 # Being tested as this very moment this very module
127 print 'expr_input' # testlist NEWLINE
128 # XXX Hard to test -- used only in calls to input()
130 print 'eval_input' # testlist ENDMARKER
131 x = eval('1, 0 or 1')
133 print 'funcdef'
134 ### 'def' NAME parameters ':' suite
135 ### parameters: '(' [varargslist] ')'
136 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
137 ### | ('**'|'*' '*') NAME)
138 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
139 ### fpdef: NAME | '(' fplist ')'
140 ### fplist: fpdef (',' fpdef)* [',']
141 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
142 ### argument: [test '='] test # Really [keyword '='] test
143 def f1(): pass
144 f1()
145 f1(*())
146 f1(*(), **{})
147 def f2(one_argument): pass
148 def f3(two, arguments): pass
149 def f4(two, (compound, (argument, list))): pass
150 def f5((compound, first), two): pass
151 verify(f2.func_code.co_varnames == ('one_argument',))
152 verify(f3.func_code.co_varnames == ('two', 'arguments'))
153 if sys.platform.startswith('java'):
154 verify(f4.func_code.co_varnames ==
155 ('two', '(compound, (argument, list))', 'compound', 'argument',
156 'list',))
157 verify(f5.func_code.co_varnames ==
158 ('(compound, first)', 'two', 'compound', 'first'))
159 else:
160 verify(f4.func_code.co_varnames == ('two', '.2', 'compound',
161 'argument', 'list'))
162 verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
163 def a1(one_arg,): pass
164 def a2(two, args,): pass
165 def v0(*rest): pass
166 def v1(a, *rest): pass
167 def v2(a, b, *rest): pass
168 def v3(a, (b, c), *rest): return a, b, c, rest
169 if sys.platform.startswith('java'):
170 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))
171 else:
172 verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
173 verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
174 def d01(a=1): pass
175 d01()
176 d01(1)
177 d01(*(1,))
178 d01(**{'a':2})
179 def d11(a, b=1): pass
180 d11(1)
181 d11(1, 2)
182 d11(1, **{'b':2})
183 def d21(a, b, c=1): pass
184 d21(1, 2)
185 d21(1, 2, 3)
186 d21(*(1, 2, 3))
187 d21(1, *(2, 3))
188 d21(1, 2, *(3,))
189 d21(1, 2, **{'c':3})
190 def d02(a=1, b=2): pass
191 d02()
192 d02(1)
193 d02(1, 2)
194 d02(*(1, 2))
195 d02(1, *(2,))
196 d02(1, **{'b':2})
197 d02(**{'a': 1, 'b': 2})
198 def d12(a, b=1, c=2): pass
199 d12(1)
200 d12(1, 2)
201 d12(1, 2, 3)
202 def d22(a, b, c=1, d=2): pass
203 d22(1, 2)
204 d22(1, 2, 3)
205 d22(1, 2, 3, 4)
206 def d01v(a=1, *rest): pass
207 d01v()
208 d01v(1)
209 d01v(1, 2)
210 d01v(*(1, 2, 3, 4))
211 d01v(*(1,))
212 d01v(**{'a':2})
213 def d11v(a, b=1, *rest): pass
214 d11v(1)
215 d11v(1, 2)
216 d11v(1, 2, 3)
217 def d21v(a, b, c=1, *rest): pass
218 d21v(1, 2)
219 d21v(1, 2, 3)
220 d21v(1, 2, 3, 4)
221 d21v(*(1, 2, 3, 4))
222 d21v(1, 2, **{'c': 3})
223 def d02v(a=1, b=2, *rest): pass
224 d02v()
225 d02v(1)
226 d02v(1, 2)
227 d02v(1, 2, 3)
228 d02v(1, *(2, 3, 4))
229 d02v(**{'a': 1, 'b': 2})
230 def d12v(a, b=1, c=2, *rest): pass
231 d12v(1)
232 d12v(1, 2)
233 d12v(1, 2, 3)
234 d12v(1, 2, 3, 4)
235 d12v(*(1, 2, 3, 4))
236 d12v(1, 2, *(3, 4, 5))
237 d12v(1, *(2,), **{'c': 3})
238 def d22v(a, b, c=1, d=2, *rest): pass
239 d22v(1, 2)
240 d22v(1, 2, 3)
241 d22v(1, 2, 3, 4)
242 d22v(1, 2, 3, 4, 5)
243 d22v(*(1, 2, 3, 4))
244 d22v(1, 2, *(3, 4, 5))
245 d22v(1, *(2, 3), **{'d': 4})
247 ### lambdef: 'lambda' [varargslist] ':' test
248 print 'lambdef'
249 l1 = lambda : 0
250 verify(l1() == 0)
251 l2 = lambda : a[d] # XXX just testing the expression
252 l3 = lambda : [2 < x for x in [-1, 3, 0L]]
253 verify(l3() == [0, 1, 0])
254 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
255 verify(l4() == 1)
256 l5 = lambda x, y, z=2: x + y + z
257 verify(l5(1, 2) == 5)
258 verify(l5(1, 2, 3) == 6)
259 check_syntax("lambda x: x = 2")
261 ### stmt: simple_stmt | compound_stmt
262 # Tested below
264 ### simple_stmt: small_stmt (';' small_stmt)* [';']
265 print 'simple_stmt'
266 x = 1; pass; del x
268 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
269 # Tested below
271 print 'expr_stmt' # (exprlist '=')* exprlist
273 1, 2, 3
274 x = 1
275 x = 1, 2, 3
276 x = y = z = 1, 2, 3
277 x, y, z = 1, 2, 3
278 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
279 # NB these variables are deleted below
281 check_syntax("x + 1 = 1")
282 check_syntax("a + 1 = b + 2")
284 print 'print_stmt' # 'print' (test ',')* [test]
285 print 1, 2, 3
286 print 1, 2, 3,
287 print
288 print 0 or 1, 0 or 1,
289 print 0 or 1
291 print 'extended print_stmt' # 'print' '>>' test ','
292 import sys
293 print >> sys.stdout, 1, 2, 3
294 print >> sys.stdout, 1, 2, 3,
295 print >> sys.stdout
296 print >> sys.stdout, 0 or 1, 0 or 1,
297 print >> sys.stdout, 0 or 1
299 # test printing to an instance
300 class Gulp:
301 def write(self, msg): pass
303 gulp = Gulp()
304 print >> gulp, 1, 2, 3
305 print >> gulp, 1, 2, 3,
306 print >> gulp
307 print >> gulp, 0 or 1, 0 or 1,
308 print >> gulp, 0 or 1
310 # test print >> None
311 def driver():
312 oldstdout = sys.stdout
313 sys.stdout = Gulp()
314 try:
315 tellme(Gulp())
316 tellme()
317 finally:
318 sys.stdout = oldstdout
320 # we should see this once
321 def tellme(file=sys.stdout):
322 print >> file, 'hello world'
324 driver()
326 # we should not see this at all
327 def tellme(file=None):
328 print >> file, 'goodbye universe'
330 driver()
332 # syntax errors
333 check_syntax('print ,')
334 check_syntax('print >> x,')
336 print 'del_stmt' # 'del' exprlist
337 del abc
338 del x, y, (z, xyz)
340 print 'pass_stmt' # 'pass'
341 pass
343 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
344 # Tested below
346 print 'break_stmt' # 'break'
347 while 1: break
349 print 'continue_stmt' # 'continue'
350 i = 1
351 while i: i = 0; continue
353 msg = ""
354 while not msg:
355 msg = "continue + try/except ok"
356 try:
357 continue
358 msg = "continue failed to continue inside try"
359 except:
360 msg = "continue inside try called except block"
361 print msg
363 msg = ""
364 while not msg:
365 msg = "finally block not called"
366 try:
367 continue
368 finally:
369 msg = "continue + try/finally ok"
370 print msg
373 # This test warrants an explanation. It is a test specifically for SF bugs
374 # #463359 and #462937. The bug is that a 'break' statement executed or
375 # exception raised inside a try/except inside a loop, *after* a continue
376 # statement has been executed in that loop, will cause the wrong number of
377 # arguments to be popped off the stack and the instruction pointer reset to
378 # a very small number (usually 0.) Because of this, the following test
379 # *must* written as a function, and the tracking vars *must* be function
380 # arguments with default values. Otherwise, the test will loop and loop.
382 print "testing continue and break in try/except in loop"
383 def test_break_continue_loop(extra_burning_oil = 1, count=0):
384 big_hippo = 2
385 while big_hippo:
386 count += 1
387 try:
388 if extra_burning_oil and big_hippo == 1:
389 extra_burning_oil -= 1
390 break
391 big_hippo -= 1
392 continue
393 except:
394 raise
395 if count > 2 or big_hippo <> 1:
396 print "continue then break in try/except in loop broken!"
397 test_break_continue_loop()
399 print 'return_stmt' # 'return' [testlist]
400 def g1(): return
401 def g2(): return 1
402 g1()
403 x = g2()
405 print 'raise_stmt' # 'raise' test [',' test]
406 try: raise RuntimeError, 'just testing'
407 except RuntimeError: pass
408 try: raise KeyboardInterrupt
409 except KeyboardInterrupt: pass
411 print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
412 import sys
413 import time, sys
414 from time import time
415 from sys import *
416 from sys import path, argv
418 print 'global_stmt' # 'global' NAME (',' NAME)*
419 def f():
420 global a
421 global a, b
422 global one, two, three, four, five, six, seven, eight, nine, ten
424 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
425 def f():
426 z = None
427 del z
428 exec 'z=1+1\n'
429 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
430 del z
431 exec 'z=1+1'
432 if z != 2: raise TestFailed, 'exec \'z=1+1\''
433 z = None
434 del z
435 import types
436 if hasattr(types, "UnicodeType"):
437 exec r"""if 1:
438 exec u'z=1+1\n'
439 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
440 del z
441 exec u'z=1+1'
442 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
445 g = {}
446 exec 'z = 1' in g
447 if g.has_key('__builtins__'): del g['__builtins__']
448 if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
449 g = {}
450 l = {}
452 import warnings
453 warnings.filterwarnings("ignore", "global statement", module="<string>")
454 exec 'global a; a = 1; b = 2' in g, l
455 if g.has_key('__builtins__'): del g['__builtins__']
456 if l.has_key('__builtins__'): del l['__builtins__']
457 if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
460 print "assert_stmt" # assert_stmt: 'assert' test [',' test]
461 assert 1
462 assert 1, 1
463 assert lambda x:x
464 assert 1, lambda x:x+1
466 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
467 # Tested below
469 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
470 if 1: pass
471 if 1: pass
472 else: pass
473 if 0: pass
474 elif 0: pass
475 if 0: pass
476 elif 0: pass
477 elif 0: pass
478 elif 0: pass
479 else: pass
481 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
482 while 0: pass
483 while 0: pass
484 else: pass
486 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
487 for i in 1, 2, 3: pass
488 for i, j, k in (): pass
489 else: pass
490 class Squares:
491 def __init__(self, max):
492 self.max = max
493 self.sofar = []
494 def __len__(self): return len(self.sofar)
495 def __getitem__(self, i):
496 if not 0 <= i < self.max: raise IndexError
497 n = len(self.sofar)
498 while n <= i:
499 self.sofar.append(n*n)
500 n = n+1
501 return self.sofar[i]
502 n = 0
503 for x in Squares(10): n = n+x
504 if n != 285: raise TestFailed, 'for over growing sequence'
506 print 'try_stmt'
507 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
508 ### | 'try' ':' suite 'finally' ':' suite
509 ### except_clause: 'except' [expr [',' expr]]
510 try:
512 except ZeroDivisionError:
513 pass
514 else:
515 pass
516 try: 1/0
517 except EOFError: pass
518 except TypeError, msg: pass
519 except RuntimeError, msg: pass
520 except: pass
521 else: pass
522 try: 1/0
523 except (EOFError, TypeError, ZeroDivisionError): pass
524 try: 1/0
525 except (EOFError, TypeError, ZeroDivisionError), msg: pass
526 try: pass
527 finally: pass
529 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
530 if 1: pass
531 if 1:
532 pass
533 if 1:
537 pass
538 pass
540 pass
543 print 'test'
544 ### and_test ('or' and_test)*
545 ### and_test: not_test ('and' not_test)*
546 ### not_test: 'not' not_test | comparison
547 if not 1: pass
548 if 1 and 1: pass
549 if 1 or 1: pass
550 if not not not 1: pass
551 if not 1 and 1 and 1: pass
552 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
554 print 'comparison'
555 ### comparison: expr (comp_op expr)*
556 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
557 if 1: pass
558 x = (1 == 1)
559 if 1 == 1: pass
560 if 1 != 1: pass
561 if 1 <> 1: pass
562 if 1 < 1: pass
563 if 1 > 1: pass
564 if 1 <= 1: pass
565 if 1 >= 1: pass
566 if 1 is 1: pass
567 if 1 is not 1: pass
568 if 1 in (): pass
569 if 1 not in (): pass
570 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
572 print 'binary mask ops'
573 x = 1 & 1
574 x = 1 ^ 1
575 x = 1 | 1
577 print 'shift ops'
578 x = 1 << 1
579 x = 1 >> 1
580 x = 1 << 1 >> 1
582 print 'additive ops'
583 x = 1
584 x = 1 + 1
585 x = 1 - 1 - 1
586 x = 1 - 1 + 1 - 1 + 1
588 print 'multiplicative ops'
589 x = 1 * 1
590 x = 1 / 1
591 x = 1 % 1
592 x = 1 / 1 * 1 % 1
594 print 'unary ops'
595 x = +1
596 x = -1
597 x = ~1
598 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
599 x = -1*1/1 + 1*1 - ---1*1
601 print 'selectors'
602 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
603 ### subscript: expr | [expr] ':' [expr]
604 f1()
605 f2(1)
606 f2(1,)
607 f3(1, 2)
608 f3(1, 2,)
609 f4(1, (2, (3, 4)))
610 v0()
611 v0(1)
612 v0(1,)
613 v0(1,2)
614 v0(1,2,3,4,5,6,7,8,9,0)
615 v1(1)
616 v1(1,)
617 v1(1,2)
618 v1(1,2,3)
619 v1(1,2,3,4,5,6,7,8,9,0)
620 v2(1,2)
621 v2(1,2,3)
622 v2(1,2,3,4)
623 v2(1,2,3,4,5,6,7,8,9,0)
624 v3(1,(2,3))
625 v3(1,(2,3),4)
626 v3(1,(2,3),4,5,6,7,8,9,0)
627 print
628 import sys, time
629 c = sys.path[0]
630 x = time.time()
631 x = sys.modules['time'].time()
632 a = '01234'
633 c = a[0]
634 c = a[-1]
635 s = a[0:5]
636 s = a[:5]
637 s = a[0:]
638 s = a[:]
639 s = a[-5:]
640 s = a[:-1]
641 s = a[-4:-3]
643 print 'atoms'
644 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
645 ### dictmaker: test ':' test (',' test ':' test)* [',']
647 x = (1)
648 x = (1 or 2 or 3)
649 x = (1 or 2 or 3, 2, 3)
651 x = []
652 x = [1]
653 x = [1 or 2 or 3]
654 x = [1 or 2 or 3, 2, 3]
655 x = []
657 x = {}
658 x = {'one': 1}
659 x = {'one': 1,}
660 x = {'one' or 'two': 1 or 2}
661 x = {'one': 1, 'two': 2}
662 x = {'one': 1, 'two': 2,}
663 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
665 x = `x`
666 x = `1 or 2 or 3`
667 x = x
668 x = 'x'
669 x = 123
671 ### exprlist: expr (',' expr)* [',']
672 ### testlist: test (',' test)* [',']
673 # These have been exercised enough above
675 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
676 class B: pass
677 class C1(B): pass
678 class C2(B): pass
679 class D(C1, C2, B): pass
680 class C:
681 def meth1(self): pass
682 def meth2(self, arg): pass
683 def meth3(self, a1, a2): pass
685 # list comprehension tests
686 nums = [1, 2, 3, 4, 5]
687 strs = ["Apple", "Banana", "Coconut"]
688 spcs = [" Apple", " Banana ", "Coco nut "]
690 print [s.strip() for s in spcs]
691 print [3 * x for x in nums]
692 print [x for x in nums if x > 2]
693 print [(i, s) for i in nums for s in strs]
694 print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
696 def test_in_func(l):
697 return [None < x < 3 for x in l if x > 2]
699 print test_in_func(nums)
701 def test_nested_front():
702 print [[y for y in [x, x + 1]] for x in [1,3,5]]
704 test_nested_front()
706 check_syntax("[i, s for i in nums for s in strs]")
707 check_syntax("[x if y]")
709 suppliers = [
710 (1, "Boeing"),
711 (2, "Ford"),
712 (3, "Macdonalds")
715 parts = [
716 (10, "Airliner"),
717 (20, "Engine"),
718 (30, "Cheeseburger")
721 suppart = [
722 (1, 10), (1, 20), (2, 20), (3, 30)
725 print [
726 (sname, pname)
727 for (sno, sname) in suppliers
728 for (pno, pname) in parts
729 for (sp_sno, sp_pno) in suppart
730 if sno == sp_sno and pno == sp_pno