blob: 7dcad0790ce7cd6009b3d615f4960e00715aa6e5 [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
15if x <> 2: raise TestFailed, 'backslash for line continuation'
16
17# Backslash does not means continuation in comments :\
18x = 0
19if x <> 0: raise TestFailed, 'backslash ending comment'
20
21print '1.1.2 Numeric literals'
22
23print '1.1.2.1 Plain integers'
24if 0xff <> 255: raise TestFailed, 'hex int'
25if 0377 <> 255: raise TestFailed, 'octal int'
Guido van Rossumdd8cb441993-12-29 15:33:08 +000026if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
27try:
Fred Drake004d5e62000-10-23 17:22:08 +000028 from sys import maxint
Guido van Rossumdd8cb441993-12-29 15:33:08 +000029except ImportError:
Fred Drake004d5e62000-10-23 17:22:08 +000030 maxint = 2147483647
Guido van Rossumdd8cb441993-12-29 15:33:08 +000031if maxint == 2147483647:
Fred Drake004d5e62000-10-23 17:22:08 +000032 if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
33 # XXX -2147483648
34 if 037777777777 != -1: raise TestFailed, 'oct -1'
35 if 0xffffffff != -1: raise TestFailed, 'hex -1'
36 for s in '2147483648', '040000000000', '0x100000000':
37 try:
38 x = eval(s)
39 except OverflowError:
40 continue
41## raise TestFailed, \
42 print \
43 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000044elif eval('maxint == 9223372036854775807'):
Fred Drake004d5e62000-10-23 17:22:08 +000045 if eval('-9223372036854775807-1 != 01000000000000000000000'):
46 raise TestFailed, 'max negative int'
47 if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
48 if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
49 for s in '9223372036854775808', '02000000000000000000000', \
50 '0x10000000000000000':
51 try:
52 x = eval(s)
53 except OverflowError:
54 continue
55 raise TestFailed, \
56 'No OverflowError on huge integer literal ' + `s`
Guido van Rossumdd8cb441993-12-29 15:33:08 +000057else:
Fred Drake004d5e62000-10-23 17:22:08 +000058 print 'Weird maxint value', maxint
Guido van Rossum3bead091992-01-27 17:00:37 +000059
60print '1.1.2.2 Long integers'
61x = 0L
62x = 0l
63x = 0xffffffffffffffffL
64x = 0xffffffffffffffffl
65x = 077777777777777777L
66x = 077777777777777777l
67x = 123456789012345678901234567890L
68x = 123456789012345678901234567890l
69
70print '1.1.2.3 Floating point'
71x = 3.14
72x = 314.
73x = 0.314
74# XXX x = 000.314
75x = .314
76x = 3e14
77x = 3E14
78x = 3e-14
79x = 3e+14
80x = 3.e14
81x = .3e14
82x = 3.1e4
83
Guido van Rossumb31c7f71993-11-11 10:31:23 +000084print '1.1.3 String literals'
85
Guido van Rossum505043f1997-04-16 00:27:45 +000086##def assert(s):
Fred Drake004d5e62000-10-23 17:22:08 +000087## if not s: raise TestFailed, 'see traceback'
Guido van Rossumb31c7f71993-11-11 10:31:23 +000088
89x = ''; y = ""; assert(len(x) == 0 and x == y)
90x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
91x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
92x = "doesn't \"shrink\" does it"
93y = 'doesn\'t "shrink" does it'
94assert(len(x) == 24 and x == y)
Guido van Rossumb6775db1994-08-01 11:34:53 +000095x = "does \"shrink\" doesn't it"
96y = 'does "shrink" doesn\'t it'
97assert(len(x) == 24 and x == y)
98x = """
99The "quick"
100brown fox
101jumps over
102the 'lazy' dog.
103"""
104y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
105assert(x == y)
106y = '''
107The "quick"
108brown fox
109jumps over
110the 'lazy' dog.
111'''; assert(x == y)
112y = "\n\
113The \"quick\"\n\
114brown fox\n\
115jumps over\n\
116the 'lazy' dog.\n\
117"; assert(x == y)
118y = '\n\
119The \"quick\"\n\
120brown fox\n\
121jumps over\n\
122the \'lazy\' dog.\n\
123'; assert(x == y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000124
125
Guido van Rossum3bead091992-01-27 17:00:37 +0000126print '1.2 Grammar'
127
128print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
129# XXX can't test in a script -- this rule is only used when interactive
130
131print 'file_input' # (NEWLINE | stmt)* ENDMARKER
132# Being tested as this very moment this very module
133
134print 'expr_input' # testlist NEWLINE
135# XXX Hard to test -- used only in calls to input()
136
137print 'eval_input' # testlist ENDMARKER
138x = eval('1, 0 or 1')
139
140print 'funcdef'
141### 'def' NAME parameters ':' suite
142### parameters: '(' [varargslist] ')'
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000143### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144### | ('**'|'*' '*') NAME)
Fred Drake004d5e62000-10-23 17:22:08 +0000145### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum3bead091992-01-27 17:00:37 +0000146### fpdef: NAME | '(' fplist ')'
147### fplist: fpdef (',' fpdef)* [',']
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000148### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
Fred Drake004d5e62000-10-23 17:22:08 +0000149### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum3bead091992-01-27 17:00:37 +0000150def f1(): pass
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000151f1()
152f1(*())
153f1(*(), **{})
Guido van Rossum3bead091992-01-27 17:00:37 +0000154def f2(one_argument): pass
155def f3(two, arguments): pass
156def f4(two, (compound, (argument, list))): pass
157def a1(one_arg,): pass
158def a2(two, args,): pass
159def v0(*rest): pass
160def v1(a, *rest): pass
161def v2(a, b, *rest): pass
162def v3(a, (b, c), *rest): pass
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
236### stmt: simple_stmt | compound_stmt
237# Tested below
238
239### simple_stmt: small_stmt (';' small_stmt)* [';']
240print 'simple_stmt'
241x = 1; pass; del x
242
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000243### 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 +0000244# Tested below
245
246print 'expr_stmt' # (exprlist '=')* exprlist
2471
2481, 2, 3
249x = 1
250x = 1, 2, 3
251x = y = z = 1, 2, 3
252x, y, z = 1, 2, 3
253abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
254# NB these variables are deleted below
255
256print 'print_stmt' # 'print' (test ',')* [test]
257print 1, 2, 3
258print 1, 2, 3,
259print
260print 0 or 1, 0 or 1,
261print 0 or 1
262
Barry Warsawefc92ee2000-08-21 15:46:50 +0000263print 'extended print_stmt' # 'print' '>>' test ','
264import sys
265print >> sys.stdout, 1, 2, 3
266print >> sys.stdout, 1, 2, 3,
267print >> sys.stdout
268print >> sys.stdout, 0 or 1, 0 or 1,
269print >> sys.stdout, 0 or 1
270
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000271# test printing to an instance
Barry Warsaw9182b452000-08-29 04:57:10 +0000272class Gulp:
Fred Drake004d5e62000-10-23 17:22:08 +0000273 def write(self, msg): pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000274
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000275gulp = Gulp()
276print >> gulp, 1, 2, 3
277print >> gulp, 1, 2, 3,
278print >> gulp
279print >> gulp, 0 or 1, 0 or 1,
280print >> gulp, 0 or 1
281
282# test print >> None
Barry Warsaw9182b452000-08-29 04:57:10 +0000283def driver():
Fred Drake004d5e62000-10-23 17:22:08 +0000284 oldstdout = sys.stdout
285 sys.stdout = Gulp()
286 try:
287 tellme(Gulp())
288 tellme()
289 finally:
290 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000291
292# we should see this once
293def tellme(file=sys.stdout):
Fred Drake004d5e62000-10-23 17:22:08 +0000294 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000295
296driver()
297
298# we should not see this at all
299def tellme(file=None):
Fred Drake004d5e62000-10-23 17:22:08 +0000300 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000301
302driver()
303
Barry Warsawefc92ee2000-08-21 15:46:50 +0000304# syntax errors
305def check_syntax(statement):
Fred Drake004d5e62000-10-23 17:22:08 +0000306 try:
307 compile(statement, '<string>', 'exec')
308 except SyntaxError:
309 pass
310 else:
311 print 'Missing SyntaxError: "%s"' % statement
Barry Warsawefc92ee2000-08-21 15:46:50 +0000312check_syntax('print ,')
313check_syntax('print >> x,')
314
Guido van Rossum3bead091992-01-27 17:00:37 +0000315print 'del_stmt' # 'del' exprlist
316del abc
317del x, y, (z, xyz)
318
319print 'pass_stmt' # 'pass'
320pass
321
322print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
323# Tested below
324
325print 'break_stmt' # 'break'
326while 1: break
327
328print 'continue_stmt' # 'continue'
329i = 1
330while i: i = 0; continue
331
332print 'return_stmt' # 'return' [testlist]
333def g1(): return
334def g2(): return 1
335g1()
336x = g2()
337
338print 'raise_stmt' # 'raise' test [',' test]
339try: raise RuntimeError, 'just testing'
340except RuntimeError: pass
341try: raise KeyboardInterrupt
342except KeyboardInterrupt: pass
343
344print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
Guido van Rossum3bead091992-01-27 17:00:37 +0000345import sys
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000346import time, sys
Guido van Rossumb6775db1994-08-01 11:34:53 +0000347from time import time
Guido van Rossum3bead091992-01-27 17:00:37 +0000348from sys import *
Guido van Rossum51b1c1c1995-03-04 22:30:54 +0000349from sys import path, argv
Guido van Rossum3bead091992-01-27 17:00:37 +0000350
351print 'global_stmt' # 'global' NAME (',' NAME)*
352def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000353 global a
354 global a, b
355 global one, two, three, four, five, six, seven, eight, nine, ten
Guido van Rossum3bead091992-01-27 17:00:37 +0000356
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000357print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
358def f():
Fred Drake004d5e62000-10-23 17:22:08 +0000359 z = None
360 del z
361 exec 'z=1+1\n'
362 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
363 del z
364 exec 'z=1+1'
365 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
366 z = None
367 del z
368 exec u'z=1+1\n'
369 if z <> 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
370 del z
371 exec u'z=1+1'
372 if z <> 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000373f()
374g = {}
375exec 'z = 1' in g
Guido van Rossum1f976121995-01-10 10:34:21 +0000376if g.has_key('__builtins__'): del g['__builtins__']
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000377if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
378g = {}
379l = {}
380exec 'global a; a = 1; b = 2' in g, l
Guido van Rossum1f976121995-01-10 10:34:21 +0000381if g.has_key('__builtins__'): del g['__builtins__']
382if l.has_key('__builtins__'): del l['__builtins__']
Guido van Rossumb3b09c91993-10-22 14:24:22 +0000383if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
384
385
Guido van Rossum3bead091992-01-27 17:00:37 +0000386### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
387# Tested below
388
389print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
390if 1: pass
391if 1: pass
392else: pass
393if 0: pass
394elif 0: pass
395if 0: pass
396elif 0: pass
397elif 0: pass
398elif 0: pass
399else: pass
400
401print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
402while 0: pass
403while 0: pass
404else: pass
405
406print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
Guido van Rossum3bead091992-01-27 17:00:37 +0000407for i in 1, 2, 3: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000408for i, j, k in (): pass
409else: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000410class Squares:
Fred Drake004d5e62000-10-23 17:22:08 +0000411 def __init__(self, max):
412 self.max = max
413 self.sofar = []
414 def __len__(self): return len(self.sofar)
415 def __getitem__(self, i):
416 if not 0 <= i < self.max: raise IndexError
417 n = len(self.sofar)
418 while n <= i:
419 self.sofar.append(n*n)
420 n = n+1
421 return self.sofar[i]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000422n = 0
423for x in Squares(10): n = n+x
424if n != 285: raise TestFailed, 'for over growing sequence'
Guido van Rossum3bead091992-01-27 17:00:37 +0000425
Guido van Rossumb6775db1994-08-01 11:34:53 +0000426print 'try_stmt'
427### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
428### | 'try' ':' suite 'finally' ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000429### except_clause: 'except' [expr [',' expr]]
Guido van Rossum85f18201992-11-27 22:53:50 +0000430try:
Fred Drake004d5e62000-10-23 17:22:08 +0000431 1/0
Guido van Rossum85f18201992-11-27 22:53:50 +0000432except ZeroDivisionError:
Fred Drake004d5e62000-10-23 17:22:08 +0000433 pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000434else:
Fred Drake004d5e62000-10-23 17:22:08 +0000435 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000436try: 1/0
437except EOFError: pass
438except TypeError, msg: pass
439except RuntimeError, msg: pass
440except: pass
Guido van Rossumb6775db1994-08-01 11:34:53 +0000441else: pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000442try: 1/0
443except (EOFError, TypeError, ZeroDivisionError): pass
444try: 1/0
445except (EOFError, TypeError, ZeroDivisionError), msg: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000446try: pass
447finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000448
449print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
450if 1: pass
451if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000452 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000453if 1:
Fred Drake004d5e62000-10-23 17:22:08 +0000454 #
455 #
456 #
457 pass
458 pass
459 #
460 pass
461 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000462
463print 'test'
464### and_test ('or' and_test)*
465### and_test: not_test ('and' not_test)*
466### not_test: 'not' not_test | comparison
467if not 1: pass
468if 1 and 1: pass
469if 1 or 1: pass
470if not not not 1: pass
471if not 1 and 1 and 1: pass
472if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
473
474print 'comparison'
475### comparison: expr (comp_op expr)*
476### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
477if 1: pass
478x = (1 == 1)
479if 1 == 1: pass
480if 1 != 1: pass
481if 1 <> 1: pass
482if 1 < 1: pass
483if 1 > 1: pass
484if 1 <= 1: pass
485if 1 >= 1: pass
486if 1 is 1: pass
487if 1 is not 1: pass
488if 1 in (): pass
489if 1 not in (): pass
Guido van Rossum85f18201992-11-27 22:53:50 +0000490if 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 +0000491
492print 'binary mask ops'
493x = 1 & 1
494x = 1 ^ 1
495x = 1 | 1
496
497print 'shift ops'
498x = 1 << 1
499x = 1 >> 1
500x = 1 << 1 >> 1
501
502print 'additive ops'
503x = 1
504x = 1 + 1
505x = 1 - 1 - 1
506x = 1 - 1 + 1 - 1 + 1
507
508print 'multiplicative ops'
509x = 1 * 1
510x = 1 / 1
511x = 1 % 1
512x = 1 / 1 * 1 % 1
513
514print 'unary ops'
515x = +1
516x = -1
517x = ~1
518x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
519x = -1*1/1 + 1*1 - ---1*1
520
521print 'selectors'
522### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
523### subscript: expr | [expr] ':' [expr]
Guido van Rossum85f18201992-11-27 22:53:50 +0000524f1()
525f2(1)
526f2(1,)
527f3(1, 2)
528f3(1, 2,)
529f4(1, (2, (3, 4)))
530v0()
531v0(1)
532v0(1,)
533v0(1,2)
534v0(1,2,3,4,5,6,7,8,9,0)
535v1(1)
536v1(1,)
537v1(1,2)
538v1(1,2,3)
539v1(1,2,3,4,5,6,7,8,9,0)
540v2(1,2)
541v2(1,2,3)
542v2(1,2,3,4)
543v2(1,2,3,4,5,6,7,8,9,0)
544v3(1,(2,3))
545v3(1,(2,3),4)
546v3(1,(2,3),4,5,6,7,8,9,0)
Jeremy Hyltonaed0d8d2000-03-28 23:51:17 +0000547print
Guido van Rossum3bead091992-01-27 17:00:37 +0000548import sys, time
549c = sys.path[0]
550x = time.time()
551x = sys.modules['time'].time()
552a = '01234'
553c = a[0]
554c = a[-1]
555s = a[0:5]
556s = a[:5]
557s = a[0:]
558s = a[:]
559s = a[-5:]
560s = a[:-1]
561s = a[-4:-3]
562
563print 'atoms'
564### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
565### dictmaker: test ':' test (',' test ':' test)* [',']
566
567x = (1)
568x = (1 or 2 or 3)
569x = (1 or 2 or 3, 2, 3)
570
571x = []
572x = [1]
573x = [1 or 2 or 3]
574x = [1 or 2 or 3, 2, 3]
575x = []
576
577x = {}
578x = {'one': 1}
579x = {'one': 1,}
580x = {'one' or 'two': 1 or 2}
581x = {'one': 1, 'two': 2}
582x = {'one': 1, 'two': 2,}
583x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
584
585x = `x`
586x = `1 or 2 or 3`
587x = x
588x = 'x'
589x = 123
590
591### exprlist: expr (',' expr)* [',']
592### testlist: test (',' test)* [',']
593# These have been exercised enough above
594
Guido van Rossum85f18201992-11-27 22:53:50 +0000595print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
Guido van Rossum3bead091992-01-27 17:00:37 +0000596class B: pass
597class C1(B): pass
598class C2(B): pass
599class D(C1, C2, B): pass
600class C:
Fred Drake004d5e62000-10-23 17:22:08 +0000601 def meth1(self): pass
602 def meth2(self, arg): pass
603 def meth3(self, a1, a2): pass
Skip Montanaro803d6e52000-08-12 18:09:51 +0000604
605# list comprehension tests
606nums = [1, 2, 3, 4, 5]
607strs = ["Apple", "Banana", "Coconut"]
608spcs = [" Apple", " Banana ", "Coco nut "]
609
610print [s.strip() for s in spcs]
611print [3 * x for x in nums]
612print [x for x in nums if x > 2]
613print [(i, s) for i in nums for s in strs]
614print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
615try:
616 eval("[i, s for i in nums for s in strs]")
617 print "FAIL: should have raised a SyntaxError!"
618except SyntaxError:
619 print "good: got a SyntaxError as expected"
620
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000621try:
622 eval("[x if y]")
623 print "FAIL: should have raised a SyntaxError!"
624except SyntaxError:
Fred Drake004d5e62000-10-23 17:22:08 +0000625 print "good: got a SyntaxError as expected"
Skip Montanaro46dfa5f2000-08-22 02:43:07 +0000626
Skip Montanaro803d6e52000-08-12 18:09:51 +0000627suppliers = [
628 (1, "Boeing"),
629 (2, "Ford"),
630 (3, "Macdonalds")
631]
632
633parts = [
634 (10, "Airliner"),
635 (20, "Engine"),
636 (30, "Cheeseburger")
637]
638
639suppart = [
640 (1, 10), (1, 20), (2, 20), (3, 30)
641]
642
643print [
644 (sname, pname)
645 for (sno, sname) in suppliers
646 for (pno, pname) in parts
647 for (sp_sno, sp_pno) in suppart
648 if sno == sp_sno and pno == sp_pno
649]