blob: c77e19e0f65d42768991dd95de960d12b936353d [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
Barry Warsaw408b6d32002-07-30 23:27:12 +00004from test.test_support import TestFailed, verify, check_syntax
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00005import sys
Guido van Rossum3bead091992-01-27 17:00:37 +00006
7print '1. Parser'
8
9print '1.1 Tokens'
10
11print '1.1.1 Backslashes'
12
13# Backslash means line continuation:
14x = 1 \
15+ 1
Fred Drake132dce22000-12-12 23:11:42 +000016if x != 2: raise TestFailed, 'backslash for line continuation'
Guido van Rossum3bead091992-01-27 17:00:37 +000017
18# Backslash does not means continuation in comments :\
19x = 0
Fred Drake132dce22000-12-12 23:11:42 +000020if x != 0: raise TestFailed, 'backslash ending comment'
Guido van Rossum3bead091992-01-27 17:00:37 +000021
22print '1.1.2 Numeric literals'
23
24print '1.1.2.1 Plain integers'
Fred Drake132dce22000-12-12 23:11:42 +000025if 0xff != 255: raise TestFailed, 'hex int'
26if 0377 != 255: raise TestFailed, 'octal int'
Guido van Rossumdd8cb441993-12-29 15:33:08 +000027if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
28try:
Fred Drake004d5e62000-10-23 17:22:08 +000029 from sys import maxint
Guido van Rossumdd8cb441993-12-29 15:33:08 +000030except ImportError:
Fred Drake004d5e62000-10-23 17:22:08 +000031 maxint = 2147483647
Guido van Rossumdd8cb441993-12-29 15:33:08 +000032if maxint == 2147483647:
Fred Drake004d5e62000-10-23 17:22:08 +000033 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
34 # XXX -2147483648
35 if 037777777777 != -1: raise TestFailed, 'oct -1'
36 if 0xffffffff != -1: raise TestFailed, 'hex -1'
37 for s in '2147483648', '040000000000', '0x100000000':
38 try:
39 x = eval(s)
40 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000041 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000042elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000043 if eval('-9223372036854775807-1 != 01000000000000000000000'):
44 raise TestFailed, 'max negative int'
45 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
46 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
47 for s in '9223372036854775808', '02000000000000000000000', \
48 '0x10000000000000000':
49 try:
50 x = eval(s)
51 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000052 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000053else:
Fred Drake004d5e62000-10-23 17:22:08 +000054 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000055
56print '1.1.2.2 Long integers'
57x = 0L
58x = 0l
59x = 0xffffffffffffffffL
60x = 0xffffffffffffffffl
61x = 077777777777777777L
62x = 077777777777777777l
63x = 123456789012345678901234567890L
64x = 123456789012345678901234567890l
65
66print '1.1.2.3 Floating point'
67x = 3.14
68x = 314.
69x = 0.314
70# XXX x = 000.314
71x = .314
72x = 3e14
73x = 3E14
74x = 3e-14
75x = 3e+14
76x = 3.e14
77x = .3e14
78x = 3.1e4
79
Guido van Rossumb31c7f71993-11-11 10:31:23 +000080print '1.1.3 String literals'
81
Marc-André Lemburg36619082001-01-17 19:11:13 +000082x = ''; y = ""; verify(len(x) == 0 and x == y)
83x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
84x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000085x = "doesn't \"shrink\" does it"
86y = 'doesn\'t "shrink" does it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000087verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000088x = "does \"shrink\" doesn't it"
89y = 'does "shrink" doesn\'t it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000090verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000091x = """
92The "quick"
93brown fox
94jumps over
95the 'lazy' dog.
96"""
97y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Marc-André Lemburg36619082001-01-17 19:11:13 +000098verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000099y = '''
100The "quick"
101brown fox
102jumps over
103the 'lazy' dog.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000104'''; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000105y = "\n\
106The \"quick\"\n\
107brown fox\n\
108jumps over\n\
109the 'lazy' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000110"; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111y = '\n\
112The \"quick\"\n\
113brown fox\n\
114jumps over\n\
115the \'lazy\' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000116'; verify(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000117
118
Guido van Rossum3bead091992-01-27 17:00:37 +0000119print '1.2 Grammar'
120
121print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
122# XXX can't test in a script -- this rule is only used when interactive
123
124print 'file_input' # (NEWLINE | stmt)* ENDMARKER
125# Being tested as this very moment this very module
126
127print 'expr_input' # testlist NEWLINE
128# XXX Hard to test -- used only in calls to input()
129
130print 'eval_input' # testlist ENDMARKER
131x = eval('1, 0 or 1')
132
133print 'funcdef'
134### 'def' NAME parameters ':' suite
135### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000136### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
137### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000138### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000139### fpdef: NAME | '(' fplist ')'
140### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000141### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000142### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000143def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000144f1()
145f1(*())
146f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000147def f2(one_argument): pass
148def f3(two, arguments): pass
149def f4(two, (compound, (argument, list))): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000150def f5((compound, first), two): pass
151verify(f2.func_code.co_varnames == ('one_argument',))
152verify(f3.func_code.co_varnames == ('two', 'arguments'))
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000153if sys.platform.startswith('java'):
154 verify(f4.func_code.co_varnames ==
Finn Bock4ab7adb2001-12-09 09:12:34 +0000155 ('two', '(compound, (argument, list))', 'compound', 'argument',
156 'list',))
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000157 verify(f5.func_code.co_varnames ==
158 ('(compound, first)', 'two', 'compound', 'first'))
159else:
160 verify(f4.func_code.co_varnames == ('two', '.2', 'compound',
161 'argument', 'list'))
162 verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000163def a1(one_arg,): pass
164def a2(two, args,): pass
165def v0(*rest): pass
166def v1(a, *rest): pass
167def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000168def v3(a, (b, c), *rest): return a, b, c, rest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000169if sys.platform.startswith('java'):
170 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))
171else:
172 verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000173verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000174def d01(a=1): pass
175d01()
176d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000177d01(*(1,))
178d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179def d11(a, b=1): pass
180d11(1)
181d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000182d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183def d21(a, b, c=1): pass
184d21(1, 2)
185d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000186d21(*(1, 2, 3))
187d21(1, *(2, 3))
188d21(1, 2, *(3,))
189d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190def d02(a=1, b=2): pass
191d02()
192d02(1)
193d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000194d02(*(1, 2))
195d02(1, *(2,))
196d02(1, **{'b':2})
197d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198def d12(a, b=1, c=2): pass
199d12(1)
200d12(1, 2)
201d12(1, 2, 3)
202def d22(a, b, c=1, d=2): pass
203d22(1, 2)
204d22(1, 2, 3)
205d22(1, 2, 3, 4)
206def d01v(a=1, *rest): pass
207d01v()
208d01v(1)
209d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000210d01v(*(1, 2, 3, 4))
211d01v(*(1,))
212d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213def d11v(a, b=1, *rest): pass
214d11v(1)
215d11v(1, 2)
216d11v(1, 2, 3)
217def d21v(a, b, c=1, *rest): pass
218d21v(1, 2)
219d21v(1, 2, 3)
220d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000221d21v(*(1, 2, 3, 4))
222d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223def d02v(a=1, b=2, *rest): pass
224d02v()
225d02v(1)
226d02v(1, 2)
227d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000228d02v(1, *(2, 3, 4))
229d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230def d12v(a, b=1, c=2, *rest): pass
231d12v(1)
232d12v(1, 2)
233d12v(1, 2, 3)
234d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000235d12v(*(1, 2, 3, 4))
236d12v(1, 2, *(3, 4, 5))
237d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238def d22v(a, b, c=1, d=2, *rest): pass
239d22v(1, 2)
240d22v(1, 2, 3)
241d22v(1, 2, 3, 4)
242d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000243d22v(*(1, 2, 3, 4))
244d22v(1, 2, *(3, 4, 5))
245d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000246
Jeremy Hylton619eea62001-01-25 20:12:27 +0000247### lambdef: 'lambda' [varargslist] ':' test
248print 'lambdef'
249l1 = lambda : 0
250verify(l1() == 0)
251l2 = lambda : a[d] # XXX just testing the expression
252l3 = lambda : [2 < x for x in [-1, 3, 0L]]
253verify(l3() == [0, 1, 0])
254l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
255verify(l4() == 1)
256l5 = lambda x, y, z=2: x + y + z
257verify(l5(1, 2) == 5)
258verify(l5(1, 2, 3) == 6)
259check_syntax("lambda x: x = 2")
260
Guido van Rossum3bead091992-01-27 17:00:37 +0000261### stmt: simple_stmt | compound_stmt
262# Tested below
263
264### simple_stmt: small_stmt (';' small_stmt)* [';']
265print 'simple_stmt'
266x = 1; pass; del x
267
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000268### 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 +0000269# Tested below
270
271print 'expr_stmt' # (exprlist '=')* exprlist
2721
2731, 2, 3
274x = 1
275x = 1, 2, 3
276x = y = z = 1, 2, 3
277x, y, z = 1, 2, 3
278abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
279# NB these variables are deleted below
280
Jeremy Hylton47793992001-02-19 15:35:26 +0000281check_syntax("x + 1 = 1")
282check_syntax("a + 1 = b + 2")
283
Guido van Rossum3bead091992-01-27 17:00:37 +0000284print 'print_stmt' # 'print' (test ',')* [test]
285print 1, 2, 3
286print 1, 2, 3,
287print
288print 0 or 1, 0 or 1,
289print 0 or 1
290
Barry Warsawefc92ee2000-08-21 15:46:50 +0000291print 'extended print_stmt' # 'print' '>>' test ','
292import sys
293print >> sys.stdout, 1, 2, 3
294print >> sys.stdout, 1, 2, 3,
295print >> sys.stdout
296print >> sys.stdout, 0 or 1, 0 or 1,
297print >> sys.stdout, 0 or 1
298
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000299# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000300class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000301 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000302
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000303gulp = Gulp()
304print >> gulp, 1, 2, 3
305print >> gulp, 1, 2, 3,
306print >> gulp
307print >> gulp, 0 or 1, 0 or 1,
308print >> gulp, 0 or 1
309
310# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000311def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000312 oldstdout = sys.stdout
313 sys.stdout = Gulp()
314 try:
315 tellme(Gulp())
316 tellme()
317 finally:
318 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000319
320# we should see this once
321def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000322 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000323
324driver()
325
326# we should not see this at all
327def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000328 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000329
330driver()
331
Barry Warsawefc92ee2000-08-21 15:46:50 +0000332# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000333check_syntax('print ,')
334check_syntax('print >> x,')
335
Guido van Rossum3bead091992-01-27 17:00:37 +0000336print 'del_stmt' # 'del' exprlist
337del abc
338del x, y, (z, xyz)
339
340print 'pass_stmt' # 'pass'
341pass
342
343print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
344# Tested below
345
346print 'break_stmt' # 'break'
347while 1: break
348
349print 'continue_stmt' # 'continue'
350i = 1
351while i: i = 0; continue
352
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000353msg = ""
354while not msg:
355 msg = "continue + try/except ok"
356 try:
357 continue
358 msg = "continue failed to continue inside try"
359 except:
360 msg = "continue inside try called except block"
361print msg
362
363msg = ""
364while not msg:
365 msg = "finally block not called"
366 try:
367 continue
368 finally:
369 msg = "continue + try/finally ok"
370print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000371
Thomas Wouters80d373c2001-09-26 12:43:39 +0000372
373# This test warrants an explanation. It is a test specifically for SF bugs
374# #463359 and #462937. The bug is that a 'break' statement executed or
375# exception raised inside a try/except inside a loop, *after* a continue
376# statement has been executed in that loop, will cause the wrong number of
377# arguments to be popped off the stack and the instruction pointer reset to
378# a very small number (usually 0.) Because of this, the following test
379# *must* written as a function, and the tracking vars *must* be function
380# arguments with default values. Otherwise, the test will loop and loop.
381
382print "testing continue and break in try/except in loop"
383def test_break_continue_loop(extra_burning_oil = 1, count=0):
384 big_hippo = 2
385 while big_hippo:
386 count += 1
387 try:
388 if extra_burning_oil and big_hippo == 1:
389 extra_burning_oil -= 1
390 break
391 big_hippo -= 1
392 continue
393 except:
394 raise
395 if count > 2 or big_hippo <> 1:
396 print "continue then break in try/except in loop broken!"
397test_break_continue_loop()
398
Guido van Rossum3bead091992-01-27 17:00:37 +0000399print 'return_stmt' # 'return' [testlist]
400def g1(): return
401def g2(): return 1
402g1()
403x = g2()
404
405print 'raise_stmt' # 'raise' test [',' test]
406try: raise RuntimeError, 'just testing'
407except RuntimeError: pass
408try: raise KeyboardInterrupt
409except KeyboardInterrupt: pass
410
411print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000412import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000413import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000415from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000416from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000417
418print 'global_stmt' # 'global' NAME (',' NAME)*
419def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000420 global a
421 global a, b
422 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000423
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000424print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
425def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000426 z = None
427 del z
428 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000429 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000430 del z
431 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000432 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000433 z = None
434 del z
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000435 import types
436 if hasattr(types, "UnicodeType"):
437 exec r"""if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000438 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000439 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000440 del z
441 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000442 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000443"""
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000444f()
445g = {}
446exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000447if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000448if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000449g = {}
450l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000451
452import warnings
453warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000454exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000455if g.has_key('__builtins__'): del g['__builtins__']
456if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000457if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000458
459
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000460print "assert_stmt" # assert_stmt: 'assert' test [',' test]
461assert 1
462assert 1, 1
463assert lambda x:x
464assert 1, lambda x:x+1
465
Guido van Rossum3bead091992-01-27 17:00:37 +0000466### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
467# Tested below
468
469print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
470if 1: pass
471if 1: pass
472else: pass
473if 0: pass
474elif 0: pass
475if 0: pass
476elif 0: pass
477elif 0: pass
478elif 0: pass
479else: pass
480
481print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
482while 0: pass
483while 0: pass
484else: pass
485
486print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000487for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000488for i, j, k in (): pass
489else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000490class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000491 def __init__(self, max):
492 self.max = max
493 self.sofar = []
494 def __len__(self): return len(self.sofar)
495 def __getitem__(self, i):
496 if not 0 <= i < self.max: raise IndexError
497 n = len(self.sofar)
498 while n <= i:
499 self.sofar.append(n*n)
500 n = n+1
501 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000502n = 0
503for x in Squares(10): n = n+x
504if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000505
Guido van Rossumb6775db1994-08-01 11:34:53 +0000506print 'try_stmt'
507### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
508### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000509### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000510try:
Fred Drake004d5e62000-10-23 17:22:08 +0000511 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000512except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000513 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000514else:
Fred Drake004d5e62000-10-23 17:22:08 +0000515 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000516try: 1/0
517except EOFError: pass
518except TypeError, msg: pass
519except RuntimeError, msg: pass
520except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000521else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000522try: 1/0
523except (EOFError, TypeError, ZeroDivisionError): pass
524try: 1/0
525except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000526try: pass
527finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000528
529print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
530if 1: pass
531if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000532 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000533if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000534 #
535 #
536 #
537 pass
538 pass
539 #
540 pass
541 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000542
543print 'test'
544### and_test ('or' and_test)*
545### and_test: not_test ('and' not_test)*
546### not_test: 'not' not_test | comparison
547if not 1: pass
548if 1 and 1: pass
549if 1 or 1: pass
550if not not not 1: pass
551if not 1 and 1 and 1: pass
552if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
553
554print 'comparison'
555### comparison: expr (comp_op expr)*
556### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
557if 1: pass
558x = (1 == 1)
559if 1 == 1: pass
560if 1 != 1: pass
561if 1 <> 1: pass
562if 1 < 1: pass
563if 1 > 1: pass
564if 1 <= 1: pass
565if 1 >= 1: pass
566if 1 is 1: pass
567if 1 is not 1: pass
568if 1 in (): pass
569if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000570if 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 +0000571
572print 'binary mask ops'
573x = 1 & 1
574x = 1 ^ 1
575x = 1 | 1
576
577print 'shift ops'
578x = 1 << 1
579x = 1 >> 1
580x = 1 << 1 >> 1
581
582print 'additive ops'
583x = 1
584x = 1 + 1
585x = 1 - 1 - 1
586x = 1 - 1 + 1 - 1 + 1
587
588print 'multiplicative ops'
589x = 1 * 1
590x = 1 / 1
591x = 1 % 1
592x = 1 / 1 * 1 % 1
593
594print 'unary ops'
595x = +1
596x = -1
597x = ~1
598x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
599x = -1*1/1 + 1*1 - ---1*1
600
601print 'selectors'
602### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
603### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000604f1()
605f2(1)
606f2(1,)
607f3(1, 2)
608f3(1, 2,)
609f4(1, (2, (3, 4)))
610v0()
611v0(1)
612v0(1,)
613v0(1,2)
614v0(1,2,3,4,5,6,7,8,9,0)
615v1(1)
616v1(1,)
617v1(1,2)
618v1(1,2,3)
619v1(1,2,3,4,5,6,7,8,9,0)
620v2(1,2)
621v2(1,2,3)
622v2(1,2,3,4)
623v2(1,2,3,4,5,6,7,8,9,0)
624v3(1,(2,3))
625v3(1,(2,3),4)
626v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000627print
Guido van Rossum3bead091992-01-27 17:00:37 +0000628import sys, time
629c = sys.path[0]
630x = time.time()
631x = sys.modules['time'].time()
632a = '01234'
633c = a[0]
634c = a[-1]
635s = a[0:5]
636s = a[:5]
637s = a[0:]
638s = a[:]
639s = a[-5:]
640s = a[:-1]
641s = a[-4:-3]
642
643print 'atoms'
644### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
645### dictmaker: test ':' test (',' test ':' test)* [',']
646
647x = (1)
648x = (1 or 2 or 3)
649x = (1 or 2 or 3, 2, 3)
650
651x = []
652x = [1]
653x = [1 or 2 or 3]
654x = [1 or 2 or 3, 2, 3]
655x = []
656
657x = {}
658x = {'one': 1}
659x = {'one': 1,}
660x = {'one' or 'two': 1 or 2}
661x = {'one': 1, 'two': 2}
662x = {'one': 1, 'two': 2,}
663x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
664
665x = `x`
666x = `1 or 2 or 3`
667x = x
668x = 'x'
669x = 123
670
671### exprlist: expr (',' expr)* [',']
672### testlist: test (',' test)* [',']
673# These have been exercised enough above
674
Guido van Rossum85f18201992-11-27 22:53:50 +0000675print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000676class B: pass
677class C1(B): pass
678class C2(B): pass
679class D(C1, C2, B): pass
680class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000681 def meth1(self): pass
682 def meth2(self, arg): pass
683 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000684
685# list comprehension tests
686nums = [1, 2, 3, 4, 5]
687strs = ["Apple", "Banana", "Coconut"]
688spcs = [" Apple", " Banana ", "Coco nut "]
689
690print [s.strip() for s in spcs]
691print [3 * x for x in nums]
692print [x for x in nums if x > 2]
693print [(i, s) for i in nums for s in strs]
694print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
Jeremy Hylton578ceee2001-01-23 01:51:40 +0000695
696def test_in_func(l):
697 return [None < x < 3 for x in l if x > 2]
698
699print test_in_func(nums)
700
Jeremy Hyltone241e292001-03-19 20:42:11 +0000701def test_nested_front():
702 print [[y for y in [x, x + 1]] for x in [1,3,5]]
703
704test_nested_front()
705
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000706check_syntax("[i, s for i in nums for s in strs]")
707check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000708
Skip Montanaro803d6e52000-08-12 18:09:51 +0000709suppliers = [
710 (1, "Boeing"),
711 (2, "Ford"),
712 (3, "Macdonalds")
713]
714
715parts = [
716 (10, "Airliner"),
717 (20, "Engine"),
718 (30, "Cheeseburger")
719]
720
721suppart = [
722 (1, 10), (1, 20), (2, 20), (3, 30)
723]
724
725print [
726 (sname, pname)
727 for (sno, sname) in suppliers
728 for (pno, pname) in parts
729 for (sp_sno, sp_pno) in suppart
730 if sno == sp_sno and pno == sp_pno
731]