blob: 022398d10a1e4caa3fb39c5a1555153fdd19f21d [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
352print 'return_stmt' # 'return' [testlist]
353def g1(): return
354def g2(): return 1
355g1()
356x = g2()
357
358print 'raise_stmt' # 'raise' test [',' test]
359try: raise RuntimeError, 'just testing'
360except RuntimeError: pass
361try: raise KeyboardInterrupt
362except KeyboardInterrupt: pass
363
364print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000365import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000366import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000367from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000368from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000369from sys import path, argv
Jeremy Hyltonac25a382001-01-30 01:25:56 +0000370check_syntax("def f(): from sys import *")
371check_syntax("def f(): global time; import ")
Guido van Rossum3bead091992-01-27 17:00:37 +0000372
373print 'global_stmt' # 'global' NAME (',' NAME)*
374def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000375 global a
376 global a, b
377 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000378
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000379print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
380def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000381 z = None
382 del z
383 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000384 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000385 del z
386 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000387 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000388 z = None
389 del z
390 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000391 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000392 del z
393 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000394 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000395f()
396g = {}
397exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000398if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000399if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000400g = {}
401l = {}
402exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000403if g.has_key('__builtins__'): del g['__builtins__']
404if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000405if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000406
407
Guido van Rossum3bead091992-01-27 17:00:37 +0000408### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
409# Tested below
410
411print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
412if 1: pass
413if 1: pass
414else: pass
415if 0: pass
416elif 0: pass
417if 0: pass
418elif 0: pass
419elif 0: pass
420elif 0: pass
421else: pass
422
423print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
424while 0: pass
425while 0: pass
426else: pass
427
428print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000429for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000430for i, j, k in (): pass
431else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000432class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000433 def __init__(self, max):
434 self.max = max
435 self.sofar = []
436 def __len__(self): return len(self.sofar)
437 def __getitem__(self, i):
438 if not 0 <= i < self.max: raise IndexError
439 n = len(self.sofar)
440 while n <= i:
441 self.sofar.append(n*n)
442 n = n+1
443 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000444n = 0
445for x in Squares(10): n = n+x
446if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000447
Guido van Rossumb6775db1994-08-01 11:34:53 +0000448print 'try_stmt'
449### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
450### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000451### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000452try:
Fred Drake004d5e62000-10-23 17:22:08 +0000453 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000454except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000455 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000456else:
Fred Drake004d5e62000-10-23 17:22:08 +0000457 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000458try: 1/0
459except EOFError: pass
460except TypeError, msg: pass
461except RuntimeError, msg: pass
462except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000463else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000464try: 1/0
465except (EOFError, TypeError, ZeroDivisionError): pass
466try: 1/0
467except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000468try: pass
469finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000470
471print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
472if 1: pass
473if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000474 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000475if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000476 #
477 #
478 #
479 pass
480 pass
481 #
482 pass
483 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000484
485print 'test'
486### and_test ('or' and_test)*
487### and_test: not_test ('and' not_test)*
488### not_test: 'not' not_test | comparison
489if not 1: pass
490if 1 and 1: pass
491if 1 or 1: pass
492if not not not 1: pass
493if not 1 and 1 and 1: pass
494if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
495
496print 'comparison'
497### comparison: expr (comp_op expr)*
498### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
499if 1: pass
500x = (1 == 1)
501if 1 == 1: pass
502if 1 != 1: pass
503if 1 <> 1: pass
504if 1 < 1: pass
505if 1 > 1: pass
506if 1 <= 1: pass
507if 1 >= 1: pass
508if 1 is 1: pass
509if 1 is not 1: pass
510if 1 in (): pass
511if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000512if 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 +0000513
514print 'binary mask ops'
515x = 1 & 1
516x = 1 ^ 1
517x = 1 | 1
518
519print 'shift ops'
520x = 1 << 1
521x = 1 >> 1
522x = 1 << 1 >> 1
523
524print 'additive ops'
525x = 1
526x = 1 + 1
527x = 1 - 1 - 1
528x = 1 - 1 + 1 - 1 + 1
529
530print 'multiplicative ops'
531x = 1 * 1
532x = 1 / 1
533x = 1 % 1
534x = 1 / 1 * 1 % 1
535
536print 'unary ops'
537x = +1
538x = -1
539x = ~1
540x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
541x = -1*1/1 + 1*1 - ---1*1
542
543print 'selectors'
544### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
545### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000546f1()
547f2(1)
548f2(1,)
549f3(1, 2)
550f3(1, 2,)
551f4(1, (2, (3, 4)))
552v0()
553v0(1)
554v0(1,)
555v0(1,2)
556v0(1,2,3,4,5,6,7,8,9,0)
557v1(1)
558v1(1,)
559v1(1,2)
560v1(1,2,3)
561v1(1,2,3,4,5,6,7,8,9,0)
562v2(1,2)
563v2(1,2,3)
564v2(1,2,3,4)
565v2(1,2,3,4,5,6,7,8,9,0)
566v3(1,(2,3))
567v3(1,(2,3),4)
568v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000569print
Guido van Rossum3bead091992-01-27 17:00:37 +0000570import sys, time
571c = sys.path[0]
572x = time.time()
573x = sys.modules['time'].time()
574a = '01234'
575c = a[0]
576c = a[-1]
577s = a[0:5]
578s = a[:5]
579s = a[0:]
580s = a[:]
581s = a[-5:]
582s = a[:-1]
583s = a[-4:-3]
584
585print 'atoms'
586### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
587### dictmaker: test ':' test (',' test ':' test)* [',']
588
589x = (1)
590x = (1 or 2 or 3)
591x = (1 or 2 or 3, 2, 3)
592
593x = []
594x = [1]
595x = [1 or 2 or 3]
596x = [1 or 2 or 3, 2, 3]
597x = []
598
599x = {}
600x = {'one': 1}
601x = {'one': 1,}
602x = {'one' or 'two': 1 or 2}
603x = {'one': 1, 'two': 2}
604x = {'one': 1, 'two': 2,}
605x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
606
607x = `x`
608x = `1 or 2 or 3`
609x = x
610x = 'x'
611x = 123
612
613### exprlist: expr (',' expr)* [',']
614### testlist: test (',' test)* [',']
615# These have been exercised enough above
616
Guido van Rossum85f18201992-11-27 22:53:50 +0000617print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000618class B: pass
619class C1(B): pass
620class C2(B): pass
621class D(C1, C2, B): pass
622class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000623 def meth1(self): pass
624 def meth2(self, arg): pass
625 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000626
627# list comprehension tests
628nums = [1, 2, 3, 4, 5]
629strs = ["Apple", "Banana", "Coconut"]
630spcs = [" Apple", " Banana ", "Coco nut "]
631
632print [s.strip() for s in spcs]
633print [3 * x for x in nums]
634print [x for x in nums if x > 2]
635print [(i, s) for i in nums for s in strs]
636print [(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 +0000637
638def test_in_func(l):
639 return [None < x < 3 for x in l if x > 2]
640
641print test_in_func(nums)
642
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000643check_syntax("[i, s for i in nums for s in strs]")
644check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000645
Skip Montanaro803d6e52000-08-12 18:09:51 +0000646suppliers = [
647 (1, "Boeing"),
648 (2, "Ford"),
649 (3, "Macdonalds")
650]
651
652parts = [
653 (10, "Airliner"),
654 (20, "Engine"),
655 (30, "Cheeseburger")
656]
657
658suppart = [
659 (1, 10), (1, 20), (2, 20), (3, 30)
660]
661
662print [
663 (sname, pname)
664 for (sno, sname) in suppliers
665 for (pno, pname) in parts
666 for (sp_sno, sp_pno) in suppart
667 if sno == sp_sno and pno == sp_pno
668]