blob: 5f77c1d018bfb36b3fdc68d72f438d6ba969d8b5 [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
Ezio Melottid80b4bf2010-03-17 13:52:48 +00004from test.test_support import run_unittest, check_syntax_error, \
5 check_py3k_warnings
Georg Brandlc6fdec62006-10-28 13:10:17 +00006import unittest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00007import sys
Georg Brandlc6fdec62006-10-28 13:10:17 +00008# testing import *
9from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +000010
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000011
Georg Brandlc6fdec62006-10-28 13:10:17 +000012class TokenTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +000013
Georg Brandlc6fdec62006-10-28 13:10:17 +000014 def testBackslash(self):
15 # Backslash means line continuation:
16 x = 1 \
17 + 1
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000018 self.assertEqual(x, 2, 'backslash for line continuation')
Guido van Rossum3bead091992-01-27 17:00:37 +000019
Georg Brandlc6fdec62006-10-28 13:10:17 +000020 # Backslash does not means continuation in comments :\
21 x = 0
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000022 self.assertEqual(x, 0, 'backslash ending comment')
Guido van Rossum3bead091992-01-27 17:00:37 +000023
Georg Brandlc6fdec62006-10-28 13:10:17 +000024 def testPlainIntegers(self):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000025 self.assertEqual(0xff, 255)
26 self.assertEqual(0377, 255)
27 self.assertEqual(2147483647, 017777777777)
Georg Brandl14404b62008-01-19 19:27:05 +000028 # "0x" is not a valid literal
29 self.assertRaises(SyntaxError, eval, "0x")
Georg Brandlc6fdec62006-10-28 13:10:17 +000030 from sys import maxint
31 if maxint == 2147483647:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000032 self.assertEqual(-2147483647-1, -020000000000)
Georg Brandlc6fdec62006-10-28 13:10:17 +000033 # XXX -2147483648
Benjamin Peterson5c8da862009-06-30 22:57:08 +000034 self.assertTrue(037777777777 > 0)
35 self.assertTrue(0xffffffff > 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +000036 for s in '2147483648', '040000000000', '0x100000000':
37 try:
38 x = eval(s)
39 except OverflowError:
40 self.fail("OverflowError on huge integer literal %r" % s)
41 elif maxint == 9223372036854775807:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000042 self.assertEqual(-9223372036854775807-1, -01000000000000000000000)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000043 self.assertTrue(01777777777777777777777 > 0)
44 self.assertTrue(0xffffffffffffffff > 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +000045 for s in '9223372036854775808', '02000000000000000000000', \
46 '0x10000000000000000':
47 try:
48 x = eval(s)
49 except OverflowError:
50 self.fail("OverflowError on huge integer literal %r" % s)
51 else:
52 self.fail('Weird maxint value %r' % maxint)
Guido van Rossum3bead091992-01-27 17:00:37 +000053
Georg Brandlc6fdec62006-10-28 13:10:17 +000054 def testLongIntegers(self):
55 x = 0L
56 x = 0l
57 x = 0xffffffffffffffffL
58 x = 0xffffffffffffffffl
59 x = 077777777777777777L
60 x = 077777777777777777l
61 x = 123456789012345678901234567890L
62 x = 123456789012345678901234567890l
Guido van Rossum3bead091992-01-27 17:00:37 +000063
Georg Brandlc6fdec62006-10-28 13:10:17 +000064 def testFloats(self):
65 x = 3.14
66 x = 314.
67 x = 0.314
68 # XXX x = 000.314
69 x = .314
70 x = 3e14
71 x = 3E14
72 x = 3e-14
73 x = 3e+14
74 x = 3.e14
75 x = .3e14
76 x = 3.1e4
Guido van Rossum3bead091992-01-27 17:00:37 +000077
Benjamin Peterson93e51aa2014-06-07 12:36:39 -070078 def test_float_exponent_tokenization(self):
79 # See issue 21642.
80 self.assertEqual(1 if 1else 0, 1)
81 self.assertEqual(1 if 0else 0, 0)
82 self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
83
Georg Brandlc6fdec62006-10-28 13:10:17 +000084 def testStringLiterals(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000085 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
86 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
87 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
Georg Brandlc6fdec62006-10-28 13:10:17 +000088 x = "doesn't \"shrink\" does it"
89 y = 'doesn\'t "shrink" does it'
Benjamin Peterson5c8da862009-06-30 22:57:08 +000090 self.assertTrue(len(x) == 24 and x == y)
Georg Brandlc6fdec62006-10-28 13:10:17 +000091 x = "does \"shrink\" doesn't it"
92 y = 'does "shrink" doesn\'t it'
Benjamin Peterson5c8da862009-06-30 22:57:08 +000093 self.assertTrue(len(x) == 24 and x == y)
Georg Brandlc6fdec62006-10-28 13:10:17 +000094 x = """
Guido van Rossumb6775db1994-08-01 11:34:53 +000095The "quick"
96brown fox
97jumps over
98the 'lazy' dog.
99"""
Georg Brandlc6fdec62006-10-28 13:10:17 +0000100 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000101 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000102 y = '''
Guido van Rossumb6775db1994-08-01 11:34:53 +0000103The "quick"
104brown fox
105jumps over
106the 'lazy' dog.
Georg Brandlc6fdec62006-10-28 13:10:17 +0000107'''
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000108 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000109 y = "\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000110The \"quick\"\n\
111brown fox\n\
112jumps over\n\
113the 'lazy' dog.\n\
Georg Brandlc6fdec62006-10-28 13:10:17 +0000114"
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000115 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000116 y = '\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117The \"quick\"\n\
118brown fox\n\
119jumps over\n\
120the \'lazy\' dog.\n\
Georg Brandlc6fdec62006-10-28 13:10:17 +0000121'
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000122 self.assertEqual(x, y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000123
124
Georg Brandlc6fdec62006-10-28 13:10:17 +0000125class GrammarTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +0000126
Georg Brandlc6fdec62006-10-28 13:10:17 +0000127 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
128 # XXX can't test in a script -- this rule is only used when interactive
Tim Petersabd8a332006-11-03 02:32:46 +0000129
Georg Brandlc6fdec62006-10-28 13:10:17 +0000130 # file_input: (NEWLINE | stmt)* ENDMARKER
131 # Being tested as this very moment this very module
Tim Petersabd8a332006-11-03 02:32:46 +0000132
Georg Brandlc6fdec62006-10-28 13:10:17 +0000133 # expr_input: testlist NEWLINE
134 # XXX Hard to test -- used only in calls to input()
Guido van Rossum3bead091992-01-27 17:00:37 +0000135
Georg Brandlc6fdec62006-10-28 13:10:17 +0000136 def testEvalInput(self):
137 # testlist ENDMARKER
138 x = eval('1, 0 or 1')
Guido van Rossum3bead091992-01-27 17:00:37 +0000139
Georg Brandlc6fdec62006-10-28 13:10:17 +0000140 def testFuncdef(self):
141 ### 'def' NAME parameters ':' suite
142 ### parameters: '(' [varargslist] ')'
143 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
144 ### | ('**'|'*' '*') NAME)
145 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
146 ### fpdef: NAME | '(' fplist ')'
147 ### fplist: fpdef (',' fpdef)* [',']
148 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
149 ### argument: [test '='] test # Really [keyword '='] test
150 def f1(): pass
151 f1()
152 f1(*())
153 f1(*(), **{})
154 def f2(one_argument): pass
155 def f3(two, arguments): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000156 # Silence Py3k warning
157 exec('def f4(two, (compound, (argument, list))): pass')
158 exec('def f5((compound, first), two): pass')
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000159 self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
160 self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments'))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000161 if sys.platform.startswith('java'):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000162 self.assertEqual(f4.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000163 ('two', '(compound, (argument, list))', 'compound', 'argument',
164 'list',))
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000165 self.assertEqual(f5.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000166 ('(compound, first)', 'two', 'compound', 'first'))
167 else:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000168 self.assertEqual(f4.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000169 ('two', '.1', 'compound', 'argument', 'list'))
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000170 self.assertEqual(f5.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000171 ('.0', 'two', 'compound', 'first'))
172 def a1(one_arg,): pass
173 def a2(two, args,): pass
174 def v0(*rest): pass
175 def v1(a, *rest): pass
176 def v2(a, b, *rest): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000177 # Silence Py3k warning
178 exec('def v3(a, (b, c), *rest): return a, b, c, rest')
Guido van Rossum3bead091992-01-27 17:00:37 +0000179
Georg Brandlc6fdec62006-10-28 13:10:17 +0000180 f1()
181 f2(1)
182 f2(1,)
183 f3(1, 2)
184 f3(1, 2,)
185 f4(1, (2, (3, 4)))
186 v0()
187 v0(1)
188 v0(1,)
189 v0(1,2)
190 v0(1,2,3,4,5,6,7,8,9,0)
191 v1(1)
192 v1(1,)
193 v1(1,2)
194 v1(1,2,3)
195 v1(1,2,3,4,5,6,7,8,9,0)
196 v2(1,2)
197 v2(1,2,3)
198 v2(1,2,3,4)
199 v2(1,2,3,4,5,6,7,8,9,0)
200 v3(1,(2,3))
201 v3(1,(2,3),4)
202 v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000203
Georg Brandlc6fdec62006-10-28 13:10:17 +0000204 # ceval unpacks the formal arguments into the first argcount names;
205 # thus, the names nested inside tuples must appear after these names.
206 if sys.platform.startswith('java'):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000207 self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000208 else:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000209 self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
210 self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000211 def d01(a=1): pass
212 d01()
213 d01(1)
214 d01(*(1,))
215 d01(**{'a':2})
216 def d11(a, b=1): pass
217 d11(1)
218 d11(1, 2)
219 d11(1, **{'b':2})
220 def d21(a, b, c=1): pass
221 d21(1, 2)
222 d21(1, 2, 3)
223 d21(*(1, 2, 3))
224 d21(1, *(2, 3))
225 d21(1, 2, *(3,))
226 d21(1, 2, **{'c':3})
227 def d02(a=1, b=2): pass
228 d02()
229 d02(1)
230 d02(1, 2)
231 d02(*(1, 2))
232 d02(1, *(2,))
233 d02(1, **{'b':2})
234 d02(**{'a': 1, 'b': 2})
235 def d12(a, b=1, c=2): pass
236 d12(1)
237 d12(1, 2)
238 d12(1, 2, 3)
239 def d22(a, b, c=1, d=2): pass
240 d22(1, 2)
241 d22(1, 2, 3)
242 d22(1, 2, 3, 4)
243 def d01v(a=1, *rest): pass
244 d01v()
245 d01v(1)
246 d01v(1, 2)
247 d01v(*(1, 2, 3, 4))
248 d01v(*(1,))
249 d01v(**{'a':2})
250 def d11v(a, b=1, *rest): pass
251 d11v(1)
252 d11v(1, 2)
253 d11v(1, 2, 3)
254 def d21v(a, b, c=1, *rest): pass
255 d21v(1, 2)
256 d21v(1, 2, 3)
257 d21v(1, 2, 3, 4)
258 d21v(*(1, 2, 3, 4))
259 d21v(1, 2, **{'c': 3})
260 def d02v(a=1, b=2, *rest): pass
261 d02v()
262 d02v(1)
263 d02v(1, 2)
264 d02v(1, 2, 3)
265 d02v(1, *(2, 3, 4))
266 d02v(**{'a': 1, 'b': 2})
267 def d12v(a, b=1, c=2, *rest): pass
268 d12v(1)
269 d12v(1, 2)
270 d12v(1, 2, 3)
271 d12v(1, 2, 3, 4)
272 d12v(*(1, 2, 3, 4))
273 d12v(1, 2, *(3, 4, 5))
274 d12v(1, *(2,), **{'c': 3})
275 def d22v(a, b, c=1, d=2, *rest): pass
276 d22v(1, 2)
277 d22v(1, 2, 3)
278 d22v(1, 2, 3, 4)
279 d22v(1, 2, 3, 4, 5)
280 d22v(*(1, 2, 3, 4))
281 d22v(1, 2, *(3, 4, 5))
282 d22v(1, *(2, 3), **{'d': 4})
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000283 # Silence Py3k warning
284 exec('def d31v((x)): pass')
285 exec('def d32v((x,)): pass')
Georg Brandlc6fdec62006-10-28 13:10:17 +0000286 d31v(1)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000287 d32v((1,))
Guido van Rossum3bead091992-01-27 17:00:37 +0000288
Benjamin Peterson80f0ed52008-08-19 19:52:46 +0000289 # keyword arguments after *arglist
290 def f(*args, **kwargs):
291 return args, kwargs
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000292 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
Benjamin Peterson80f0ed52008-08-19 19:52:46 +0000293 {'x':2, 'y':5}))
294 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
295 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
296
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +0000297 # Check ast errors in *args and *kwargs
298 check_syntax_error(self, "f(*g(1=2))")
299 check_syntax_error(self, "f(**g(1=2))")
300
Georg Brandlc6fdec62006-10-28 13:10:17 +0000301 def testLambdef(self):
302 ### lambdef: 'lambda' [varargslist] ':' test
303 l1 = lambda : 0
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000304 self.assertEqual(l1(), 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000305 l2 = lambda : a[d] # XXX just testing the expression
306 l3 = lambda : [2 < x for x in [-1, 3, 0L]]
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000307 self.assertEqual(l3(), [0, 1, 0])
Georg Brandlc6fdec62006-10-28 13:10:17 +0000308 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000309 self.assertEqual(l4(), 1)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000310 l5 = lambda x, y, z=2: x + y + z
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000311 self.assertEqual(l5(1, 2), 5)
312 self.assertEqual(l5(1, 2, 3), 6)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000313 check_syntax_error(self, "lambda x: x = 2")
Georg Brandla161f602008-06-15 19:54:36 +0000314 check_syntax_error(self, "lambda (None,): None")
Jeremy Hylton619eea62001-01-25 20:12:27 +0000315
Georg Brandlc6fdec62006-10-28 13:10:17 +0000316 ### stmt: simple_stmt | compound_stmt
317 # Tested below
Guido van Rossum3bead091992-01-27 17:00:37 +0000318
Georg Brandlc6fdec62006-10-28 13:10:17 +0000319 def testSimpleStmt(self):
320 ### simple_stmt: small_stmt (';' small_stmt)* [';']
321 x = 1; pass; del x
322 def foo():
Ezio Melottic2077b02011-03-16 12:34:31 +0200323 # verify statements that end with semi-colons
Georg Brandlc6fdec62006-10-28 13:10:17 +0000324 x = 1; pass; del x;
325 foo()
Guido van Rossum3bead091992-01-27 17:00:37 +0000326
Georg Brandlc6fdec62006-10-28 13:10:17 +0000327 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
328 # Tested below
Guido van Rossum3bead091992-01-27 17:00:37 +0000329
Georg Brandlc6fdec62006-10-28 13:10:17 +0000330 def testExprStmt(self):
331 # (exprlist '=')* exprlist
332 1
333 1, 2, 3
334 x = 1
335 x = 1, 2, 3
336 x = y = z = 1, 2, 3
337 x, y, z = 1, 2, 3
338 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
Guido van Rossum3bead091992-01-27 17:00:37 +0000339
Georg Brandlc6fdec62006-10-28 13:10:17 +0000340 check_syntax_error(self, "x + 1 = 1")
341 check_syntax_error(self, "a + 1 = b + 2")
Jeremy Hylton47793992001-02-19 15:35:26 +0000342
Georg Brandlc6fdec62006-10-28 13:10:17 +0000343 def testPrintStmt(self):
344 # 'print' (test ',')* [test]
345 import StringIO
Guido van Rossum3bead091992-01-27 17:00:37 +0000346
Georg Brandlc6fdec62006-10-28 13:10:17 +0000347 # Can't test printing to real stdout without comparing output
348 # which is not available in unittest.
349 save_stdout = sys.stdout
350 sys.stdout = StringIO.StringIO()
Tim Petersabd8a332006-11-03 02:32:46 +0000351
Georg Brandlc6fdec62006-10-28 13:10:17 +0000352 print 1, 2, 3
353 print 1, 2, 3,
354 print
355 print 0 or 1, 0 or 1,
356 print 0 or 1
Barry Warsawefc92ee2000-08-21 15:46:50 +0000357
Georg Brandlc6fdec62006-10-28 13:10:17 +0000358 # 'print' '>>' test ','
359 print >> sys.stdout, 1, 2, 3
360 print >> sys.stdout, 1, 2, 3,
361 print >> sys.stdout
362 print >> sys.stdout, 0 or 1, 0 or 1,
363 print >> sys.stdout, 0 or 1
Barry Warsaw9182b452000-08-29 04:57:10 +0000364
Georg Brandlc6fdec62006-10-28 13:10:17 +0000365 # test printing to an instance
366 class Gulp:
367 def write(self, msg): pass
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000368
Georg Brandlc6fdec62006-10-28 13:10:17 +0000369 gulp = Gulp()
370 print >> gulp, 1, 2, 3
371 print >> gulp, 1, 2, 3,
372 print >> gulp
373 print >> gulp, 0 or 1, 0 or 1,
374 print >> gulp, 0 or 1
Barry Warsaw9182b452000-08-29 04:57:10 +0000375
Georg Brandlc6fdec62006-10-28 13:10:17 +0000376 # test print >> None
377 def driver():
378 oldstdout = sys.stdout
379 sys.stdout = Gulp()
380 try:
381 tellme(Gulp())
382 tellme()
383 finally:
384 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000385
Georg Brandlc6fdec62006-10-28 13:10:17 +0000386 # we should see this once
387 def tellme(file=sys.stdout):
388 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000389
Georg Brandlc6fdec62006-10-28 13:10:17 +0000390 driver()
Barry Warsaw9182b452000-08-29 04:57:10 +0000391
Georg Brandlc6fdec62006-10-28 13:10:17 +0000392 # we should not see this at all
393 def tellme(file=None):
394 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000395
Georg Brandlc6fdec62006-10-28 13:10:17 +0000396 driver()
Barry Warsawefc92ee2000-08-21 15:46:50 +0000397
Georg Brandlc6fdec62006-10-28 13:10:17 +0000398 self.assertEqual(sys.stdout.getvalue(), '''\
3991 2 3
4001 2 3
4011 1 1
4021 2 3
4031 2 3
4041 1 1
405hello world
406''')
407 sys.stdout = save_stdout
Guido van Rossum3bead091992-01-27 17:00:37 +0000408
Georg Brandlc6fdec62006-10-28 13:10:17 +0000409 # syntax errors
410 check_syntax_error(self, 'print ,')
411 check_syntax_error(self, 'print >> x,')
Guido van Rossum3bead091992-01-27 17:00:37 +0000412
Georg Brandlc6fdec62006-10-28 13:10:17 +0000413 def testDelStmt(self):
414 # 'del' exprlist
415 abc = [1,2,3]
416 x, y, z = abc
417 xyz = x, y, z
Guido van Rossum3bead091992-01-27 17:00:37 +0000418
Georg Brandlc6fdec62006-10-28 13:10:17 +0000419 del abc
420 del x, y, (z, xyz)
Guido van Rossum3bead091992-01-27 17:00:37 +0000421
Georg Brandlc6fdec62006-10-28 13:10:17 +0000422 def testPassStmt(self):
423 # 'pass'
424 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000425
Georg Brandlc6fdec62006-10-28 13:10:17 +0000426 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
427 # Tested below
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000428
Georg Brandlc6fdec62006-10-28 13:10:17 +0000429 def testBreakStmt(self):
430 # 'break'
431 while 1: break
Tim Peters10fb3862001-02-09 20:17:14 +0000432
Georg Brandlc6fdec62006-10-28 13:10:17 +0000433 def testContinueStmt(self):
434 # 'continue'
435 i = 1
436 while i: i = 0; continue
Thomas Wouters80d373c2001-09-26 12:43:39 +0000437
Georg Brandlc6fdec62006-10-28 13:10:17 +0000438 msg = ""
439 while not msg:
440 msg = "ok"
441 try:
442 continue
443 msg = "continue failed to continue inside try"
444 except:
445 msg = "continue inside try called except block"
446 if msg != "ok":
447 self.fail(msg)
Thomas Wouters80d373c2001-09-26 12:43:39 +0000448
Georg Brandlc6fdec62006-10-28 13:10:17 +0000449 msg = ""
450 while not msg:
451 msg = "finally block not called"
452 try:
453 continue
454 finally:
455 msg = "ok"
456 if msg != "ok":
457 self.fail(msg)
458
459 def test_break_continue_loop(self):
460 # This test warrants an explanation. It is a test specifically for SF bugs
461 # #463359 and #462937. The bug is that a 'break' statement executed or
462 # exception raised inside a try/except inside a loop, *after* a continue
463 # statement has been executed in that loop, will cause the wrong number of
464 # arguments to be popped off the stack and the instruction pointer reset to
465 # a very small number (usually 0.) Because of this, the following test
466 # *must* written as a function, and the tracking vars *must* be function
467 # arguments with default values. Otherwise, the test will loop and loop.
468
469 def test_inner(extra_burning_oil = 1, count=0):
470 big_hippo = 2
471 while big_hippo:
472 count += 1
473 try:
474 if extra_burning_oil and big_hippo == 1:
475 extra_burning_oil -= 1
476 break
477 big_hippo -= 1
478 continue
479 except:
480 raise
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000481 if count > 2 or big_hippo != 1:
Georg Brandlc6fdec62006-10-28 13:10:17 +0000482 self.fail("continue then break in try/except in loop broken!")
483 test_inner()
484
485 def testReturn(self):
486 # 'return' [testlist]
487 def g1(): return
488 def g2(): return 1
489 g1()
490 x = g2()
491 check_syntax_error(self, "class foo:return 1")
492
493 def testYield(self):
494 check_syntax_error(self, "class foo:yield 1")
495
496 def testRaise(self):
497 # 'raise' test [',' test]
498 try: raise RuntimeError, 'just testing'
499 except RuntimeError: pass
500 try: raise KeyboardInterrupt
501 except KeyboardInterrupt: pass
502
503 def testImport(self):
504 # 'import' dotted_as_names
505 import sys
506 import time, sys
507 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
508 from time import time
509 from time import (time)
510 # not testable inside a function, but already done at top of the module
511 # from sys import *
512 from sys import path, argv
513 from sys import (path, argv)
514 from sys import (path, argv,)
515
516 def testGlobal(self):
517 # 'global' NAME (',' NAME)*
518 global a
519 global a, b
520 global one, two, three, four, five, six, seven, eight, nine, ten
521
522 def testExec(self):
523 # 'exec' expr ['in' expr [',' expr]]
524 z = None
525 del z
526 exec 'z=1+1\n'
527 if z != 2: self.fail('exec \'z=1+1\'\\n')
528 del z
529 exec 'z=1+1'
530 if z != 2: self.fail('exec \'z=1+1\'')
531 z = None
532 del z
533 import types
534 if hasattr(types, "UnicodeType"):
535 exec r"""if 1:
536 exec u'z=1+1\n'
537 if z != 2: self.fail('exec u\'z=1+1\'\\n')
538 del z
539 exec u'z=1+1'
540 if z != 2: self.fail('exec u\'z=1+1\'')"""
541 g = {}
542 exec 'z = 1' in g
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000543 if '__builtins__' in g: del g['__builtins__']
Georg Brandlc6fdec62006-10-28 13:10:17 +0000544 if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
545 g = {}
546 l = {}
547
Georg Brandlc6fdec62006-10-28 13:10:17 +0000548 exec 'global a; a = 1; b = 2' in g, l
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000549 if '__builtins__' in g: del g['__builtins__']
550 if '__builtins__' in l: del l['__builtins__']
Georg Brandlc6fdec62006-10-28 13:10:17 +0000551 if (g, l) != ({'a':1}, {'b':2}):
552 self.fail('exec ... in g (%s), l (%s)' %(g,l))
553
554 def testAssert(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000555 # assertTruestmt: 'assert' test [',' test]
Georg Brandlc6fdec62006-10-28 13:10:17 +0000556 assert 1
557 assert 1, 1
558 assert lambda x:x
559 assert 1, lambda x:x+1
Ezio Melottiab731a32011-12-02 18:17:30 +0200560
561 try:
562 assert True
563 except AssertionError as e:
564 self.fail("'assert True' should not have raised an AssertionError")
565
566 try:
567 assert True, 'this should always pass'
568 except AssertionError as e:
569 self.fail("'assert True, msg' should not have "
570 "raised an AssertionError")
571
572 # these tests fail if python is run with -O, so check __debug__
573 @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
574 def testAssert2(self):
Thomas Wouters80d373c2001-09-26 12:43:39 +0000575 try:
Georg Brandlc6fdec62006-10-28 13:10:17 +0000576 assert 0, "msg"
577 except AssertionError, e:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000578 self.assertEqual(e.args[0], "msg")
Georg Brandlfacd2732006-10-29 09:18:00 +0000579 else:
Ezio Melottiab731a32011-12-02 18:17:30 +0200580 self.fail("AssertionError not raised by assert 0")
581
582 try:
583 assert False
584 except AssertionError as e:
585 self.assertEqual(len(e.args), 0)
586 else:
587 self.fail("AssertionError not raised by 'assert False'")
588
Thomas Wouters80d373c2001-09-26 12:43:39 +0000589
Georg Brandlc6fdec62006-10-28 13:10:17 +0000590 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
591 # Tested below
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
Georg Brandlc6fdec62006-10-28 13:10:17 +0000593 def testIf(self):
594 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
595 if 1: pass
596 if 1: pass
597 else: pass
598 if 0: pass
599 elif 0: pass
600 if 0: pass
601 elif 0: pass
602 elif 0: pass
603 elif 0: pass
604 else: pass
Tim Petersabd8a332006-11-03 02:32:46 +0000605
Georg Brandlc6fdec62006-10-28 13:10:17 +0000606 def testWhile(self):
607 # 'while' test ':' suite ['else' ':' suite]
608 while 0: pass
609 while 0: pass
610 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000611
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +0000612 # Issue1920: "while 0" is optimized away,
613 # ensure that the "else" clause is still present.
614 x = 0
615 while 0:
616 x = 1
617 else:
618 x = 2
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000619 self.assertEqual(x, 2)
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +0000620
Georg Brandlc6fdec62006-10-28 13:10:17 +0000621 def testFor(self):
622 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
623 for i in 1, 2, 3: pass
624 for i, j, k in (): pass
625 else: pass
626 class Squares:
627 def __init__(self, max):
628 self.max = max
629 self.sofar = []
630 def __len__(self): return len(self.sofar)
631 def __getitem__(self, i):
632 if not 0 <= i < self.max: raise IndexError
633 n = len(self.sofar)
634 while n <= i:
635 self.sofar.append(n*n)
636 n = n+1
637 return self.sofar[i]
638 n = 0
639 for x in Squares(10): n = n+x
640 if n != 285:
641 self.fail('for over growing sequence')
Guido van Rossum3bead091992-01-27 17:00:37 +0000642
Georg Brandlc6fdec62006-10-28 13:10:17 +0000643 result = []
644 for x, in [(1,), (2,), (3,)]:
645 result.append(x)
646 self.assertEqual(result, [1, 2, 3])
Guido van Rossum3bead091992-01-27 17:00:37 +0000647
Georg Brandlc6fdec62006-10-28 13:10:17 +0000648 def testTry(self):
649 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
650 ### | 'try' ':' suite 'finally' ':' suite
Collin Winter62903052007-05-18 23:11:24 +0000651 ### except_clause: 'except' [expr [('as' | ',') expr]]
Georg Brandlc6fdec62006-10-28 13:10:17 +0000652 try:
653 1/0
654 except ZeroDivisionError:
655 pass
656 else:
657 pass
658 try: 1/0
659 except EOFError: pass
Collin Winter62903052007-05-18 23:11:24 +0000660 except TypeError as msg: pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000661 except RuntimeError, msg: pass
662 except: pass
663 else: pass
664 try: 1/0
665 except (EOFError, TypeError, ZeroDivisionError): pass
666 try: 1/0
667 except (EOFError, TypeError, ZeroDivisionError), msg: pass
668 try: pass
669 finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000670
Georg Brandlc6fdec62006-10-28 13:10:17 +0000671 def testSuite(self):
672 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
673 if 1: pass
674 if 1:
675 pass
676 if 1:
677 #
678 #
679 #
680 pass
681 pass
682 #
683 pass
684 #
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000685
Georg Brandlc6fdec62006-10-28 13:10:17 +0000686 def testTest(self):
687 ### and_test ('or' and_test)*
688 ### and_test: not_test ('and' not_test)*
689 ### not_test: 'not' not_test | comparison
690 if not 1: pass
691 if 1 and 1: pass
692 if 1 or 1: pass
693 if not not not 1: pass
694 if not 1 and 1 and 1: pass
695 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
696
697 def testComparison(self):
698 ### comparison: expr (comp_op expr)*
699 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
700 if 1: pass
701 x = (1 == 1)
702 if 1 == 1: pass
703 if 1 != 1: pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000704 if 1 < 1: pass
705 if 1 > 1: pass
706 if 1 <= 1: pass
707 if 1 >= 1: pass
708 if 1 is 1: pass
709 if 1 is not 1: pass
710 if 1 in (): pass
711 if 1 not in (): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000712 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
713 # Silence Py3k warning
714 if eval('1 <> 1'): pass
715 if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000716
717 def testBinaryMaskOps(self):
718 x = 1 & 1
719 x = 1 ^ 1
720 x = 1 | 1
721
722 def testShiftOps(self):
723 x = 1 << 1
724 x = 1 >> 1
725 x = 1 << 1 >> 1
726
727 def testAdditiveOps(self):
728 x = 1
729 x = 1 + 1
730 x = 1 - 1 - 1
731 x = 1 - 1 + 1 - 1 + 1
732
733 def testMultiplicativeOps(self):
734 x = 1 * 1
735 x = 1 / 1
736 x = 1 % 1
737 x = 1 / 1 * 1 % 1
738
739 def testUnaryOps(self):
740 x = +1
741 x = -1
742 x = ~1
743 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
744 x = -1*1/1 + 1*1 - ---1*1
745
746 def testSelectors(self):
747 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
748 ### subscript: expr | [expr] ':' [expr]
Tim Petersabd8a332006-11-03 02:32:46 +0000749
Georg Brandlc6fdec62006-10-28 13:10:17 +0000750 import sys, time
751 c = sys.path[0]
752 x = time.time()
753 x = sys.modules['time'].time()
754 a = '01234'
755 c = a[0]
756 c = a[-1]
757 s = a[0:5]
758 s = a[:5]
759 s = a[0:]
760 s = a[:]
761 s = a[-5:]
762 s = a[:-1]
763 s = a[-4:-3]
764 # A rough test of SF bug 1333982. http://python.org/sf/1333982
765 # The testing here is fairly incomplete.
766 # Test cases should include: commas with 1 and 2 colons
767 d = {}
768 d[1] = 1
769 d[1,] = 2
770 d[1,2] = 3
771 d[1,2,3] = 4
772 L = list(d)
773 L.sort()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000774 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
Georg Brandlc6fdec62006-10-28 13:10:17 +0000775
776 def testAtoms(self):
777 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000778 ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Georg Brandlc6fdec62006-10-28 13:10:17 +0000779
780 x = (1)
781 x = (1 or 2 or 3)
782 x = (1 or 2 or 3, 2, 3)
783
784 x = []
785 x = [1]
786 x = [1 or 2 or 3]
787 x = [1 or 2 or 3, 2, 3]
788 x = []
789
790 x = {}
791 x = {'one': 1}
792 x = {'one': 1,}
793 x = {'one' or 'two': 1 or 2}
794 x = {'one': 1, 'two': 2}
795 x = {'one': 1, 'two': 2,}
796 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
797
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000798 x = {'one'}
799 x = {'one', 1,}
800 x = {'one', 'two', 'three'}
801 x = {2, 3, 4,}
802
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000803 # Silence Py3k warning
804 x = eval('`x`')
805 x = eval('`1 or 2 or 3`')
806 self.assertEqual(eval('`1,2`'), '(1, 2)')
Neal Norwitz85dbec62006-11-04 19:25:22 +0000807
Georg Brandlc6fdec62006-10-28 13:10:17 +0000808 x = x
809 x = 'x'
810 x = 123
811
812 ### exprlist: expr (',' expr)* [',']
813 ### testlist: test (',' test)* [',']
814 # These have been exercised enough above
815
816 def testClassdef(self):
817 # 'class' NAME ['(' [testlist] ')'] ':' suite
818 class B: pass
819 class B2(): pass
820 class C1(B): pass
821 class C2(B): pass
822 class D(C1, C2, B): pass
823 class C:
824 def meth1(self): pass
825 def meth2(self, arg): pass
826 def meth3(self, a1, a2): pass
Christian Heimes5224d282008-02-23 15:01:05 +0000827 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
828 # decorators: decorator+
829 # decorated: decorators (classdef | funcdef)
830 def class_decorator(x):
831 x.decorated = True
832 return x
833 @class_decorator
834 class G:
835 pass
836 self.assertEqual(G.decorated, True)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000837
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000838 def testDictcomps(self):
839 # dictorsetmaker: ( (test ':' test (comp_for |
840 # (',' test ':' test)* [','])) |
841 # (test (comp_for | (',' test)* [','])) )
842 nums = [1, 2, 3]
843 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
844
Georg Brandlc6fdec62006-10-28 13:10:17 +0000845 def testListcomps(self):
846 # list comprehension tests
847 nums = [1, 2, 3, 4, 5]
848 strs = ["Apple", "Banana", "Coconut"]
849 spcs = [" Apple", " Banana ", "Coco nut "]
850
851 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
852 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
853 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
854 self.assertEqual([(i, s) for i in nums for s in strs],
855 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
856 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
857 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
858 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
859 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
860 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
861 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
862 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
863 (5, 'Banana'), (5, 'Coconut')])
864 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
865 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
866
867 def test_in_func(l):
868 return [None < x < 3 for x in l if x > 2]
869
870 self.assertEqual(test_in_func(nums), [False, False, False])
871
872 def test_nested_front():
873 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
874 [[1, 2], [3, 4], [5, 6]])
875
876 test_nested_front()
877
878 check_syntax_error(self, "[i, s for i in nums for s in strs]")
879 check_syntax_error(self, "[x if y]")
880
881 suppliers = [
882 (1, "Boeing"),
883 (2, "Ford"),
884 (3, "Macdonalds")
885 ]
886
887 parts = [
888 (10, "Airliner"),
889 (20, "Engine"),
890 (30, "Cheeseburger")
891 ]
892
893 suppart = [
894 (1, 10), (1, 20), (2, 20), (3, 30)
895 ]
896
897 x = [
898 (sname, pname)
899 for (sno, sname) in suppliers
900 for (pno, pname) in parts
901 for (sp_sno, sp_pno) in suppart
902 if sno == sp_sno and pno == sp_pno
903 ]
904
905 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
906 ('Macdonalds', 'Cheeseburger')])
907
908 def testGenexps(self):
909 # generator expression tests
910 g = ([x for x in range(10)] for x in range(1))
911 self.assertEqual(g.next(), [x for x in range(10)])
912 try:
913 g.next()
914 self.fail('should produce StopIteration exception')
915 except StopIteration:
916 pass
917
918 a = 1
919 try:
920 g = (a for d in a)
921 g.next()
922 self.fail('should produce TypeError')
923 except TypeError:
924 pass
925
926 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
927 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
928
929 a = [x for x in range(10)]
930 b = (x for x in (y for y in a))
931 self.assertEqual(sum(b), sum([x for x in range(10)]))
932
933 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
934 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
935 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
936 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
937 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
938 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
939 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
940 check_syntax_error(self, "foo(x for x in range(10), 100)")
941 check_syntax_error(self, "foo(100, x for x in range(10))")
942
943 def testComprehensionSpecials(self):
944 # test for outmost iterable precomputation
945 x = 10; g = (i for i in range(x)); x = 5
946 self.assertEqual(len(list(g)), 10)
947
948 # This should hold, since we're only precomputing outmost iterable.
949 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
950 x = 5; t = True;
951 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
952
953 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
954 # even though it's silly. Make sure it works (ifelse broke this.)
955 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
956 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
957
958 # verify unpacking single element tuples in listcomp/genexp.
959 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
960 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
961
Benjamin Peterson46508922009-05-29 21:48:19 +0000962 def test_with_statement(self):
963 class manager(object):
964 def __enter__(self):
965 return (1, 2)
966 def __exit__(self, *args):
967 pass
968
969 with manager():
970 pass
971 with manager() as x:
972 pass
973 with manager() as (x, y):
974 pass
975 with manager(), manager():
976 pass
977 with manager() as x, manager() as y:
978 pass
979 with manager() as x, manager():
980 pass
981
Georg Brandlc6fdec62006-10-28 13:10:17 +0000982 def testIfElseExpr(self):
983 # Test ifelse expressions in various cases
984 def _checkeval(msg, ret):
985 "helper to check that evaluation of expressions is done correctly"
986 print x
987 return ret
988
989 self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
990 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
991 self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
992 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
993 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
994 self.assertEqual((5 and 6 if 0 else 1), 1)
995 self.assertEqual(((5 and 6) if 0 else 1), 1)
996 self.assertEqual((5 and (6 if 1 else 1)), 6)
997 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
998 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
999 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1000 self.assertEqual((not 5 if 1 else 1), False)
1001 self.assertEqual((not 5 if 0 else 1), 1)
1002 self.assertEqual((6 + 1 if 1 else 2), 7)
1003 self.assertEqual((6 - 1 if 1 else 2), 5)
1004 self.assertEqual((6 * 2 if 1 else 4), 12)
1005 self.assertEqual((6 / 2 if 1 else 3), 3)
1006 self.assertEqual((6 < 4 if 0 else 2), 2)
Guido van Rossumb3b09c91993-10-22 14:24:22 +00001007
Benjamin Petersonb2e31a12009-10-31 03:56:15 +00001008 def test_paren_evaluation(self):
1009 self.assertEqual(16 // (4 // 2), 8)
1010 self.assertEqual((16 // 4) // 2, 2)
1011 self.assertEqual(16 // 4 // 2, 2)
1012 self.assertTrue(False is (2 is 3))
1013 self.assertFalse((False is 2) is 3)
1014 self.assertFalse(False is 2 is 3)
1015
Guido van Rossumb3b09c91993-10-22 14:24:22 +00001016
Georg Brandlc6fdec62006-10-28 13:10:17 +00001017def test_main():
Ezio Melottid80b4bf2010-03-17 13:52:48 +00001018 with check_py3k_warnings(
1019 ("backquote not supported", SyntaxWarning),
1020 ("tuple parameter unpacking has been removed", SyntaxWarning),
1021 ("parenthesized argument names are invalid", SyntaxWarning),
1022 ("classic int division", DeprecationWarning),
1023 (".+ not supported in 3.x", DeprecationWarning)):
1024 run_unittest(TokenTests, GrammarTests)
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +00001025
Georg Brandlc6fdec62006-10-28 13:10:17 +00001026if __name__ == '__main__':
1027 test_main()