blob: da3dad529c122bbfab297772a9f169102de3dcfe [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 *
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 ==
155 ('two', '(compound, (argument, list))',))
156 verify(f5.func_code.co_varnames ==
157 ('(compound, first)', 'two', 'compound', 'first'))
158else:
159 verify(f4.func_code.co_varnames == ('two', '.2', 'compound',
160 'argument', 'list'))
161 verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000162def a1(one_arg,): pass
163def a2(two, args,): pass
164def v0(*rest): pass
165def v1(a, *rest): pass
166def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000167def v3(a, (b, c), *rest): return a, b, c, rest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000168if sys.platform.startswith('java'):
169 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))
170else:
171 verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000172verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173def d01(a=1): pass
174d01()
175d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000176d01(*(1,))
177d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000178def d11(a, b=1): pass
179d11(1)
180d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000181d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182def d21(a, b, c=1): pass
183d21(1, 2)
184d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000185d21(*(1, 2, 3))
186d21(1, *(2, 3))
187d21(1, 2, *(3,))
188d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000189def d02(a=1, b=2): pass
190d02()
191d02(1)
192d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000193d02(*(1, 2))
194d02(1, *(2,))
195d02(1, **{'b':2})
196d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197def d12(a, b=1, c=2): pass
198d12(1)
199d12(1, 2)
200d12(1, 2, 3)
201def d22(a, b, c=1, d=2): pass
202d22(1, 2)
203d22(1, 2, 3)
204d22(1, 2, 3, 4)
205def d01v(a=1, *rest): pass
206d01v()
207d01v(1)
208d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000209d01v(*(1, 2, 3, 4))
210d01v(*(1,))
211d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212def d11v(a, b=1, *rest): pass
213d11v(1)
214d11v(1, 2)
215d11v(1, 2, 3)
216def d21v(a, b, c=1, *rest): pass
217d21v(1, 2)
218d21v(1, 2, 3)
219d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000220d21v(*(1, 2, 3, 4))
221d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222def d02v(a=1, b=2, *rest): pass
223d02v()
224d02v(1)
225d02v(1, 2)
226d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000227d02v(1, *(2, 3, 4))
228d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000229def d12v(a, b=1, c=2, *rest): pass
230d12v(1)
231d12v(1, 2)
232d12v(1, 2, 3)
233d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000234d12v(*(1, 2, 3, 4))
235d12v(1, 2, *(3, 4, 5))
236d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237def d22v(a, b, c=1, d=2, *rest): pass
238d22v(1, 2)
239d22v(1, 2, 3)
240d22v(1, 2, 3, 4)
241d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000242d22v(*(1, 2, 3, 4))
243d22v(1, 2, *(3, 4, 5))
244d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000245
Jeremy Hylton619eea62001-01-25 20:12:27 +0000246### lambdef: 'lambda' [varargslist] ':' test
247print 'lambdef'
248l1 = lambda : 0
249verify(l1() == 0)
250l2 = lambda : a[d] # XXX just testing the expression
251l3 = lambda : [2 < x for x in [-1, 3, 0L]]
252verify(l3() == [0, 1, 0])
253l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
254verify(l4() == 1)
255l5 = lambda x, y, z=2: x + y + z
256verify(l5(1, 2) == 5)
257verify(l5(1, 2, 3) == 6)
258check_syntax("lambda x: x = 2")
259
Guido van Rossum3bead091992-01-27 17:00:37 +0000260### stmt: simple_stmt | compound_stmt
261# Tested below
262
263### simple_stmt: small_stmt (';' small_stmt)* [';']
264print 'simple_stmt'
265x = 1; pass; del x
266
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000267### 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 +0000268# Tested below
269
270print 'expr_stmt' # (exprlist '=')* exprlist
2711
2721, 2, 3
273x = 1
274x = 1, 2, 3
275x = y = z = 1, 2, 3
276x, y, z = 1, 2, 3
277abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
278# NB these variables are deleted below
279
Jeremy Hylton47793992001-02-19 15:35:26 +0000280check_syntax("x + 1 = 1")
281check_syntax("a + 1 = b + 2")
282
Guido van Rossum3bead091992-01-27 17:00:37 +0000283print 'print_stmt' # 'print' (test ',')* [test]
284print 1, 2, 3
285print 1, 2, 3,
286print
287print 0 or 1, 0 or 1,
288print 0 or 1
289
Barry Warsawefc92ee2000-08-21 15:46:50 +0000290print 'extended print_stmt' # 'print' '>>' test ','
291import sys
292print >> sys.stdout, 1, 2, 3
293print >> sys.stdout, 1, 2, 3,
294print >> sys.stdout
295print >> sys.stdout, 0 or 1, 0 or 1,
296print >> sys.stdout, 0 or 1
297
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000298# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000299class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000300 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000301
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000302gulp = Gulp()
303print >> gulp, 1, 2, 3
304print >> gulp, 1, 2, 3,
305print >> gulp
306print >> gulp, 0 or 1, 0 or 1,
307print >> gulp, 0 or 1
308
309# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000310def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000311 oldstdout = sys.stdout
312 sys.stdout = Gulp()
313 try:
314 tellme(Gulp())
315 tellme()
316 finally:
317 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000318
319# we should see this once
320def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000321 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000322
323driver()
324
325# we should not see this at all
326def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000327 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000328
329driver()
330
Barry Warsawefc92ee2000-08-21 15:46:50 +0000331# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000332check_syntax('print ,')
333check_syntax('print >> x,')
334
Guido van Rossum3bead091992-01-27 17:00:37 +0000335print 'del_stmt' # 'del' exprlist
336del abc
337del x, y, (z, xyz)
338
339print 'pass_stmt' # 'pass'
340pass
341
342print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
343# Tested below
344
345print 'break_stmt' # 'break'
346while 1: break
347
348print 'continue_stmt' # 'continue'
349i = 1
350while i: i = 0; continue
351
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000352msg = ""
353while not msg:
354 msg = "continue + try/except ok"
355 try:
356 continue
357 msg = "continue failed to continue inside try"
358 except:
359 msg = "continue inside try called except block"
360print msg
361
362msg = ""
363while not msg:
364 msg = "finally block not called"
365 try:
366 continue
367 finally:
368 msg = "continue + try/finally ok"
369print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000370
Thomas Wouters80d373c2001-09-26 12:43:39 +0000371
372# This test warrants an explanation. It is a test specifically for SF bugs
373# #463359 and #462937. The bug is that a 'break' statement executed or
374# exception raised inside a try/except inside a loop, *after* a continue
375# statement has been executed in that loop, will cause the wrong number of
376# arguments to be popped off the stack and the instruction pointer reset to
377# a very small number (usually 0.) Because of this, the following test
378# *must* written as a function, and the tracking vars *must* be function
379# arguments with default values. Otherwise, the test will loop and loop.
380
381print "testing continue and break in try/except in loop"
382def test_break_continue_loop(extra_burning_oil = 1, count=0):
383 big_hippo = 2
384 while big_hippo:
385 count += 1
386 try:
387 if extra_burning_oil and big_hippo == 1:
388 extra_burning_oil -= 1
389 break
390 big_hippo -= 1
391 continue
392 except:
393 raise
394 if count > 2 or big_hippo <> 1:
395 print "continue then break in try/except in loop broken!"
396test_break_continue_loop()
397
Guido van Rossum3bead091992-01-27 17:00:37 +0000398print 'return_stmt' # 'return' [testlist]
399def g1(): return
400def g2(): return 1
401g1()
402x = g2()
403
404print 'raise_stmt' # 'raise' test [',' test]
405try: raise RuntimeError, 'just testing'
406except RuntimeError: pass
407try: raise KeyboardInterrupt
408except KeyboardInterrupt: pass
409
410print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000411import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000412import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000413from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000414from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000415from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000416
417print 'global_stmt' # 'global' NAME (',' NAME)*
418def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000419 global a
420 global a, b
421 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000422
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000423print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
424def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000425 z = None
426 del z
427 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000428 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000429 del z
430 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000431 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000432 z = None
433 del z
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000434 import types
435 if hasattr(types, "UnicodeType"):
436 exec r"""if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000437 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000438 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000439 del z
440 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000441 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000442"""
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000443f()
444g = {}
445exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000446if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000447if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000448g = {}
449l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000450
451import warnings
452warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000453exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000454if g.has_key('__builtins__'): del g['__builtins__']
455if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000456if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000457
458
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000459print "assert_stmt" # assert_stmt: 'assert' test [',' test]
460assert 1
461assert 1, 1
462assert lambda x:x
463assert 1, lambda x:x+1
464
Guido van Rossum3bead091992-01-27 17:00:37 +0000465### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
466# Tested below
467
468print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
469if 1: pass
470if 1: pass
471else: pass
472if 0: pass
473elif 0: pass
474if 0: pass
475elif 0: pass
476elif 0: pass
477elif 0: pass
478else: pass
479
480print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
481while 0: pass
482while 0: pass
483else: pass
484
485print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000486for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000487for i, j, k in (): pass
488else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000489class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000490 def __init__(self, max):
491 self.max = max
492 self.sofar = []
493 def __len__(self): return len(self.sofar)
494 def __getitem__(self, i):
495 if not 0 <= i < self.max: raise IndexError
496 n = len(self.sofar)
497 while n <= i:
498 self.sofar.append(n*n)
499 n = n+1
500 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000501n = 0
502for x in Squares(10): n = n+x
503if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000504
Guido van Rossumb6775db1994-08-01 11:34:53 +0000505print 'try_stmt'
506### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
507### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000508### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000509try:
Fred Drake004d5e62000-10-23 17:22:08 +0000510 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000511except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000512 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000513else:
Fred Drake004d5e62000-10-23 17:22:08 +0000514 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000515try: 1/0
516except EOFError: pass
517except TypeError, msg: pass
518except RuntimeError, msg: pass
519except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000520else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000521try: 1/0
522except (EOFError, TypeError, ZeroDivisionError): pass
523try: 1/0
524except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000525try: pass
526finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000527
528print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
529if 1: pass
530if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000531 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000532if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000533 #
534 #
535 #
536 pass
537 pass
538 #
539 pass
540 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000541
542print 'test'
543### and_test ('or' and_test)*
544### and_test: not_test ('and' not_test)*
545### not_test: 'not' not_test | comparison
546if not 1: pass
547if 1 and 1: pass
548if 1 or 1: pass
549if not not not 1: pass
550if not 1 and 1 and 1: pass
551if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
552
553print 'comparison'
554### comparison: expr (comp_op expr)*
555### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
556if 1: pass
557x = (1 == 1)
558if 1 == 1: pass
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 is 1: pass
566if 1 is not 1: pass
567if 1 in (): pass
568if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000569if 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 +0000570
571print 'binary mask ops'
572x = 1 & 1
573x = 1 ^ 1
574x = 1 | 1
575
576print 'shift ops'
577x = 1 << 1
578x = 1 >> 1
579x = 1 << 1 >> 1
580
581print 'additive ops'
582x = 1
583x = 1 + 1
584x = 1 - 1 - 1
585x = 1 - 1 + 1 - 1 + 1
586
587print 'multiplicative ops'
588x = 1 * 1
589x = 1 / 1
590x = 1 % 1
591x = 1 / 1 * 1 % 1
592
593print 'unary ops'
594x = +1
595x = -1
596x = ~1
597x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
598x = -1*1/1 + 1*1 - ---1*1
599
600print 'selectors'
601### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
602### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000603f1()
604f2(1)
605f2(1,)
606f3(1, 2)
607f3(1, 2,)
608f4(1, (2, (3, 4)))
609v0()
610v0(1)
611v0(1,)
612v0(1,2)
613v0(1,2,3,4,5,6,7,8,9,0)
614v1(1)
615v1(1,)
616v1(1,2)
617v1(1,2,3)
618v1(1,2,3,4,5,6,7,8,9,0)
619v2(1,2)
620v2(1,2,3)
621v2(1,2,3,4)
622v2(1,2,3,4,5,6,7,8,9,0)
623v3(1,(2,3))
624v3(1,(2,3),4)
625v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000626print
Guido van Rossum3bead091992-01-27 17:00:37 +0000627import sys, time
628c = sys.path[0]
629x = time.time()
630x = sys.modules['time'].time()
631a = '01234'
632c = a[0]
633c = a[-1]
634s = a[0:5]
635s = a[:5]
636s = a[0:]
637s = a[:]
638s = a[-5:]
639s = a[:-1]
640s = a[-4:-3]
641
642print 'atoms'
643### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
644### dictmaker: test ':' test (',' test ':' test)* [',']
645
646x = (1)
647x = (1 or 2 or 3)
648x = (1 or 2 or 3, 2, 3)
649
650x = []
651x = [1]
652x = [1 or 2 or 3]
653x = [1 or 2 or 3, 2, 3]
654x = []
655
656x = {}
657x = {'one': 1}
658x = {'one': 1,}
659x = {'one' or 'two': 1 or 2}
660x = {'one': 1, 'two': 2}
661x = {'one': 1, 'two': 2,}
662x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
663
664x = `x`
665x = `1 or 2 or 3`
666x = x
667x = 'x'
668x = 123
669
670### exprlist: expr (',' expr)* [',']
671### testlist: test (',' test)* [',']
672# These have been exercised enough above
673
Guido van Rossum85f18201992-11-27 22:53:50 +0000674print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000675class B: pass
676class C1(B): pass
677class C2(B): pass
678class D(C1, C2, B): pass
679class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000680 def meth1(self): pass
681 def meth2(self, arg): pass
682 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000683
684# list comprehension tests
685nums = [1, 2, 3, 4, 5]
686strs = ["Apple", "Banana", "Coconut"]
687spcs = [" Apple", " Banana ", "Coco nut "]
688
689print [s.strip() for s in spcs]
690print [3 * x for x in nums]
691print [x for x in nums if x > 2]
692print [(i, s) for i in nums for s in strs]
693print [(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 +0000694
695def test_in_func(l):
696 return [None < x < 3 for x in l if x > 2]
697
698print test_in_func(nums)
699
Jeremy Hyltone241e292001-03-19 20:42:11 +0000700def test_nested_front():
701 print [[y for y in [x, x + 1]] for x in [1,3,5]]
702
703test_nested_front()
704
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000705check_syntax("[i, s for i in nums for s in strs]")
706check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000707
Skip Montanaro803d6e52000-08-12 18:09:51 +0000708suppliers = [
709 (1, "Boeing"),
710 (2, "Ford"),
711 (3, "Macdonalds")
712]
713
714parts = [
715 (10, "Airliner"),
716 (20, "Engine"),
717 (30, "Cheeseburger")
718]
719
720suppart = [
721 (1, 10), (1, 20), (2, 20), (3, 30)
722]
723
724print [
725 (sname, pname)
726 for (sno, sname) in suppliers
727 for (pno, pname) in parts
728 for (sp_sno, sp_pno) in suppart
729 if sno == sp_sno and pno == sp_pno
730]