blob: f07f75b2352231e300232f1e598f91338e7d1d01 [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
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000100### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
Guido van Rossum3bead091992-01-27 17:00:37 +0000101# 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
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000168print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
169def f():
170 z = None
171 del z
172 exec 'z=1+1\n'
173 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
174 del z
175 exec 'z=1+1'
176 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
177f()
178g = {}
179exec 'z = 1' in g
180if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
181g = {}
182l = {}
183exec 'global a; a = 1; b = 2' in g, l
184if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
185
186
Guido van Rossum3bead091992-01-27 17:00:37 +0000187### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
188# Tested below
189
190print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
191if 1: pass
192if 1: pass
193else: pass
194if 0: pass
195elif 0: pass
196if 0: pass
197elif 0: pass
198elif 0: pass
199elif 0: pass
200else: pass
201
202print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
203while 0: pass
204while 0: pass
205else: pass
206
207print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
208[1]
209for i in 1, 2, 3: pass
210[2]
211for i, j, k in (): pass
212else: pass
213[3]
214
Guido van Rossum627efd91992-03-31 18:54:11 +0000215print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000216### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000217try:
218 1/0
219except ZeroDivisionError:
220 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000221try: 1/0
222except EOFError: pass
223except TypeError, msg: pass
224except RuntimeError, msg: pass
225except: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000226try: 1/0
227except (EOFError, TypeError, ZeroDivisionError): pass
228try: 1/0
229except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000230try: pass
231finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000232
233print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
234if 1: pass
235if 1:
236 pass
237if 1:
238 #
239 #
240 #
241 pass
242 pass
243 #
244 pass
245 #
246
247print 'test'
248### and_test ('or' and_test)*
249### and_test: not_test ('and' not_test)*
250### not_test: 'not' not_test | comparison
251if not 1: pass
252if 1 and 1: pass
253if 1 or 1: pass
254if not not not 1: pass
255if not 1 and 1 and 1: pass
256if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
257
258print 'comparison'
259### comparison: expr (comp_op expr)*
260### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
261if 1: pass
262x = (1 == 1)
263if 1 == 1: pass
264if 1 != 1: pass
265if 1 <> 1: pass
266if 1 < 1: pass
267if 1 > 1: pass
268if 1 <= 1: pass
269if 1 >= 1: pass
270if 1 is 1: pass
271if 1 is not 1: pass
272if 1 in (): pass
273if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000274if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000275
276print 'binary mask ops'
277x = 1 & 1
278x = 1 ^ 1
279x = 1 | 1
280
281print 'shift ops'
282x = 1 << 1
283x = 1 >> 1
284x = 1 << 1 >> 1
285
286print 'additive ops'
287x = 1
288x = 1 + 1
289x = 1 - 1 - 1
290x = 1 - 1 + 1 - 1 + 1
291
292print 'multiplicative ops'
293x = 1 * 1
294x = 1 / 1
295x = 1 % 1
296x = 1 / 1 * 1 % 1
297
298print 'unary ops'
299x = +1
300x = -1
301x = ~1
302x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
303x = -1*1/1 + 1*1 - ---1*1
304
305print 'selectors'
306### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
307### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000308f1()
309f2(1)
310f2(1,)
311f3(1, 2)
312f3(1, 2,)
313f4(1, (2, (3, 4)))
314v0()
315v0(1)
316v0(1,)
317v0(1,2)
318v0(1,2,3,4,5,6,7,8,9,0)
319v1(1)
320v1(1,)
321v1(1,2)
322v1(1,2,3)
323v1(1,2,3,4,5,6,7,8,9,0)
324v2(1,2)
325v2(1,2,3)
326v2(1,2,3,4)
327v2(1,2,3,4,5,6,7,8,9,0)
328v3(1,(2,3))
329v3(1,(2,3),4)
330v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000331import sys, time
332c = sys.path[0]
333x = time.time()
334x = sys.modules['time'].time()
335a = '01234'
336c = a[0]
337c = a[-1]
338s = a[0:5]
339s = a[:5]
340s = a[0:]
341s = a[:]
342s = a[-5:]
343s = a[:-1]
344s = a[-4:-3]
345
346print 'atoms'
347### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
348### dictmaker: test ':' test (',' test ':' test)* [',']
349
350x = (1)
351x = (1 or 2 or 3)
352x = (1 or 2 or 3, 2, 3)
353
354x = []
355x = [1]
356x = [1 or 2 or 3]
357x = [1 or 2 or 3, 2, 3]
358x = []
359
360x = {}
361x = {'one': 1}
362x = {'one': 1,}
363x = {'one' or 'two': 1 or 2}
364x = {'one': 1, 'two': 2}
365x = {'one': 1, 'two': 2,}
366x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
367
368x = `x`
369x = `1 or 2 or 3`
370x = x
371x = 'x'
372x = 123
373
374### exprlist: expr (',' expr)* [',']
375### testlist: test (',' test)* [',']
376# These have been exercised enough above
377
Guido van Rossum85f18201992-11-27 22:53:50 +0000378print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000379class B: pass
380class C1(B): pass
381class C2(B): pass
382class D(C1, C2, B): pass
383class C:
384 def meth1(self): pass
385 def meth2(self, arg): pass
386 def meth3(self, a1, a2): pass