blob: e5ba73e6d6a0b6e13a5827ca54414e6c7c3e2b76 [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
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000397 import types
398 if hasattr(types, "UnicodeType"):
399 exec r"""if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000400 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000401 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000402 del z
403 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000404 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumdbb718f2001-09-21 19:22:34 +0000405"""
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000406f()
407g = {}
408exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000409if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000410if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000411g = {}
412l = {}
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000413
414import warnings
415warnings.filterwarnings("ignore", "global statement", module="<string>")
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000416exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000417if g.has_key('__builtins__'): del g['__builtins__']
418if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000419if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000420
421
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000422print "assert_stmt" # assert_stmt: 'assert' test [',' test]
423assert 1
424assert 1, 1
425assert lambda x:x
426assert 1, lambda x:x+1
427
Guido van Rossum3bead091992-01-27 17:00:37 +0000428### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
429# Tested below
430
431print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
432if 1: pass
433if 1: pass
434else: pass
435if 0: pass
436elif 0: pass
437if 0: pass
438elif 0: pass
439elif 0: pass
440elif 0: pass
441else: pass
442
443print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
444while 0: pass
445while 0: pass
446else: pass
447
448print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000449for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000450for i, j, k in (): pass
451else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000452class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000453 def __init__(self, max):
454 self.max = max
455 self.sofar = []
456 def __len__(self): return len(self.sofar)
457 def __getitem__(self, i):
458 if not 0 <= i < self.max: raise IndexError
459 n = len(self.sofar)
460 while n <= i:
461 self.sofar.append(n*n)
462 n = n+1
463 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000464n = 0
465for x in Squares(10): n = n+x
466if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000467
Guido van Rossumb6775db1994-08-01 11:34:53 +0000468print 'try_stmt'
469### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
470### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000471### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000472try:
Fred Drake004d5e62000-10-23 17:22:08 +0000473 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000474except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000475 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000476else:
Fred Drake004d5e62000-10-23 17:22:08 +0000477 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000478try: 1/0
479except EOFError: pass
480except TypeError, msg: pass
481except RuntimeError, msg: pass
482except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000483else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000484try: 1/0
485except (EOFError, TypeError, ZeroDivisionError): pass
486try: 1/0
487except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000488try: pass
489finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000490
491print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
492if 1: pass
493if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000494 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000495if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000496 #
497 #
498 #
499 pass
500 pass
501 #
502 pass
503 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000504
505print 'test'
506### and_test ('or' and_test)*
507### and_test: not_test ('and' not_test)*
508### not_test: 'not' not_test | comparison
509if not 1: pass
510if 1 and 1: pass
511if 1 or 1: pass
512if not not not 1: pass
513if not 1 and 1 and 1: pass
514if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
515
516print 'comparison'
517### comparison: expr (comp_op expr)*
518### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
519if 1: pass
520x = (1 == 1)
521if 1 == 1: pass
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 is 1: pass
529if 1 is not 1: pass
530if 1 in (): pass
531if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000532if 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 +0000533
534print 'binary mask ops'
535x = 1 & 1
536x = 1 ^ 1
537x = 1 | 1
538
539print 'shift ops'
540x = 1 << 1
541x = 1 >> 1
542x = 1 << 1 >> 1
543
544print 'additive ops'
545x = 1
546x = 1 + 1
547x = 1 - 1 - 1
548x = 1 - 1 + 1 - 1 + 1
549
550print 'multiplicative ops'
551x = 1 * 1
552x = 1 / 1
553x = 1 % 1
554x = 1 / 1 * 1 % 1
555
556print 'unary ops'
557x = +1
558x = -1
559x = ~1
560x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
561x = -1*1/1 + 1*1 - ---1*1
562
563print 'selectors'
564### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
565### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000566f1()
567f2(1)
568f2(1,)
569f3(1, 2)
570f3(1, 2,)
571f4(1, (2, (3, 4)))
572v0()
573v0(1)
574v0(1,)
575v0(1,2)
576v0(1,2,3,4,5,6,7,8,9,0)
577v1(1)
578v1(1,)
579v1(1,2)
580v1(1,2,3)
581v1(1,2,3,4,5,6,7,8,9,0)
582v2(1,2)
583v2(1,2,3)
584v2(1,2,3,4)
585v2(1,2,3,4,5,6,7,8,9,0)
586v3(1,(2,3))
587v3(1,(2,3),4)
588v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000589print
Guido van Rossum3bead091992-01-27 17:00:37 +0000590import sys, time
591c = sys.path[0]
592x = time.time()
593x = sys.modules['time'].time()
594a = '01234'
595c = a[0]
596c = a[-1]
597s = a[0:5]
598s = a[:5]
599s = a[0:]
600s = a[:]
601s = a[-5:]
602s = a[:-1]
603s = a[-4:-3]
604
605print 'atoms'
606### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
607### dictmaker: test ':' test (',' test ':' test)* [',']
608
609x = (1)
610x = (1 or 2 or 3)
611x = (1 or 2 or 3, 2, 3)
612
613x = []
614x = [1]
615x = [1 or 2 or 3]
616x = [1 or 2 or 3, 2, 3]
617x = []
618
619x = {}
620x = {'one': 1}
621x = {'one': 1,}
622x = {'one' or 'two': 1 or 2}
623x = {'one': 1, 'two': 2}
624x = {'one': 1, 'two': 2,}
625x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
626
627x = `x`
628x = `1 or 2 or 3`
629x = x
630x = 'x'
631x = 123
632
633### exprlist: expr (',' expr)* [',']
634### testlist: test (',' test)* [',']
635# These have been exercised enough above
636
Guido van Rossum85f18201992-11-27 22:53:50 +0000637print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000638class B: pass
639class C1(B): pass
640class C2(B): pass
641class D(C1, C2, B): pass
642class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000643 def meth1(self): pass
644 def meth2(self, arg): pass
645 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000646
647# list comprehension tests
648nums = [1, 2, 3, 4, 5]
649strs = ["Apple", "Banana", "Coconut"]
650spcs = [" Apple", " Banana ", "Coco nut "]
651
652print [s.strip() for s in spcs]
653print [3 * x for x in nums]
654print [x for x in nums if x > 2]
655print [(i, s) for i in nums for s in strs]
656print [(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 +0000657
658def test_in_func(l):
659 return [None < x < 3 for x in l if x > 2]
660
661print test_in_func(nums)
662
Jeremy Hyltone241e292001-03-19 20:42:11 +0000663def test_nested_front():
664 print [[y for y in [x, x + 1]] for x in [1,3,5]]
665
666test_nested_front()
667
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000668check_syntax("[i, s for i in nums for s in strs]")
669check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000670
Skip Montanaro803d6e52000-08-12 18:09:51 +0000671suppliers = [
672 (1, "Boeing"),
673 (2, "Ford"),
674 (3, "Macdonalds")
675]
676
677parts = [
678 (10, "Airliner"),
679 (20, "Engine"),
680 (30, "Cheeseburger")
681]
682
683suppart = [
684 (1, 10), (1, 20), (2, 20), (3, 30)
685]
686
687print [
688 (sname, pname)
689 for (sno, sname) in suppliers
690 for (pno, pname) in parts
691 for (sp_sno, sp_pno) in suppart
692 if sno == sp_sno and pno == sp_pno
693]