blob: a8d26dcdcc4daf3ab21368bf202e5cc98e98eb3f [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
249### stmt: simple_stmt | compound_stmt
250# Tested below
251
252### simple_stmt: small_stmt (';' small_stmt)* [';']
253print 'simple_stmt'
254x = 1; pass; del x
255
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000256### 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 +0000257# Tested below
258
259print 'expr_stmt' # (exprlist '=')* exprlist
2601
2611, 2, 3
262x = 1
263x = 1, 2, 3
264x = y = z = 1, 2, 3
265x, y, z = 1, 2, 3
266abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
267# NB these variables are deleted below
268
269print 'print_stmt' # 'print' (test ',')* [test]
270print 1, 2, 3
271print 1, 2, 3,
272print
273print 0 or 1, 0 or 1,
274print 0 or 1
275
Barry Warsawefc92ee2000-08-21 15:46:50 +0000276print 'extended print_stmt' # 'print' '>>' test ','
277import sys
278print >> sys.stdout, 1, 2, 3
279print >> sys.stdout, 1, 2, 3,
280print >> sys.stdout
281print >> sys.stdout, 0 or 1, 0 or 1,
282print >> sys.stdout, 0 or 1
283
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000284# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000285class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000286 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000287
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000288gulp = Gulp()
289print >> gulp, 1, 2, 3
290print >> gulp, 1, 2, 3,
291print >> gulp
292print >> gulp, 0 or 1, 0 or 1,
293print >> gulp, 0 or 1
294
295# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000296def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000297 oldstdout = sys.stdout
298 sys.stdout = Gulp()
299 try:
300 tellme(Gulp())
301 tellme()
302 finally:
303 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000304
305# we should see this once
306def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000307 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000308
309driver()
310
311# we should not see this at all
312def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000313 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000314
315driver()
316
Barry Warsawefc92ee2000-08-21 15:46:50 +0000317# syntax errors
Barry Warsawefc92ee2000-08-21 15:46:50 +0000318check_syntax('print ,')
319check_syntax('print >> x,')
320
Guido van Rossum3bead091992-01-27 17:00:37 +0000321print 'del_stmt' # 'del' exprlist
322del abc
323del x, y, (z, xyz)
324
325print 'pass_stmt' # 'pass'
326pass
327
328print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
329# Tested below
330
331print 'break_stmt' # 'break'
332while 1: break
333
334print 'continue_stmt' # 'continue'
335i = 1
336while i: i = 0; continue
337
338print 'return_stmt' # 'return' [testlist]
339def g1(): return
340def g2(): return 1
341g1()
342x = g2()
343
344print 'raise_stmt' # 'raise' test [',' test]
345try: raise RuntimeError, 'just testing'
346except RuntimeError: pass
347try: raise KeyboardInterrupt
348except KeyboardInterrupt: pass
349
350print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000351import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000352import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000353from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000354from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000355from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000356
357print 'global_stmt' # 'global' NAME (',' NAME)*
358def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000359 global a
360 global a, b
361 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000362
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000363print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
364def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000365 z = None
366 del z
367 exec 'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000368 if z != 2: raise TestFailed, 'exec \'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000369 del z
370 exec 'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000371 if z != 2: raise TestFailed, 'exec \'z=1+1\''
Fred Drake004d5e62000-10-23 17:22:08 +0000372 z = None
373 del z
374 exec u'z=1+1\n'
Fred Drake132dce22000-12-12 23:11:42 +0000375 if z != 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
Fred Drake004d5e62000-10-23 17:22:08 +0000376 del z
377 exec u'z=1+1'
Fred Drake132dce22000-12-12 23:11:42 +0000378 if z != 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000379f()
380g = {}
381exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000382if g.has_key('__builtins__'): del g['__builtins__']
Fred Drake132dce22000-12-12 23:11:42 +0000383if g != {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000384g = {}
385l = {}
386exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000387if g.has_key('__builtins__'): del g['__builtins__']
388if l.has_key('__builtins__'): del l['__builtins__']
Jeremy Hyltone1bb5f92001-01-19 03:26:33 +0000389if (g, l) != ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g (%s), l (%s)' %(g,l)
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000390
391
Guido van Rossum3bead091992-01-27 17:00:37 +0000392### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
393# Tested below
394
395print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
396if 1: pass
397if 1: pass
398else: pass
399if 0: pass
400elif 0: pass
401if 0: pass
402elif 0: pass
403elif 0: pass
404elif 0: pass
405else: pass
406
407print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
408while 0: pass
409while 0: pass
410else: pass
411
412print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000413for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000414for i, j, k in (): pass
415else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000416class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000417 def __init__(self, max):
418 self.max = max
419 self.sofar = []
420 def __len__(self): return len(self.sofar)
421 def __getitem__(self, i):
422 if not 0 <= i < self.max: raise IndexError
423 n = len(self.sofar)
424 while n <= i:
425 self.sofar.append(n*n)
426 n = n+1
427 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000428n = 0
429for x in Squares(10): n = n+x
430if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000431
Guido van Rossumb6775db1994-08-01 11:34:53 +0000432print 'try_stmt'
433### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
434### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000435### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000436try:
Fred Drake004d5e62000-10-23 17:22:08 +0000437 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000438except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000439 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000440else:
Fred Drake004d5e62000-10-23 17:22:08 +0000441 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000442try: 1/0
443except EOFError: pass
444except TypeError, msg: pass
445except RuntimeError, msg: pass
446except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000447else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000448try: 1/0
449except (EOFError, TypeError, ZeroDivisionError): pass
450try: 1/0
451except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000452try: pass
453finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000454
455print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
456if 1: pass
457if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000458 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000459if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000460 #
461 #
462 #
463 pass
464 pass
465 #
466 pass
467 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000468
469print 'test'
470### and_test ('or' and_test)*
471### and_test: not_test ('and' not_test)*
472### not_test: 'not' not_test | comparison
473if not 1: pass
474if 1 and 1: pass
475if 1 or 1: pass
476if not not not 1: pass
477if not 1 and 1 and 1: pass
478if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
479
480print 'comparison'
481### comparison: expr (comp_op expr)*
482### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
483if 1: pass
484x = (1 == 1)
485if 1 == 1: pass
486if 1 != 1: pass
487if 1 <> 1: pass
488if 1 < 1: pass
489if 1 > 1: pass
490if 1 <= 1: pass
491if 1 >= 1: pass
492if 1 is 1: pass
493if 1 is not 1: pass
494if 1 in (): pass
495if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000496if 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 +0000497
498print 'binary mask ops'
499x = 1 & 1
500x = 1 ^ 1
501x = 1 | 1
502
503print 'shift ops'
504x = 1 << 1
505x = 1 >> 1
506x = 1 << 1 >> 1
507
508print 'additive ops'
509x = 1
510x = 1 + 1
511x = 1 - 1 - 1
512x = 1 - 1 + 1 - 1 + 1
513
514print 'multiplicative ops'
515x = 1 * 1
516x = 1 / 1
517x = 1 % 1
518x = 1 / 1 * 1 % 1
519
520print 'unary ops'
521x = +1
522x = -1
523x = ~1
524x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
525x = -1*1/1 + 1*1 - ---1*1
526
527print 'selectors'
528### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
529### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000530f1()
531f2(1)
532f2(1,)
533f3(1, 2)
534f3(1, 2,)
535f4(1, (2, (3, 4)))
536v0()
537v0(1)
538v0(1,)
539v0(1,2)
540v0(1,2,3,4,5,6,7,8,9,0)
541v1(1)
542v1(1,)
543v1(1,2)
544v1(1,2,3)
545v1(1,2,3,4,5,6,7,8,9,0)
546v2(1,2)
547v2(1,2,3)
548v2(1,2,3,4)
549v2(1,2,3,4,5,6,7,8,9,0)
550v3(1,(2,3))
551v3(1,(2,3),4)
552v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000553print
Guido van Rossum3bead091992-01-27 17:00:37 +0000554import sys, time
555c = sys.path[0]
556x = time.time()
557x = sys.modules['time'].time()
558a = '01234'
559c = a[0]
560c = a[-1]
561s = a[0:5]
562s = a[:5]
563s = a[0:]
564s = a[:]
565s = a[-5:]
566s = a[:-1]
567s = a[-4:-3]
568
569print 'atoms'
570### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
571### dictmaker: test ':' test (',' test ':' test)* [',']
572
573x = (1)
574x = (1 or 2 or 3)
575x = (1 or 2 or 3, 2, 3)
576
577x = []
578x = [1]
579x = [1 or 2 or 3]
580x = [1 or 2 or 3, 2, 3]
581x = []
582
583x = {}
584x = {'one': 1}
585x = {'one': 1,}
586x = {'one' or 'two': 1 or 2}
587x = {'one': 1, 'two': 2}
588x = {'one': 1, 'two': 2,}
589x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
590
591x = `x`
592x = `1 or 2 or 3`
593x = x
594x = 'x'
595x = 123
596
597### exprlist: expr (',' expr)* [',']
598### testlist: test (',' test)* [',']
599# These have been exercised enough above
600
Guido van Rossum85f18201992-11-27 22:53:50 +0000601print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000602class B: pass
603class C1(B): pass
604class C2(B): pass
605class D(C1, C2, B): pass
606class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000607 def meth1(self): pass
608 def meth2(self, arg): pass
609 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000610
611# list comprehension tests
612nums = [1, 2, 3, 4, 5]
613strs = ["Apple", "Banana", "Coconut"]
614spcs = [" Apple", " Banana ", "Coco nut "]
615
616print [s.strip() for s in spcs]
617print [3 * x for x in nums]
618print [x for x in nums if x > 2]
619print [(i, s) for i in nums for s in strs]
620print [(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 +0000621
622def test_in_func(l):
623 return [None < x < 3 for x in l if x > 2]
624
625print test_in_func(nums)
626
Jeremy Hylton92e9f292001-01-25 17:03:37 +0000627check_syntax("[i, s for i in nums for s in strs]")
628check_syntax("[x if y]")
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000629
Skip Montanaro803d6e52000-08-12 18:09:51 +0000630suppliers = [
631 (1, "Boeing"),
632 (2, "Ford"),
633 (3, "Macdonalds")
634]
635
636parts = [
637 (10, "Airliner"),
638 (20, "Engine"),
639 (30, "Cheeseburger")
640]
641
642suppart = [
643 (1, 10), (1, 20), (2, 20), (3, 30)
644]
645
646print [
647 (sname, pname)
648 for (sno, sname) in suppliers
649 for (pno, pname) in parts
650 for (sp_sno, sp_pno) in suppart
651 if sno == sp_sno and pno == sp_pno
652]