blob: 88c22b89d444a3f93cb398c3fc1731904d806914 [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
Zachary Ware38c707e2015-04-13 15:00:43 -05004from test.support import check_syntax_error
Yury Selivanov75445082015-05-11 22:57:16 -04005import inspect
Thomas Wouters89f507f2006-12-13 04:49:30 +00006import unittest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00007import sys
Thomas Wouters89f507f2006-12-13 04:49:30 +00008# testing import *
9from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +000010
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070011# different import patterns to check that __annotations__ does not interfere
12# with import machinery
13import test.ann_module as ann_module
14import typing
15from collections import ChainMap
16from test import ann_module2
17import test
18
Brett Cannona721aba2016-09-09 14:57:09 -070019# These are shared with test_tokenize and other test modules.
20#
21# Note: since several test cases filter out floats by looking for "e" and ".",
22# don't add hexadecimal literals that contain "e" or "E".
23VALID_UNDERSCORE_LITERALS = [
24 '0_0_0',
25 '4_2',
26 '1_0000_0000',
27 '0b1001_0100',
28 '0xffff_ffff',
29 '0o5_7_7',
30 '1_00_00.5',
31 '1_00_00.5e5',
32 '1_00_00e5_1',
33 '1e1_0',
34 '.1_4',
35 '.1_4e1',
36 '0b_0',
37 '0x_f',
38 '0o_5',
39 '1_00_00j',
40 '1_00_00.5j',
41 '1_00_00e5_1j',
42 '.1_4j',
43 '(1_2.5+3_3j)',
44 '(.5_6j)',
45]
46INVALID_UNDERSCORE_LITERALS = [
47 # Trailing underscores:
48 '0_',
49 '42_',
50 '1.4j_',
51 '0x_',
52 '0b1_',
53 '0xf_',
54 '0o5_',
55 '0 if 1_Else 1',
56 # Underscores in the base selector:
57 '0_b0',
58 '0_xf',
59 '0_o5',
60 # Old-style octal, still disallowed:
61 '0_7',
62 '09_99',
63 # Multiple consecutive underscores:
64 '4_______2',
65 '0.1__4',
66 '0.1__4j',
67 '0b1001__0100',
68 '0xffff__ffff',
69 '0x___',
70 '0o5__77',
71 '1e1__0',
72 '1e1__0j',
73 # Underscore right before a dot:
74 '1_.4',
75 '1_.4j',
76 # Underscore right after a dot:
77 '1._4',
78 '1._4j',
79 '._5',
80 '._5j',
81 # Underscore right after a sign:
82 '1.0e+_1',
83 '1.0e+_1j',
84 # Underscore right before j:
85 '1.4_j',
86 '1.4e5_j',
87 # Underscore right before e:
88 '1_e1',
89 '1.4_e1',
90 '1.4_e1j',
91 # Underscore right after e:
92 '1e_1',
93 '1.4e_1',
94 '1.4e_1j',
95 # Complex cases with parens:
96 '(1+1.5_j_)',
97 '(1+1.5_j)',
98]
99
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000100
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101class TokenTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +0000102
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500103 def test_backslash(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 # Backslash means line continuation:
105 x = 1 \
106 + 1
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000107 self.assertEqual(x, 2, 'backslash for line continuation')
Guido van Rossum3bead091992-01-27 17:00:37 +0000108
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 # Backslash does not means continuation in comments :\
110 x = 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000111 self.assertEqual(x, 0, 'backslash ending comment')
Guido van Rossum3bead091992-01-27 17:00:37 +0000112
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500113 def test_plain_integers(self):
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000114 self.assertEqual(type(000), type(0))
115 self.assertEqual(0xff, 255)
116 self.assertEqual(0o377, 255)
117 self.assertEqual(2147483647, 0o17777777777)
118 self.assertEqual(0b1001, 9)
Georg Brandlfceab5a2008-01-19 20:08:23 +0000119 # "0x" is not a valid literal
120 self.assertRaises(SyntaxError, eval, "0x")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000121 from sys import maxsize
122 if maxsize == 2147483647:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000123 self.assertEqual(-2147483647-1, -0o20000000000)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 # XXX -2147483648
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000125 self.assertTrue(0o37777777777 > 0)
126 self.assertTrue(0xffffffff > 0)
127 self.assertTrue(0b1111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000128 for s in ('2147483648', '0o40000000000', '0x100000000',
129 '0b10000000000000000000000000000000'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 try:
131 x = eval(s)
132 except OverflowError:
133 self.fail("OverflowError on huge integer literal %r" % s)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000134 elif maxsize == 9223372036854775807:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000135 self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000136 self.assertTrue(0o1777777777777777777777 > 0)
137 self.assertTrue(0xffffffffffffffff > 0)
138 self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000139 for s in '9223372036854775808', '0o2000000000000000000000', \
140 '0x10000000000000000', \
141 '0b100000000000000000000000000000000000000000000000000000000000000':
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142 try:
143 x = eval(s)
144 except OverflowError:
145 self.fail("OverflowError on huge integer literal %r" % s)
146 else:
Christian Heimesa37d4c62007-12-04 23:02:19 +0000147 self.fail('Weird maxsize value %r' % maxsize)
Guido van Rossum3bead091992-01-27 17:00:37 +0000148
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500149 def test_long_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000150 x = 0
Guido van Rossume2a383d2007-01-15 16:59:06 +0000151 x = 0xffffffffffffffff
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000152 x = 0Xffffffffffffffff
153 x = 0o77777777777777777
154 x = 0O77777777777777777
Guido van Rossume2a383d2007-01-15 16:59:06 +0000155 x = 123456789012345678901234567890
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000156 x = 0b100000000000000000000000000000000000000000000000000000000000000000000
157 x = 0B111111111111111111111111111111111111111111111111111111111111111111111
Guido van Rossum3bead091992-01-27 17:00:37 +0000158
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500159 def test_floats(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 x = 3.14
161 x = 314.
162 x = 0.314
163 # XXX x = 000.314
164 x = .314
165 x = 3e14
166 x = 3E14
167 x = 3e-14
168 x = 3e+14
169 x = 3.e14
170 x = .3e14
171 x = 3.1e4
Guido van Rossum3bead091992-01-27 17:00:37 +0000172
Benjamin Petersonc4161622014-06-07 12:36:39 -0700173 def test_float_exponent_tokenization(self):
174 # See issue 21642.
175 self.assertEqual(1 if 1else 0, 1)
176 self.assertEqual(1 if 0else 0, 0)
177 self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
178
Brett Cannona721aba2016-09-09 14:57:09 -0700179 def test_underscore_literals(self):
180 for lit in VALID_UNDERSCORE_LITERALS:
181 self.assertEqual(eval(lit), eval(lit.replace('_', '')))
182 for lit in INVALID_UNDERSCORE_LITERALS:
183 self.assertRaises(SyntaxError, eval, lit)
184 # Sanity check: no literal begins with an underscore
185 self.assertRaises(NameError, eval, "_0")
186
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500187 def test_string_literals(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000188 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
189 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
190 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 x = "doesn't \"shrink\" does it"
192 y = 'doesn\'t "shrink" does it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000193 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194 x = "does \"shrink\" doesn't it"
195 y = 'does "shrink" doesn\'t it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000196 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197 x = """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198The "quick"
199brown fox
200jumps over
201the 'lazy' dog.
202"""
Thomas Wouters89f507f2006-12-13 04:49:30 +0000203 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000204 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205 y = '''
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206The "quick"
207brown fox
208jumps over
209the 'lazy' dog.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210'''
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000211 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000212 y = "\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000213The \"quick\"\n\
214brown fox\n\
215jumps over\n\
216the 'lazy' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000217"
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000218 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000219 y = '\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220The \"quick\"\n\
221brown fox\n\
222jumps over\n\
223the \'lazy\' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000224'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000225 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000226
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500227 def test_ellipsis(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000228 x = ...
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000229 self.assertTrue(x is Ellipsis)
Georg Brandldde00282007-03-18 19:01:53 +0000230 self.assertRaises(SyntaxError, eval, ".. .")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000231
Benjamin Peterson758888d2011-05-30 11:12:38 -0500232 def test_eof_error(self):
233 samples = ("def foo(", "\ndef foo(", "def foo(\n")
234 for s in samples:
235 with self.assertRaises(SyntaxError) as cm:
236 compile(s, "<test>", "exec")
237 self.assertIn("unexpected EOF", str(cm.exception))
238
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700239var_annot_global: int # a global annotated is necessary for test_var_annot
240
241# custom namespace for testing __annotations__
242
243class CNS:
244 def __init__(self):
245 self._dct = {}
246 def __setitem__(self, item, value):
247 self._dct[item.lower()] = value
248 def __getitem__(self, item):
249 return self._dct[item]
250
251
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252class GrammarTests(unittest.TestCase):
253
254 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
255 # XXX can't test in a script -- this rule is only used when interactive
256
257 # file_input: (NEWLINE | stmt)* ENDMARKER
258 # Being tested as this very moment this very module
259
260 # expr_input: testlist NEWLINE
261 # XXX Hard to test -- used only in calls to input()
262
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500263 def test_eval_input(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000264 # testlist ENDMARKER
265 x = eval('1, 0 or 1')
266
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700267 def test_var_annot_basics(self):
268 # all these should be allowed
269 var1: int = 5
270 var2: [int, str]
271 my_lst = [42]
272 def one():
273 return 1
274 int.new_attr: int
275 [list][0]: type
276 my_lst[one()-1]: int = 5
277 self.assertEqual(my_lst, [5])
278
279 def test_var_annot_syntax_errors(self):
280 # parser pass
281 check_syntax_error(self, "def f: int")
282 check_syntax_error(self, "x: int: str")
283 check_syntax_error(self, "def f():\n"
284 " nonlocal x: int\n")
285 # AST pass
286 check_syntax_error(self, "[x, 0]: int\n")
287 check_syntax_error(self, "f(): int\n")
288 check_syntax_error(self, "(x,): int")
289 check_syntax_error(self, "def f():\n"
290 " (x, y): int = (1, 2)\n")
291 # symtable pass
292 check_syntax_error(self, "def f():\n"
293 " x: int\n"
294 " global x\n")
295 check_syntax_error(self, "def f():\n"
296 " global x\n"
297 " x: int\n")
298
299 def test_var_annot_basic_semantics(self):
300 # execution order
301 with self.assertRaises(ZeroDivisionError):
302 no_name[does_not_exist]: no_name_again = 1/0
303 with self.assertRaises(NameError):
304 no_name[does_not_exist]: 1/0 = 0
305 global var_annot_global
306
307 # function semantics
308 def f():
309 st: str = "Hello"
310 a.b: int = (1, 2)
311 return st
312 self.assertEqual(f.__annotations__, {})
313 def f_OK():
314 x: 1/0
315 f_OK()
316 def fbad():
317 x: int
318 print(x)
319 with self.assertRaises(UnboundLocalError):
320 fbad()
321 def f2bad():
322 (no_such_global): int
323 print(no_such_global)
324 try:
325 f2bad()
326 except Exception as e:
327 self.assertIs(type(e), NameError)
328
329 # class semantics
330 class C:
Guido van Rossum015d8742016-09-11 09:45:24 -0700331 __foo: int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700332 s: str = "attr"
333 z = 2
334 def __init__(self, x):
335 self.x: int = x
Guido van Rossum015d8742016-09-11 09:45:24 -0700336 self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700337 with self.assertRaises(NameError):
338 class CBad:
339 no_such_name_defined.attr: int = 0
340 with self.assertRaises(NameError):
341 class Cbad2(C):
342 x: int
343 x.y: list = []
344
345 def test_var_annot_metaclass_semantics(self):
346 class CMeta(type):
347 @classmethod
348 def __prepare__(metacls, name, bases, **kwds):
349 return {'__annotations__': CNS()}
350 class CC(metaclass=CMeta):
351 XX: 'ANNOT'
352 self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
353
354 def test_var_annot_module_semantics(self):
355 with self.assertRaises(AttributeError):
356 print(test.__annotations__)
357 self.assertEqual(ann_module.__annotations__,
358 {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int]})
359 self.assertEqual(ann_module.M.__annotations__,
360 {'123': 123, 'o': type})
361 self.assertEqual(ann_module2.__annotations__, {})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700362
363 def test_var_annot_in_module(self):
364 # check that functions fail the same way when executed
365 # outside of module where they were defined
366 from test.ann_module3 import f_bad_ann, g_bad_ann, D_bad_ann
367 with self.assertRaises(NameError):
368 f_bad_ann()
369 with self.assertRaises(NameError):
370 g_bad_ann()
371 with self.assertRaises(NameError):
372 D_bad_ann(5)
373
374 def test_var_annot_simple_exec(self):
375 gns = {}; lns= {}
376 exec("'docstring'\n"
377 "__annotations__[1] = 2\n"
378 "x: int = 5\n", gns, lns)
379 self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
380 with self.assertRaises(KeyError):
381 gns['__annotations__']
382
383 def test_var_annot_custom_maps(self):
384 # tests with custom locals() and __annotations__
385 ns = {'__annotations__': CNS()}
386 exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)
387 self.assertEqual(ns['__annotations__']['x'], int)
388 self.assertEqual(ns['__annotations__']['z'], str)
389 with self.assertRaises(KeyError):
390 ns['__annotations__']['w']
391 nonloc_ns = {}
392 class CNS2:
393 def __init__(self):
394 self._dct = {}
395 def __setitem__(self, item, value):
396 nonlocal nonloc_ns
397 self._dct[item] = value
398 nonloc_ns[item] = value
399 def __getitem__(self, item):
400 return self._dct[item]
401 exec('x: int = 1', {}, CNS2())
402 self.assertEqual(nonloc_ns['__annotations__']['x'], int)
403
404 def test_var_annot_refleak(self):
405 # complex case: custom locals plus custom __annotations__
406 # this was causing refleak
407 cns = CNS()
408 nonloc_ns = {'__annotations__': cns}
409 class CNS2:
410 def __init__(self):
411 self._dct = {'__annotations__': cns}
412 def __setitem__(self, item, value):
413 nonlocal nonloc_ns
414 self._dct[item] = value
415 nonloc_ns[item] = value
416 def __getitem__(self, item):
417 return self._dct[item]
418 exec('X: str', {}, CNS2())
419 self.assertEqual(nonloc_ns['__annotations__']['x'], str)
420
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500421 def test_funcdef(self):
Neal Norwitzc1505362006-12-28 06:47:50 +0000422 ### [decorators] 'def' NAME parameters ['->' test] ':' suite
423 ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
424 ### decorators: decorator+
425 ### parameters: '(' [typedargslist] ')'
426 ### typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000427 ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000428 ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000429 ### tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +0000430 ### varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000431 ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000432 ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000433 ### vfpdef: NAME
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 def f1(): pass
435 f1()
436 f1(*())
437 f1(*(), **{})
438 def f2(one_argument): pass
439 def f3(two, arguments): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000440 self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
441 self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000442 def a1(one_arg,): pass
443 def a2(two, args,): pass
444 def v0(*rest): pass
445 def v1(a, *rest): pass
446 def v2(a, b, *rest): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000447
448 f1()
449 f2(1)
450 f2(1,)
451 f3(1, 2)
452 f3(1, 2,)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000453 v0()
454 v0(1)
455 v0(1,)
456 v0(1,2)
457 v0(1,2,3,4,5,6,7,8,9,0)
458 v1(1)
459 v1(1,)
460 v1(1,2)
461 v1(1,2,3)
462 v1(1,2,3,4,5,6,7,8,9,0)
463 v2(1,2)
464 v2(1,2,3)
465 v2(1,2,3,4)
466 v2(1,2,3,4,5,6,7,8,9,0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000467
Thomas Wouters89f507f2006-12-13 04:49:30 +0000468 def d01(a=1): pass
469 d01()
470 d01(1)
471 d01(*(1,))
Yury Selivanov14acf5f2015-08-05 17:54:10 -0400472 d01(*[] or [2])
473 d01(*() or (), *{} and (), **() or {})
Thomas Wouters89f507f2006-12-13 04:49:30 +0000474 d01(**{'a':2})
Benjamin Petersonde12b792015-05-16 09:44:45 -0400475 d01(**{'a':2} or {})
Thomas Wouters89f507f2006-12-13 04:49:30 +0000476 def d11(a, b=1): pass
477 d11(1)
478 d11(1, 2)
479 d11(1, **{'b':2})
480 def d21(a, b, c=1): pass
481 d21(1, 2)
482 d21(1, 2, 3)
483 d21(*(1, 2, 3))
484 d21(1, *(2, 3))
485 d21(1, 2, *(3,))
486 d21(1, 2, **{'c':3})
487 def d02(a=1, b=2): pass
488 d02()
489 d02(1)
490 d02(1, 2)
491 d02(*(1, 2))
492 d02(1, *(2,))
493 d02(1, **{'b':2})
494 d02(**{'a': 1, 'b': 2})
495 def d12(a, b=1, c=2): pass
496 d12(1)
497 d12(1, 2)
498 d12(1, 2, 3)
499 def d22(a, b, c=1, d=2): pass
500 d22(1, 2)
501 d22(1, 2, 3)
502 d22(1, 2, 3, 4)
503 def d01v(a=1, *rest): pass
504 d01v()
505 d01v(1)
506 d01v(1, 2)
507 d01v(*(1, 2, 3, 4))
508 d01v(*(1,))
509 d01v(**{'a':2})
510 def d11v(a, b=1, *rest): pass
511 d11v(1)
512 d11v(1, 2)
513 d11v(1, 2, 3)
514 def d21v(a, b, c=1, *rest): pass
515 d21v(1, 2)
516 d21v(1, 2, 3)
517 d21v(1, 2, 3, 4)
518 d21v(*(1, 2, 3, 4))
519 d21v(1, 2, **{'c': 3})
520 def d02v(a=1, b=2, *rest): pass
521 d02v()
522 d02v(1)
523 d02v(1, 2)
524 d02v(1, 2, 3)
525 d02v(1, *(2, 3, 4))
526 d02v(**{'a': 1, 'b': 2})
527 def d12v(a, b=1, c=2, *rest): pass
528 d12v(1)
529 d12v(1, 2)
530 d12v(1, 2, 3)
531 d12v(1, 2, 3, 4)
532 d12v(*(1, 2, 3, 4))
533 d12v(1, 2, *(3, 4, 5))
534 d12v(1, *(2,), **{'c': 3})
535 def d22v(a, b, c=1, d=2, *rest): pass
536 d22v(1, 2)
537 d22v(1, 2, 3)
538 d22v(1, 2, 3, 4)
539 d22v(1, 2, 3, 4, 5)
540 d22v(*(1, 2, 3, 4))
541 d22v(1, 2, *(3, 4, 5))
542 d22v(1, *(2, 3), **{'d': 4})
Georg Brandld8b690f2008-05-16 17:28:50 +0000543
544 # keyword argument type tests
545 try:
546 str('x', **{b'foo':1 })
547 except TypeError:
548 pass
549 else:
550 self.fail('Bytes should not work as keyword argument names')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000551 # keyword only argument tests
552 def pos0key1(*, key): return key
553 pos0key1(key=100)
554 def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
555 pos2key2(1, 2, k1=100)
556 pos2key2(1, 2, k1=100, k2=200)
557 pos2key2(1, 2, k2=100, k1=200)
558 def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
559 pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
560 pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
561
Robert Collinsdf395992015-08-12 08:00:06 +1200562 self.assertRaises(SyntaxError, eval, "def f(*): pass")
563 self.assertRaises(SyntaxError, eval, "def f(*,): pass")
564 self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
565
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000566 # keyword arguments after *arglist
567 def f(*args, **kwargs):
568 return args, kwargs
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000569 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000570 {'x':2, 'y':5}))
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400571 self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000572 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400573 self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
574 ((), {'eggs':'scrambled', 'spam':'fried'}))
575 self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
576 ((), {'eggs':'scrambled', 'spam':'fried'}))
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000577
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200578 # Check ast errors in *args and *kwargs
579 check_syntax_error(self, "f(*g(1=2))")
580 check_syntax_error(self, "f(**g(1=2))")
581
Neal Norwitzc1505362006-12-28 06:47:50 +0000582 # argument annotation tests
583 def f(x) -> list: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000584 self.assertEqual(f.__annotations__, {'return': list})
Zachary Warece17f762015-08-01 21:55:36 -0500585 def f(x: int): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000586 self.assertEqual(f.__annotations__, {'x': int})
Zachary Warece17f762015-08-01 21:55:36 -0500587 def f(*x: str): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000588 self.assertEqual(f.__annotations__, {'x': str})
Zachary Warece17f762015-08-01 21:55:36 -0500589 def f(**x: float): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000590 self.assertEqual(f.__annotations__, {'x': float})
Zachary Warece17f762015-08-01 21:55:36 -0500591 def f(x, y: 1+2): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000592 self.assertEqual(f.__annotations__, {'y': 3})
Zachary Warece17f762015-08-01 21:55:36 -0500593 def f(a, b: 1, c: 2, d): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000594 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
Zachary Warece17f762015-08-01 21:55:36 -0500595 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000596 self.assertEqual(f.__annotations__,
Zachary Warece17f762015-08-01 21:55:36 -0500597 {'b': 1, 'c': 2, 'e': 3, 'g': 6})
598 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
599 **k: 11) -> 12: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000600 self.assertEqual(f.__annotations__,
Zachary Warece17f762015-08-01 21:55:36 -0500601 {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
602 'k': 11, 'return': 12})
Yury Selivanov34ce99f2014-02-18 12:49:41 -0500603 # Check for issue #20625 -- annotations mangling
604 class Spam:
Zachary Warece17f762015-08-01 21:55:36 -0500605 def f(self, *, __kw: 1):
Yury Selivanov34ce99f2014-02-18 12:49:41 -0500606 pass
607 class Ham(Spam): pass
Benjamin Petersonbcfcfc52014-03-09 20:59:24 -0500608 self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
609 self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
Nick Coghlan71011e22007-04-23 11:05:01 +0000610 # Check for SF Bug #1697248 - mixing decorators and a return annotation
611 def null(x): return x
612 @null
613 def f(x) -> list: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000614 self.assertEqual(f.__annotations__, {'return': list})
Nick Coghlan71011e22007-04-23 11:05:01 +0000615
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300616 # test closures with a variety of opargs
Guido van Rossum0240b922007-02-26 21:23:50 +0000617 closure = 1
618 def f(): return closure
619 def f(x=1): return closure
620 def f(*, k=1): return closure
621 def f() -> int: return closure
Neal Norwitzc1505362006-12-28 06:47:50 +0000622
Robert Collinsdf395992015-08-12 08:00:06 +1200623 # Check trailing commas are permitted in funcdef argument list
624 def f(a,): pass
625 def f(*args,): pass
626 def f(**kwds,): pass
627 def f(a, *args,): pass
628 def f(a, **kwds,): pass
629 def f(*args, b,): pass
630 def f(*, b,): pass
631 def f(*args, **kwds,): pass
632 def f(a, *args, b,): pass
633 def f(a, *, b,): pass
634 def f(a, *args, **kwds,): pass
635 def f(*args, b, **kwds,): pass
636 def f(*, b, **kwds,): pass
637 def f(a, *args, b, **kwds,): pass
638 def f(a, *, b, **kwds,): pass
639
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500640 def test_lambdef(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000641 ### lambdef: 'lambda' [varargslist] ':' test
642 l1 = lambda : 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000643 self.assertEqual(l1(), 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000644 l2 = lambda : a[d] # XXX just testing the expression
Guido van Rossume2a383d2007-01-15 16:59:06 +0000645 l3 = lambda : [2 < x for x in [-1, 3, 0]]
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000646 self.assertEqual(l3(), [0, 1, 0])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000647 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000648 self.assertEqual(l4(), 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000649 l5 = lambda x, y, z=2: x + y + z
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000650 self.assertEqual(l5(1, 2), 5)
651 self.assertEqual(l5(1, 2, 3), 6)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000652 check_syntax_error(self, "lambda x: x = 2")
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000653 check_syntax_error(self, "lambda (None,): None")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000654 l6 = lambda x, y, *, k=20: x+y+k
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000655 self.assertEqual(l6(1,2), 1+2+20)
656 self.assertEqual(l6(1,2,k=10), 1+2+10)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000657
Robert Collinsdf395992015-08-12 08:00:06 +1200658 # check that trailing commas are permitted
659 l10 = lambda a,: 0
660 l11 = lambda *args,: 0
661 l12 = lambda **kwds,: 0
662 l13 = lambda a, *args,: 0
663 l14 = lambda a, **kwds,: 0
664 l15 = lambda *args, b,: 0
665 l16 = lambda *, b,: 0
666 l17 = lambda *args, **kwds,: 0
667 l18 = lambda a, *args, b,: 0
668 l19 = lambda a, *, b,: 0
669 l20 = lambda a, *args, **kwds,: 0
670 l21 = lambda *args, b, **kwds,: 0
671 l22 = lambda *, b, **kwds,: 0
672 l23 = lambda a, *args, b, **kwds,: 0
673 l24 = lambda a, *, b, **kwds,: 0
674
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000675
Thomas Wouters89f507f2006-12-13 04:49:30 +0000676 ### stmt: simple_stmt | compound_stmt
677 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000678
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500679 def test_simple_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000680 ### simple_stmt: small_stmt (';' small_stmt)* [';']
681 x = 1; pass; del x
682 def foo():
Ezio Melotti13925002011-03-16 11:05:33 +0200683 # verify statements that end with semi-colons
Thomas Wouters89f507f2006-12-13 04:49:30 +0000684 x = 1; pass; del x;
685 foo()
Georg Brandl52318d62006-09-06 07:06:08 +0000686
Guido van Rossumd8faa362007-04-27 19:54:29 +0000687 ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +0000688 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000689
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500690 def test_expr_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000691 # (exprlist '=')* exprlist
Victor Stinner15a30952016-02-08 22:45:06 +0100692 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000693 1, 2, 3
694 x = 1
695 x = 1, 2, 3
696 x = y = z = 1, 2, 3
697 x, y, z = 1, 2, 3
698 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
Guido van Rossum3bead091992-01-27 17:00:37 +0000699
Thomas Wouters89f507f2006-12-13 04:49:30 +0000700 check_syntax_error(self, "x + 1 = 1")
701 check_syntax_error(self, "a + 1 = b + 2")
Guido van Rossum3bead091992-01-27 17:00:37 +0000702
Nick Coghlan5b1fdc12014-06-16 19:48:02 +1000703 # Check the heuristic for print & exec covers significant cases
704 # As well as placing some limits on false positives
705 def test_former_statements_refer_to_builtins(self):
706 keywords = "print", "exec"
707 # Cases where we want the custom error
708 cases = [
709 "{} foo",
710 "{} {{1:foo}}",
711 "if 1: {} foo",
712 "if 1: {} {{1:foo}}",
713 "if 1:\n {} foo",
714 "if 1:\n {} {{1:foo}}",
715 ]
716 for keyword in keywords:
717 custom_msg = "call to '{}'".format(keyword)
718 for case in cases:
719 source = case.format(keyword)
720 with self.subTest(source=source):
721 with self.assertRaisesRegex(SyntaxError, custom_msg):
722 exec(source)
723 source = source.replace("foo", "(foo.)")
724 with self.subTest(source=source):
725 with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
726 exec(source)
727
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500728 def test_del_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000729 # 'del' exprlist
730 abc = [1,2,3]
731 x, y, z = abc
732 xyz = x, y, z
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000733
Thomas Wouters89f507f2006-12-13 04:49:30 +0000734 del abc
735 del x, y, (z, xyz)
Barry Warsaw9182b452000-08-29 04:57:10 +0000736
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500737 def test_pass_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000738 # 'pass'
739 pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000740
Thomas Wouters89f507f2006-12-13 04:49:30 +0000741 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
742 # Tested below
Barry Warsaw9182b452000-08-29 04:57:10 +0000743
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500744 def test_break_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745 # 'break'
746 while 1: break
Barry Warsaw9182b452000-08-29 04:57:10 +0000747
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500748 def test_continue_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000749 # 'continue'
750 i = 1
751 while i: i = 0; continue
Barry Warsaw9182b452000-08-29 04:57:10 +0000752
Thomas Wouters89f507f2006-12-13 04:49:30 +0000753 msg = ""
754 while not msg:
755 msg = "ok"
756 try:
757 continue
758 msg = "continue failed to continue inside try"
759 except:
760 msg = "continue inside try called except block"
761 if msg != "ok":
762 self.fail(msg)
Barry Warsawefc92ee2000-08-21 15:46:50 +0000763
Thomas Wouters89f507f2006-12-13 04:49:30 +0000764 msg = ""
765 while not msg:
766 msg = "finally block not called"
767 try:
768 continue
769 finally:
770 msg = "ok"
771 if msg != "ok":
772 self.fail(msg)
Guido van Rossum3bead091992-01-27 17:00:37 +0000773
Thomas Wouters89f507f2006-12-13 04:49:30 +0000774 def test_break_continue_loop(self):
775 # This test warrants an explanation. It is a test specifically for SF bugs
776 # #463359 and #462937. The bug is that a 'break' statement executed or
777 # exception raised inside a try/except inside a loop, *after* a continue
778 # statement has been executed in that loop, will cause the wrong number of
779 # arguments to be popped off the stack and the instruction pointer reset to
780 # a very small number (usually 0.) Because of this, the following test
781 # *must* written as a function, and the tracking vars *must* be function
782 # arguments with default values. Otherwise, the test will loop and loop.
Guido van Rossum3bead091992-01-27 17:00:37 +0000783
Thomas Wouters89f507f2006-12-13 04:49:30 +0000784 def test_inner(extra_burning_oil = 1, count=0):
785 big_hippo = 2
786 while big_hippo:
787 count += 1
788 try:
789 if extra_burning_oil and big_hippo == 1:
790 extra_burning_oil -= 1
791 break
792 big_hippo -= 1
793 continue
794 except:
795 raise
796 if count > 2 or big_hippo != 1:
797 self.fail("continue then break in try/except in loop broken!")
798 test_inner()
Guido van Rossum3bead091992-01-27 17:00:37 +0000799
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500800 def test_return(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000801 # 'return' [testlist]
802 def g1(): return
803 def g2(): return 1
804 g1()
805 x = g2()
806 check_syntax_error(self, "class foo:return 1")
Guido van Rossum3bead091992-01-27 17:00:37 +0000807
Serhiy Storchaka7cc42c32018-01-02 02:38:35 +0200808 def test_break_in_finally(self):
809 count = 0
810 while count < 2:
811 count += 1
812 try:
813 pass
814 finally:
815 break
816 self.assertEqual(count, 1)
817
818 count = 0
819 while count < 2:
820 count += 1
821 try:
822 continue
823 finally:
824 break
825 self.assertEqual(count, 1)
826
827 count = 0
828 while count < 2:
829 count += 1
830 try:
831 1/0
832 finally:
833 break
834 self.assertEqual(count, 1)
835
836 for count in [0, 1]:
837 self.assertEqual(count, 0)
838 try:
839 pass
840 finally:
841 break
842 self.assertEqual(count, 0)
843
844 for count in [0, 1]:
845 self.assertEqual(count, 0)
846 try:
847 continue
848 finally:
849 break
850 self.assertEqual(count, 0)
851
852 for count in [0, 1]:
853 self.assertEqual(count, 0)
854 try:
855 1/0
856 finally:
857 break
858 self.assertEqual(count, 0)
859
860 def test_return_in_finally(self):
861 def g1():
862 try:
863 pass
864 finally:
865 return 1
866 self.assertEqual(g1(), 1)
867
868 def g2():
869 try:
870 return 2
871 finally:
872 return 3
873 self.assertEqual(g2(), 3)
874
875 def g3():
876 try:
877 1/0
878 finally:
879 return 4
880 self.assertEqual(g3(), 4)
881
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500882 def test_yield(self):
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000883 # Allowed as standalone statement
884 def g(): yield 1
885 def g(): yield from ()
886 # Allowed as RHS of assignment
887 def g(): x = yield 1
888 def g(): x = yield from ()
889 # Ordinary yield accepts implicit tuples
890 def g(): yield 1, 1
891 def g(): x = yield 1, 1
892 # 'yield from' does not
893 check_syntax_error(self, "def g(): yield from (), 1")
894 check_syntax_error(self, "def g(): x = yield from (), 1")
895 # Requires parentheses as subexpression
896 def g(): 1, (yield 1)
897 def g(): 1, (yield from ())
898 check_syntax_error(self, "def g(): 1, yield 1")
899 check_syntax_error(self, "def g(): 1, yield from ()")
900 # Requires parentheses as call argument
901 def g(): f((yield 1))
902 def g(): f((yield 1), 1)
903 def g(): f((yield from ()))
904 def g(): f((yield from ()), 1)
905 check_syntax_error(self, "def g(): f(yield 1)")
906 check_syntax_error(self, "def g(): f(yield 1, 1)")
907 check_syntax_error(self, "def g(): f(yield from ())")
908 check_syntax_error(self, "def g(): f(yield from (), 1)")
909 # Not allowed at top level
910 check_syntax_error(self, "yield")
911 check_syntax_error(self, "yield from")
912 # Not allowed at class scope
Thomas Wouters89f507f2006-12-13 04:49:30 +0000913 check_syntax_error(self, "class foo:yield 1")
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000914 check_syntax_error(self, "class foo:yield from ()")
Yury Selivanovf315c1c2015-07-23 09:10:44 +0300915 # Check annotation refleak on SyntaxError
916 check_syntax_error(self, "def g(a:(yield)): pass")
Guido van Rossum3bead091992-01-27 17:00:37 +0000917
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +0200918 def test_yield_in_comprehensions(self):
919 # Check yield in comprehensions
920 def g(): [x for x in [(yield 1)]]
921 def g(): [x for x in [(yield from ())]]
922
923 def check(code, warntext):
924 with self.assertWarnsRegex(DeprecationWarning, warntext):
925 compile(code, '<test string>', 'exec')
926 import warnings
927 with warnings.catch_warnings():
928 warnings.filterwarnings('error', category=DeprecationWarning)
929 with self.assertRaisesRegex(SyntaxError, warntext):
930 compile(code, '<test string>', 'exec')
931
932 check("def g(): [(yield x) for x in ()]",
933 "'yield' inside list comprehension")
934 check("def g(): [x for x in () if not (yield x)]",
935 "'yield' inside list comprehension")
936 check("def g(): [y for x in () for y in [(yield x)]]",
937 "'yield' inside list comprehension")
938 check("def g(): {(yield x) for x in ()}",
939 "'yield' inside set comprehension")
940 check("def g(): {(yield x): x for x in ()}",
941 "'yield' inside dict comprehension")
942 check("def g(): {x: (yield x) for x in ()}",
943 "'yield' inside dict comprehension")
944 check("def g(): ((yield x) for x in ())",
945 "'yield' inside generator expression")
946 check("def g(): [(yield from x) for x in ()]",
947 "'yield' inside list comprehension")
948 check("class C: [(yield x) for x in ()]",
949 "'yield' inside list comprehension")
950 check("[(yield x) for x in ()]",
951 "'yield' inside list comprehension")
952
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500953 def test_raise(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000954 # 'raise' test [',' test]
Collin Winter828f04a2007-08-31 00:04:24 +0000955 try: raise RuntimeError('just testing')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000956 except RuntimeError: pass
957 try: raise KeyboardInterrupt
958 except KeyboardInterrupt: pass
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000959
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500960 def test_import(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000961 # 'import' dotted_as_names
962 import sys
963 import time, sys
964 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
965 from time import time
966 from time import (time)
967 # not testable inside a function, but already done at top of the module
968 # from sys import *
969 from sys import path, argv
970 from sys import (path, argv)
971 from sys import (path, argv,)
Tim Peters10fb3862001-02-09 20:17:14 +0000972
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500973 def test_global(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974 # 'global' NAME (',' NAME)*
975 global a
976 global a, b
977 global one, two, three, four, five, six, seven, eight, nine, ten
Thomas Wouters80d373c2001-09-26 12:43:39 +0000978
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500979 def test_nonlocal(self):
Benjamin Petersona933e522008-10-24 22:16:39 +0000980 # 'nonlocal' NAME (',' NAME)*
981 x = 0
982 y = 0
983 def f():
984 nonlocal x
985 nonlocal x, y
986
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500987 def test_assert(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000988 # assertTruestmt: 'assert' test [',' test]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 assert 1
990 assert 1, 1
991 assert lambda x:x
992 assert 1, lambda x:x+1
Ezio Melotti6cc5bf72011-12-02 18:22:52 +0200993
994 try:
995 assert True
996 except AssertionError as e:
997 self.fail("'assert True' should not have raised an AssertionError")
998
999 try:
1000 assert True, 'this should always pass'
1001 except AssertionError as e:
1002 self.fail("'assert True, msg' should not have "
1003 "raised an AssertionError")
1004
1005 # these tests fail if python is run with -O, so check __debug__
1006 @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
1007 def testAssert2(self):
Thomas Wouters80d373c2001-09-26 12:43:39 +00001008 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001009 assert 0, "msg"
Guido van Rossumb940e112007-01-10 16:19:56 +00001010 except AssertionError as e:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001011 self.assertEqual(e.args[0], "msg")
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012 else:
Ezio Melotti6cc5bf72011-12-02 18:22:52 +02001013 self.fail("AssertionError not raised by assert 0")
1014
1015 try:
1016 assert False
1017 except AssertionError as e:
1018 self.assertEqual(len(e.args), 0)
1019 else:
1020 self.fail("AssertionError not raised by 'assert False'")
1021
Thomas Wouters80d373c2001-09-26 12:43:39 +00001022
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1024 # Tested below
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001026 def test_if(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001027 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1028 if 1: pass
1029 if 1: pass
1030 else: pass
1031 if 0: pass
1032 elif 0: pass
1033 if 0: pass
1034 elif 0: pass
1035 elif 0: pass
1036 elif 0: pass
1037 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001038
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001039 def test_while(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001040 # 'while' test ':' suite ['else' ':' suite]
1041 while 0: pass
1042 while 0: pass
1043 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001044
Christian Heimes969fe572008-01-25 11:23:10 +00001045 # Issue1920: "while 0" is optimized away,
1046 # ensure that the "else" clause is still present.
1047 x = 0
1048 while 0:
1049 x = 1
1050 else:
1051 x = 2
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001052 self.assertEqual(x, 2)
Christian Heimes969fe572008-01-25 11:23:10 +00001053
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001054 def test_for(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001055 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
1056 for i in 1, 2, 3: pass
1057 for i, j, k in (): pass
1058 else: pass
1059 class Squares:
1060 def __init__(self, max):
1061 self.max = max
1062 self.sofar = []
1063 def __len__(self): return len(self.sofar)
1064 def __getitem__(self, i):
1065 if not 0 <= i < self.max: raise IndexError
1066 n = len(self.sofar)
1067 while n <= i:
1068 self.sofar.append(n*n)
1069 n = n+1
1070 return self.sofar[i]
1071 n = 0
1072 for x in Squares(10): n = n+x
1073 if n != 285:
1074 self.fail('for over growing sequence')
Guido van Rossum3bead091992-01-27 17:00:37 +00001075
Thomas Wouters89f507f2006-12-13 04:49:30 +00001076 result = []
1077 for x, in [(1,), (2,), (3,)]:
1078 result.append(x)
1079 self.assertEqual(result, [1, 2, 3])
Guido van Rossum3bead091992-01-27 17:00:37 +00001080
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001081 def test_try(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001082 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1083 ### | 'try' ':' suite 'finally' ':' suite
Guido van Rossumb940e112007-01-10 16:19:56 +00001084 ### except_clause: 'except' [expr ['as' expr]]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001085 try:
1086 1/0
1087 except ZeroDivisionError:
1088 pass
1089 else:
1090 pass
1091 try: 1/0
1092 except EOFError: pass
Guido van Rossumb940e112007-01-10 16:19:56 +00001093 except TypeError as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +00001094 except: pass
1095 else: pass
1096 try: 1/0
1097 except (EOFError, TypeError, ZeroDivisionError): pass
1098 try: 1/0
Guido van Rossumb940e112007-01-10 16:19:56 +00001099 except (EOFError, TypeError, ZeroDivisionError) as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +00001100 try: pass
1101 finally: pass
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +00001102
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001103 def test_suite(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001104 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
1105 if 1: pass
1106 if 1:
1107 pass
1108 if 1:
1109 #
1110 #
1111 #
1112 pass
1113 pass
1114 #
1115 pass
1116 #
Guido van Rossum3bead091992-01-27 17:00:37 +00001117
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001118 def test_test(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001119 ### and_test ('or' and_test)*
1120 ### and_test: not_test ('and' not_test)*
1121 ### not_test: 'not' not_test | comparison
1122 if not 1: pass
1123 if 1 and 1: pass
1124 if 1 or 1: pass
1125 if not not not 1: pass
1126 if not 1 and 1 and 1: pass
1127 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001128
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001129 def test_comparison(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001130 ### comparison: expr (comp_op expr)*
1131 ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1132 if 1: pass
1133 x = (1 == 1)
1134 if 1 == 1: pass
1135 if 1 != 1: pass
1136 if 1 < 1: pass
1137 if 1 > 1: pass
1138 if 1 <= 1: pass
1139 if 1 >= 1: pass
1140 if 1 is 1: pass
1141 if 1 is not 1: pass
1142 if 1 in (): pass
1143 if 1 not in (): pass
1144 if 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 +00001145
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001146 def test_binary_mask_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147 x = 1 & 1
1148 x = 1 ^ 1
1149 x = 1 | 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001150
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001151 def test_shift_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001152 x = 1 << 1
1153 x = 1 >> 1
1154 x = 1 << 1 >> 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001155
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001156 def test_additive_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001157 x = 1
1158 x = 1 + 1
1159 x = 1 - 1 - 1
1160 x = 1 - 1 + 1 - 1 + 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001161
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001162 def test_multiplicative_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001163 x = 1 * 1
1164 x = 1 / 1
1165 x = 1 % 1
1166 x = 1 / 1 * 1 % 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001167
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001168 def test_unary_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001169 x = +1
1170 x = -1
1171 x = ~1
1172 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
1173 x = -1*1/1 + 1*1 - ---1*1
Guido van Rossum3bead091992-01-27 17:00:37 +00001174
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001175 def test_selectors(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001176 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
1177 ### subscript: expr | [expr] ':' [expr]
Guido van Rossum3bead091992-01-27 17:00:37 +00001178
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179 import sys, time
1180 c = sys.path[0]
1181 x = time.time()
1182 x = sys.modules['time'].time()
1183 a = '01234'
1184 c = a[0]
1185 c = a[-1]
1186 s = a[0:5]
1187 s = a[:5]
1188 s = a[0:]
1189 s = a[:]
1190 s = a[-5:]
1191 s = a[:-1]
1192 s = a[-4:-3]
1193 # A rough test of SF bug 1333982. http://python.org/sf/1333982
1194 # The testing here is fairly incomplete.
1195 # Test cases should include: commas with 1 and 2 colons
1196 d = {}
1197 d[1] = 1
1198 d[1,] = 2
1199 d[1,2] = 3
1200 d[1,2,3] = 4
1201 L = list(d)
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001202 L.sort(key=lambda x: (type(x).__name__, x))
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001203 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
Guido van Rossum3bead091992-01-27 17:00:37 +00001204
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001205 def test_atoms(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001206 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
1207 ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Guido van Rossum3bead091992-01-27 17:00:37 +00001208
Thomas Wouters89f507f2006-12-13 04:49:30 +00001209 x = (1)
1210 x = (1 or 2 or 3)
1211 x = (1 or 2 or 3, 2, 3)
Guido van Rossum3bead091992-01-27 17:00:37 +00001212
Thomas Wouters89f507f2006-12-13 04:49:30 +00001213 x = []
1214 x = [1]
1215 x = [1 or 2 or 3]
1216 x = [1 or 2 or 3, 2, 3]
1217 x = []
Guido van Rossum3bead091992-01-27 17:00:37 +00001218
Thomas Wouters89f507f2006-12-13 04:49:30 +00001219 x = {}
1220 x = {'one': 1}
1221 x = {'one': 1,}
1222 x = {'one' or 'two': 1 or 2}
1223 x = {'one': 1, 'two': 2}
1224 x = {'one': 1, 'two': 2,}
1225 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
Guido van Rossum3bead091992-01-27 17:00:37 +00001226
Thomas Wouters89f507f2006-12-13 04:49:30 +00001227 x = {'one'}
1228 x = {'one', 1,}
1229 x = {'one', 'two', 'three'}
1230 x = {2, 3, 4,}
1231
1232 x = x
1233 x = 'x'
1234 x = 123
1235
1236 ### exprlist: expr (',' expr)* [',']
1237 ### testlist: test (',' test)* [',']
1238 # These have been exercised enough above
1239
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001240 def test_classdef(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001241 # 'class' NAME ['(' [testlist] ')'] ':' suite
1242 class B: pass
1243 class B2(): pass
1244 class C1(B): pass
1245 class C2(B): pass
1246 class D(C1, C2, B): pass
1247 class C:
1248 def meth1(self): pass
1249 def meth2(self, arg): pass
1250 def meth3(self, a1, a2): pass
1251
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001252 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
1253 # decorators: decorator+
1254 # decorated: decorators (classdef | funcdef)
1255 def class_decorator(x): return x
1256 @class_decorator
1257 class G: pass
1258
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001259 def test_dictcomps(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001260 # dictorsetmaker: ( (test ':' test (comp_for |
1261 # (',' test ':' test)* [','])) |
1262 # (test (comp_for | (',' test)* [','])) )
1263 nums = [1, 2, 3]
1264 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
1265
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001266 def test_listcomps(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267 # list comprehension tests
1268 nums = [1, 2, 3, 4, 5]
1269 strs = ["Apple", "Banana", "Coconut"]
1270 spcs = [" Apple", " Banana ", "Coco nut "]
1271
1272 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
1273 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
1274 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
1275 self.assertEqual([(i, s) for i in nums for s in strs],
1276 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
1277 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
1278 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
1279 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
1280 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
1281 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
1282 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
1283 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
1284 (5, 'Banana'), (5, 'Coconut')])
1285 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
1286 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
1287
1288 def test_in_func(l):
1289 return [0 < x < 3 for x in l if x > 2]
1290
1291 self.assertEqual(test_in_func(nums), [False, False, False])
1292
1293 def test_nested_front():
1294 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
1295 [[1, 2], [3, 4], [5, 6]])
1296
1297 test_nested_front()
1298
1299 check_syntax_error(self, "[i, s for i in nums for s in strs]")
1300 check_syntax_error(self, "[x if y]")
1301
1302 suppliers = [
1303 (1, "Boeing"),
1304 (2, "Ford"),
1305 (3, "Macdonalds")
1306 ]
1307
1308 parts = [
1309 (10, "Airliner"),
1310 (20, "Engine"),
1311 (30, "Cheeseburger")
1312 ]
1313
1314 suppart = [
1315 (1, 10), (1, 20), (2, 20), (3, 30)
1316 ]
1317
1318 x = [
1319 (sname, pname)
1320 for (sno, sname) in suppliers
1321 for (pno, pname) in parts
1322 for (sp_sno, sp_pno) in suppart
1323 if sno == sp_sno and pno == sp_pno
1324 ]
1325
1326 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
1327 ('Macdonalds', 'Cheeseburger')])
1328
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001329 def test_genexps(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001330 # generator expression tests
1331 g = ([x for x in range(10)] for x in range(1))
Georg Brandla18af4e2007-04-21 15:47:16 +00001332 self.assertEqual(next(g), [x for x in range(10)])
Thomas Wouters89f507f2006-12-13 04:49:30 +00001333 try:
Georg Brandla18af4e2007-04-21 15:47:16 +00001334 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001335 self.fail('should produce StopIteration exception')
1336 except StopIteration:
1337 pass
1338
1339 a = 1
1340 try:
1341 g = (a for d in a)
Georg Brandla18af4e2007-04-21 15:47:16 +00001342 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001343 self.fail('should produce TypeError')
1344 except TypeError:
1345 pass
1346
1347 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
1348 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
1349
1350 a = [x for x in range(10)]
1351 b = (x for x in (y for y in a))
1352 self.assertEqual(sum(b), sum([x for x in range(10)]))
1353
1354 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
1355 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
1356 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
1357 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
1358 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
1359 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)]))
1360 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
1361 check_syntax_error(self, "foo(x for x in range(10), 100)")
1362 check_syntax_error(self, "foo(100, x for x in range(10))")
1363
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001364 def test_comprehension_specials(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001365 # test for outmost iterable precomputation
1366 x = 10; g = (i for i in range(x)); x = 5
1367 self.assertEqual(len(list(g)), 10)
1368
1369 # This should hold, since we're only precomputing outmost iterable.
1370 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
1371 x = 5; t = True;
1372 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
1373
1374 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
1375 # even though it's silly. Make sure it works (ifelse broke this.)
1376 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
1377 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
1378
1379 # verify unpacking single element tuples in listcomp/genexp.
1380 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
1381 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
1382
Benjamin Petersonf17ab892009-05-29 21:55:57 +00001383 def test_with_statement(self):
1384 class manager(object):
1385 def __enter__(self):
1386 return (1, 2)
1387 def __exit__(self, *args):
1388 pass
1389
1390 with manager():
1391 pass
1392 with manager() as x:
1393 pass
1394 with manager() as (x, y):
1395 pass
1396 with manager(), manager():
1397 pass
1398 with manager() as x, manager() as y:
1399 pass
1400 with manager() as x, manager():
1401 pass
1402
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001403 def test_if_else_expr(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001404 # Test ifelse expressions in various cases
1405 def _checkeval(msg, ret):
1406 "helper to check that evaluation of expressions is done correctly"
Victor Stinnerc6ec54d2016-04-12 18:33:41 +02001407 print(msg)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001408 return ret
1409
Nick Coghlan650f0d02007-04-15 12:05:43 +00001410 # the next line is not allowed anymore
1411 #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
Thomas Wouters89f507f2006-12-13 04:49:30 +00001412 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
1413 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])
1414 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
1415 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
1416 self.assertEqual((5 and 6 if 0 else 1), 1)
1417 self.assertEqual(((5 and 6) if 0 else 1), 1)
1418 self.assertEqual((5 and (6 if 1 else 1)), 6)
1419 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
1420 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
1421 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1422 self.assertEqual((not 5 if 1 else 1), False)
1423 self.assertEqual((not 5 if 0 else 1), 1)
1424 self.assertEqual((6 + 1 if 1 else 2), 7)
1425 self.assertEqual((6 - 1 if 1 else 2), 5)
1426 self.assertEqual((6 * 2 if 1 else 4), 12)
1427 self.assertEqual((6 / 2 if 1 else 3), 3)
1428 self.assertEqual((6 < 4 if 0 else 2), 2)
Jeremy Hylton7b03bad2006-02-28 17:46:23 +00001429
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001430 def test_paren_evaluation(self):
1431 self.assertEqual(16 // (4 // 2), 8)
1432 self.assertEqual((16 // 4) // 2, 2)
1433 self.assertEqual(16 // 4 // 2, 2)
1434 self.assertTrue(False is (2 is 3))
1435 self.assertFalse((False is 2) is 3)
1436 self.assertFalse(False is 2 is 3)
1437
Benjamin Petersond51374e2014-04-09 23:55:56 -04001438 def test_matrix_mul(self):
1439 # This is not intended to be a comprehensive test, rather just to be few
1440 # samples of the @ operator in test_grammar.py.
1441 class M:
1442 def __matmul__(self, o):
1443 return 4
1444 def __imatmul__(self, o):
1445 self.other = o
1446 return self
1447 m = M()
1448 self.assertEqual(m @ m, 4)
1449 m @= 42
1450 self.assertEqual(m.other, 42)
1451
Yury Selivanov75445082015-05-11 22:57:16 -04001452 def test_async_await(self):
Yury Selivanov75445082015-05-11 22:57:16 -04001453 async def test():
1454 def sum():
Yury Selivanov8fb307c2015-07-22 13:33:45 +03001455 pass
Yury Selivanov75445082015-05-11 22:57:16 -04001456 if 1:
1457 await someobj()
1458
1459 self.assertEqual(test.__name__, 'test')
1460 self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
1461
1462 def decorator(func):
1463 setattr(func, '_marked', True)
1464 return func
1465
1466 @decorator
1467 async def test2():
1468 return 22
1469 self.assertTrue(test2._marked)
1470 self.assertEqual(test2.__name__, 'test2')
1471 self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
1472
1473 def test_async_for(self):
1474 class Done(Exception): pass
1475
1476 class AIter:
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001477 def __aiter__(self):
Yury Selivanov75445082015-05-11 22:57:16 -04001478 return self
1479 async def __anext__(self):
1480 raise StopAsyncIteration
1481
1482 async def foo():
1483 async for i in AIter():
1484 pass
1485 async for i, j in AIter():
1486 pass
1487 async for i in AIter():
1488 pass
1489 else:
1490 pass
1491 raise Done
1492
1493 with self.assertRaises(Done):
1494 foo().send(None)
1495
1496 def test_async_with(self):
1497 class Done(Exception): pass
1498
1499 class manager:
1500 async def __aenter__(self):
1501 return (1, 2)
1502 async def __aexit__(self, *exc):
1503 return False
1504
1505 async def foo():
1506 async with manager():
1507 pass
1508 async with manager() as x:
1509 pass
1510 async with manager() as (x, y):
1511 pass
1512 async with manager(), manager():
1513 pass
1514 async with manager() as x, manager() as y:
1515 pass
1516 async with manager() as x, manager():
1517 pass
1518 raise Done
1519
1520 with self.assertRaises(Done):
1521 foo().send(None)
1522
Guido van Rossum3bead091992-01-27 17:00:37 +00001523
Thomas Wouters89f507f2006-12-13 04:49:30 +00001524if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -05001525 unittest.main()