1 # Python test set -- part 1, grammar.
2 # This just tests whether the parser accepts them all.
4 from test_support
import *
10 print '1.1.1 Backslashes'
12 # Backslash means line continuation:
15 if x
!= 2: raise TestFailed
, 'backslash for line continuation'
17 # Backslash does not means continuation in comments :\
19 if x
!= 0: raise TestFailed
, 'backslash ending comment'
21 print '1.1.2 Numeric literals'
23 print '1.1.2.1 Plain integers'
24 if 0xff != 255: raise TestFailed
, 'hex int'
25 if 0377 != 255: raise TestFailed
, 'octal int'
26 if 2147483647 != 017777777777: raise TestFailed
, 'large positive int'
28 from sys
import maxint
31 if maxint
== 2147483647:
32 if -2147483647-1 != 020000000000: raise TestFailed
, 'max negative int'
34 if 037777777777 != -1: raise TestFailed
, 'oct -1'
35 if 0xffffffff != -1: raise TestFailed
, 'hex -1'
36 for s
in '2147483648', '040000000000', '0x100000000':
41 ## raise TestFailed, \
43 'No OverflowError on huge integer literal ' + `s`
44 elif eval('maxint == 9223372036854775807'):
45 if eval('-9223372036854775807-1 != 01000000000000000000000'):
46 raise TestFailed
, 'max negative int'
47 if eval('01777777777777777777777') != -1: raise TestFailed
, 'oct -1'
48 if eval('0xffffffffffffffff') != -1: raise TestFailed
, 'hex -1'
49 for s
in '9223372036854775808', '02000000000000000000000', \
50 '0x10000000000000000':
56 'No OverflowError on huge integer literal ' + `s`
58 print 'Weird maxint value', maxint
60 print '1.1.2.2 Long integers'
63 x
= 0xffffffffffffffffL
64 x
= 0xffffffffffffffffl
65 x
= 077777777777777777L
66 x
= 077777777777777777l
67 x
= 123456789012345678901234567890L
68 x
= 123456789012345678901234567890l
70 print '1.1.2.3 Floating point'
84 print '1.1.3 String literals'
86 x
= ''; y
= ""; verify(len(x
) == 0 and x
== y
)
87 x
= '\''; y
= "'"; verify(len(x
) == 1 and x
== y
and ord(x
) == 39)
88 x
= '"'; y
= "\""; verify(len(x
) == 1 and x
== y
and ord(x
) == 34)
89 x
= "doesn't \"shrink\" does it"
90 y
= 'doesn\'t "shrink" does it'
91 verify(len(x
) == 24 and x
== y
)
92 x
= "does \"shrink\" doesn't it"
93 y
= 'does "shrink" doesn\'t it'
94 verify(len(x
) == 24 and x
== y
)
101 y
= '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
125 print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
126 # XXX can't test in a script -- this rule is only used when interactive
128 print 'file_input' # (NEWLINE | stmt)* ENDMARKER
129 # Being tested as this very moment this very module
131 print 'expr_input' # testlist NEWLINE
132 # XXX Hard to test -- used only in calls to input()
134 print 'eval_input' # testlist ENDMARKER
135 x
= eval('1, 0 or 1')
138 ### 'def' NAME parameters ':' suite
139 ### parameters: '(' [varargslist] ')'
140 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
141 ### | ('**'|'*' '*') NAME)
142 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
143 ### fpdef: NAME | '(' fplist ')'
144 ### fplist: fpdef (',' fpdef)* [',']
145 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
146 ### argument: [test '='] test # Really [keyword '='] test
151 def f2(one_argument
): pass
152 def f3(two
, arguments
): pass
153 def f4(two
, (compound
, (argument
, list))): pass
154 def f5((compound
, first
), two
): pass
155 verify(f2
.func_code
.co_varnames
== ('one_argument',))
156 verify(f3
.func_code
.co_varnames
== ('two', 'arguments'))
157 verify(f4
.func_code
.co_varnames
== ('two', '.2', 'compound', 'argument',
159 verify(f5
.func_code
.co_varnames
== ('.0', 'two', 'compound', 'first'))
160 def a1(one_arg
,): pass
161 def a2(two
, args
,): pass
163 def v1(a
, *rest
): pass
164 def v2(a
, b
, *rest
): pass
165 def v3(a
, (b
, c
), *rest
): return a
, b
, c
, rest
166 verify(v3
.func_code
.co_varnames
== ('a', '.2', 'rest', 'b', 'c'))
167 verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
173 def d11(a
, b
=1): pass
177 def d21(a
, b
, c
=1): pass
184 def d02(a
=1, b
=2): pass
191 d02(**{'a': 1, 'b': 2})
192 def d12(a
, b
=1, c
=2): pass
196 def d22(a
, b
, c
=1, d
=2): pass
200 def d01v(a
=1, *rest
): pass
207 def d11v(a
, b
=1, *rest
): pass
211 def d21v(a
, b
, c
=1, *rest
): pass
216 d21v(1, 2, **{'c': 3})
217 def d02v(a
=1, b
=2, *rest
): pass
223 d02v(**{'a': 1, 'b': 2})
224 def d12v(a
, b
=1, c
=2, *rest
): pass
230 d12v(1, 2, *(3, 4, 5))
231 d12v(1, *(2,), **{'c': 3})
232 def d22v(a
, b
, c
=1, d
=2, *rest
): pass
238 d22v(1, 2, *(3, 4, 5))
239 d22v(1, *(2, 3), **{'d': 4})
241 ### lambdef: 'lambda' [varargslist] ':' test
245 l2
= lambda : a
[d
] # XXX just testing the expression
246 l3
= lambda : [2 < x
for x
in [-1, 3, 0L]]
247 verify(l3() == [0, 1, 0])
248 l4
= lambda x
= lambda y
= lambda z
=1 : z
: y() : x()
250 l5
= lambda x
, y
, z
=2: x
+ y
+ z
251 verify(l5(1, 2) == 5)
252 verify(l5(1, 2, 3) == 6)
253 check_syntax("lambda x: x = 2")
255 ### stmt: simple_stmt | compound_stmt
258 ### simple_stmt: small_stmt (';' small_stmt)* [';']
262 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
265 print 'expr_stmt' # (exprlist '=')* exprlist
272 abc
= a
, b
, c
= x
, y
, z
= xyz
= 1, 2, (3, 4)
273 # NB these variables are deleted below
275 check_syntax("x + 1 = 1")
276 check_syntax("a + 1 = b + 2")
278 print 'print_stmt' # 'print' (test ',')* [test]
282 print 0 or 1, 0 or 1,
285 print 'extended print_stmt' # 'print' '>>' test ','
287 print >> sys
.stdout
, 1, 2, 3
288 print >> sys
.stdout
, 1, 2, 3,
290 print >> sys
.stdout
, 0 or 1, 0 or 1,
291 print >> sys
.stdout
, 0 or 1
293 # test printing to an instance
295 def write(self
, msg
): pass
298 print >> gulp
, 1, 2, 3
299 print >> gulp
, 1, 2, 3,
301 print >> gulp
, 0 or 1, 0 or 1,
302 print >> gulp
, 0 or 1
306 oldstdout
= sys
.stdout
312 sys
.stdout
= oldstdout
314 # we should see this once
315 def tellme(file=sys
.stdout
):
316 print >> file, 'hello world'
320 # we should not see this at all
321 def tellme(file=None):
322 print >> file, 'goodbye universe'
327 check_syntax('print ,')
328 check_syntax('print >> x,')
330 print 'del_stmt' # 'del' exprlist
334 print 'pass_stmt' # 'pass'
337 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
340 print 'break_stmt' # 'break'
343 print 'continue_stmt' # 'continue'
345 while i
: i
= 0; continue
349 msg
= "continue + try/except ok"
352 msg
= "continue failed to continue inside try"
354 msg
= "continue inside try called except block"
359 msg
= "finally block not called"
363 msg
= "continue + try/finally ok"
366 print 'return_stmt' # 'return' [testlist]
372 print 'raise_stmt' # 'raise' test [',' test]
373 try: raise RuntimeError, 'just testing'
374 except RuntimeError: pass
375 try: raise KeyboardInterrupt
376 except KeyboardInterrupt: pass
378 print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
381 from time
import time
383 from sys
import path
, argv
385 print 'global_stmt' # 'global' NAME (',' NAME)*
389 global one
, two
, three
, four
, five
, six
, seven
, eight
, nine
, ten
391 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
396 if z
!= 2: raise TestFailed
, 'exec \'z=1+1\'\\n'
399 if z
!= 2: raise TestFailed
, 'exec \'z=1+1\''
403 if z
!= 2: raise TestFailed
, 'exec u\'z=1+1\'\\n'
406 if z
!= 2: raise TestFailed
, 'exec u\'z=1+1\''
410 if g
.has_key('__builtins__'): del g
['__builtins__']
411 if g
!= {'z': 1}: raise TestFailed
, 'exec \'z = 1\' in g'
416 warnings
.filterwarnings("ignore", "global statement", module
="<string>")
417 exec 'global a; a = 1; b = 2' in g
, l
418 if g
.has_key('__builtins__'): del g
['__builtins__']
419 if l
.has_key('__builtins__'): del l
['__builtins__']
420 if (g
, l
) != ({'a':1}, {'b':2}): raise TestFailed
, 'exec ... in g (%s), l (%s)' %(g
,l
)
423 print "assert_stmt" # assert_stmt: 'assert' test [',' test]
427 assert 1, lambda x
:x
+1
429 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
432 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
444 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
449 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
450 for i
in 1, 2, 3: pass
451 for i
, j
, k
in (): pass
454 def __init__(self
, max):
457 def __len__(self
): return len(self
.sofar
)
458 def __getitem__(self
, i
):
459 if not 0 <= i
< self
.max: raise IndexError
462 self
.sofar
.append(n
*n
)
466 for x
in Squares(10): n
= n
+x
467 if n
!= 285: raise TestFailed
, 'for over growing sequence'
470 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
471 ### | 'try' ':' suite 'finally' ':' suite
472 ### except_clause: 'except' [expr [',' expr]]
475 except ZeroDivisionError:
480 except EOFError: pass
481 except TypeError, msg
: pass
482 except RuntimeError, msg
: pass
486 except (EOFError, TypeError, ZeroDivisionError): pass
488 except (EOFError, TypeError, ZeroDivisionError), msg
: pass
492 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
507 ### and_test ('or' and_test)*
508 ### and_test: not_test ('and' not_test)*
509 ### not_test: 'not' not_test | comparison
513 if not not not 1: pass
514 if not 1 and 1 and 1: pass
515 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
518 ### comparison: expr (comp_op expr)*
519 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
533 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
535 print 'binary mask ops'
549 x
= 1 - 1 + 1 - 1 + 1
551 print 'multiplicative ops'
561 x
= ~
1 ^
1 & 1 |
1 & 1 ^
-1
562 x
= -1*1/1 + 1*1 - ---1*1
565 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
566 ### subscript: expr | [expr] ':' [expr]
577 v0(1,2,3,4,5,6,7,8,9,0)
582 v1(1,2,3,4,5,6,7,8,9,0)
586 v2(1,2,3,4,5,6,7,8,9,0)
589 v3(1,(2,3),4,5,6,7,8,9,0)
594 x
= sys
.modules
['time'].time()
607 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
608 ### dictmaker: test ':' test (',' test ':' test)* [',']
612 x
= (1 or 2 or 3, 2, 3)
617 x
= [1 or 2 or 3, 2, 3]
623 x
= {'one' or 'two': 1 or 2}
624 x
= {'one': 1, 'two': 2}
625 x
= {'one': 1, 'two': 2,}
626 x
= {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
634 ### exprlist: expr (',' expr)* [',']
635 ### testlist: test (',' test)* [',']
636 # These have been exercised enough above
638 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
642 class D(C1
, C2
, B
): pass
644 def meth1(self
): pass
645 def meth2(self
, arg
): pass
646 def meth3(self
, a1
, a2
): pass
648 # list comprehension tests
649 nums
= [1, 2, 3, 4, 5]
650 strs
= ["Apple", "Banana", "Coconut"]
651 spcs
= [" Apple", " Banana ", "Coco nut "]
653 print [s
.strip() for s
in spcs
]
654 print [3 * x
for x
in nums
]
655 print [x
for x
in nums
if x
> 2]
656 print [(i
, s
) for i
in nums
for s
in strs
]
657 print [(i
, s
) for i
in nums
for s
in [f
for f
in strs
if "n" in f
]]
660 return [None < x
< 3 for x
in l
if x
> 2]
662 print test_in_func(nums
)
664 def test_nested_front():
665 print [[y
for y
in [x
, x
+ 1]] for x
in [1,3,5]]
669 check_syntax("[i, s for i in nums for s in strs]")
670 check_syntax("[x if y]")
685 (1, 10), (1, 20), (2, 20), (3, 30)
690 for (sno
, sname
) in suppliers
691 for (pno
, pname
) in parts
692 for (sp_sno
, sp_pno
) in suppart
693 if sno
== sp_sno
and pno
== sp_pno