1 # Python test set -- part 1, grammar.
2 # This just tests whether the parser accepts them all.
4 from test_support
import *
11 print '1.1.1 Backslashes'
13 # Backslash means line continuation:
16 if x
!= 2: raise TestFailed
, 'backslash for line continuation'
18 # Backslash does not means continuation in comments :\
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'
29 from sys
import maxint
32 if maxint
== 2147483647:
33 if -2147483647-1 != 020000000000: raise TestFailed
, 'max negative int'
35 if 037777777777 != -1: raise TestFailed
, 'oct -1'
36 if 0xffffffff != -1: raise TestFailed
, 'hex -1'
37 for s
in '2147483648', '040000000000', '0x100000000':
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':
52 print "OverflowError on huge integer literal " + `s`
54 print 'Weird maxint value', maxint
56 print '1.1.2.2 Long integers'
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'
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
)
97 y
= '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
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')
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
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',
157 verify(f5
.func_code
.co_varnames
==
158 ('(compound, first)', 'two', 'compound', 'first'))
160 verify(f4
.func_code
.co_varnames
== ('two', '.2', 'compound',
162 verify(f5
.func_code
.co_varnames
== ('.0', 'two', 'compound', 'first'))
163 def a1(one_arg
,): pass
164 def a2(two
, args
,): 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'))
172 verify(v3
.func_code
.co_varnames
== ('a', '.2', 'rest', 'b', 'c'))
173 verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
179 def d11(a
, b
=1): pass
183 def d21(a
, b
, c
=1): pass
190 def d02(a
=1, b
=2): pass
197 d02(**{'a': 1, 'b': 2})
198 def d12(a
, b
=1, c
=2): pass
202 def d22(a
, b
, c
=1, d
=2): pass
206 def d01v(a
=1, *rest
): pass
213 def d11v(a
, b
=1, *rest
): pass
217 def d21v(a
, b
, c
=1, *rest
): pass
222 d21v(1, 2, **{'c': 3})
223 def d02v(a
=1, b
=2, *rest
): pass
229 d02v(**{'a': 1, 'b': 2})
230 def d12v(a
, b
=1, c
=2, *rest
): pass
236 d12v(1, 2, *(3, 4, 5))
237 d12v(1, *(2,), **{'c': 3})
238 def d22v(a
, b
, c
=1, d
=2, *rest
): pass
244 d22v(1, 2, *(3, 4, 5))
245 d22v(1, *(2, 3), **{'d': 4})
247 ### lambdef: 'lambda' [varargslist] ':' test
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()
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
264 ### simple_stmt: small_stmt (';' small_stmt)* [';']
268 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
271 print 'expr_stmt' # (exprlist '=')* exprlist
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]
288 print 0 or 1, 0 or 1,
291 print 'extended print_stmt' # 'print' '>>' test ','
293 print >> sys
.stdout
, 1, 2, 3
294 print >> sys
.stdout
, 1, 2, 3,
296 print >> sys
.stdout
, 0 or 1, 0 or 1,
297 print >> sys
.stdout
, 0 or 1
299 # test printing to an instance
301 def write(self
, msg
): pass
304 print >> gulp
, 1, 2, 3
305 print >> gulp
, 1, 2, 3,
307 print >> gulp
, 0 or 1, 0 or 1,
308 print >> gulp
, 0 or 1
312 oldstdout
= sys
.stdout
318 sys
.stdout
= oldstdout
320 # we should see this once
321 def tellme(file=sys
.stdout
):
322 print >> file, 'hello world'
326 # we should not see this at all
327 def tellme(file=None):
328 print >> file, 'goodbye universe'
333 check_syntax('print ,')
334 check_syntax('print >> x,')
336 print 'del_stmt' # 'del' exprlist
340 print 'pass_stmt' # 'pass'
343 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
346 print 'break_stmt' # 'break'
349 print 'continue_stmt' # 'continue'
351 while i
: i
= 0; continue
355 msg
= "continue + try/except ok"
358 msg
= "continue failed to continue inside try"
360 msg
= "continue inside try called except block"
365 msg
= "finally block not called"
369 msg
= "continue + try/finally ok"
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):
388 if extra_burning_oil
and big_hippo
== 1:
389 extra_burning_oil
-= 1
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]
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)*)
414 from time
import time
416 from sys
import path
, argv
418 print 'global_stmt' # 'global' NAME (',' NAME)*
422 global one
, two
, three
, four
, five
, six
, seven
, eight
, nine
, ten
424 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
429 if z
!= 2: raise TestFailed
, 'exec \'z=1+1\'\\n'
432 if z
!= 2: raise TestFailed
, 'exec \'z=1+1\''
436 if hasattr(types
, "UnicodeType"):
439 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
442 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
447 if g
.has_key('__builtins__'): del g
['__builtins__']
448 if g
!= {'z': 1}: raise TestFailed
, 'exec \'z = 1\' in g'
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]
464 assert 1, lambda x
:x
+1
466 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
469 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
481 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
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
491 def __init__(self
, max):
494 def __len__(self
): return len(self
.sofar
)
495 def __getitem__(self
, i
):
496 if not 0 <= i
< self
.max: raise IndexError
499 self
.sofar
.append(n
*n
)
503 for x
in Squares(10): n
= n
+x
504 if n
!= 285: raise TestFailed
, 'for over growing sequence'
507 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
508 ### | 'try' ':' suite 'finally' ':' suite
509 ### except_clause: 'except' [expr [',' expr]]
512 except ZeroDivisionError:
517 except EOFError: pass
518 except TypeError, msg
: pass
519 except RuntimeError, msg
: pass
523 except (EOFError, TypeError, ZeroDivisionError): pass
525 except (EOFError, TypeError, ZeroDivisionError), msg
: pass
529 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
544 ### and_test ('or' and_test)*
545 ### and_test: not_test ('and' not_test)*
546 ### not_test: 'not' not_test | comparison
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
555 ### comparison: expr (comp_op expr)*
556 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
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'
586 x
= 1 - 1 + 1 - 1 + 1
588 print 'multiplicative ops'
598 x
= ~
1 ^
1 & 1 |
1 & 1 ^
-1
599 x
= -1*1/1 + 1*1 - ---1*1
602 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
603 ### subscript: expr | [expr] ':' [expr]
614 v0(1,2,3,4,5,6,7,8,9,0)
619 v1(1,2,3,4,5,6,7,8,9,0)
623 v2(1,2,3,4,5,6,7,8,9,0)
626 v3(1,(2,3),4,5,6,7,8,9,0)
631 x
= sys
.modules
['time'].time()
644 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
645 ### dictmaker: test ':' test (',' test ':' test)* [',']
649 x
= (1 or 2 or 3, 2, 3)
654 x
= [1 or 2 or 3, 2, 3]
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}
671 ### exprlist: expr (',' expr)* [',']
672 ### testlist: test (',' test)* [',']
673 # These have been exercised enough above
675 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
679 class D(C1
, C2
, B
): pass
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
]]
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]]
706 check_syntax("[i, s for i in nums for s in strs]")
707 check_syntax("[x if y]")
722 (1, 10), (1, 20), (2, 20), (3, 30)
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