blob: 254e00654017e65051809703af941de07b5c26d4 [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
Fred Drake132dce22000-12-12 23:11:42 +000015if x != 2: raise TestFailed, 'backslash for line continuation'
Guido van Rossum3bead091992-01-27 17:00:37 +000016
17# Backslash does not means continuation in comments :\
18x = 0
Fred Drake132dce22000-12-12 23:11:42 +000019if x != 0: raise TestFailed, 'backslash ending comment'
Guido van Rossum3bead091992-01-27 17:00:37 +000020
21print '1.1.2 Numeric literals'
22
23print '1.1.2.1 Plain integers'
Fred Drake132dce22000-12-12 23:11:42 +000024if 0xff != 255: raise TestFailed, 'hex int'
25if 0377 != 255: raise TestFailed, 'octal int'
Guido van Rossumdd8cb441993-12-29 15:33:08 +000026if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
27try:
Fred Drake004d5e62000-10-23 17:22:08 +000028 from sys import maxint
Guido van Rossumdd8cb441993-12-29 15:33:08 +000029except ImportError:
Fred Drake004d5e62000-10-23 17:22:08 +000030 maxint = 2147483647
Guido van Rossumdd8cb441993-12-29 15:33:08 +000031if maxint == 2147483647:
Fred Drake004d5e62000-10-23 17:22:08 +000032 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
33 # XXX -2147483648
34 if 037777777777 != -1: raise TestFailed, 'oct -1'
35 if 0xffffffff != -1: raise TestFailed, 'hex -1'
36 for s in '2147483648', '040000000000', '0x100000000':
37 try:
38 x = eval(s)
39 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000040 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000041elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000042 if eval('-9223372036854775807-1 != 01000000000000000000000'):
43 raise TestFailed, 'max negative int'
44 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
45 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
46 for s in '9223372036854775808', '02000000000000000000000', \
47 '0x10000000000000000':
48 try:
49 x = eval(s)
50 except OverflowError:
Tim Peters9f448152001-08-27 21:50:42 +000051 print "OverflowError on huge integer literal " + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000052else:
Fred Drake004d5e62000-10-23 17:22:08 +000053 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000054
55print '1.1.2.2 Long integers'
56x = 0L
57x = 0l
58x = 0xffffffffffffffffL
59x = 0xffffffffffffffffl
60x = 077777777777777777L
61x = 077777777777777777l
62x = 123456789012345678901234567890L
63x = 123456789012345678901234567890l
64
65print '1.1.2.3 Floating point'
66x = 3.14
67x = 314.
68x = 0.314
69# XXX x = 000.314
70x = .314
71x = 3e14
72x = 3E14
73x = 3e-14
74x = 3e+14
75x = 3.e14
76x = .3e14
77x = 3.1e4
78
Guido van Rossumb31c7f71993-11-11 10:31:23 +000079print '1.1.3 String literals'
80
Marc-André Lemburg36619082001-01-17 19:11:13 +000081x = ''; y = ""; verify(len(x) == 0 and x == y)
82x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
83x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000084x = "doesn't \"shrink\" does it"
85y = 'doesn\'t "shrink" does it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000086verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000087x = "does \"shrink\" doesn't it"
88y = 'does "shrink" doesn\'t it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000089verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000090x = """
91The "quick"
92brown fox
93jumps over
94the 'lazy' dog.
95"""
96y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Marc-André Lemburg36619082001-01-17 19:11:13 +000097verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000098y = '''
99The "quick"
100brown fox
101jumps over
102the 'lazy' dog.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000103'''; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104y = "\n\
105The \"quick\"\n\
106brown fox\n\
107jumps over\n\
108the 'lazy' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000109"; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000110y = '\n\
111The \"quick\"\n\
112brown fox\n\
113jumps over\n\
114the \'lazy\' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000115'; verify(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000116
117
Guido van Rossum3bead091992-01-27 17:00:37 +0000118print '1.2 Grammar'
119
120print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
121# XXX can't test in a script -- this rule is only used when interactive
122
123print 'file_input' # (NEWLINE | stmt)* ENDMARKER
124# Being tested as this very moment this very module
125
126print 'expr_input' # testlist NEWLINE
127# XXX Hard to test -- used only in calls to input()
128
129print 'eval_input' # testlist ENDMARKER
130x = eval('1, 0 or 1')
131
132print 'funcdef'
133### 'def' NAME parameters ':' suite
134### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000135### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
136### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000137### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000138### fpdef: NAME | '(' fplist ')'
139### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000140### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000141### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000142def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000143f1()
144f1(*())
145f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000146def f2(one_argument): pass
147def f3(two, arguments): pass
148def f4(two, (compound, (argument, list))): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000149def f5((compound, first), two): pass
150verify(f2.func_code.co_varnames == ('one_argument',))
151verify(f3.func_code.co_varnames == ('two', 'arguments'))
152verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 'argument',
153 'list'))
154verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000155def a1(one_arg,): pass
156def a2(two, args,): pass
157def v0(*rest): pass
158def v1(a, *rest): pass
159def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000160def v3(a, (b, c), *rest): return a, b, c, rest
161verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
162verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000163def d01(a=1): pass
164d01()
165d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000166d01(*(1,))
167d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168def d11(a, b=1): pass
169d11(1)
170d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000171d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000172def d21(a, b, c=1): pass
173d21(1, 2)
174d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000175d21(*(1, 2, 3))
176d21(1, *(2, 3))
177d21(1, 2, *(3,))
178d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179def d02(a=1, b=2): pass
180d02()
181d02(1)
182d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000183d02(*(1, 2))
184d02(1, *(2,))
185d02(1, **{'b':2})
186d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187def d12(a, b=1, c=2): pass
188d12(1)
189d12(1, 2)
190d12(1, 2, 3)
191def d22(a, b, c=1, d=2): pass
192d22(1, 2)
193d22(1, 2, 3)
194d22(1, 2, 3, 4)
195def d01v(a=1, *rest): pass
196d01v()
197d01v(1)
198d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000199d01v(*(1, 2, 3, 4))
200d01v(*(1,))
201d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000202def d11v(a, b=1, *rest): pass
203d11v(1)
204d11v(1, 2)
205d11v(1, 2, 3)
206def d21v(a, b, c=1, *rest): pass
207d21v(1, 2)
208d21v(1, 2, 3)
209d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000210d21v(*(1, 2, 3, 4))
211d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000212def d02v(a=1, b=2, *rest): pass
213d02v()
214d02v(1)
215d02v(1, 2)
216d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000217d02v(1, *(2, 3, 4))
218d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219def d12v(a, b=1, c=2, *rest): pass
220d12v(1)
221d12v(1, 2)
222d12v(1, 2, 3)
223d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000224d12v(*(1, 2, 3, 4))
225d12v(1, 2, *(3, 4, 5))
226d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227def d22v(a, b, c=1, d=2, *rest): pass
228d22v(1, 2)
229d22v(1, 2, 3)
230d22v(1, 2, 3, 4)
231d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000232d22v(*(1, 2, 3, 4))
233d22v(1, 2, *(3, 4, 5))
234d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000235
Jeremy Hylton619eea62001-01-25 20:12:27 +0000236### lambdef: 'lambda' [varargslist] ':' test
237print 'lambdef'
238l1 = lambda : 0
239verify(l1() == 0)
240l2 = lambda : a[d] # XXX just testing the expression
241l3 = lambda : [2 < x for x in [-1, 3, 0L]]
242verify(l3() == [0, 1, 0])
243l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
244verify(l4() == 1)
245l5 = lambda x, y, z=2: x + y + z
246verify(l5(1, 2) == 5)
247verify(l5(1, 2, 3) == 6)
248check_syntax("lambda x: x = 2")
249
Guido van Rossum3bead091992-01-27 17:00:37 +0000250### stmt: simple_stmt | compound_stmt
251# Tested below
252
253### simple_stmt: small_stmt (';' small_stmt)* [';']
254print 'simple_stmt'
255x = 1; pass; del x
256
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000257### 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 +0000258# Tested below
259
260print 'expr_stmt' # (exprlist '=')* exprlist
2611
2621, 2, 3
263x = 1
264x = 1, 2, 3
265x = y = z = 1, 2, 3
266x, y, z = 1, 2, 3
267abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
268# NB these variables are deleted below
269
Jeremy Hylton47793992001-02-19 15:35:26 +0000270check_syntax("x + 1 = 1")
271check_syntax("a + 1 = b + 2")
272
Guido van Rossum3bead091992-01-27 17:00:37 +0000273print 'print_stmt' # 'print' (test ',')* [test]
274print 1, 2, 3
275print 1, 2, 3,
276print
277print 0 or 1, 0 or 1,
278print 0 or 1
279
Barry Warsawefc92ee2000-08-21 15:46:50 +0000280print 'extended print_stmt' # 'print' '>>' test ','
281import sys
282print >> sys.stdout, 1, 2, 3
283print >> sys.stdout, 1, 2, 3,
284print >> sys.stdout
285print >> sys.stdout, 0 or 1, 0 or 1,
286print >> sys.stdout, 0 or 1
287
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000288# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000289class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000290 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000291
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000292gulp = Gulp()
293print >> gulp, 1, 2, 3
294print >> gulp, 1, 2, 3,
295print >> gulp
296print >> gulp, 0 or 1, 0 or 1,
297print >> gulp, 0 or 1
298
299# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000300def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000301 oldstdout = sys.stdout
302 sys.stdout = Gulp()
303 try:
304 tellme(Gulp())
305 tellme()
306 finally:
307 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000308
309# we should see this once
310def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000311 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000312
313driver()
314
315# we should not see this at all
316def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000317 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000318
319driver()
320
Barry Warsawefc92ee2000-08-21 15:46:50 +0000321# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000322check_syntax('print ,')
323check_syntax('print >> x,')
324
Guido van Rossum3bead091992-01-27 17:00:37 +0000325print 'del_stmt' # 'del' exprlist
326del abc
327del x, y, (z, xyz)
328
329print 'pass_stmt' # 'pass'
330pass
331
332print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
333# Tested below
334
335print 'break_stmt' # 'break'
336while 1: break
337
338print 'continue_stmt' # 'continue'
339i = 1
340while i: i = 0; continue
341
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000342msg = ""
343while not msg:
344 msg = "continue + try/except ok"
345 try:
346 continue
347 msg = "continue failed to continue inside try"
348 except:
349 msg = "continue inside try called except block"
350print msg
351
352msg = ""
353while not msg:
354 msg = "finally block not called"
355 try:
356 continue
357 finally:
358 msg = "continue + try/finally ok"
359print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000360
Guido van Rossum3bead091992-01-27 17:00:37 +0000361print 'return_stmt' # 'return' [testlist]
362def g1(): return
363def g2(): return 1
364g1()
365x = g2()
366
367print 'raise_stmt' # 'raise' test [',' test]
368try: raise RuntimeError, 'just testing'
369except RuntimeError: pass
370try: raise KeyboardInterrupt
371except KeyboardInterrupt: pass
372
373print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000374import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000375import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000377from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000378from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000379
380print 'global_stmt' # 'global' NAME (',' NAME)*
381def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000382 global a
383 global a, b
384 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000385
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000386print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
387def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000388 z = None
389 del z
390 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000391 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000392 del z
393 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000394 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000395 z = None
396 del z
397 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000398 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000399 del z
400 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000401 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000402f()
403g = {}
404exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000405if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000406if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000407g = {}
408l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000409
410import warnings
411warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000412exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000413if g.has_key('__builtins__'): del g['__builtins__']
414if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000415if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000416
417
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000418print "assert_stmt" # assert_stmt: 'assert' test [',' test]
419assert 1
420assert 1, 1
421assert lambda x:x
422assert 1, lambda x:x+1
423
Guido van Rossum3bead091992-01-27 17:00:37 +0000424### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
425# Tested below
426
427print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
428if 1: pass
429if 1: pass
430else: pass
431if 0: pass
432elif 0: pass
433if 0: pass
434elif 0: pass
435elif 0: pass
436elif 0: pass
437else: pass
438
439print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
440while 0: pass
441while 0: pass
442else: pass
443
444print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000445for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000446for i, j, k in (): pass
447else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000448class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000449 def __init__(self, max):
450 self.max = max
451 self.sofar = []
452 def __len__(self): return len(self.sofar)
453 def __getitem__(self, i):
454 if not 0 <= i < self.max: raise IndexError
455 n = len(self.sofar)
456 while n <= i:
457 self.sofar.append(n*n)
458 n = n+1
459 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000460n = 0
461for x in Squares(10): n = n+x
462if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000463
Guido van Rossumb6775db1994-08-01 11:34:53 +0000464print 'try_stmt'
465### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
466### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000467### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000468try:
Fred Drake004d5e62000-10-23 17:22:08 +0000469 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000470except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000471 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000472else:
Fred Drake004d5e62000-10-23 17:22:08 +0000473 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000474try: 1/0
475except EOFError: pass
476except TypeError, msg: pass
477except RuntimeError, msg: pass
478except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000479else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000480try: 1/0
481except (EOFError, TypeError, ZeroDivisionError): pass
482try: 1/0
483except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000484try: pass
485finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000486
487print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
488if 1: pass
489if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000490 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000491if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000492 #
493 #
494 #
495 pass
496 pass
497 #
498 pass
499 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000500
501print 'test'
502### and_test ('or' and_test)*
503### and_test: not_test ('and' not_test)*
504### not_test: 'not' not_test | comparison
505if not 1: pass
506if 1 and 1: pass
507if 1 or 1: pass
508if not not not 1: pass
509if not 1 and 1 and 1: pass
510if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
511
512print 'comparison'
513### comparison: expr (comp_op expr)*
514### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
515if 1: pass
516x = (1 == 1)
517if 1 == 1: pass
518if 1 != 1: pass
519if 1 <> 1: pass
520if 1 < 1: pass
521if 1 > 1: pass
522if 1 <= 1: pass
523if 1 >= 1: pass
524if 1 is 1: pass
525if 1 is not 1: pass
526if 1 in (): pass
527if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000528if 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 +0000529
530print 'binary mask ops'
531x = 1 & 1
532x = 1 ^ 1
533x = 1 | 1
534
535print 'shift ops'
536x = 1 << 1
537x = 1 >> 1
538x = 1 << 1 >> 1
539
540print 'additive ops'
541x = 1
542x = 1 + 1
543x = 1 - 1 - 1
544x = 1 - 1 + 1 - 1 + 1
545
546print 'multiplicative ops'
547x = 1 * 1
548x = 1 / 1
549x = 1 % 1
550x = 1 / 1 * 1 % 1
551
552print 'unary ops'
553x = +1
554x = -1
555x = ~1
556x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
557x = -1*1/1 + 1*1 - ---1*1
558
559print 'selectors'
560### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
561### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000562f1()
563f2(1)
564f2(1,)
565f3(1, 2)
566f3(1, 2,)
567f4(1, (2, (3, 4)))
568v0()
569v0(1)
570v0(1,)
571v0(1,2)
572v0(1,2,3,4,5,6,7,8,9,0)
573v1(1)
574v1(1,)
575v1(1,2)
576v1(1,2,3)
577v1(1,2,3,4,5,6,7,8,9,0)
578v2(1,2)
579v2(1,2,3)
580v2(1,2,3,4)
581v2(1,2,3,4,5,6,7,8,9,0)
582v3(1,(2,3))
583v3(1,(2,3),4)
584v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000585print
Guido van Rossum3bead091992-01-27 17:00:37 +0000586import sys, time
587c = sys.path[0]
588x = time.time()
589x = sys.modules['time'].time()
590a = '01234'
591c = a[0]
592c = a[-1]
593s = a[0:5]
594s = a[:5]
595s = a[0:]
596s = a[:]
597s = a[-5:]
598s = a[:-1]
599s = a[-4:-3]
600
601print 'atoms'
602### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
603### dictmaker: test ':' test (',' test ':' test)* [',']
604
605x = (1)
606x = (1 or 2 or 3)
607x = (1 or 2 or 3, 2, 3)
608
609x = []
610x = [1]
611x = [1 or 2 or 3]
612x = [1 or 2 or 3, 2, 3]
613x = []
614
615x = {}
616x = {'one': 1}
617x = {'one': 1,}
618x = {'one' or 'two': 1 or 2}
619x = {'one': 1, 'two': 2}
620x = {'one': 1, 'two': 2,}
621x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
622
623x = `x`
624x = `1 or 2 or 3`
625x = x
626x = 'x'
627x = 123
628
629### exprlist: expr (',' expr)* [',']
630### testlist: test (',' test)* [',']
631# These have been exercised enough above
632
Guido van Rossum85f18201992-11-27 22:53:50 +0000633print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000634class B: pass
635class C1(B): pass
636class C2(B): pass
637class D(C1, C2, B): pass
638class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000639 def meth1(self): pass
640 def meth2(self, arg): pass
641 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000642
643# list comprehension tests
644nums = [1, 2, 3, 4, 5]
645strs = ["Apple", "Banana", "Coconut"]
646spcs = [" Apple", " Banana ", "Coco nut "]
647
648print [s.strip() for s in spcs]
649print [3 * x for x in nums]
650print [x for x in nums if x > 2]
651print [(i, s) for i in nums for s in strs]
652print [(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 +0000653
654def test_in_func(l):
655 return [None < x < 3 for x in l if x > 2]
656
657print test_in_func(nums)
658
Jeremy Hyltone241e292001-03-19 20:42:11 +0000659def test_nested_front():
660 print [[y for y in [x, x + 1]] for x in [1,3,5]]
661
662test_nested_front()
663
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000664check_syntax("[i, s for i in nums for s in strs]")
665check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000666
Skip Montanaro803d6e52000-08-12 18:09:51 +0000667suppliers = [
668 (1, "Boeing"),
669 (2, "Ford"),
670 (3, "Macdonalds")
671]
672
673parts = [
674 (10, "Airliner"),
675 (20, "Engine"),
676 (30, "Cheeseburger")
677]
678
679suppart = [
680 (1, 10), (1, 20), (2, 20), (3, 30)
681]
682
683print [
684 (sname, pname)
685 for (sno, sname) in suppliers
686 for (pno, pname) in parts
687 for (sp_sno, sp_pno) in suppart
688 if sno == sp_sno and pno == sp_pno
689]