blob: 04b14318fd2189cfd8805121f55dfbe7fc3ffb94 [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:
40 continue
41## raise TestFailed, \
42 print \
43 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000044elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000045 if eval('-9223372036854775807-1 != 01000000000000000000000'):
46 raise TestFailed, 'max negative int'
47 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
48 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
49 for s in '9223372036854775808', '02000000000000000000000', \
50 '0x10000000000000000':
51 try:
52 x = eval(s)
53 except OverflowError:
54 continue
55 raise TestFailed, \
56 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000057else:
Fred Drake004d5e62000-10-23 17:22:08 +000058 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000059
60print '1.1.2.2 Long integers'
61x = 0L
62x = 0l
63x = 0xffffffffffffffffL
64x = 0xffffffffffffffffl
65x = 077777777777777777L
66x = 077777777777777777l
67x = 123456789012345678901234567890L
68x = 123456789012345678901234567890l
69
70print '1.1.2.3 Floating point'
71x = 3.14
72x = 314.
73x = 0.314
74# XXX x = 000.314
75x = .314
76x = 3e14
77x = 3E14
78x = 3e-14
79x = 3e+14
80x = 3.e14
81x = .3e14
82x = 3.1e4
83
Guido van Rossumb31c7f71993-11-11 10:31:23 +000084print '1.1.3 String literals'
85
Marc-André Lemburg36619082001-01-17 19:11:13 +000086x = ''; y = ""; verify(len(x) == 0 and x == y)
87x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
88x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000089x = "doesn't \"shrink\" does it"
90y = 'doesn\'t "shrink" does it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000091verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000092x = "does \"shrink\" doesn't it"
93y = 'does "shrink" doesn\'t 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 = """
96The "quick"
97brown fox
98jumps over
99the 'lazy' dog.
100"""
101y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Marc-André Lemburg36619082001-01-17 19:11:13 +0000102verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000103y = '''
104The "quick"
105brown fox
106jumps over
107the 'lazy' dog.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000108'''; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109y = "\n\
110The \"quick\"\n\
111brown fox\n\
112jumps over\n\
113the 'lazy' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000114"; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000115y = '\n\
116The \"quick\"\n\
117brown fox\n\
118jumps over\n\
119the \'lazy\' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000120'; verify(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000121
122
Guido van Rossum3bead091992-01-27 17:00:37 +0000123print '1.2 Grammar'
124
125print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
126# XXX can't test in a script -- this rule is only used when interactive
127
128print 'file_input' # (NEWLINE | stmt)* ENDMARKER
129# Being tested as this very moment this very module
130
131print 'expr_input' # testlist NEWLINE
132# XXX Hard to test -- used only in calls to input()
133
134print 'eval_input' # testlist ENDMARKER
135x = eval('1, 0 or 1')
136
137print 'funcdef'
138### 'def' NAME parameters ':' suite
139### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000140### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
141### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000142### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000143### fpdef: NAME | '(' fplist ')'
144### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000145### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000146### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000147def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000148f1()
149f1(*())
150f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000151def f2(one_argument): pass
152def f3(two, arguments): pass
153def f4(two, (compound, (argument, list))): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000154def f5((compound, first), two): pass
155verify(f2.func_code.co_varnames == ('one_argument',))
156verify(f3.func_code.co_varnames == ('two', 'arguments'))
157verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 'argument',
158 'list'))
159verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000160def a1(one_arg,): pass
161def a2(two, args,): pass
162def v0(*rest): pass
163def v1(a, *rest): pass
164def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000165def v3(a, (b, c), *rest): return a, b, c, rest
166verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
167verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000168def d01(a=1): pass
169d01()
170d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000171d01(*(1,))
172d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173def d11(a, b=1): pass
174d11(1)
175d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000176d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177def d21(a, b, c=1): pass
178d21(1, 2)
179d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000180d21(*(1, 2, 3))
181d21(1, *(2, 3))
182d21(1, 2, *(3,))
183d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184def d02(a=1, b=2): pass
185d02()
186d02(1)
187d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000188d02(*(1, 2))
189d02(1, *(2,))
190d02(1, **{'b':2})
191d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192def d12(a, b=1, c=2): pass
193d12(1)
194d12(1, 2)
195d12(1, 2, 3)
196def d22(a, b, c=1, d=2): pass
197d22(1, 2)
198d22(1, 2, 3)
199d22(1, 2, 3, 4)
200def d01v(a=1, *rest): pass
201d01v()
202d01v(1)
203d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000204d01v(*(1, 2, 3, 4))
205d01v(*(1,))
206d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207def d11v(a, b=1, *rest): pass
208d11v(1)
209d11v(1, 2)
210d11v(1, 2, 3)
211def d21v(a, b, c=1, *rest): pass
212d21v(1, 2)
213d21v(1, 2, 3)
214d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000215d21v(*(1, 2, 3, 4))
216d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000217def d02v(a=1, b=2, *rest): pass
218d02v()
219d02v(1)
220d02v(1, 2)
221d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000222d02v(1, *(2, 3, 4))
223d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000224def d12v(a, b=1, c=2, *rest): pass
225d12v(1)
226d12v(1, 2)
227d12v(1, 2, 3)
228d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000229d12v(*(1, 2, 3, 4))
230d12v(1, 2, *(3, 4, 5))
231d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232def d22v(a, b, c=1, d=2, *rest): pass
233d22v(1, 2)
234d22v(1, 2, 3)
235d22v(1, 2, 3, 4)
236d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000237d22v(*(1, 2, 3, 4))
238d22v(1, 2, *(3, 4, 5))
239d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000240
Jeremy Hylton619eea62001-01-25 20:12:27 +0000241### lambdef: 'lambda' [varargslist] ':' test
242print 'lambdef'
243l1 = lambda : 0
244verify(l1() == 0)
245l2 = lambda : a[d] # XXX just testing the expression
246l3 = lambda : [2 < x for x in [-1, 3, 0L]]
247verify(l3() == [0, 1, 0])
248l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
249verify(l4() == 1)
250l5 = lambda x, y, z=2: x + y + z
251verify(l5(1, 2) == 5)
252verify(l5(1, 2, 3) == 6)
253check_syntax("lambda x: x = 2")
254
Guido van Rossum3bead091992-01-27 17:00:37 +0000255### stmt: simple_stmt | compound_stmt
256# Tested below
257
258### simple_stmt: small_stmt (';' small_stmt)* [';']
259print 'simple_stmt'
260x = 1; pass; del x
261
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000262### 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 +0000263# Tested below
264
265print 'expr_stmt' # (exprlist '=')* exprlist
2661
2671, 2, 3
268x = 1
269x = 1, 2, 3
270x = y = z = 1, 2, 3
271x, y, z = 1, 2, 3
272abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
273# NB these variables are deleted below
274
Jeremy Hylton47793992001-02-19 15:35:26 +0000275check_syntax("x + 1 = 1")
276check_syntax("a + 1 = b + 2")
277
Guido van Rossum3bead091992-01-27 17:00:37 +0000278print 'print_stmt' # 'print' (test ',')* [test]
279print 1, 2, 3
280print 1, 2, 3,
281print
282print 0 or 1, 0 or 1,
283print 0 or 1
284
Barry Warsawefc92ee2000-08-21 15:46:50 +0000285print 'extended print_stmt' # 'print' '>>' test ','
286import sys
287print >> sys.stdout, 1, 2, 3
288print >> sys.stdout, 1, 2, 3,
289print >> sys.stdout
290print >> sys.stdout, 0 or 1, 0 or 1,
291print >> sys.stdout, 0 or 1
292
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000293# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000294class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000295 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000296
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000297gulp = Gulp()
298print >> gulp, 1, 2, 3
299print >> gulp, 1, 2, 3,
300print >> gulp
301print >> gulp, 0 or 1, 0 or 1,
302print >> gulp, 0 or 1
303
304# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000305def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000306 oldstdout = sys.stdout
307 sys.stdout = Gulp()
308 try:
309 tellme(Gulp())
310 tellme()
311 finally:
312 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000313
314# we should see this once
315def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000316 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000317
318driver()
319
320# we should not see this at all
321def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000322 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000323
324driver()
325
Barry Warsawefc92ee2000-08-21 15:46:50 +0000326# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000327check_syntax('print ,')
328check_syntax('print >> x,')
329
Guido van Rossum3bead091992-01-27 17:00:37 +0000330print 'del_stmt' # 'del' exprlist
331del abc
332del x, y, (z, xyz)
333
334print 'pass_stmt' # 'pass'
335pass
336
337print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
338# Tested below
339
340print 'break_stmt' # 'break'
341while 1: break
342
343print 'continue_stmt' # 'continue'
344i = 1
345while i: i = 0; continue
346
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000347msg = ""
348while not msg:
349 msg = "continue + try/except ok"
350 try:
351 continue
352 msg = "continue failed to continue inside try"
353 except:
354 msg = "continue inside try called except block"
355print msg
356
357msg = ""
358while not msg:
359 msg = "finally block not called"
360 try:
361 continue
362 finally:
363 msg = "continue + try/finally ok"
364print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000365
Guido van Rossum3bead091992-01-27 17:00:37 +0000366print 'return_stmt' # 'return' [testlist]
367def g1(): return
368def g2(): return 1
369g1()
370x = g2()
371
372print 'raise_stmt' # 'raise' test [',' test]
373try: raise RuntimeError, 'just testing'
374except RuntimeError: pass
375try: raise KeyboardInterrupt
376except KeyboardInterrupt: pass
377
378print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000379import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000380import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000381from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000382from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000383from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000384
385print 'global_stmt' # 'global' NAME (',' NAME)*
386def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000387 global a
388 global a, b
389 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000390
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000391print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
392def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000393 z = None
394 del z
395 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000396 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000397 del z
398 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000399 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000400 z = None
401 del z
402 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000403 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000404 del z
405 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000406 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000407f()
408g = {}
409exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000410if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000411if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000412g = {}
413l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000414
415import warnings
416warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000417exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000418if g.has_key('__builtins__'): del g['__builtins__']
419if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000420if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000421
422
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000423print "assert_stmt" # assert_stmt: 'assert' test [',' test]
424assert 1
425assert 1, 1
426assert lambda x:x
427assert 1, lambda x:x+1
428
Guido van Rossum3bead091992-01-27 17:00:37 +0000429### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
430# Tested below
431
432print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
433if 1: pass
434if 1: pass
435else: pass
436if 0: pass
437elif 0: pass
438if 0: pass
439elif 0: pass
440elif 0: pass
441elif 0: pass
442else: pass
443
444print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
445while 0: pass
446while 0: pass
447else: pass
448
449print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000450for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000451for i, j, k in (): pass
452else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000453class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000454 def __init__(self, max):
455 self.max = max
456 self.sofar = []
457 def __len__(self): return len(self.sofar)
458 def __getitem__(self, i):
459 if not 0 <= i < self.max: raise IndexError
460 n = len(self.sofar)
461 while n <= i:
462 self.sofar.append(n*n)
463 n = n+1
464 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000465n = 0
466for x in Squares(10): n = n+x
467if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000468
Guido van Rossumb6775db1994-08-01 11:34:53 +0000469print 'try_stmt'
470### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
471### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000472### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000473try:
Fred Drake004d5e62000-10-23 17:22:08 +0000474 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000475except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000476 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000477else:
Fred Drake004d5e62000-10-23 17:22:08 +0000478 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000479try: 1/0
480except EOFError: pass
481except TypeError, msg: pass
482except RuntimeError, msg: pass
483except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000484else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000485try: 1/0
486except (EOFError, TypeError, ZeroDivisionError): pass
487try: 1/0
488except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000489try: pass
490finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000491
492print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
493if 1: pass
494if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000495 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000496if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000497 #
498 #
499 #
500 pass
501 pass
502 #
503 pass
504 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000505
506print 'test'
507### and_test ('or' and_test)*
508### and_test: not_test ('and' not_test)*
509### not_test: 'not' not_test | comparison
510if not 1: pass
511if 1 and 1: pass
512if 1 or 1: pass
513if not not not 1: pass
514if not 1 and 1 and 1: pass
515if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
516
517print 'comparison'
518### comparison: expr (comp_op expr)*
519### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
520if 1: pass
521x = (1 == 1)
522if 1 == 1: pass
523if 1 != 1: pass
524if 1 <> 1: pass
525if 1 < 1: pass
526if 1 > 1: pass
527if 1 <= 1: pass
528if 1 >= 1: pass
529if 1 is 1: pass
530if 1 is not 1: pass
531if 1 in (): pass
532if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000533if 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 +0000534
535print 'binary mask ops'
536x = 1 & 1
537x = 1 ^ 1
538x = 1 | 1
539
540print 'shift ops'
541x = 1 << 1
542x = 1 >> 1
543x = 1 << 1 >> 1
544
545print 'additive ops'
546x = 1
547x = 1 + 1
548x = 1 - 1 - 1
549x = 1 - 1 + 1 - 1 + 1
550
551print 'multiplicative ops'
552x = 1 * 1
553x = 1 / 1
554x = 1 % 1
555x = 1 / 1 * 1 % 1
556
557print 'unary ops'
558x = +1
559x = -1
560x = ~1
561x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
562x = -1*1/1 + 1*1 - ---1*1
563
564print 'selectors'
565### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
566### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000567f1()
568f2(1)
569f2(1,)
570f3(1, 2)
571f3(1, 2,)
572f4(1, (2, (3, 4)))
573v0()
574v0(1)
575v0(1,)
576v0(1,2)
577v0(1,2,3,4,5,6,7,8,9,0)
578v1(1)
579v1(1,)
580v1(1,2)
581v1(1,2,3)
582v1(1,2,3,4,5,6,7,8,9,0)
583v2(1,2)
584v2(1,2,3)
585v2(1,2,3,4)
586v2(1,2,3,4,5,6,7,8,9,0)
587v3(1,(2,3))
588v3(1,(2,3),4)
589v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000590print
Guido van Rossum3bead091992-01-27 17:00:37 +0000591import sys, time
592c = sys.path[0]
593x = time.time()
594x = sys.modules['time'].time()
595a = '01234'
596c = a[0]
597c = a[-1]
598s = a[0:5]
599s = a[:5]
600s = a[0:]
601s = a[:]
602s = a[-5:]
603s = a[:-1]
604s = a[-4:-3]
605
606print 'atoms'
607### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
608### dictmaker: test ':' test (',' test ':' test)* [',']
609
610x = (1)
611x = (1 or 2 or 3)
612x = (1 or 2 or 3, 2, 3)
613
614x = []
615x = [1]
616x = [1 or 2 or 3]
617x = [1 or 2 or 3, 2, 3]
618x = []
619
620x = {}
621x = {'one': 1}
622x = {'one': 1,}
623x = {'one' or 'two': 1 or 2}
624x = {'one': 1, 'two': 2}
625x = {'one': 1, 'two': 2,}
626x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
627
628x = `x`
629x = `1 or 2 or 3`
630x = x
631x = 'x'
632x = 123
633
634### exprlist: expr (',' expr)* [',']
635### testlist: test (',' test)* [',']
636# These have been exercised enough above
637
Guido van Rossum85f18201992-11-27 22:53:50 +0000638print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000639class B: pass
640class C1(B): pass
641class C2(B): pass
642class D(C1, C2, B): pass
643class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000644 def meth1(self): pass
645 def meth2(self, arg): pass
646 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000647
648# list comprehension tests
649nums = [1, 2, 3, 4, 5]
650strs = ["Apple", "Banana", "Coconut"]
651spcs = [" Apple", " Banana ", "Coco nut "]
652
653print [s.strip() for s in spcs]
654print [3 * x for x in nums]
655print [x for x in nums if x > 2]
656print [(i, s) for i in nums for s in strs]
657print [(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 +0000658
659def test_in_func(l):
660 return [None < x < 3 for x in l if x > 2]
661
662print test_in_func(nums)
663
Jeremy Hyltone241e292001-03-19 20:42:11 +0000664def test_nested_front():
665 print [[y for y in [x, x + 1]] for x in [1,3,5]]
666
667test_nested_front()
668
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000669check_syntax("[i, s for i in nums for s in strs]")
670check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000671
Skip Montanaro803d6e52000-08-12 18:09:51 +0000672suppliers = [
673 (1, "Boeing"),
674 (2, "Ford"),
675 (3, "Macdonalds")
676]
677
678parts = [
679 (10, "Airliner"),
680 (20, "Engine"),
681 (30, "Cheeseburger")
682]
683
684suppart = [
685 (1, 10), (1, 20), (2, 20), (3, 30)
686]
687
688print [
689 (sname, pname)
690 for (sno, sname) in suppliers
691 for (pno, pname) in parts
692 for (sp_sno, sp_pno) in suppart
693 if sno == sp_sno and pno == sp_pno
694]