blob: e0667d82f793363ccc7f68b55e66f0e830d687da [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
Guido van Rossumf0253f22002-08-29 14:57:26 +00004# NOTE: When you run this test as a script from the command line, you
5# get warnings about certain hex/oct constants. Since those are
6# issued by the parser, you can't suppress them by adding a
7# filterwarnings() call to this module. Therefore, to shut up the
8# regression test, the filterwarnings() call has been added to
9# regrtest.py.
10
Barry Warsaw408b6d32002-07-30 23:27:12 +000011from test.test_support import TestFailed, verify, check_syntax
Jeremy Hylton7d3dff22001-10-10 01:45:02 +000012import sys
Guido van Rossum3bead091992-01-27 17:00:37 +000013
14print '1. Parser'
15
16print '1.1 Tokens'
17
18print '1.1.1 Backslashes'
19
20# Backslash means line continuation:
21x = 1 \
22+ 1
Fred Drake132dce22000-12-12 23:11:42 +000023if x != 2: raise TestFailed, 'backslash for line continuation'
Guido van Rossum3bead091992-01-27 17:00:37 +000024
25# Backslash does not means continuation in comments :\
26x = 0
Fred Drake132dce22000-12-12 23:11:42 +000027if x != 0: raise TestFailed, 'backslash ending comment'
Guido van Rossum3bead091992-01-27 17:00:37 +000028
29print '1.1.2 Numeric literals'
30
31print '1.1.2.1 Plain integers'
Fred Drake132dce22000-12-12 23:11:42 +000032if 0xff != 255: raise TestFailed, 'hex int'
33if 0377 != 255: raise TestFailed, 'octal int'
Guido van Rossumdd8cb441993-12-29 15:33:08 +000034if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
35try:
Fred Drake004d5e62000-10-23 17:22:08 +000036 from sys import maxint
Guido van Rossumdd8cb441993-12-29 15:33:08 +000037except ImportError:
Fred Drake004d5e62000-10-23 17:22:08 +000038 maxint = 2147483647
Guido van Rossumdd8cb441993-12-29 15:33:08 +000039if maxint == 2147483647:
Barry Warsaw18bd1122002-08-29 13:09:47 +000040 if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int'
Fred Drake004d5e62000-10-23 17:22:08 +000041 # XXX -2147483648
Guido van Rossumf0253f22002-08-29 14:57:26 +000042 if 037777777777 != -1: raise TestFailed, 'oct -1'
43 if 0xffffffff != -1: raise TestFailed, 'hex -1'
Fred Drake004d5e62000-10-23 17:22:08 +000044 for s in '2147483648', '040000000000', '0x100000000':
45 try:
46 x = eval(s)
47 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000048 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000049elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000050 if eval('-9223372036854775807-1 != 01000000000000000000000'):
51 raise TestFailed, 'max negative int'
52 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
53 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
54 for s in '9223372036854775808', '02000000000000000000000', \
55 '0x10000000000000000':
56 try:
57 x = eval(s)
58 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000059 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000060else:
Fred Drake004d5e62000-10-23 17:22:08 +000061 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000062
63print '1.1.2.2 Long integers'
64x = 0L
65x = 0l
66x = 0xffffffffffffffffL
67x = 0xffffffffffffffffl
68x = 077777777777777777L
69x = 077777777777777777l
70x = 123456789012345678901234567890L
71x = 123456789012345678901234567890l
72
73print '1.1.2.3 Floating point'
74x = 3.14
75x = 314.
76x = 0.314
77# XXX x = 000.314
78x = .314
79x = 3e14
80x = 3E14
81x = 3e-14
82x = 3e+14
83x = 3.e14
84x = .3e14
85x = 3.1e4
86
Guido van Rossumb31c7f71993-11-11 10:31:23 +000087print '1.1.3 String literals'
88
Marc-André Lemburg36619082001-01-17 19:11:13 +000089x = ''; y = ""; verify(len(x) == 0 and x == y)
90x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
91x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000092x = "doesn't \"shrink\" does it"
93y = 'doesn\'t "shrink" does it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000094verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000095x = "does \"shrink\" doesn't it"
96y = 'does "shrink" doesn\'t it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000097verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000098x = """
99The "quick"
100brown fox
101jumps over
102the 'lazy' dog.
103"""
104y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Marc-André Lemburg36619082001-01-17 19:11:13 +0000105verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000106y = '''
107The "quick"
108brown fox
109jumps over
110the 'lazy' dog.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000111'''; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000112y = "\n\
113The \"quick\"\n\
114brown fox\n\
115jumps over\n\
116the 'lazy' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000117"; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118y = '\n\
119The \"quick\"\n\
120brown fox\n\
121jumps over\n\
122the \'lazy\' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000123'; verify(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000124
125
Guido van Rossum3bead091992-01-27 17:00:37 +0000126print '1.2 Grammar'
127
128print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
129# XXX can't test in a script -- this rule is only used when interactive
130
131print 'file_input' # (NEWLINE | stmt)* ENDMARKER
132# Being tested as this very moment this very module
133
134print 'expr_input' # testlist NEWLINE
135# XXX Hard to test -- used only in calls to input()
136
137print 'eval_input' # testlist ENDMARKER
138x = eval('1, 0 or 1')
139
140print 'funcdef'
141### 'def' NAME parameters ':' suite
142### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000143### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000145### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000146### fpdef: NAME | '(' fplist ')'
147### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000148### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000149### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000150def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000151f1()
152f1(*())
153f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000154def f2(one_argument): pass
155def f3(two, arguments): pass
156def f4(two, (compound, (argument, list))): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000157def f5((compound, first), two): pass
158verify(f2.func_code.co_varnames == ('one_argument',))
159verify(f3.func_code.co_varnames == ('two', 'arguments'))
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000160if sys.platform.startswith('java'):
161 verify(f4.func_code.co_varnames ==
Finn Bock4ab7adb2001-12-09 09:12:34 +0000162 ('two', '(compound, (argument, list))', 'compound', 'argument',
163 'list',))
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000164 verify(f5.func_code.co_varnames ==
165 ('(compound, first)', 'two', 'compound', 'first'))
166else:
167 verify(f4.func_code.co_varnames == ('two', '.2', 'compound',
168 'argument', 'list'))
169 verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000170def a1(one_arg,): pass
171def a2(two, args,): pass
172def v0(*rest): pass
173def v1(a, *rest): pass
174def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000175def v3(a, (b, c), *rest): return a, b, c, rest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +0000176if sys.platform.startswith('java'):
177 verify(v3.func_code.co_varnames == ('a', '(b, c)', 'rest', 'b', 'c'))
178else:
179 verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000180verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181def d01(a=1): pass
182d01()
183d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000184d01(*(1,))
185d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000186def d11(a, b=1): pass
187d11(1)
188d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000189d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190def d21(a, b, c=1): pass
191d21(1, 2)
192d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000193d21(*(1, 2, 3))
194d21(1, *(2, 3))
195d21(1, 2, *(3,))
196d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197def d02(a=1, b=2): pass
198d02()
199d02(1)
200d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000201d02(*(1, 2))
202d02(1, *(2,))
203d02(1, **{'b':2})
204d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205def d12(a, b=1, c=2): pass
206d12(1)
207d12(1, 2)
208d12(1, 2, 3)
209def d22(a, b, c=1, d=2): pass
210d22(1, 2)
211d22(1, 2, 3)
212d22(1, 2, 3, 4)
213def d01v(a=1, *rest): pass
214d01v()
215d01v(1)
216d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000217d01v(*(1, 2, 3, 4))
218d01v(*(1,))
219d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220def d11v(a, b=1, *rest): pass
221d11v(1)
222d11v(1, 2)
223d11v(1, 2, 3)
224def d21v(a, b, c=1, *rest): pass
225d21v(1, 2)
226d21v(1, 2, 3)
227d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000228d21v(*(1, 2, 3, 4))
229d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230def d02v(a=1, b=2, *rest): pass
231d02v()
232d02v(1)
233d02v(1, 2)
234d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000235d02v(1, *(2, 3, 4))
236d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237def d12v(a, b=1, c=2, *rest): pass
238d12v(1)
239d12v(1, 2)
240d12v(1, 2, 3)
241d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000242d12v(*(1, 2, 3, 4))
243d12v(1, 2, *(3, 4, 5))
244d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245def d22v(a, b, c=1, d=2, *rest): pass
246d22v(1, 2)
247d22v(1, 2, 3)
248d22v(1, 2, 3, 4)
249d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000250d22v(*(1, 2, 3, 4))
251d22v(1, 2, *(3, 4, 5))
252d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000253
Jeremy Hylton619eea62001-01-25 20:12:27 +0000254### lambdef: 'lambda' [varargslist] ':' test
255print 'lambdef'
256l1 = lambda : 0
257verify(l1() == 0)
258l2 = lambda : a[d] # XXX just testing the expression
259l3 = lambda : [2 < x for x in [-1, 3, 0L]]
260verify(l3() == [0, 1, 0])
261l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
262verify(l4() == 1)
263l5 = lambda x, y, z=2: x + y + z
264verify(l5(1, 2) == 5)
265verify(l5(1, 2, 3) == 6)
266check_syntax("lambda x: x = 2")
267
Guido van Rossum3bead091992-01-27 17:00:37 +0000268### stmt: simple_stmt | compound_stmt
269# Tested below
270
271### simple_stmt: small_stmt (';' small_stmt)* [';']
272print 'simple_stmt'
273x = 1; pass; del x
274
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000275### 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 +0000276# Tested below
277
278print 'expr_stmt' # (exprlist '=')* exprlist
2791
2801, 2, 3
281x = 1
282x = 1, 2, 3
283x = y = z = 1, 2, 3
284x, y, z = 1, 2, 3
285abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
286# NB these variables are deleted below
287
Jeremy Hylton47793992001-02-19 15:35:26 +0000288check_syntax("x + 1 = 1")
289check_syntax("a + 1 = b + 2")
290
Guido van Rossum3bead091992-01-27 17:00:37 +0000291print 'print_stmt' # 'print' (test ',')* [test]
292print 1, 2, 3
293print 1, 2, 3,
294print
295print 0 or 1, 0 or 1,
296print 0 or 1
297
Barry Warsawefc92ee2000-08-21 15:46:50 +0000298print 'extended print_stmt' # 'print' '>>' test ','
299import sys
300print >> sys.stdout, 1, 2, 3
301print >> sys.stdout, 1, 2, 3,
302print >> sys.stdout
303print >> sys.stdout, 0 or 1, 0 or 1,
304print >> sys.stdout, 0 or 1
305
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000306# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000307class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000308 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000309
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000310gulp = Gulp()
311print >> gulp, 1, 2, 3
312print >> gulp, 1, 2, 3,
313print >> gulp
314print >> gulp, 0 or 1, 0 or 1,
315print >> gulp, 0 or 1
316
317# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000318def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000319 oldstdout = sys.stdout
320 sys.stdout = Gulp()
321 try:
322 tellme(Gulp())
323 tellme()
324 finally:
325 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000326
327# we should see this once
328def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000329 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000330
331driver()
332
333# we should not see this at all
334def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000335 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000336
337driver()
338
Barry Warsawefc92ee2000-08-21 15:46:50 +0000339# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000340check_syntax('print ,')
341check_syntax('print >> x,')
342
Guido van Rossum3bead091992-01-27 17:00:37 +0000343print 'del_stmt' # 'del' exprlist
344del abc
345del x, y, (z, xyz)
346
347print 'pass_stmt' # 'pass'
348pass
349
350print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
351# Tested below
352
353print 'break_stmt' # 'break'
354while 1: break
355
356print 'continue_stmt' # 'continue'
357i = 1
358while i: i = 0; continue
359
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000360msg = ""
361while not msg:
362 msg = "continue + try/except ok"
363 try:
364 continue
365 msg = "continue failed to continue inside try"
366 except:
367 msg = "continue inside try called except block"
368print msg
369
370msg = ""
371while not msg:
372 msg = "finally block not called"
373 try:
374 continue
375 finally:
376 msg = "continue + try/finally ok"
377print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000378
Thomas Wouters80d373c2001-09-26 12:43:39 +0000379
380# This test warrants an explanation. It is a test specifically for SF bugs
381# #463359 and #462937. The bug is that a 'break' statement executed or
382# exception raised inside a try/except inside a loop, *after* a continue
383# statement has been executed in that loop, will cause the wrong number of
384# arguments to be popped off the stack and the instruction pointer reset to
385# a very small number (usually 0.) Because of this, the following test
386# *must* written as a function, and the tracking vars *must* be function
387# arguments with default values. Otherwise, the test will loop and loop.
388
389print "testing continue and break in try/except in loop"
390def test_break_continue_loop(extra_burning_oil = 1, count=0):
391 big_hippo = 2
392 while big_hippo:
393 count += 1
394 try:
395 if extra_burning_oil and big_hippo == 1:
396 extra_burning_oil -= 1
397 break
398 big_hippo -= 1
399 continue
400 except:
401 raise
402 if count > 2 or big_hippo <> 1:
403 print "continue then break in try/except in loop broken!"
404test_break_continue_loop()
405
Guido van Rossum3bead091992-01-27 17:00:37 +0000406print 'return_stmt' # 'return' [testlist]
407def g1(): return
408def g2(): return 1
409g1()
410x = g2()
411
412print 'raise_stmt' # 'raise' test [',' test]
413try: raise RuntimeError, 'just testing'
414except RuntimeError: pass
415try: raise KeyboardInterrupt
416except KeyboardInterrupt: pass
417
418print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000419import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000420import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000421from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000422from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000423from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000424
425print 'global_stmt' # 'global' NAME (',' NAME)*
426def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000427 global a
428 global a, b
429 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000430
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000431print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
432def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000433 z = None
434 del z
435 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000436 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000437 del z
438 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000439 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000440 z = None
441 del z
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000442 import types
443 if hasattr(types, "UnicodeType"):
444 exec r"""if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000445 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000446 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000447 del z
448 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000449 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000450"""
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000451f()
452g = {}
453exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000454if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000455if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000456g = {}
457l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000458
459import warnings
460warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000461exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000462if g.has_key('__builtins__'): del g['__builtins__']
463if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000464if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000465
466
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000467print "assert_stmt" # assert_stmt: 'assert' test [',' test]
468assert 1
469assert 1, 1
470assert lambda x:x
471assert 1, lambda x:x+1
472
Guido van Rossum3bead091992-01-27 17:00:37 +0000473### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
474# Tested below
475
476print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
477if 1: pass
478if 1: pass
479else: pass
480if 0: pass
481elif 0: pass
482if 0: pass
483elif 0: pass
484elif 0: pass
485elif 0: pass
486else: pass
487
488print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
489while 0: pass
490while 0: pass
491else: pass
492
493print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000494for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000495for i, j, k in (): pass
496else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000497class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000498 def __init__(self, max):
499 self.max = max
500 self.sofar = []
501 def __len__(self): return len(self.sofar)
502 def __getitem__(self, i):
503 if not 0 <= i < self.max: raise IndexError
504 n = len(self.sofar)
505 while n <= i:
506 self.sofar.append(n*n)
507 n = n+1
508 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509n = 0
510for x in Squares(10): n = n+x
511if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000512
Guido van Rossumb6775db1994-08-01 11:34:53 +0000513print 'try_stmt'
514### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
515### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000516### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000517try:
Fred Drake004d5e62000-10-23 17:22:08 +0000518 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000519except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000520 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000521else:
Fred Drake004d5e62000-10-23 17:22:08 +0000522 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000523try: 1/0
524except EOFError: pass
525except TypeError, msg: pass
526except RuntimeError, msg: pass
527except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000528else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000529try: 1/0
530except (EOFError, TypeError, ZeroDivisionError): pass
531try: 1/0
532except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000533try: pass
534finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000535
536print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
537if 1: pass
538if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000539 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000540if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000541 #
542 #
543 #
544 pass
545 pass
546 #
547 pass
548 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000549
550print 'test'
551### and_test ('or' and_test)*
552### and_test: not_test ('and' not_test)*
553### not_test: 'not' not_test | comparison
554if not 1: pass
555if 1 and 1: pass
556if 1 or 1: pass
557if not not not 1: pass
558if not 1 and 1 and 1: pass
559if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
560
561print 'comparison'
562### comparison: expr (comp_op expr)*
563### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
564if 1: pass
565x = (1 == 1)
566if 1 == 1: pass
567if 1 != 1: pass
568if 1 <> 1: pass
569if 1 < 1: pass
570if 1 > 1: pass
571if 1 <= 1: pass
572if 1 >= 1: pass
573if 1 is 1: pass
574if 1 is not 1: pass
575if 1 in (): pass
576if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000577if 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 +0000578
579print 'binary mask ops'
580x = 1 & 1
581x = 1 ^ 1
582x = 1 | 1
583
584print 'shift ops'
585x = 1 << 1
586x = 1 >> 1
587x = 1 << 1 >> 1
588
589print 'additive ops'
590x = 1
591x = 1 + 1
592x = 1 - 1 - 1
593x = 1 - 1 + 1 - 1 + 1
594
595print 'multiplicative ops'
596x = 1 * 1
597x = 1 / 1
598x = 1 % 1
599x = 1 / 1 * 1 % 1
600
601print 'unary ops'
602x = +1
603x = -1
604x = ~1
605x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
606x = -1*1/1 + 1*1 - ---1*1
607
608print 'selectors'
609### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
610### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000611f1()
612f2(1)
613f2(1,)
614f3(1, 2)
615f3(1, 2,)
616f4(1, (2, (3, 4)))
617v0()
618v0(1)
619v0(1,)
620v0(1,2)
621v0(1,2,3,4,5,6,7,8,9,0)
622v1(1)
623v1(1,)
624v1(1,2)
625v1(1,2,3)
626v1(1,2,3,4,5,6,7,8,9,0)
627v2(1,2)
628v2(1,2,3)
629v2(1,2,3,4)
630v2(1,2,3,4,5,6,7,8,9,0)
631v3(1,(2,3))
632v3(1,(2,3),4)
633v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000634print
Guido van Rossum3bead091992-01-27 17:00:37 +0000635import sys, time
636c = sys.path[0]
637x = time.time()
638x = sys.modules['time'].time()
639a = '01234'
640c = a[0]
641c = a[-1]
642s = a[0:5]
643s = a[:5]
644s = a[0:]
645s = a[:]
646s = a[-5:]
647s = a[:-1]
648s = a[-4:-3]
649
650print 'atoms'
651### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
652### dictmaker: test ':' test (',' test ':' test)* [',']
653
654x = (1)
655x = (1 or 2 or 3)
656x = (1 or 2 or 3, 2, 3)
657
658x = []
659x = [1]
660x = [1 or 2 or 3]
661x = [1 or 2 or 3, 2, 3]
662x = []
663
664x = {}
665x = {'one': 1}
666x = {'one': 1,}
667x = {'one' or 'two': 1 or 2}
668x = {'one': 1, 'two': 2}
669x = {'one': 1, 'two': 2,}
670x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
671
672x = `x`
673x = `1 or 2 or 3`
674x = x
675x = 'x'
676x = 123
677
678### exprlist: expr (',' expr)* [',']
679### testlist: test (',' test)* [',']
680# These have been exercised enough above
681
Guido van Rossum85f18201992-11-27 22:53:50 +0000682print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000683class B: pass
684class C1(B): pass
685class C2(B): pass
686class D(C1, C2, B): pass
687class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000688 def meth1(self): pass
689 def meth2(self, arg): pass
690 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000691
692# list comprehension tests
693nums = [1, 2, 3, 4, 5]
694strs = ["Apple", "Banana", "Coconut"]
695spcs = [" Apple", " Banana ", "Coco nut "]
696
697print [s.strip() for s in spcs]
698print [3 * x for x in nums]
699print [x for x in nums if x > 2]
700print [(i, s) for i in nums for s in strs]
701print [(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 +0000702
703def test_in_func(l):
704 return [None < x < 3 for x in l if x > 2]
705
706print test_in_func(nums)
707
Jeremy Hyltone241e292001-03-19 20:42:11 +0000708def test_nested_front():
709 print [[y for y in [x, x + 1]] for x in [1,3,5]]
710
711test_nested_front()
712
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000713check_syntax("[i, s for i in nums for s in strs]")
714check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000715
Skip Montanaro803d6e52000-08-12 18:09:51 +0000716suppliers = [
717 (1, "Boeing"),
718 (2, "Ford"),
719 (3, "Macdonalds")
720]
721
722parts = [
723 (10, "Airliner"),
724 (20, "Engine"),
725 (30, "Cheeseburger")
726]
727
728suppart = [
729 (1, 10), (1, 20), (2, 20), (3, 30)
730]
731
732print [
733 (sname, pname)
734 for (sno, sname) in suppliers
735 for (pno, pname) in parts
736 for (sp_sno, sp_pno) in suppart
737 if sno == sp_sno and pno == sp_pno
738]