blob: 587d7ecfcf47763416439c680e053935b29a32cb [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 = {}
414exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000415if g.has_key('__builtins__'): del g['__builtins__']
416if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000417if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000418
419
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000420print "assert_stmt" # assert_stmt: 'assert' test [',' test]
421assert 1
422assert 1, 1
423assert lambda x:x
424assert 1, lambda x:x+1
425
Guido van Rossum3bead091992-01-27 17:00:37 +0000426### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
427# Tested below
428
429print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
430if 1: pass
431if 1: pass
432else: pass
433if 0: pass
434elif 0: pass
435if 0: pass
436elif 0: pass
437elif 0: pass
438elif 0: pass
439else: pass
440
441print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
442while 0: pass
443while 0: pass
444else: pass
445
446print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000447for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000448for i, j, k in (): pass
449else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000450class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000451 def __init__(self, max):
452 self.max = max
453 self.sofar = []
454 def __len__(self): return len(self.sofar)
455 def __getitem__(self, i):
456 if not 0 <= i < self.max: raise IndexError
457 n = len(self.sofar)
458 while n <= i:
459 self.sofar.append(n*n)
460 n = n+1
461 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000462n = 0
463for x in Squares(10): n = n+x
464if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000465
Guido van Rossumb6775db1994-08-01 11:34:53 +0000466print 'try_stmt'
467### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
468### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000469### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000470try:
Fred Drake004d5e62000-10-23 17:22:08 +0000471 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000472except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000473 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474else:
Fred Drake004d5e62000-10-23 17:22:08 +0000475 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000476try: 1/0
477except EOFError: pass
478except TypeError, msg: pass
479except RuntimeError, msg: pass
480except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000481else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000482try: 1/0
483except (EOFError, TypeError, ZeroDivisionError): pass
484try: 1/0
485except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000486try: pass
487finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000488
489print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
490if 1: pass
491if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000492 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000493if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000494 #
495 #
496 #
497 pass
498 pass
499 #
500 pass
501 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000502
503print 'test'
504### and_test ('or' and_test)*
505### and_test: not_test ('and' not_test)*
506### not_test: 'not' not_test | comparison
507if not 1: pass
508if 1 and 1: pass
509if 1 or 1: pass
510if not not not 1: pass
511if not 1 and 1 and 1: pass
512if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
513
514print 'comparison'
515### comparison: expr (comp_op expr)*
516### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
517if 1: pass
518x = (1 == 1)
519if 1 == 1: pass
520if 1 != 1: pass
521if 1 <> 1: pass
522if 1 < 1: pass
523if 1 > 1: pass
524if 1 <= 1: pass
525if 1 >= 1: pass
526if 1 is 1: pass
527if 1 is not 1: pass
528if 1 in (): pass
529if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000530if 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 +0000531
532print 'binary mask ops'
533x = 1 & 1
534x = 1 ^ 1
535x = 1 | 1
536
537print 'shift ops'
538x = 1 << 1
539x = 1 >> 1
540x = 1 << 1 >> 1
541
542print 'additive ops'
543x = 1
544x = 1 + 1
545x = 1 - 1 - 1
546x = 1 - 1 + 1 - 1 + 1
547
548print 'multiplicative ops'
549x = 1 * 1
550x = 1 / 1
551x = 1 % 1
552x = 1 / 1 * 1 % 1
553
554print 'unary ops'
555x = +1
556x = -1
557x = ~1
558x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
559x = -1*1/1 + 1*1 - ---1*1
560
561print 'selectors'
562### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
563### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000564f1()
565f2(1)
566f2(1,)
567f3(1, 2)
568f3(1, 2,)
569f4(1, (2, (3, 4)))
570v0()
571v0(1)
572v0(1,)
573v0(1,2)
574v0(1,2,3,4,5,6,7,8,9,0)
575v1(1)
576v1(1,)
577v1(1,2)
578v1(1,2,3)
579v1(1,2,3,4,5,6,7,8,9,0)
580v2(1,2)
581v2(1,2,3)
582v2(1,2,3,4)
583v2(1,2,3,4,5,6,7,8,9,0)
584v3(1,(2,3))
585v3(1,(2,3),4)
586v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000587print
Guido van Rossum3bead091992-01-27 17:00:37 +0000588import sys, time
589c = sys.path[0]
590x = time.time()
591x = sys.modules['time'].time()
592a = '01234'
593c = a[0]
594c = a[-1]
595s = a[0:5]
596s = a[:5]
597s = a[0:]
598s = a[:]
599s = a[-5:]
600s = a[:-1]
601s = a[-4:-3]
602
603print 'atoms'
604### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
605### dictmaker: test ':' test (',' test ':' test)* [',']
606
607x = (1)
608x = (1 or 2 or 3)
609x = (1 or 2 or 3, 2, 3)
610
611x = []
612x = [1]
613x = [1 or 2 or 3]
614x = [1 or 2 or 3, 2, 3]
615x = []
616
617x = {}
618x = {'one': 1}
619x = {'one': 1,}
620x = {'one' or 'two': 1 or 2}
621x = {'one': 1, 'two': 2}
622x = {'one': 1, 'two': 2,}
623x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
624
625x = `x`
626x = `1 or 2 or 3`
627x = x
628x = 'x'
629x = 123
630
631### exprlist: expr (',' expr)* [',']
632### testlist: test (',' test)* [',']
633# These have been exercised enough above
634
Guido van Rossum85f18201992-11-27 22:53:50 +0000635print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000636class B: pass
637class C1(B): pass
638class C2(B): pass
639class D(C1, C2, B): pass
640class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000641 def meth1(self): pass
642 def meth2(self, arg): pass
643 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000644
645# list comprehension tests
646nums = [1, 2, 3, 4, 5]
647strs = ["Apple", "Banana", "Coconut"]
648spcs = [" Apple", " Banana ", "Coco nut "]
649
650print [s.strip() for s in spcs]
651print [3 * x for x in nums]
652print [x for x in nums if x > 2]
653print [(i, s) for i in nums for s in strs]
654print [(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 +0000655
656def test_in_func(l):
657 return [None < x < 3 for x in l if x > 2]
658
659print test_in_func(nums)
660
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000661check_syntax("[i, s for i in nums for s in strs]")
662check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000663
Skip Montanaro803d6e52000-08-12 18:09:51 +0000664suppliers = [
665 (1, "Boeing"),
666 (2, "Ford"),
667 (3, "Macdonalds")
668]
669
670parts = [
671 (10, "Airliner"),
672 (20, "Engine"),
673 (30, "Cheeseburger")
674]
675
676suppart = [
677 (1, 10), (1, 20), (2, 20), (3, 30)
678]
679
680print [
681 (sname, pname)
682 for (sno, sname) in suppliers
683 for (pno, pname) in parts
684 for (sp_sno, sp_pno) in suppart
685 if sno == sp_sno and pno == sp_pno
686]