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'
87 ## if not s: raise TestFailed, 'see traceback'
89 x
= ''; y
= ""; assert(len(x
) == 0 and x
== y
)
90 x
= '\''; y
= "'"; assert(len(x
) == 1 and x
== y
and ord(x
) == 39)
91 x
= '"'; y
= "\""; assert(len(x
) == 1 and x
== y
and ord(x
) == 34)
92 x
= "doesn't \"shrink\" does it"
93 y
= 'doesn\'t "shrink" does it'
94 assert(len(x
) == 24 and x
== y
)
95 x
= "does \"shrink\" doesn't it"
96 y
= 'does "shrink" doesn\'t it'
97 assert(len(x
) == 24 and x
== y
)
104 y
= '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
128 print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
129 # XXX can't test in a script -- this rule is only used when interactive
131 print 'file_input' # (NEWLINE | stmt)* ENDMARKER
132 # Being tested as this very moment this very module
134 print 'expr_input' # testlist NEWLINE
135 # XXX Hard to test -- used only in calls to input()
137 print 'eval_input' # testlist ENDMARKER
138 x
= eval('1, 0 or 1')
141 ### 'def' NAME parameters ':' suite
142 ### parameters: '(' [varargslist] ')'
143 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144 ### | ('**'|'*' '*') NAME)
145 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
146 ### fpdef: NAME | '(' fplist ')'
147 ### fplist: fpdef (',' fpdef)* [',']
148 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
149 ### argument: [test '='] test # Really [keyword '='] test
154 def f2(one_argument
): pass
155 def f3(two
, arguments
): pass
156 def f4(two
, (compound
, (argument
, list))): pass
157 def a1(one_arg
,): pass
158 def a2(two
, args
,): pass
160 def v1(a
, *rest
): pass
161 def v2(a
, b
, *rest
): pass
162 def v3(a
, (b
, c
), *rest
): pass
168 def d11(a
, b
=1): pass
172 def d21(a
, b
, c
=1): pass
179 def d02(a
=1, b
=2): pass
186 d02(**{'a': 1, 'b': 2})
187 def d12(a
, b
=1, c
=2): pass
191 def d22(a
, b
, c
=1, d
=2): pass
195 def d01v(a
=1, *rest
): pass
202 def d11v(a
, b
=1, *rest
): pass
206 def d21v(a
, b
, c
=1, *rest
): pass
211 d21v(1, 2, **{'c': 3})
212 def d02v(a
=1, b
=2, *rest
): pass
218 d02v(**{'a': 1, 'b': 2})
219 def d12v(a
, b
=1, c
=2, *rest
): pass
225 d12v(1, 2, *(3, 4, 5))
226 d12v(1, *(2,), **{'c': 3})
227 def d22v(a
, b
, c
=1, d
=2, *rest
): pass
233 d22v(1, 2, *(3, 4, 5))
234 d22v(1, *(2, 3), **{'d': 4})
236 ### stmt: simple_stmt | compound_stmt
239 ### simple_stmt: small_stmt (';' small_stmt)* [';']
243 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
246 print 'expr_stmt' # (exprlist '=')* exprlist
253 abc
= a
, b
, c
= x
, y
, z
= xyz
= 1, 2, (3, 4)
254 # NB these variables are deleted below
256 print 'print_stmt' # 'print' (test ',')* [test]
260 print 0 or 1, 0 or 1,
263 print 'del_stmt' # 'del' exprlist
267 print 'pass_stmt' # 'pass'
270 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
273 print 'break_stmt' # 'break'
276 print 'continue_stmt' # 'continue'
278 while i
: i
= 0; continue
280 print 'return_stmt' # 'return' [testlist]
286 print 'raise_stmt' # 'raise' test [',' test]
287 try: raise RuntimeError, 'just testing'
288 except RuntimeError: pass
289 try: raise KeyboardInterrupt
290 except KeyboardInterrupt: pass
292 print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
295 from time
import time
297 from sys
import path
, argv
299 print 'global_stmt' # 'global' NAME (',' NAME)*
303 global one
, two
, three
, four
, five
, six
, seven
, eight
, nine
, ten
305 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
310 if z
<> 2: raise TestFailed
, 'exec \'z=1+1\'\\n'
313 if z
<> 2: raise TestFailed
, 'exec \'z=1+1\''
317 if g
.has_key('__builtins__'): del g
['__builtins__']
318 if g
<> {'z': 1}: raise TestFailed
, 'exec \'z = 1\' in g'
321 exec 'global a; a = 1; b = 2' in g
, l
322 if g
.has_key('__builtins__'): del g
['__builtins__']
323 if l
.has_key('__builtins__'): del l
['__builtins__']
324 if (g
, l
) <> ({'a':1}, {'b':2}): raise TestFailed
, 'exec ... in g, l'
327 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
330 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
342 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
347 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
348 for i
in 1, 2, 3: pass
349 for i
, j
, k
in (): pass
352 def __init__(self
, max):
355 def __len__(self
): return len(self
.sofar
)
356 def __getitem__(self
, i
):
357 if not 0 <= i
< self
.max: raise IndexError
360 self
.sofar
.append(n
*n
)
364 for x
in Squares(10): n
= n
+x
365 if n
!= 285: raise TestFailed
, 'for over growing sequence'
368 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
369 ### | 'try' ':' suite 'finally' ':' suite
370 ### except_clause: 'except' [expr [',' expr]]
373 except ZeroDivisionError:
378 except EOFError: pass
379 except TypeError, msg
: pass
380 except RuntimeError, msg
: pass
384 except (EOFError, TypeError, ZeroDivisionError): pass
386 except (EOFError, TypeError, ZeroDivisionError), msg
: pass
390 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
405 ### and_test ('or' and_test)*
406 ### and_test: not_test ('and' not_test)*
407 ### not_test: 'not' not_test | comparison
411 if not not not 1: pass
412 if not 1 and 1 and 1: pass
413 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
416 ### comparison: expr (comp_op expr)*
417 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
431 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
433 print 'binary mask ops'
447 x
= 1 - 1 + 1 - 1 + 1
449 print 'multiplicative ops'
459 x
= ~
1 ^
1 & 1 |
1 & 1 ^
-1
460 x
= -1*1/1 + 1*1 - ---1*1
463 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
464 ### subscript: expr | [expr] ':' [expr]
475 v0(1,2,3,4,5,6,7,8,9,0)
480 v1(1,2,3,4,5,6,7,8,9,0)
484 v2(1,2,3,4,5,6,7,8,9,0)
487 v3(1,(2,3),4,5,6,7,8,9,0)
492 x
= sys
.modules
['time'].time()
505 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
506 ### dictmaker: test ':' test (',' test ':' test)* [',']
510 x
= (1 or 2 or 3, 2, 3)
515 x
= [1 or 2 or 3, 2, 3]
521 x
= {'one' or 'two': 1 or 2}
522 x
= {'one': 1, 'two': 2}
523 x
= {'one': 1, 'two': 2,}
524 x
= {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
532 ### exprlist: expr (',' expr)* [',']
533 ### testlist: test (',' test)* [',']
534 # These have been exercised enough above
536 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
540 class D(C1
, C2
, B
): pass
542 def meth1(self
): pass
543 def meth2(self
, arg
): pass
544 def meth3(self
, a1
, a2
): pass