blob: 53ae0705915148311327c18e90a5d11289bc420e [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
Jeremy Hylton92e9f292001-01-25 17:03:37 +00006def check_syntax(statement):
7 try:
8 compile(statement, '<string>', 'exec')
9 except SyntaxError:
10 print 'SyntaxError expected for "%s"' % statement
11 else:
12 print 'Missing SyntaxError: "%s"' % statement
13
Guido van Rossum3bead091992-01-27 17:00:37 +000014print '1. Parser'
15
16print '1.1 Tokens'
17
18print '1.1.1 Backslashes'
19
20# Backslash means line continuation:
21x = 1 \
22+ 1
Fred Drake132dce22000-12-12 23:11:42 +000023if x != 2: raise TestFailed, 'backslash for line continuation'
Guido van Rossum3bead091992-01-27 17:00:37 +000024
25# Backslash does not means continuation in comments :\
26x = 0
Fred Drake132dce22000-12-12 23:11:42 +000027if x != 0: raise TestFailed, 'backslash ending comment'
Guido van Rossum3bead091992-01-27 17:00:37 +000028
29print '1.1.2 Numeric literals'
30
31print '1.1.2.1 Plain integers'
Fred Drake132dce22000-12-12 23:11:42 +000032if 0xff != 255: raise TestFailed, 'hex int'
33if 0377 != 255: raise TestFailed, 'octal int'
Guido van Rossumdd8cb441993-12-29 15:33:08 +000034if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
35try:
Fred Drake004d5e62000-10-23 17:22:08 +000036 from sys import maxint
Guido van Rossumdd8cb441993-12-29 15:33:08 +000037except ImportError:
Fred Drake004d5e62000-10-23 17:22:08 +000038 maxint = 2147483647
Guido van Rossumdd8cb441993-12-29 15:33:08 +000039if maxint == 2147483647:
Fred Drake004d5e62000-10-23 17:22:08 +000040 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
41 # XXX -2147483648
42 if 037777777777 != -1: raise TestFailed, 'oct -1'
43 if 0xffffffff != -1: raise TestFailed, 'hex -1'
44 for s in '2147483648', '040000000000', '0x100000000':
45 try:
46 x = eval(s)
47 except OverflowError:
48 continue
49## raise TestFailed, \
50 print \
51 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000052elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000053 if eval('-9223372036854775807-1 != 01000000000000000000000'):
54 raise TestFailed, 'max negative int'
55 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
56 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
57 for s in '9223372036854775808', '02000000000000000000000', \
58 '0x10000000000000000':
59 try:
60 x = eval(s)
61 except OverflowError:
62 continue
63 raise TestFailed, \
64 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000065else:
Fred Drake004d5e62000-10-23 17:22:08 +000066 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000067
68print '1.1.2.2 Long integers'
69x = 0L
70x = 0l
71x = 0xffffffffffffffffL
72x = 0xffffffffffffffffl
73x = 077777777777777777L
74x = 077777777777777777l
75x = 123456789012345678901234567890L
76x = 123456789012345678901234567890l
77
78print '1.1.2.3 Floating point'
79x = 3.14
80x = 314.
81x = 0.314
82# XXX x = 000.314
83x = .314
84x = 3e14
85x = 3E14
86x = 3e-14
87x = 3e+14
88x = 3.e14
89x = .3e14
90x = 3.1e4
91
Guido van Rossumb31c7f71993-11-11 10:31:23 +000092print '1.1.3 String literals'
93
Marc-André Lemburg36619082001-01-17 19:11:13 +000094x = ''; y = ""; verify(len(x) == 0 and x == y)
95x = '\''; y = "'"; verify(len(x) == 1 and x == y and ord(x) == 39)
96x = '"'; y = "\""; verify(len(x) == 1 and x == y and ord(x) == 34)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000097x = "doesn't \"shrink\" does it"
98y = 'doesn\'t "shrink" does it'
Marc-André Lemburg36619082001-01-17 19:11:13 +000099verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000100x = "does \"shrink\" doesn't it"
101y = 'does "shrink" doesn\'t it'
Marc-André Lemburg36619082001-01-17 19:11:13 +0000102verify(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000103x = """
104The "quick"
105brown fox
106jumps over
107the 'lazy' dog.
108"""
109y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Marc-André Lemburg36619082001-01-17 19:11:13 +0000110verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111y = '''
112The "quick"
113brown fox
114jumps over
115the 'lazy' dog.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000116'''; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117y = "\n\
118The \"quick\"\n\
119brown fox\n\
120jumps over\n\
121the 'lazy' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000122"; verify(x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000123y = '\n\
124The \"quick\"\n\
125brown fox\n\
126jumps over\n\
127the \'lazy\' dog.\n\
Marc-André Lemburg36619082001-01-17 19:11:13 +0000128'; verify(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000129
130
Guido van Rossum3bead091992-01-27 17:00:37 +0000131print '1.2 Grammar'
132
133print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
134# XXX can't test in a script -- this rule is only used when interactive
135
136print 'file_input' # (NEWLINE | stmt)* ENDMARKER
137# Being tested as this very moment this very module
138
139print 'expr_input' # testlist NEWLINE
140# XXX Hard to test -- used only in calls to input()
141
142print 'eval_input' # testlist ENDMARKER
143x = eval('1, 0 or 1')
144
145print 'funcdef'
146### 'def' NAME parameters ':' suite
147### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000148### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
149### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000150### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000151### fpdef: NAME | '(' fplist ')'
152### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000153### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000154### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000155def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000156f1()
157f1(*())
158f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000159def f2(one_argument): pass
160def f3(two, arguments): pass
161def f4(two, (compound, (argument, list))): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000162def f5((compound, first), two): pass
163verify(f2.func_code.co_varnames == ('one_argument',))
164verify(f3.func_code.co_varnames == ('two', 'arguments'))
165verify(f4.func_code.co_varnames == ('two', '.2', 'compound', 'argument',
166 'list'))
167verify(f5.func_code.co_varnames == ('.0', 'two', 'compound', 'first'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000168def a1(one_arg,): pass
169def a2(two, args,): pass
170def v0(*rest): pass
171def v1(a, *rest): pass
172def v2(a, b, *rest): pass
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000173def v3(a, (b, c), *rest): return a, b, c, rest
174verify(v3.func_code.co_varnames == ('a', '.2', 'rest', 'b', 'c'))
175verify(v3(1, (2, 3), 4) == (1, 2, 3, (4,)))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176def d01(a=1): pass
177d01()
178d01(1)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000179d01(*(1,))
180d01(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181def d11(a, b=1): pass
182d11(1)
183d11(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000184d11(1, **{'b':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185def d21(a, b, c=1): pass
186d21(1, 2)
187d21(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000188d21(*(1, 2, 3))
189d21(1, *(2, 3))
190d21(1, 2, *(3,))
191d21(1, 2, **{'c':3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192def d02(a=1, b=2): pass
193d02()
194d02(1)
195d02(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000196d02(*(1, 2))
197d02(1, *(2,))
198d02(1, **{'b':2})
199d02(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000200def d12(a, b=1, c=2): pass
201d12(1)
202d12(1, 2)
203d12(1, 2, 3)
204def d22(a, b, c=1, d=2): pass
205d22(1, 2)
206d22(1, 2, 3)
207d22(1, 2, 3, 4)
208def d01v(a=1, *rest): pass
209d01v()
210d01v(1)
211d01v(1, 2)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000212d01v(*(1, 2, 3, 4))
213d01v(*(1,))
214d01v(**{'a':2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215def d11v(a, b=1, *rest): pass
216d11v(1)
217d11v(1, 2)
218d11v(1, 2, 3)
219def d21v(a, b, c=1, *rest): pass
220d21v(1, 2)
221d21v(1, 2, 3)
222d21v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000223d21v(*(1, 2, 3, 4))
224d21v(1, 2, **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000225def d02v(a=1, b=2, *rest): pass
226d02v()
227d02v(1)
228d02v(1, 2)
229d02v(1, 2, 3)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000230d02v(1, *(2, 3, 4))
231d02v(**{'a': 1, 'b': 2})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232def d12v(a, b=1, c=2, *rest): pass
233d12v(1)
234d12v(1, 2)
235d12v(1, 2, 3)
236d12v(1, 2, 3, 4)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000237d12v(*(1, 2, 3, 4))
238d12v(1, 2, *(3, 4, 5))
239d12v(1, *(2,), **{'c': 3})
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240def d22v(a, b, c=1, d=2, *rest): pass
241d22v(1, 2)
242d22v(1, 2, 3)
243d22v(1, 2, 3, 4)
244d22v(1, 2, 3, 4, 5)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000245d22v(*(1, 2, 3, 4))
246d22v(1, 2, *(3, 4, 5))
247d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum3bead091992-01-27 17:00:37 +0000248
Jeremy Hylton619eea62001-01-25 20:12:27 +0000249### lambdef: 'lambda' [varargslist] ':' test
250print 'lambdef'
251l1 = lambda : 0
252verify(l1() == 0)
253l2 = lambda : a[d] # XXX just testing the expression
254l3 = lambda : [2 < x for x in [-1, 3, 0L]]
255verify(l3() == [0, 1, 0])
256l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
257verify(l4() == 1)
258l5 = lambda x, y, z=2: x + y + z
259verify(l5(1, 2) == 5)
260verify(l5(1, 2, 3) == 6)
261check_syntax("lambda x: x = 2")
262
Guido van Rossum3bead091992-01-27 17:00:37 +0000263### stmt: simple_stmt | compound_stmt
264# Tested below
265
266### simple_stmt: small_stmt (';' small_stmt)* [';']
267print 'simple_stmt'
268x = 1; pass; del x
269
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000270### 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 +0000271# Tested below
272
273print 'expr_stmt' # (exprlist '=')* exprlist
2741
2751, 2, 3
276x = 1
277x = 1, 2, 3
278x = y = z = 1, 2, 3
279x, y, z = 1, 2, 3
280abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
281# NB these variables are deleted below
282
283print 'print_stmt' # 'print' (test ',')* [test]
284print 1, 2, 3
285print 1, 2, 3,
286print
287print 0 or 1, 0 or 1,
288print 0 or 1
289
Barry Warsawefc92ee2000-08-21 15:46:50 +0000290print 'extended print_stmt' # 'print' '>>' test ','
291import sys
292print >> sys.stdout, 1, 2, 3
293print >> sys.stdout, 1, 2, 3,
294print >> sys.stdout
295print >> sys.stdout, 0 or 1, 0 or 1,
296print >> sys.stdout, 0 or 1
297
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000298# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000299class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000300 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000301
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000302gulp = Gulp()
303print >> gulp, 1, 2, 3
304print >> gulp, 1, 2, 3,
305print >> gulp
306print >> gulp, 0 or 1, 0 or 1,
307print >> gulp, 0 or 1
308
309# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000310def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000311 oldstdout = sys.stdout
312 sys.stdout = Gulp()
313 try:
314 tellme(Gulp())
315 tellme()
316 finally:
317 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000318
319# we should see this once
320def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000321 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000322
323driver()
324
325# we should not see this at all
326def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000327 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000328
329driver()
330
Barry Warsawefc92ee2000-08-21 15:46:50 +0000331# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000332check_syntax('print ,')
333check_syntax('print >> x,')
334
Guido van Rossum3bead091992-01-27 17:00:37 +0000335print 'del_stmt' # 'del' exprlist
336del abc
337del x, y, (z, xyz)
338
339print 'pass_stmt' # 'pass'
340pass
341
342print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
343# Tested below
344
345print 'break_stmt' # 'break'
346while 1: break
347
348print 'continue_stmt' # 'continue'
349i = 1
350while i: i = 0; continue
351
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000352msg = ""
353while not msg:
354 msg = "continue + try/except ok"
355 try:
356 continue
357 msg = "continue failed to continue inside try"
358 except:
359 msg = "continue inside try called except block"
360print msg
361
362msg = ""
363while not msg:
364 msg = "finally block not called"
365 try:
366 continue
367 finally:
368 msg = "continue + try/finally ok"
369print msg
Tim Peters10fb3862001-02-09 20:17:14 +0000370
Guido van Rossum3bead091992-01-27 17:00:37 +0000371print 'return_stmt' # 'return' [testlist]
372def g1(): return
373def g2(): return 1
374g1()
375x = g2()
376
377print 'raise_stmt' # 'raise' test [',' test]
378try: raise RuntimeError, 'just testing'
379except RuntimeError: pass
380try: raise KeyboardInterrupt
381except KeyboardInterrupt: pass
382
383print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000384import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000385import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000386from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000387from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000388from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000389
390print 'global_stmt' # 'global' NAME (',' NAME)*
391def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000392 global a
393 global a, b
394 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000395
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000396print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
397def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000398 z = None
399 del z
400 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000401 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000402 del z
403 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000404 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000405 z = None
406 del z
407 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000408 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000409 del z
410 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000411 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000412f()
413g = {}
414exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000415if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000416if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000417g = {}
418l = {}
419exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000420if g.has_key('__builtins__'): del g['__builtins__']
421if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000422if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000423
424
Guido van Rossum3bead091992-01-27 17:00:37 +0000425### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
426# Tested below
427
428print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
429if 1: pass
430if 1: pass
431else: pass
432if 0: pass
433elif 0: pass
434if 0: pass
435elif 0: pass
436elif 0: pass
437elif 0: pass
438else: pass
439
440print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
441while 0: pass
442while 0: pass
443else: pass
444
445print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000446for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000447for i, j, k in (): pass
448else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000449class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000450 def __init__(self, max):
451 self.max = max
452 self.sofar = []
453 def __len__(self): return len(self.sofar)
454 def __getitem__(self, i):
455 if not 0 <= i < self.max: raise IndexError
456 n = len(self.sofar)
457 while n <= i:
458 self.sofar.append(n*n)
459 n = n+1
460 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000461n = 0
462for x in Squares(10): n = n+x
463if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000464
Guido van Rossumb6775db1994-08-01 11:34:53 +0000465print 'try_stmt'
466### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
467### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000468### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000469try:
Fred Drake004d5e62000-10-23 17:22:08 +0000470 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000471except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000472 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000473else:
Fred Drake004d5e62000-10-23 17:22:08 +0000474 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000475try: 1/0
476except EOFError: pass
477except TypeError, msg: pass
478except RuntimeError, msg: pass
479except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000480else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000481try: 1/0
482except (EOFError, TypeError, ZeroDivisionError): pass
483try: 1/0
484except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000485try: pass
486finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000487
488print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
489if 1: pass
490if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000491 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000492if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000493 #
494 #
495 #
496 pass
497 pass
498 #
499 pass
500 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000501
502print 'test'
503### and_test ('or' and_test)*
504### and_test: not_test ('and' not_test)*
505### not_test: 'not' not_test | comparison
506if not 1: pass
507if 1 and 1: pass
508if 1 or 1: pass
509if not not not 1: pass
510if not 1 and 1 and 1: pass
511if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
512
513print 'comparison'
514### comparison: expr (comp_op expr)*
515### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
516if 1: pass
517x = (1 == 1)
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 >= 1: pass
525if 1 is 1: pass
526if 1 is not 1: pass
527if 1 in (): pass
528if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000529if 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 +0000530
531print 'binary mask ops'
532x = 1 & 1
533x = 1 ^ 1
534x = 1 | 1
535
536print 'shift ops'
537x = 1 << 1
538x = 1 >> 1
539x = 1 << 1 >> 1
540
541print 'additive ops'
542x = 1
543x = 1 + 1
544x = 1 - 1 - 1
545x = 1 - 1 + 1 - 1 + 1
546
547print 'multiplicative ops'
548x = 1 * 1
549x = 1 / 1
550x = 1 % 1
551x = 1 / 1 * 1 % 1
552
553print 'unary ops'
554x = +1
555x = -1
556x = ~1
557x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
558x = -1*1/1 + 1*1 - ---1*1
559
560print 'selectors'
561### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
562### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000563f1()
564f2(1)
565f2(1,)
566f3(1, 2)
567f3(1, 2,)
568f4(1, (2, (3, 4)))
569v0()
570v0(1)
571v0(1,)
572v0(1,2)
573v0(1,2,3,4,5,6,7,8,9,0)
574v1(1)
575v1(1,)
576v1(1,2)
577v1(1,2,3)
578v1(1,2,3,4,5,6,7,8,9,0)
579v2(1,2)
580v2(1,2,3)
581v2(1,2,3,4)
582v2(1,2,3,4,5,6,7,8,9,0)
583v3(1,(2,3))
584v3(1,(2,3),4)
585v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000586print
Guido van Rossum3bead091992-01-27 17:00:37 +0000587import sys, time
588c = sys.path[0]
589x = time.time()
590x = sys.modules['time'].time()
591a = '01234'
592c = a[0]
593c = a[-1]
594s = a[0:5]
595s = a[:5]
596s = a[0:]
597s = a[:]
598s = a[-5:]
599s = a[:-1]
600s = a[-4:-3]
601
602print 'atoms'
603### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
604### dictmaker: test ':' test (',' test ':' test)* [',']
605
606x = (1)
607x = (1 or 2 or 3)
608x = (1 or 2 or 3, 2, 3)
609
610x = []
611x = [1]
612x = [1 or 2 or 3]
613x = [1 or 2 or 3, 2, 3]
614x = []
615
616x = {}
617x = {'one': 1}
618x = {'one': 1,}
619x = {'one' or 'two': 1 or 2}
620x = {'one': 1, 'two': 2}
621x = {'one': 1, 'two': 2,}
622x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
623
624x = `x`
625x = `1 or 2 or 3`
626x = x
627x = 'x'
628x = 123
629
630### exprlist: expr (',' expr)* [',']
631### testlist: test (',' test)* [',']
632# These have been exercised enough above
633
Guido van Rossum85f18201992-11-27 22:53:50 +0000634print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000635class B: pass
636class C1(B): pass
637class C2(B): pass
638class D(C1, C2, B): pass
639class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000640 def meth1(self): pass
641 def meth2(self, arg): pass
642 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000643
644# list comprehension tests
645nums = [1, 2, 3, 4, 5]
646strs = ["Apple", "Banana", "Coconut"]
647spcs = [" Apple", " Banana ", "Coco nut "]
648
649print [s.strip() for s in spcs]
650print [3 * x for x in nums]
651print [x for x in nums if x > 2]
652print [(i, s) for i in nums for s in strs]
653print [(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 +0000654
655def test_in_func(l):
656 return [None < x < 3 for x in l if x > 2]
657
658print test_in_func(nums)
659
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000660check_syntax("[i, s for i in nums for s in strs]")
661check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000662
Skip Montanaro803d6e52000-08-12 18:09:51 +0000663suppliers = [
664 (1, "Boeing"),
665 (2, "Ford"),
666 (3, "Macdonalds")
667]
668
669parts = [
670 (10, "Airliner"),
671 (20, "Engine"),
672 (30, "Cheeseburger")
673]
674
675suppart = [
676 (1, 10), (1, 20), (2, 20), (3, 30)
677]
678
679print [
680 (sname, pname)
681 for (sno, sname) in suppliers
682 for (pno, pname) in parts
683 for (sp_sno, sp_pno) in suppart
684 if sno == sp_sno and pno == sp_pno
685]