blob: 6cfda0fb816d685e9bfb9def6777be0bbce8ed8d [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 1, grammar.
2# This just tests whether the parser accepts them all.
3
4from test_support import *
5
6print '1. Parser'
7
8print '1.1 Tokens'
9
10print '1.1.1 Backslashes'
11
12# Backslash means line continuation:
13x = 1 \
14+ 1
15if x <> 2: raise TestFailed, 'backslash for line continuation'
16
17# Backslash does not means continuation in comments :\
18x = 0
19if x <> 0: raise TestFailed, 'backslash ending comment'
20
21print '1.1.2 Numeric literals'
22
23print '1.1.2.1 Plain integers'
24if 0xff <> 255: raise TestFailed, 'hex int'
25if 0377 <> 255: raise TestFailed, 'octal int'
26if 2147483647 != 017777777777: raise TestFailed, 'max positive int'
27if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
28# XXX -2147483648
29if 037777777777 != -1: raise TestFailed, 'oct -1'
30if 0xffffffff != -1: raise TestFailed, 'hex -1'
31for s in '2147483648', '040000000000', '0x100000000':
32 try:
33 x = eval(s)
34 except OverflowError:
35 continue
36 raise TestFailed, 'No OverflowError on huge integer literal ' + `s`
37
38print '1.1.2.2 Long integers'
39x = 0L
40x = 0l
41x = 0xffffffffffffffffL
42x = 0xffffffffffffffffl
43x = 077777777777777777L
44x = 077777777777777777l
45x = 123456789012345678901234567890L
46x = 123456789012345678901234567890l
47
48print '1.1.2.3 Floating point'
49x = 3.14
50x = 314.
51x = 0.314
52# XXX x = 000.314
53x = .314
54x = 3e14
55x = 3E14
56x = 3e-14
57x = 3e+14
58x = 3.e14
59x = .3e14
60x = 3.1e4
61
62print '1.2 Grammar'
63
64print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
65# XXX can't test in a script -- this rule is only used when interactive
66
67print 'file_input' # (NEWLINE | stmt)* ENDMARKER
68# Being tested as this very moment this very module
69
70print 'expr_input' # testlist NEWLINE
71# XXX Hard to test -- used only in calls to input()
72
73print 'eval_input' # testlist ENDMARKER
74x = eval('1, 0 or 1')
75
76print 'funcdef'
77### 'def' NAME parameters ':' suite
78### parameters: '(' [varargslist] ')'
Guido van Rossum33693ea1992-04-03 16:33:00 +000079### varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +000080### fpdef: NAME | '(' fplist ')'
81### fplist: fpdef (',' fpdef)* [',']
82def f1(): pass
83def f2(one_argument): pass
84def f3(two, arguments): pass
85def f4(two, (compound, (argument, list))): pass
86def a1(one_arg,): pass
87def a2(two, args,): pass
88def v0(*rest): pass
89def v1(a, *rest): pass
90def v2(a, b, *rest): pass
91def v3(a, (b, c), *rest): pass
92
93### stmt: simple_stmt | compound_stmt
94# Tested below
95
96### simple_stmt: small_stmt (';' small_stmt)* [';']
97print 'simple_stmt'
98x = 1; pass; del x
99
100### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt
101# Tested below
102
103print 'expr_stmt' # (exprlist '=')* exprlist
1041
1051, 2, 3
106x = 1
107x = 1, 2, 3
108x = y = z = 1, 2, 3
109x, y, z = 1, 2, 3
110abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
111# NB these variables are deleted below
112
113print 'print_stmt' # 'print' (test ',')* [test]
114print 1, 2, 3
115print 1, 2, 3,
116print
117print 0 or 1, 0 or 1,
118print 0 or 1
119
120print 'del_stmt' # 'del' exprlist
121del abc
122del x, y, (z, xyz)
123
124print 'pass_stmt' # 'pass'
125pass
126
127print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
128# Tested below
129
130print 'break_stmt' # 'break'
131while 1: break
132
133print 'continue_stmt' # 'continue'
134i = 1
135while i: i = 0; continue
136
137print 'return_stmt' # 'return' [testlist]
138def g1(): return
139def g2(): return 1
140g1()
141x = g2()
142
143print 'raise_stmt' # 'raise' test [',' test]
144try: raise RuntimeError, 'just testing'
145except RuntimeError: pass
146try: raise KeyboardInterrupt
147except KeyboardInterrupt: pass
148
149print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
150[1]
151import sys
152[2]
153import time, math
154[3]
155from time import sleep
156[4]
157from sys import *
158[5]
159from math import sin, cos
160[6]
161
162print 'global_stmt' # 'global' NAME (',' NAME)*
163def f():
164 global a
165 global a, b
166 global one, two, three, four, five, six, seven, eight, nine, ten
167
168### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
169# Tested below
170
171print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
172if 1: pass
173if 1: pass
174else: pass
175if 0: pass
176elif 0: pass
177if 0: pass
178elif 0: pass
179elif 0: pass
180elif 0: pass
181else: pass
182
183print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
184while 0: pass
185while 0: pass
186else: pass
187
188print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
189[1]
190for i in 1, 2, 3: pass
191[2]
192for i, j, k in (): pass
193else: pass
194[3]
195
Guido van Rossum627efd91992-03-31 18:54:11 +0000196print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000197### except_clause: 'except' [expr [',' expr]]
Guido van Rossum3bead091992-01-27 17:00:37 +0000198try: 1/0
199except ZeroDivisionError: pass
200try: 1/0
201except EOFError: pass
202except TypeError, msg: pass
203except RuntimeError, msg: pass
204except: pass
205try: pass
206finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000207
208print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
209if 1: pass
210if 1:
211 pass
212if 1:
213 #
214 #
215 #
216 pass
217 pass
218 #
219 pass
220 #
221
222print 'test'
223### and_test ('or' and_test)*
224### and_test: not_test ('and' not_test)*
225### not_test: 'not' not_test | comparison
226if not 1: pass
227if 1 and 1: pass
228if 1 or 1: pass
229if not not not 1: pass
230if not 1 and 1 and 1: pass
231if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
232
233print 'comparison'
234### comparison: expr (comp_op expr)*
235### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
236if 1: pass
237x = (1 == 1)
238if 1 == 1: pass
239if 1 != 1: pass
240if 1 <> 1: pass
241if 1 < 1: pass
242if 1 > 1: pass
243if 1 <= 1: pass
244if 1 >= 1: pass
245if 1 is 1: pass
246if 1 is not 1: pass
247if 1 in (): pass
248if 1 not in (): pass
249if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 in 1 not in 1 is 1 is not 1: pass
250
251print 'binary mask ops'
252x = 1 & 1
253x = 1 ^ 1
254x = 1 | 1
255
256print 'shift ops'
257x = 1 << 1
258x = 1 >> 1
259x = 1 << 1 >> 1
260
261print 'additive ops'
262x = 1
263x = 1 + 1
264x = 1 - 1 - 1
265x = 1 - 1 + 1 - 1 + 1
266
267print 'multiplicative ops'
268x = 1 * 1
269x = 1 / 1
270x = 1 % 1
271x = 1 / 1 * 1 % 1
272
273print 'unary ops'
274x = +1
275x = -1
276x = ~1
277x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
278x = -1*1/1 + 1*1 - ---1*1
279
280print 'selectors'
281### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
282### subscript: expr | [expr] ':' [expr]
283import sys, time
284c = sys.path[0]
285x = time.time()
286x = sys.modules['time'].time()
287a = '01234'
288c = a[0]
289c = a[-1]
290s = a[0:5]
291s = a[:5]
292s = a[0:]
293s = a[:]
294s = a[-5:]
295s = a[:-1]
296s = a[-4:-3]
297
298print 'atoms'
299### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
300### dictmaker: test ':' test (',' test ':' test)* [',']
301
302x = (1)
303x = (1 or 2 or 3)
304x = (1 or 2 or 3, 2, 3)
305
306x = []
307x = [1]
308x = [1 or 2 or 3]
309x = [1 or 2 or 3, 2, 3]
310x = []
311
312x = {}
313x = {'one': 1}
314x = {'one': 1,}
315x = {'one' or 'two': 1 or 2}
316x = {'one': 1, 'two': 2}
317x = {'one': 1, 'two': 2,}
318x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
319
320x = `x`
321x = `1 or 2 or 3`
322x = x
323x = 'x'
324x = 123
325
326### exprlist: expr (',' expr)* [',']
327### testlist: test (',' test)* [',']
328# These have been exercised enough above
329
330print 'classdef' # 'class' NAME parameters ['=' baselist] ':' suite
331### baselist: atom arguments (',' atom arguments)*
332### arguments: '(' [testlist] ')'
333class B: pass
334class C1(B): pass
335class C2(B): pass
336class D(C1, C2, B): pass
337class C:
338 def meth1(self): pass
339 def meth2(self, arg): pass
340 def meth3(self, a1, a2): pass