blob: 68cae81f5076f4076ba05e651ddd888121a2dae6 [file] [log] [blame]
Guido van Rossum5c971671996-07-22 15:23:25 +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'
26if 2147483647 != 017777777777: raise TestFailed, 'large positive int'
27try:
28 from sys import maxint
29except ImportError:
30 maxint = 2147483647
31if maxint == 2147483647:
32 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`
44elif eval('maxint == 9223372036854775807'):
45 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`
57else:
58 print 'Weird maxint value', maxint
59
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
84print '1.1.3 String literals'
85
Guido van Rossumde554ec1997-05-08 23:14:57 +000086##def assert(s):
87## if not s: raise TestFailed, 'see traceback'
Guido van Rossum5c971671996-07-22 15:23:25 +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)
95x = "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)
124
125
126print '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] ')'
Guido van Rossumaad67612000-05-08 17:31:04 +0000143### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144### | ('**'|'*' '*') NAME)
145### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Guido van Rossum5c971671996-07-22 15:23:25 +0000146### fpdef: NAME | '(' fplist ')'
147### fplist: fpdef (',' fpdef)* [',']
Guido van Rossumaad67612000-05-08 17:31:04 +0000148### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
149### argument: [test '='] test # Really [keyword '='] test
Guido van Rossum5c971671996-07-22 15:23:25 +0000150def f1(): pass
Guido van Rossumaad67612000-05-08 17:31:04 +0000151f1()
152f1(*())
153f1(*(), **{})
Guido van Rossum5c971671996-07-22 15:23:25 +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
163def d01(a=1): pass
164d01()
165d01(1)
Guido van Rossumaad67612000-05-08 17:31:04 +0000166d01(*(1,))
167d01(**{'a':2})
Guido van Rossum5c971671996-07-22 15:23:25 +0000168def d11(a, b=1): pass
169d11(1)
170d11(1, 2)
Guido van Rossumaad67612000-05-08 17:31:04 +0000171d11(1, **{'b':2})
Guido van Rossum5c971671996-07-22 15:23:25 +0000172def d21(a, b, c=1): pass
173d21(1, 2)
174d21(1, 2, 3)
Guido van Rossumaad67612000-05-08 17:31:04 +0000175d21(*(1, 2, 3))
176d21(1, *(2, 3))
177d21(1, 2, *(3,))
178d21(1, 2, **{'c':3})
Guido van Rossum5c971671996-07-22 15:23:25 +0000179def d02(a=1, b=2): pass
180d02()
181d02(1)
182d02(1, 2)
Guido van Rossumaad67612000-05-08 17:31:04 +0000183d02(*(1, 2))
184d02(1, *(2,))
185d02(1, **{'b':2})
186d02(**{'a': 1, 'b': 2})
Guido van Rossum5c971671996-07-22 15:23:25 +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)
Guido van Rossumaad67612000-05-08 17:31:04 +0000199d01v(*(1, 2, 3, 4))
200d01v(*(1,))
201d01v(**{'a':2})
Guido van Rossum5c971671996-07-22 15:23:25 +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)
Guido van Rossumaad67612000-05-08 17:31:04 +0000210d21v(*(1, 2, 3, 4))
211d21v(1, 2, **{'c': 3})
Guido van Rossum5c971671996-07-22 15:23:25 +0000212def d02v(a=1, b=2, *rest): pass
213d02v()
214d02v(1)
215d02v(1, 2)
216d02v(1, 2, 3)
Guido van Rossumaad67612000-05-08 17:31:04 +0000217d02v(1, *(2, 3, 4))
218d02v(**{'a': 1, 'b': 2})
Guido van Rossum5c971671996-07-22 15:23:25 +0000219def d12v(a, b=1, c=2, *rest): pass
220d12v(1)
221d12v(1, 2)
222d12v(1, 2, 3)
223d12v(1, 2, 3, 4)
Guido van Rossumaad67612000-05-08 17:31:04 +0000224d12v(*(1, 2, 3, 4))
225d12v(1, 2, *(3, 4, 5))
226d12v(1, *(2,), **{'c': 3})
Guido van Rossum5c971671996-07-22 15:23:25 +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)
Guido van Rossumaad67612000-05-08 17:31:04 +0000232d22v(*(1, 2, 3, 4))
233d22v(1, 2, *(3, 4, 5))
234d22v(1, *(2, 3), **{'d': 4})
Guido van Rossum5c971671996-07-22 15:23:25 +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
243### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
244# 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
Guido van Rossum8d691c82000-09-01 19:25:51 +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
271# test print >> None
272class Gulp:
273 def write(self, msg): pass
274
275def driver():
276 oldstdout = sys.stdout
277 sys.stdout = Gulp()
278 try:
279 tellme(Gulp())
280 tellme()
281 finally:
282 sys.stdout = oldstdout
283
284# we should see this once
285def tellme(file=sys.stdout):
286 print >> file, 'hello world'
287
288driver()
289
290# we should not see this at all
291def tellme(file=None):
292 print >> file, 'goodbye universe'
293
294driver()
295
296# syntax errors
297def check_syntax(statement):
298 try:
299 compile(statement, '<string>', 'exec')
300 except SyntaxError:
301 pass
302 else:
303 print 'Missing SyntaxError: "%s"' % statement
304check_syntax('print ,')
305check_syntax('print >> x,')
306
Guido van Rossum5c971671996-07-22 15:23:25 +0000307print 'del_stmt' # 'del' exprlist
308del abc
309del x, y, (z, xyz)
310
311print 'pass_stmt' # 'pass'
312pass
313
314print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
315# Tested below
316
317print 'break_stmt' # 'break'
318while 1: break
319
320print 'continue_stmt' # 'continue'
321i = 1
322while i: i = 0; continue
323
324print 'return_stmt' # 'return' [testlist]
325def g1(): return
326def g2(): return 1
327g1()
328x = g2()
329
330print 'raise_stmt' # 'raise' test [',' test]
331try: raise RuntimeError, 'just testing'
332except RuntimeError: pass
333try: raise KeyboardInterrupt
334except KeyboardInterrupt: pass
335
336print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
337import sys
338import time, sys
339from time import time
340from sys import *
341from sys import path, argv
342
343print 'global_stmt' # 'global' NAME (',' NAME)*
344def f():
345 global a
346 global a, b
347 global one, two, three, four, five, six, seven, eight, nine, ten
348
349print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
350def f():
351 z = None
352 del z
353 exec 'z=1+1\n'
354 if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
355 del z
356 exec 'z=1+1'
357 if z <> 2: raise TestFailed, 'exec \'z=1+1\''
Jeremy Hyltond635b1d2000-09-26 17:32:27 +0000358 z = None
359 del z
360 exec u'z=1+1\n'
361 if z <> 2: raise TestFailed, 'exec u\'z=1+1\'\\n'
362 del z
363 exec u'z=1+1'
364 if z <> 2: raise TestFailed, 'exec u\'z=1+1\''
Guido van Rossum5c971671996-07-22 15:23:25 +0000365f()
366g = {}
367exec 'z = 1' in g
368if g.has_key('__builtins__'): del g['__builtins__']
369if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
370g = {}
371l = {}
372exec 'global a; a = 1; b = 2' in g, l
373if g.has_key('__builtins__'): del g['__builtins__']
374if l.has_key('__builtins__'): del l['__builtins__']
375if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
376
377
378### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
379# Tested below
380
381print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
382if 1: pass
383if 1: pass
384else: pass
385if 0: pass
386elif 0: pass
387if 0: pass
388elif 0: pass
389elif 0: pass
390elif 0: pass
391else: pass
392
393print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
394while 0: pass
395while 0: pass
396else: pass
397
398print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
399for i in 1, 2, 3: pass
400for i, j, k in (): pass
401else: pass
402class Squares:
403 def __init__(self, max):
404 self.max = max
405 self.sofar = []
406 def __len__(self): return len(self.sofar)
407 def __getitem__(self, i):
408 if not 0 <= i < self.max: raise IndexError
409 n = len(self.sofar)
410 while n <= i:
411 self.sofar.append(n*n)
412 n = n+1
413 return self.sofar[i]
414n = 0
415for x in Squares(10): n = n+x
416if n != 285: raise TestFailed, 'for over growing sequence'
417
418print 'try_stmt'
419### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
420### | 'try' ':' suite 'finally' ':' suite
421### except_clause: 'except' [expr [',' expr]]
422try:
423 1/0
424except ZeroDivisionError:
425 pass
426else:
427 pass
428try: 1/0
429except EOFError: pass
430except TypeError, msg: pass
431except RuntimeError, msg: pass
432except: pass
433else: pass
434try: 1/0
435except (EOFError, TypeError, ZeroDivisionError): pass
436try: 1/0
437except (EOFError, TypeError, ZeroDivisionError), msg: pass
438try: pass
439finally: pass
440
441print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
442if 1: pass
443if 1:
444 pass
445if 1:
446 #
447 #
448 #
449 pass
450 pass
451 #
452 pass
453 #
454
455print 'test'
456### and_test ('or' and_test)*
457### and_test: not_test ('and' not_test)*
458### not_test: 'not' not_test | comparison
459if not 1: pass
460if 1 and 1: pass
461if 1 or 1: pass
462if not not not 1: pass
463if not 1 and 1 and 1: pass
464if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
465
466print 'comparison'
467### comparison: expr (comp_op expr)*
468### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
469if 1: pass
470x = (1 == 1)
471if 1 == 1: pass
472if 1 != 1: pass
473if 1 <> 1: pass
474if 1 < 1: pass
475if 1 > 1: pass
476if 1 <= 1: pass
477if 1 >= 1: pass
478if 1 is 1: pass
479if 1 is not 1: pass
480if 1 in (): pass
481if 1 not in (): pass
482if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
483
484print 'binary mask ops'
485x = 1 & 1
486x = 1 ^ 1
487x = 1 | 1
488
489print 'shift ops'
490x = 1 << 1
491x = 1 >> 1
492x = 1 << 1 >> 1
493
494print 'additive ops'
495x = 1
496x = 1 + 1
497x = 1 - 1 - 1
498x = 1 - 1 + 1 - 1 + 1
499
500print 'multiplicative ops'
501x = 1 * 1
502x = 1 / 1
503x = 1 % 1
504x = 1 / 1 * 1 % 1
505
506print 'unary ops'
507x = +1
508x = -1
509x = ~1
510x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
511x = -1*1/1 + 1*1 - ---1*1
512
513print 'selectors'
514### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
515### subscript: expr | [expr] ':' [expr]
516f1()
517f2(1)
518f2(1,)
519f3(1, 2)
520f3(1, 2,)
521f4(1, (2, (3, 4)))
522v0()
523v0(1)
524v0(1,)
525v0(1,2)
526v0(1,2,3,4,5,6,7,8,9,0)
527v1(1)
528v1(1,)
529v1(1,2)
530v1(1,2,3)
531v1(1,2,3,4,5,6,7,8,9,0)
532v2(1,2)
533v2(1,2,3)
534v2(1,2,3,4)
535v2(1,2,3,4,5,6,7,8,9,0)
536v3(1,(2,3))
537v3(1,(2,3),4)
538v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossumaad67612000-05-08 17:31:04 +0000539print
Guido van Rossum5c971671996-07-22 15:23:25 +0000540import sys, time
541c = sys.path[0]
542x = time.time()
543x = sys.modules['time'].time()
544a = '01234'
545c = a[0]
546c = a[-1]
547s = a[0:5]
548s = a[:5]
549s = a[0:]
550s = a[:]
551s = a[-5:]
552s = a[:-1]
553s = a[-4:-3]
554
555print 'atoms'
556### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
557### dictmaker: test ':' test (',' test ':' test)* [',']
558
559x = (1)
560x = (1 or 2 or 3)
561x = (1 or 2 or 3, 2, 3)
562
563x = []
564x = [1]
565x = [1 or 2 or 3]
566x = [1 or 2 or 3, 2, 3]
567x = []
568
569x = {}
570x = {'one': 1}
571x = {'one': 1,}
572x = {'one' or 'two': 1 or 2}
573x = {'one': 1, 'two': 2}
574x = {'one': 1, 'two': 2,}
575x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
576
577x = `x`
578x = `1 or 2 or 3`
579x = x
580x = 'x'
581x = 123
582
583### exprlist: expr (',' expr)* [',']
584### testlist: test (',' test)* [',']
585# These have been exercised enough above
586
587print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
588class B: pass
589class C1(B): pass
590class C2(B): pass
591class D(C1, C2, B): pass
592class C:
593 def meth1(self): pass
594 def meth2(self, arg): pass
595 def meth3(self, a1, a2): pass
Guido van Rossum8d691c82000-09-01 19:25:51 +0000596
597# list comprehension tests
598nums = [1, 2, 3, 4, 5]
599strs = ["Apple", "Banana", "Coconut"]
600spcs = [" Apple", " Banana ", "Coco nut "]
601
602print [s.strip() for s in spcs]
603print [3 * x for x in nums]
604print [x for x in nums if x > 2]
605print [(i, s) for i in nums for s in strs]
606print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
607try:
608 eval("[i, s for i in nums for s in strs]")
609 print "FAIL: should have raised a SyntaxError!"
610except SyntaxError:
611 print "good: got a SyntaxError as expected"
612
613try:
614 eval("[x if y]")
615 print "FAIL: should have raised a SyntaxError!"
616except SyntaxError:
617 print "good: got a SyntaxError as expected"
618
619suppliers = [
620 (1, "Boeing"),
621 (2, "Ford"),
622 (3, "Macdonalds")
623]
624
625parts = [
626 (10, "Airliner"),
627 (20, "Engine"),
628 (30, "Cheeseburger")
629]
630
631suppart = [
632 (1, 10), (1, 20), (2, 20), (3, 30)
633]
634
635print [
636 (sname, pname)
637 for (sno, sname) in suppliers
638 for (pno, pname) in parts
639 for (sp_sno, sp_pno) in suppart
640 if sno == sp_sno and pno == sp_pno
641]