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
144 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
145 ### fpdef: NAME | '(' fplist ')'
146 ### fplist: fpdef (',' fpdef)* [',']
148 def f2(one_argument
): pass
149 def f3(two
, arguments
): pass
150 def f4(two
, (compound
, (argument
, list))): pass
151 def a1(one_arg
,): pass
152 def a2(two
, args
,): pass
154 def v1(a
, *rest
): pass
155 def v2(a
, b
, *rest
): pass
156 def v3(a
, (b
, c
), *rest
): pass
160 def d11(a
, b
=1): pass
163 def d21(a
, b
, c
=1): pass
166 def d02(a
=1, b
=2): pass
170 def d12(a
, b
=1, c
=2): pass
174 def d22(a
, b
, c
=1, d
=2): pass
178 def d01v(a
=1, *rest
): pass
182 def d11v(a
, b
=1, *rest
): pass
186 def d21v(a
, b
, c
=1, *rest
): pass
190 def d02v(a
=1, b
=2, *rest
): pass
195 def d12v(a
, b
=1, c
=2, *rest
): pass
200 def d22v(a
, b
, c
=1, d
=2, *rest
): pass
206 ### stmt: simple_stmt | compound_stmt
209 ### simple_stmt: small_stmt (';' small_stmt)* [';']
213 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
216 print 'expr_stmt' # (exprlist '=')* exprlist
223 abc
= a
, b
, c
= x
, y
, z
= xyz
= 1, 2, (3, 4)
224 # NB these variables are deleted below
226 print 'print_stmt' # 'print' (test ',')* [test]
230 print 0 or 1, 0 or 1,
233 print 'del_stmt' # 'del' exprlist
237 print 'pass_stmt' # 'pass'
240 print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
243 print 'break_stmt' # 'break'
246 print 'continue_stmt' # 'continue'
248 while i
: i
= 0; continue
250 print 'return_stmt' # 'return' [testlist]
256 print 'raise_stmt' # 'raise' test [',' test]
257 try: raise RuntimeError, 'just testing'
258 except RuntimeError: pass
259 try: raise KeyboardInterrupt
260 except KeyboardInterrupt: pass
262 print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
265 from time
import time
267 from sys
import path
, argv
269 print 'global_stmt' # 'global' NAME (',' NAME)*
273 global one
, two
, three
, four
, five
, six
, seven
, eight
, nine
, ten
275 print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
280 if z
<> 2: raise TestFailed
, 'exec \'z=1+1\'\\n'
283 if z
<> 2: raise TestFailed
, 'exec \'z=1+1\''
287 if g
.has_key('__builtins__'): del g
['__builtins__']
288 if g
<> {'z': 1}: raise TestFailed
, 'exec \'z = 1\' in g'
291 exec 'global a; a = 1; b = 2' in g
, l
292 if g
.has_key('__builtins__'): del g
['__builtins__']
293 if l
.has_key('__builtins__'): del l
['__builtins__']
294 if (g
, l
) <> ({'a':1}, {'b':2}): raise TestFailed
, 'exec ... in g, l'
297 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
300 print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
312 print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
317 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
318 for i
in 1, 2, 3: pass
319 for i
, j
, k
in (): pass
322 def __init__(self
, max):
325 def __len__(self
): return len(self
.sofar
)
326 def __getitem__(self
, i
):
327 if not 0 <= i
< self
.max: raise IndexError
330 self
.sofar
.append(n
*n
)
334 for x
in Squares(10): n
= n
+x
335 if n
!= 285: raise TestFailed
, 'for over growing sequence'
338 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
339 ### | 'try' ':' suite 'finally' ':' suite
340 ### except_clause: 'except' [expr [',' expr]]
343 except ZeroDivisionError:
348 except EOFError: pass
349 except TypeError, msg
: pass
350 except RuntimeError, msg
: pass
354 except (EOFError, TypeError, ZeroDivisionError): pass
356 except (EOFError, TypeError, ZeroDivisionError), msg
: pass
360 print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
375 ### and_test ('or' and_test)*
376 ### and_test: not_test ('and' not_test)*
377 ### not_test: 'not' not_test | comparison
381 if not not not 1: pass
382 if not 1 and 1 and 1: pass
383 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
386 ### comparison: expr (comp_op expr)*
387 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
401 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
403 print 'binary mask ops'
417 x
= 1 - 1 + 1 - 1 + 1
419 print 'multiplicative ops'
429 x
= ~
1 ^
1 & 1 |
1 & 1 ^
-1
430 x
= -1*1/1 + 1*1 - ---1*1
433 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
434 ### subscript: expr | [expr] ':' [expr]
445 v0(1,2,3,4,5,6,7,8,9,0)
450 v1(1,2,3,4,5,6,7,8,9,0)
454 v2(1,2,3,4,5,6,7,8,9,0)
457 v3(1,(2,3),4,5,6,7,8,9,0)
461 x
= sys
.modules
['time'].time()
474 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
475 ### dictmaker: test ':' test (',' test ':' test)* [',']
479 x
= (1 or 2 or 3, 2, 3)
484 x
= [1 or 2 or 3, 2, 3]
490 x
= {'one' or 'two': 1 or 2}
491 x
= {'one': 1, 'two': 2}
492 x
= {'one': 1, 'two': 2,}
493 x
= {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
501 ### exprlist: expr (',' expr)* [',']
502 ### testlist: test (',' test)* [',']
503 # These have been exercised enough above
505 print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
509 class D(C1
, C2
, B
): pass
511 def meth1(self
): pass
512 def meth2(self
, arg
): pass
513 def meth3(self
, a1
, a2
): pass