blob: b6c457465609af9c4ec44d07d5dcf8705b8170d9 [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
Hai Shi0c4f0f32020-06-30 21:46:31 +08004from test.support import check_syntax_error
Erlend Egeberg Aaslande467ec42021-05-01 01:23:14 +02005from test.support import import_helper
Hai Shi0c4f0f32020-06-30 21:46:31 +08006from test.support.warnings_helper import check_syntax_warning
Yury Selivanov75445082015-05-11 22:57:16 -04007import inspect
Thomas Wouters89f507f2006-12-13 04:49:30 +00008import unittest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00009import sys
Serhiy Storchakad31e7732018-10-21 10:09:39 +030010import warnings
Thomas Wouters89f507f2006-12-13 04:49:30 +000011# testing import *
12from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +000013
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070014# different import patterns to check that __annotations__ does not interfere
15# with import machinery
16import test.ann_module as ann_module
17import typing
18from collections import ChainMap
19from test import ann_module2
20import test
21
Brett Cannona721aba2016-09-09 14:57:09 -070022# These are shared with test_tokenize and other test modules.
23#
24# Note: since several test cases filter out floats by looking for "e" and ".",
25# don't add hexadecimal literals that contain "e" or "E".
26VALID_UNDERSCORE_LITERALS = [
27 '0_0_0',
28 '4_2',
29 '1_0000_0000',
30 '0b1001_0100',
31 '0xffff_ffff',
32 '0o5_7_7',
33 '1_00_00.5',
34 '1_00_00.5e5',
35 '1_00_00e5_1',
36 '1e1_0',
37 '.1_4',
38 '.1_4e1',
39 '0b_0',
40 '0x_f',
41 '0o_5',
42 '1_00_00j',
43 '1_00_00.5j',
44 '1_00_00e5_1j',
45 '.1_4j',
46 '(1_2.5+3_3j)',
47 '(.5_6j)',
48]
49INVALID_UNDERSCORE_LITERALS = [
50 # Trailing underscores:
51 '0_',
52 '42_',
53 '1.4j_',
54 '0x_',
55 '0b1_',
56 '0xf_',
57 '0o5_',
58 '0 if 1_Else 1',
59 # Underscores in the base selector:
60 '0_b0',
61 '0_xf',
62 '0_o5',
63 # Old-style octal, still disallowed:
64 '0_7',
65 '09_99',
66 # Multiple consecutive underscores:
67 '4_______2',
68 '0.1__4',
69 '0.1__4j',
70 '0b1001__0100',
71 '0xffff__ffff',
72 '0x___',
73 '0o5__77',
74 '1e1__0',
75 '1e1__0j',
76 # Underscore right before a dot:
77 '1_.4',
78 '1_.4j',
79 # Underscore right after a dot:
80 '1._4',
81 '1._4j',
82 '._5',
83 '._5j',
84 # Underscore right after a sign:
85 '1.0e+_1',
86 '1.0e+_1j',
87 # Underscore right before j:
88 '1.4_j',
89 '1.4e5_j',
90 # Underscore right before e:
91 '1_e1',
92 '1.4_e1',
93 '1.4_e1j',
94 # Underscore right after e:
95 '1e_1',
96 '1.4e_1',
97 '1.4e_1j',
98 # Complex cases with parens:
99 '(1+1.5_j_)',
100 '(1+1.5_j)',
101]
102
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000103
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104class TokenTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +0000105
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +0200106 from test.support import check_syntax_error
Serhiy Storchakacf7303e2018-07-09 15:09:35 +0300107
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500108 def test_backslash(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 # Backslash means line continuation:
110 x = 1 \
111 + 1
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000112 self.assertEqual(x, 2, 'backslash for line continuation')
Guido van Rossum3bead091992-01-27 17:00:37 +0000113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 # Backslash does not means continuation in comments :\
115 x = 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000116 self.assertEqual(x, 0, 'backslash ending comment')
Guido van Rossum3bead091992-01-27 17:00:37 +0000117
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500118 def test_plain_integers(self):
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000119 self.assertEqual(type(000), type(0))
120 self.assertEqual(0xff, 255)
121 self.assertEqual(0o377, 255)
122 self.assertEqual(2147483647, 0o17777777777)
123 self.assertEqual(0b1001, 9)
Georg Brandlfceab5a2008-01-19 20:08:23 +0000124 # "0x" is not a valid literal
125 self.assertRaises(SyntaxError, eval, "0x")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000126 from sys import maxsize
127 if maxsize == 2147483647:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000128 self.assertEqual(-2147483647-1, -0o20000000000)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000129 # XXX -2147483648
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000130 self.assertTrue(0o37777777777 > 0)
131 self.assertTrue(0xffffffff > 0)
132 self.assertTrue(0b1111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000133 for s in ('2147483648', '0o40000000000', '0x100000000',
134 '0b10000000000000000000000000000000'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135 try:
136 x = eval(s)
137 except OverflowError:
138 self.fail("OverflowError on huge integer literal %r" % s)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000139 elif maxsize == 9223372036854775807:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000140 self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000141 self.assertTrue(0o1777777777777777777777 > 0)
142 self.assertTrue(0xffffffffffffffff > 0)
143 self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000144 for s in '9223372036854775808', '0o2000000000000000000000', \
145 '0x10000000000000000', \
146 '0b100000000000000000000000000000000000000000000000000000000000000':
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147 try:
148 x = eval(s)
149 except OverflowError:
150 self.fail("OverflowError on huge integer literal %r" % s)
151 else:
Christian Heimesa37d4c62007-12-04 23:02:19 +0000152 self.fail('Weird maxsize value %r' % maxsize)
Guido van Rossum3bead091992-01-27 17:00:37 +0000153
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500154 def test_long_integers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000155 x = 0
Guido van Rossume2a383d2007-01-15 16:59:06 +0000156 x = 0xffffffffffffffff
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000157 x = 0Xffffffffffffffff
158 x = 0o77777777777777777
159 x = 0O77777777777777777
Guido van Rossume2a383d2007-01-15 16:59:06 +0000160 x = 123456789012345678901234567890
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000161 x = 0b100000000000000000000000000000000000000000000000000000000000000000000
162 x = 0B111111111111111111111111111111111111111111111111111111111111111111111
Guido van Rossum3bead091992-01-27 17:00:37 +0000163
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500164 def test_floats(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000165 x = 3.14
166 x = 314.
167 x = 0.314
168 # XXX x = 000.314
169 x = .314
170 x = 3e14
171 x = 3E14
172 x = 3e-14
173 x = 3e+14
174 x = 3.e14
175 x = .3e14
176 x = 3.1e4
Guido van Rossum3bead091992-01-27 17:00:37 +0000177
Benjamin Petersonc4161622014-06-07 12:36:39 -0700178 def test_float_exponent_tokenization(self):
179 # See issue 21642.
Miss Islington (bot)eeefa7f2021-06-08 16:52:23 -0700180 with warnings.catch_warnings():
181 warnings.simplefilter('ignore', DeprecationWarning)
182 self.assertEqual(eval("1 if 1else 0"), 1)
183 self.assertEqual(eval("1 if 0else 0"), 0)
Benjamin Petersonc4161622014-06-07 12:36:39 -0700184 self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
185
Brett Cannona721aba2016-09-09 14:57:09 -0700186 def test_underscore_literals(self):
187 for lit in VALID_UNDERSCORE_LITERALS:
188 self.assertEqual(eval(lit), eval(lit.replace('_', '')))
189 for lit in INVALID_UNDERSCORE_LITERALS:
190 self.assertRaises(SyntaxError, eval, lit)
191 # Sanity check: no literal begins with an underscore
192 self.assertRaises(NameError, eval, "_0")
193
Serhiy Storchakacf7303e2018-07-09 15:09:35 +0300194 def test_bad_numerical_literals(self):
195 check = self.check_syntax_error
196 check("0b12", "invalid digit '2' in binary literal")
197 check("0b1_2", "invalid digit '2' in binary literal")
198 check("0b2", "invalid digit '2' in binary literal")
199 check("0b1_", "invalid binary literal")
200 check("0b", "invalid binary literal")
201 check("0o18", "invalid digit '8' in octal literal")
202 check("0o1_8", "invalid digit '8' in octal literal")
203 check("0o8", "invalid digit '8' in octal literal")
204 check("0o1_", "invalid octal literal")
205 check("0o", "invalid octal literal")
206 check("0x1_", "invalid hexadecimal literal")
207 check("0x", "invalid hexadecimal literal")
208 check("1_", "invalid decimal literal")
209 check("012",
210 "leading zeros in decimal integer literals are not permitted; "
211 "use an 0o prefix for octal integers")
212 check("1.2_", "invalid decimal literal")
213 check("1e2_", "invalid decimal literal")
214 check("1e+", "invalid decimal literal")
215
Miss Islington (bot)eeefa7f2021-06-08 16:52:23 -0700216 def test_end_of_numerical_literals(self):
217 def check(test):
218 with self.assertWarns(DeprecationWarning):
219 compile(test, "<testcase>", "eval")
220
221 def check_error(test):
222 with warnings.catch_warnings(record=True) as w:
223 with self.assertRaises(SyntaxError):
224 compile(test, "<testcase>", "eval")
225 self.assertEqual(w, [])
226
227 check_error("0xfand x")
228 check("0o7and x")
229 check("0b1and x")
230 check("9and x")
231 check("0and x")
232 check("1.and x")
233 check("1e3and x")
234 check("1jand x")
235
236 check("0xfor x")
237 check("0o7or x")
238 check("0b1or x")
239 check("9or x")
240 check_error("0or x")
241 check("1.or x")
242 check("1e3or x")
243 check("1jor x")
244
245 check("0xfin x")
246 check("0o7in x")
247 check("0b1in x")
248 check("9in x")
249 check("0in x")
250 check("1.in x")
251 check("1e3in x")
252 check("1jin x")
253
254 with warnings.catch_warnings():
255 warnings.simplefilter('ignore', SyntaxWarning)
256 check("0xfis x")
257 check("0o7is x")
258 check("0b1is x")
259 check("9is x")
260 check("0is x")
261 check("1.is x")
262 check("1e3is x")
263 check("1jis x")
264
265 check("0xfif x else y")
266 check("0o7if x else y")
267 check("0b1if x else y")
268 check("9if x else y")
269 check("0if x else y")
270 check("1.if x else y")
271 check("1e3if x else y")
272 check("1jif x else y")
273
274 check_error("x if 0xfelse y")
275 check("x if 0o7else y")
276 check("x if 0b1else y")
277 check("x if 9else y")
278 check("x if 0else y")
279 check("x if 1.else y")
280 check("x if 1e3else y")
281 check("x if 1jelse y")
282
283 check("[0x1ffor x in ()]")
284 check("[0x1for x in ()]")
285 check("[0xfor x in ()]")
286 check("[0o7for x in ()]")
287 check("[0b1for x in ()]")
288 check("[9for x in ()]")
289 check("[1.for x in ()]")
290 check("[1e3for x in ()]")
291 check("[1jfor x in ()]")
292
293 check_error("0xfspam")
294 check_error("0o7spam")
295 check_error("0b1spam")
296 check_error("9spam")
297 check_error("0spam")
298 check_error("1.spam")
299 check_error("1e3spam")
300 check_error("1jspam")
301
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500302 def test_string_literals(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000303 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
304 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
305 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306 x = "doesn't \"shrink\" does it"
307 y = 'doesn\'t "shrink" does it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000308 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000309 x = "does \"shrink\" doesn't it"
310 y = 'does "shrink" doesn\'t it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000311 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000312 x = """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000313The "quick"
314brown fox
315jumps over
316the 'lazy' dog.
317"""
Thomas Wouters89f507f2006-12-13 04:49:30 +0000318 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000319 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000320 y = '''
Guido van Rossumb6775db1994-08-01 11:34:53 +0000321The "quick"
322brown fox
323jumps over
324the 'lazy' dog.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325'''
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000326 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327 y = "\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000328The \"quick\"\n\
329brown fox\n\
330jumps over\n\
331the 'lazy' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000332"
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000333 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000334 y = '\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000335The \"quick\"\n\
336brown fox\n\
337jumps over\n\
338the \'lazy\' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000340 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000341
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500342 def test_ellipsis(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000343 x = ...
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000344 self.assertTrue(x is Ellipsis)
Georg Brandldde00282007-03-18 19:01:53 +0000345 self.assertRaises(SyntaxError, eval, ".. .")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346
Benjamin Peterson758888d2011-05-30 11:12:38 -0500347 def test_eof_error(self):
348 samples = ("def foo(", "\ndef foo(", "def foo(\n")
349 for s in samples:
350 with self.assertRaises(SyntaxError) as cm:
351 compile(s, "<test>", "exec")
Pablo Galindod6d63712021-01-19 23:59:33 +0000352 self.assertIn("was never closed", str(cm.exception))
Benjamin Peterson758888d2011-05-30 11:12:38 -0500353
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700354var_annot_global: int # a global annotated is necessary for test_var_annot
355
356# custom namespace for testing __annotations__
357
358class CNS:
359 def __init__(self):
360 self._dct = {}
361 def __setitem__(self, item, value):
362 self._dct[item.lower()] = value
363 def __getitem__(self, item):
364 return self._dct[item]
365
366
Thomas Wouters89f507f2006-12-13 04:49:30 +0000367class GrammarTests(unittest.TestCase):
368
Hai Shi0c4f0f32020-06-30 21:46:31 +0800369 from test.support import check_syntax_error
370 from test.support.warnings_helper import check_syntax_warning
tsukasa-aua8ef4572021-03-16 22:14:41 +1100371 from test.support.warnings_helper import check_no_warnings
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +0200372
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
374 # XXX can't test in a script -- this rule is only used when interactive
375
376 # file_input: (NEWLINE | stmt)* ENDMARKER
377 # Being tested as this very moment this very module
378
379 # expr_input: testlist NEWLINE
380 # XXX Hard to test -- used only in calls to input()
381
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500382 def test_eval_input(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000383 # testlist ENDMARKER
384 x = eval('1, 0 or 1')
385
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700386 def test_var_annot_basics(self):
387 # all these should be allowed
388 var1: int = 5
389 var2: [int, str]
390 my_lst = [42]
391 def one():
392 return 1
393 int.new_attr: int
394 [list][0]: type
395 my_lst[one()-1]: int = 5
396 self.assertEqual(my_lst, [5])
397
398 def test_var_annot_syntax_errors(self):
399 # parser pass
400 check_syntax_error(self, "def f: int")
401 check_syntax_error(self, "x: int: str")
402 check_syntax_error(self, "def f():\n"
403 " nonlocal x: int\n")
404 # AST pass
405 check_syntax_error(self, "[x, 0]: int\n")
406 check_syntax_error(self, "f(): int\n")
407 check_syntax_error(self, "(x,): int")
408 check_syntax_error(self, "def f():\n"
409 " (x, y): int = (1, 2)\n")
410 # symtable pass
411 check_syntax_error(self, "def f():\n"
412 " x: int\n"
413 " global x\n")
414 check_syntax_error(self, "def f():\n"
415 " global x\n"
416 " x: int\n")
417
418 def test_var_annot_basic_semantics(self):
419 # execution order
420 with self.assertRaises(ZeroDivisionError):
421 no_name[does_not_exist]: no_name_again = 1/0
422 with self.assertRaises(NameError):
423 no_name[does_not_exist]: 1/0 = 0
424 global var_annot_global
425
426 # function semantics
427 def f():
428 st: str = "Hello"
429 a.b: int = (1, 2)
430 return st
431 self.assertEqual(f.__annotations__, {})
432 def f_OK():
433 x: 1/0
434 f_OK()
435 def fbad():
436 x: int
437 print(x)
438 with self.assertRaises(UnboundLocalError):
439 fbad()
440 def f2bad():
441 (no_such_global): int
442 print(no_such_global)
443 try:
444 f2bad()
445 except Exception as e:
446 self.assertIs(type(e), NameError)
447
448 # class semantics
449 class C:
Guido van Rossum015d8742016-09-11 09:45:24 -0700450 __foo: int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700451 s: str = "attr"
452 z = 2
453 def __init__(self, x):
454 self.x: int = x
Pablo Galindob0544ba2021-04-21 12:41:19 +0100455 self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700456 with self.assertRaises(NameError):
457 class CBad:
458 no_such_name_defined.attr: int = 0
459 with self.assertRaises(NameError):
460 class Cbad2(C):
461 x: int
462 x.y: list = []
463
464 def test_var_annot_metaclass_semantics(self):
465 class CMeta(type):
466 @classmethod
467 def __prepare__(metacls, name, bases, **kwds):
468 return {'__annotations__': CNS()}
469 class CC(metaclass=CMeta):
470 XX: 'ANNOT'
Pablo Galindob0544ba2021-04-21 12:41:19 +0100471 self.assertEqual(CC.__annotations__['xx'], 'ANNOT')
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700472
473 def test_var_annot_module_semantics(self):
larryhastings2f2b6982021-04-29 20:09:08 -0700474 self.assertEqual(test.__annotations__, {})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700475 self.assertEqual(ann_module.__annotations__,
Ken Jina2721642021-07-19 22:22:59 +0800476 {1: 2, 'x': int, 'y': str, 'f': typing.Tuple[int, int], 'u': int | float})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700477 self.assertEqual(ann_module.M.__annotations__,
Pablo Galindob0544ba2021-04-21 12:41:19 +0100478 {'123': 123, 'o': type})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700479 self.assertEqual(ann_module2.__annotations__, {})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700480
481 def test_var_annot_in_module(self):
482 # check that functions fail the same way when executed
483 # outside of module where they were defined
Erlend Egeberg Aaslande467ec42021-05-01 01:23:14 +0200484 ann_module3 = import_helper.import_fresh_module("test.ann_module3")
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700485 with self.assertRaises(NameError):
Erlend Egeberg Aaslande467ec42021-05-01 01:23:14 +0200486 ann_module3.f_bad_ann()
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700487 with self.assertRaises(NameError):
Erlend Egeberg Aaslande467ec42021-05-01 01:23:14 +0200488 ann_module3.g_bad_ann()
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700489 with self.assertRaises(NameError):
Erlend Egeberg Aaslande467ec42021-05-01 01:23:14 +0200490 ann_module3.D_bad_ann(5)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700491
492 def test_var_annot_simple_exec(self):
493 gns = {}; lns= {}
494 exec("'docstring'\n"
495 "__annotations__[1] = 2\n"
496 "x: int = 5\n", gns, lns)
Pablo Galindob0544ba2021-04-21 12:41:19 +0100497 self.assertEqual(lns["__annotations__"], {1: 2, 'x': int})
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700498 with self.assertRaises(KeyError):
499 gns['__annotations__']
500
501 def test_var_annot_custom_maps(self):
502 # tests with custom locals() and __annotations__
503 ns = {'__annotations__': CNS()}
504 exec('X: int; Z: str = "Z"; (w): complex = 1j', ns)
Pablo Galindob0544ba2021-04-21 12:41:19 +0100505 self.assertEqual(ns['__annotations__']['x'], int)
506 self.assertEqual(ns['__annotations__']['z'], str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700507 with self.assertRaises(KeyError):
508 ns['__annotations__']['w']
509 nonloc_ns = {}
510 class CNS2:
511 def __init__(self):
512 self._dct = {}
513 def __setitem__(self, item, value):
514 nonlocal nonloc_ns
515 self._dct[item] = value
516 nonloc_ns[item] = value
517 def __getitem__(self, item):
518 return self._dct[item]
519 exec('x: int = 1', {}, CNS2())
Pablo Galindob0544ba2021-04-21 12:41:19 +0100520 self.assertEqual(nonloc_ns['__annotations__']['x'], int)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700521
522 def test_var_annot_refleak(self):
523 # complex case: custom locals plus custom __annotations__
524 # this was causing refleak
525 cns = CNS()
526 nonloc_ns = {'__annotations__': cns}
527 class CNS2:
528 def __init__(self):
529 self._dct = {'__annotations__': cns}
530 def __setitem__(self, item, value):
531 nonlocal nonloc_ns
532 self._dct[item] = value
533 nonloc_ns[item] = value
534 def __getitem__(self, item):
535 return self._dct[item]
536 exec('X: str', {}, CNS2())
Pablo Galindob0544ba2021-04-21 12:41:19 +0100537 self.assertEqual(nonloc_ns['__annotations__']['x'], str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700538
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +0000539 def test_var_annot_rhs(self):
540 ns = {}
541 exec('x: tuple = 1, 2', ns)
542 self.assertEqual(ns['x'], (1, 2))
543 stmt = ('def f():\n'
544 ' x: int = yield')
545 exec(stmt, ns)
546 self.assertEqual(list(ns['f']()), [None])
547
Pablo Galindo8565f6b2019-06-03 08:34:20 +0100548 ns = {"a": 1, 'b': (2, 3, 4), "c":5, "Tuple": typing.Tuple}
549 exec('x: Tuple[int, ...] = a,*b,c', ns)
550 self.assertEqual(ns['x'], (1, 2, 3, 4, 5))
551
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500552 def test_funcdef(self):
Neal Norwitzc1505362006-12-28 06:47:50 +0000553 ### [decorators] 'def' NAME parameters ['->' test] ':' suite
Brandt Bucherbe501ca2020-03-03 14:25:44 -0800554 ### decorator: '@' namedexpr_test NEWLINE
Neal Norwitzc1505362006-12-28 06:47:50 +0000555 ### decorators: decorator+
556 ### parameters: '(' [typedargslist] ')'
557 ### typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000558 ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000559 ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000560 ### tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +0000561 ### varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000562 ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000563 ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000564 ### vfpdef: NAME
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 def f1(): pass
566 f1()
567 f1(*())
568 f1(*(), **{})
569 def f2(one_argument): pass
570 def f3(two, arguments): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000571 self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
572 self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000573 def a1(one_arg,): pass
574 def a2(two, args,): pass
575 def v0(*rest): pass
576 def v1(a, *rest): pass
577 def v2(a, b, *rest): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000578
579 f1()
580 f2(1)
581 f2(1,)
582 f3(1, 2)
583 f3(1, 2,)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000584 v0()
585 v0(1)
586 v0(1,)
587 v0(1,2)
588 v0(1,2,3,4,5,6,7,8,9,0)
589 v1(1)
590 v1(1,)
591 v1(1,2)
592 v1(1,2,3)
593 v1(1,2,3,4,5,6,7,8,9,0)
594 v2(1,2)
595 v2(1,2,3)
596 v2(1,2,3,4)
597 v2(1,2,3,4,5,6,7,8,9,0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000598
Thomas Wouters89f507f2006-12-13 04:49:30 +0000599 def d01(a=1): pass
600 d01()
601 d01(1)
602 d01(*(1,))
Yury Selivanov14acf5f2015-08-05 17:54:10 -0400603 d01(*[] or [2])
604 d01(*() or (), *{} and (), **() or {})
Thomas Wouters89f507f2006-12-13 04:49:30 +0000605 d01(**{'a':2})
Benjamin Petersonde12b792015-05-16 09:44:45 -0400606 d01(**{'a':2} or {})
Thomas Wouters89f507f2006-12-13 04:49:30 +0000607 def d11(a, b=1): pass
608 d11(1)
609 d11(1, 2)
610 d11(1, **{'b':2})
611 def d21(a, b, c=1): pass
612 d21(1, 2)
613 d21(1, 2, 3)
614 d21(*(1, 2, 3))
615 d21(1, *(2, 3))
616 d21(1, 2, *(3,))
617 d21(1, 2, **{'c':3})
618 def d02(a=1, b=2): pass
619 d02()
620 d02(1)
621 d02(1, 2)
622 d02(*(1, 2))
623 d02(1, *(2,))
624 d02(1, **{'b':2})
625 d02(**{'a': 1, 'b': 2})
626 def d12(a, b=1, c=2): pass
627 d12(1)
628 d12(1, 2)
629 d12(1, 2, 3)
630 def d22(a, b, c=1, d=2): pass
631 d22(1, 2)
632 d22(1, 2, 3)
633 d22(1, 2, 3, 4)
634 def d01v(a=1, *rest): pass
635 d01v()
636 d01v(1)
637 d01v(1, 2)
638 d01v(*(1, 2, 3, 4))
639 d01v(*(1,))
640 d01v(**{'a':2})
641 def d11v(a, b=1, *rest): pass
642 d11v(1)
643 d11v(1, 2)
644 d11v(1, 2, 3)
645 def d21v(a, b, c=1, *rest): pass
646 d21v(1, 2)
647 d21v(1, 2, 3)
648 d21v(1, 2, 3, 4)
649 d21v(*(1, 2, 3, 4))
650 d21v(1, 2, **{'c': 3})
651 def d02v(a=1, b=2, *rest): pass
652 d02v()
653 d02v(1)
654 d02v(1, 2)
655 d02v(1, 2, 3)
656 d02v(1, *(2, 3, 4))
657 d02v(**{'a': 1, 'b': 2})
658 def d12v(a, b=1, c=2, *rest): pass
659 d12v(1)
660 d12v(1, 2)
661 d12v(1, 2, 3)
662 d12v(1, 2, 3, 4)
663 d12v(*(1, 2, 3, 4))
664 d12v(1, 2, *(3, 4, 5))
665 d12v(1, *(2,), **{'c': 3})
666 def d22v(a, b, c=1, d=2, *rest): pass
667 d22v(1, 2)
668 d22v(1, 2, 3)
669 d22v(1, 2, 3, 4)
670 d22v(1, 2, 3, 4, 5)
671 d22v(*(1, 2, 3, 4))
672 d22v(1, 2, *(3, 4, 5))
673 d22v(1, *(2, 3), **{'d': 4})
Georg Brandld8b690f2008-05-16 17:28:50 +0000674
675 # keyword argument type tests
Serhiy Storchaka12f43342020-07-20 15:53:55 +0300676 with warnings.catch_warnings():
677 warnings.simplefilter('ignore', BytesWarning)
678 try:
679 str('x', **{b'foo':1 })
680 except TypeError:
681 pass
682 else:
683 self.fail('Bytes should not work as keyword argument names')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000684 # keyword only argument tests
685 def pos0key1(*, key): return key
686 pos0key1(key=100)
687 def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
688 pos2key2(1, 2, k1=100)
689 pos2key2(1, 2, k1=100, k2=200)
690 pos2key2(1, 2, k2=100, k1=200)
691 def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
692 pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
693 pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
694
Robert Collinsdf395992015-08-12 08:00:06 +1200695 self.assertRaises(SyntaxError, eval, "def f(*): pass")
696 self.assertRaises(SyntaxError, eval, "def f(*,): pass")
697 self.assertRaises(SyntaxError, eval, "def f(*, **kwds): pass")
698
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000699 # keyword arguments after *arglist
700 def f(*args, **kwargs):
701 return args, kwargs
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000702 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000703 {'x':2, 'y':5}))
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400704 self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000705 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400706 self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
707 ((), {'eggs':'scrambled', 'spam':'fried'}))
708 self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
709 ((), {'eggs':'scrambled', 'spam':'fried'}))
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000710
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +0200711 # Check ast errors in *args and *kwargs
712 check_syntax_error(self, "f(*g(1=2))")
713 check_syntax_error(self, "f(**g(1=2))")
714
Neal Norwitzc1505362006-12-28 06:47:50 +0000715 # argument annotation tests
716 def f(x) -> list: pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100717 self.assertEqual(f.__annotations__, {'return': list})
Zachary Warece17f762015-08-01 21:55:36 -0500718 def f(x: int): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100719 self.assertEqual(f.__annotations__, {'x': int})
Pablo Galindoa0c01bf2019-05-31 15:19:50 +0100720 def f(x: int, /): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100721 self.assertEqual(f.__annotations__, {'x': int})
Pablo Galindoa0c01bf2019-05-31 15:19:50 +0100722 def f(x: int = 34, /): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100723 self.assertEqual(f.__annotations__, {'x': int})
Zachary Warece17f762015-08-01 21:55:36 -0500724 def f(*x: str): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100725 self.assertEqual(f.__annotations__, {'x': str})
Zachary Warece17f762015-08-01 21:55:36 -0500726 def f(**x: float): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100727 self.assertEqual(f.__annotations__, {'x': float})
728 def f(x, y: 1+2): pass
729 self.assertEqual(f.__annotations__, {'y': 3})
730 def f(x, y: 1+2, /): pass
731 self.assertEqual(f.__annotations__, {'y': 3})
Zachary Warece17f762015-08-01 21:55:36 -0500732 def f(a, b: 1, c: 2, d): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100733 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
Pablo Galindoa0c01bf2019-05-31 15:19:50 +0100734 def f(a, b: 1, /, c: 2, d): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100735 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
Zachary Warece17f762015-08-01 21:55:36 -0500736 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000737 self.assertEqual(f.__annotations__,
Pablo Galindob0544ba2021-04-21 12:41:19 +0100738 {'b': 1, 'c': 2, 'e': 3, 'g': 6})
Zachary Warece17f762015-08-01 21:55:36 -0500739 def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10,
740 **k: 11) -> 12: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000741 self.assertEqual(f.__annotations__,
Pablo Galindob0544ba2021-04-21 12:41:19 +0100742 {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
743 'k': 11, 'return': 12})
Pablo Galindoa0c01bf2019-05-31 15:19:50 +0100744 def f(a, b: 1, c: 2, d, e: 3 = 4, f: int = 5, /, *g: 6, h: 7, i=8, j: 9 = 10,
745 **k: 11) -> 12: pass
746 self.assertEqual(f.__annotations__,
Pablo Galindob0544ba2021-04-21 12:41:19 +0100747 {'b': 1, 'c': 2, 'e': 3, 'f': int, 'g': 6, 'h': 7, 'j': 9,
748 'k': 11, 'return': 12})
Yury Selivanov34ce99f2014-02-18 12:49:41 -0500749 # Check for issue #20625 -- annotations mangling
750 class Spam:
Zachary Warece17f762015-08-01 21:55:36 -0500751 def f(self, *, __kw: 1):
Yury Selivanov34ce99f2014-02-18 12:49:41 -0500752 pass
753 class Ham(Spam): pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100754 self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
755 self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
Nick Coghlan71011e22007-04-23 11:05:01 +0000756 # Check for SF Bug #1697248 - mixing decorators and a return annotation
757 def null(x): return x
758 @null
759 def f(x) -> list: pass
Pablo Galindob0544ba2021-04-21 12:41:19 +0100760 self.assertEqual(f.__annotations__, {'return': list})
Nick Coghlan71011e22007-04-23 11:05:01 +0000761
Brandt Bucherbe501ca2020-03-03 14:25:44 -0800762 # Test expressions as decorators (PEP 614):
763 @False or null
764 def f(x): pass
765 @d := null
766 def f(x): pass
767 @lambda f: null(f)
768 def f(x): pass
769 @[..., null, ...][1]
770 def f(x): pass
771 @null(null)(null)
772 def f(x): pass
773 @[null][0].__call__.__call__
774 def f(x): pass
775
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300776 # test closures with a variety of opargs
Guido van Rossum0240b922007-02-26 21:23:50 +0000777 closure = 1
778 def f(): return closure
779 def f(x=1): return closure
780 def f(*, k=1): return closure
781 def f() -> int: return closure
Neal Norwitzc1505362006-12-28 06:47:50 +0000782
Robert Collinsdf395992015-08-12 08:00:06 +1200783 # Check trailing commas are permitted in funcdef argument list
784 def f(a,): pass
785 def f(*args,): pass
786 def f(**kwds,): pass
787 def f(a, *args,): pass
788 def f(a, **kwds,): pass
789 def f(*args, b,): pass
790 def f(*, b,): pass
791 def f(*args, **kwds,): pass
792 def f(a, *args, b,): pass
793 def f(a, *, b,): pass
794 def f(a, *args, **kwds,): pass
795 def f(*args, b, **kwds,): pass
796 def f(*, b, **kwds,): pass
797 def f(a, *args, b, **kwds,): pass
798 def f(a, *, b, **kwds,): pass
799
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500800 def test_lambdef(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000801 ### lambdef: 'lambda' [varargslist] ':' test
802 l1 = lambda : 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000803 self.assertEqual(l1(), 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000804 l2 = lambda : a[d] # XXX just testing the expression
Guido van Rossume2a383d2007-01-15 16:59:06 +0000805 l3 = lambda : [2 < x for x in [-1, 3, 0]]
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000806 self.assertEqual(l3(), [0, 1, 0])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000807 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000808 self.assertEqual(l4(), 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000809 l5 = lambda x, y, z=2: x + y + z
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000810 self.assertEqual(l5(1, 2), 5)
811 self.assertEqual(l5(1, 2, 3), 6)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000812 check_syntax_error(self, "lambda x: x = 2")
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000813 check_syntax_error(self, "lambda (None,): None")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000814 l6 = lambda x, y, *, k=20: x+y+k
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000815 self.assertEqual(l6(1,2), 1+2+20)
816 self.assertEqual(l6(1,2,k=10), 1+2+10)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000817
Robert Collinsdf395992015-08-12 08:00:06 +1200818 # check that trailing commas are permitted
819 l10 = lambda a,: 0
820 l11 = lambda *args,: 0
821 l12 = lambda **kwds,: 0
822 l13 = lambda a, *args,: 0
823 l14 = lambda a, **kwds,: 0
824 l15 = lambda *args, b,: 0
825 l16 = lambda *, b,: 0
826 l17 = lambda *args, **kwds,: 0
827 l18 = lambda a, *args, b,: 0
828 l19 = lambda a, *, b,: 0
829 l20 = lambda a, *args, **kwds,: 0
830 l21 = lambda *args, b, **kwds,: 0
831 l22 = lambda *, b, **kwds,: 0
832 l23 = lambda a, *args, b, **kwds,: 0
833 l24 = lambda a, *, b, **kwds,: 0
834
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000835
Thomas Wouters89f507f2006-12-13 04:49:30 +0000836 ### stmt: simple_stmt | compound_stmt
837 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000838
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500839 def test_simple_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000840 ### simple_stmt: small_stmt (';' small_stmt)* [';']
841 x = 1; pass; del x
842 def foo():
Ezio Melotti13925002011-03-16 11:05:33 +0200843 # verify statements that end with semi-colons
Thomas Wouters89f507f2006-12-13 04:49:30 +0000844 x = 1; pass; del x;
845 foo()
Georg Brandl52318d62006-09-06 07:06:08 +0000846
Guido van Rossumd8faa362007-04-27 19:54:29 +0000847 ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +0000848 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000849
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500850 def test_expr_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000851 # (exprlist '=')* exprlist
Victor Stinner15a30952016-02-08 22:45:06 +0100852 1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000853 1, 2, 3
854 x = 1
855 x = 1, 2, 3
856 x = y = z = 1, 2, 3
857 x, y, z = 1, 2, 3
858 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
Guido van Rossum3bead091992-01-27 17:00:37 +0000859
Thomas Wouters89f507f2006-12-13 04:49:30 +0000860 check_syntax_error(self, "x + 1 = 1")
861 check_syntax_error(self, "a + 1 = b + 2")
Guido van Rossum3bead091992-01-27 17:00:37 +0000862
Nick Coghlan5b1fdc12014-06-16 19:48:02 +1000863 # Check the heuristic for print & exec covers significant cases
864 # As well as placing some limits on false positives
865 def test_former_statements_refer_to_builtins(self):
866 keywords = "print", "exec"
867 # Cases where we want the custom error
868 cases = [
869 "{} foo",
870 "{} {{1:foo}}",
871 "if 1: {} foo",
872 "if 1: {} {{1:foo}}",
873 "if 1:\n {} foo",
874 "if 1:\n {} {{1:foo}}",
875 ]
876 for keyword in keywords:
877 custom_msg = "call to '{}'".format(keyword)
878 for case in cases:
879 source = case.format(keyword)
880 with self.subTest(source=source):
881 with self.assertRaisesRegex(SyntaxError, custom_msg):
882 exec(source)
883 source = source.replace("foo", "(foo.)")
884 with self.subTest(source=source):
885 with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
886 exec(source)
887
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500888 def test_del_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889 # 'del' exprlist
890 abc = [1,2,3]
891 x, y, z = abc
892 xyz = x, y, z
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000893
Thomas Wouters89f507f2006-12-13 04:49:30 +0000894 del abc
895 del x, y, (z, xyz)
Barry Warsaw9182b452000-08-29 04:57:10 +0000896
Shantanu27c0d9b2020-05-11 14:53:58 -0700897 x, y, z = "xyz"
898 del x
899 del y,
900 del (z)
901 del ()
902
903 a, b, c, d, e, f, g = "abcdefg"
904 del a, (b, c), (d, (e, f))
905
906 a, b, c, d, e, f, g = "abcdefg"
907 del a, [b, c], (d, [e, f])
908
909 abcd = list("abcd")
910 del abcd[1:2]
911
912 compile("del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()", "<testcase>", "exec")
913
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500914 def test_pass_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000915 # 'pass'
916 pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000917
Thomas Wouters89f507f2006-12-13 04:49:30 +0000918 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
919 # Tested below
Barry Warsaw9182b452000-08-29 04:57:10 +0000920
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500921 def test_break_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000922 # 'break'
923 while 1: break
Barry Warsaw9182b452000-08-29 04:57:10 +0000924
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500925 def test_continue_stmt(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000926 # 'continue'
927 i = 1
928 while i: i = 0; continue
Barry Warsaw9182b452000-08-29 04:57:10 +0000929
Thomas Wouters89f507f2006-12-13 04:49:30 +0000930 msg = ""
931 while not msg:
932 msg = "ok"
933 try:
934 continue
935 msg = "continue failed to continue inside try"
936 except:
937 msg = "continue inside try called except block"
938 if msg != "ok":
939 self.fail(msg)
Barry Warsawefc92ee2000-08-21 15:46:50 +0000940
Thomas Wouters89f507f2006-12-13 04:49:30 +0000941 msg = ""
942 while not msg:
943 msg = "finally block not called"
944 try:
945 continue
946 finally:
947 msg = "ok"
948 if msg != "ok":
949 self.fail(msg)
Guido van Rossum3bead091992-01-27 17:00:37 +0000950
Thomas Wouters89f507f2006-12-13 04:49:30 +0000951 def test_break_continue_loop(self):
952 # This test warrants an explanation. It is a test specifically for SF bugs
953 # #463359 and #462937. The bug is that a 'break' statement executed or
954 # exception raised inside a try/except inside a loop, *after* a continue
955 # statement has been executed in that loop, will cause the wrong number of
956 # arguments to be popped off the stack and the instruction pointer reset to
957 # a very small number (usually 0.) Because of this, the following test
958 # *must* written as a function, and the tracking vars *must* be function
959 # arguments with default values. Otherwise, the test will loop and loop.
Guido van Rossum3bead091992-01-27 17:00:37 +0000960
Thomas Wouters89f507f2006-12-13 04:49:30 +0000961 def test_inner(extra_burning_oil = 1, count=0):
962 big_hippo = 2
963 while big_hippo:
964 count += 1
965 try:
966 if extra_burning_oil and big_hippo == 1:
967 extra_burning_oil -= 1
968 break
969 big_hippo -= 1
970 continue
971 except:
972 raise
973 if count > 2 or big_hippo != 1:
974 self.fail("continue then break in try/except in loop broken!")
975 test_inner()
Guido van Rossum3bead091992-01-27 17:00:37 +0000976
Benjamin Petersonc8507bf2011-05-30 10:52:48 -0500977 def test_return(self):
David Cuthbertfd97d1f2018-09-21 18:31:15 -0700978 # 'return' [testlist_star_expr]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 def g1(): return
980 def g2(): return 1
David Cuthbertfd97d1f2018-09-21 18:31:15 -0700981 def g3():
982 z = [2, 3]
983 return 1, *z
984
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 g1()
986 x = g2()
David Cuthbertfd97d1f2018-09-21 18:31:15 -0700987 y = g3()
988 self.assertEqual(y, (1, 2, 3), "unparenthesized star expr return")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000989 check_syntax_error(self, "class foo:return 1")
Guido van Rossum3bead091992-01-27 17:00:37 +0000990
Serhiy Storchaka7cc42c32018-01-02 02:38:35 +0200991 def test_break_in_finally(self):
992 count = 0
993 while count < 2:
994 count += 1
995 try:
996 pass
997 finally:
998 break
999 self.assertEqual(count, 1)
1000
1001 count = 0
1002 while count < 2:
1003 count += 1
1004 try:
1005 continue
1006 finally:
1007 break
1008 self.assertEqual(count, 1)
1009
1010 count = 0
1011 while count < 2:
1012 count += 1
1013 try:
1014 1/0
1015 finally:
1016 break
1017 self.assertEqual(count, 1)
1018
1019 for count in [0, 1]:
1020 self.assertEqual(count, 0)
1021 try:
1022 pass
1023 finally:
1024 break
1025 self.assertEqual(count, 0)
1026
1027 for count in [0, 1]:
1028 self.assertEqual(count, 0)
1029 try:
1030 continue
1031 finally:
1032 break
1033 self.assertEqual(count, 0)
1034
1035 for count in [0, 1]:
1036 self.assertEqual(count, 0)
1037 try:
1038 1/0
1039 finally:
1040 break
1041 self.assertEqual(count, 0)
1042
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +02001043 def test_continue_in_finally(self):
1044 count = 0
1045 while count < 2:
1046 count += 1
1047 try:
1048 pass
1049 finally:
1050 continue
1051 break
1052 self.assertEqual(count, 2)
1053
1054 count = 0
1055 while count < 2:
1056 count += 1
1057 try:
1058 break
1059 finally:
1060 continue
1061 self.assertEqual(count, 2)
1062
1063 count = 0
1064 while count < 2:
1065 count += 1
1066 try:
1067 1/0
1068 finally:
1069 continue
1070 break
1071 self.assertEqual(count, 2)
1072
1073 for count in [0, 1]:
1074 try:
1075 pass
1076 finally:
1077 continue
1078 break
1079 self.assertEqual(count, 1)
1080
1081 for count in [0, 1]:
1082 try:
1083 break
1084 finally:
1085 continue
1086 self.assertEqual(count, 1)
1087
1088 for count in [0, 1]:
1089 try:
1090 1/0
1091 finally:
1092 continue
1093 break
1094 self.assertEqual(count, 1)
1095
Serhiy Storchaka7cc42c32018-01-02 02:38:35 +02001096 def test_return_in_finally(self):
1097 def g1():
1098 try:
1099 pass
1100 finally:
1101 return 1
1102 self.assertEqual(g1(), 1)
1103
1104 def g2():
1105 try:
1106 return 2
1107 finally:
1108 return 3
1109 self.assertEqual(g2(), 3)
1110
1111 def g3():
1112 try:
1113 1/0
1114 finally:
1115 return 4
1116 self.assertEqual(g3(), 4)
1117
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001118 def test_break_in_finally_after_return(self):
1119 # See issue #37830
1120 def g1(x):
1121 for count in [0, 1]:
1122 count2 = 0
1123 while count2 < 20:
1124 count2 += 10
1125 try:
1126 return count + count2
1127 finally:
1128 if x:
1129 break
1130 return 'end', count, count2
1131 self.assertEqual(g1(False), 10)
1132 self.assertEqual(g1(True), ('end', 1, 10))
1133
1134 def g2(x):
1135 for count in [0, 1]:
1136 for count2 in [10, 20]:
1137 try:
1138 return count + count2
1139 finally:
1140 if x:
1141 break
1142 return 'end', count, count2
1143 self.assertEqual(g2(False), 10)
1144 self.assertEqual(g2(True), ('end', 1, 10))
1145
1146 def test_continue_in_finally_after_return(self):
1147 # See issue #37830
1148 def g1(x):
1149 count = 0
1150 while count < 100:
1151 count += 1
1152 try:
1153 return count
1154 finally:
1155 if x:
1156 continue
1157 return 'end', count
1158 self.assertEqual(g1(False), 1)
1159 self.assertEqual(g1(True), ('end', 100))
1160
1161 def g2(x):
1162 for count in [0, 1]:
1163 try:
1164 return count
1165 finally:
1166 if x:
1167 continue
1168 return 'end', count
1169 self.assertEqual(g2(False), 0)
1170 self.assertEqual(g2(True), ('end', 1))
1171
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001172 def test_yield(self):
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001173 # Allowed as standalone statement
1174 def g(): yield 1
1175 def g(): yield from ()
1176 # Allowed as RHS of assignment
1177 def g(): x = yield 1
1178 def g(): x = yield from ()
1179 # Ordinary yield accepts implicit tuples
1180 def g(): yield 1, 1
1181 def g(): x = yield 1, 1
1182 # 'yield from' does not
1183 check_syntax_error(self, "def g(): yield from (), 1")
1184 check_syntax_error(self, "def g(): x = yield from (), 1")
1185 # Requires parentheses as subexpression
1186 def g(): 1, (yield 1)
1187 def g(): 1, (yield from ())
1188 check_syntax_error(self, "def g(): 1, yield 1")
1189 check_syntax_error(self, "def g(): 1, yield from ()")
1190 # Requires parentheses as call argument
1191 def g(): f((yield 1))
1192 def g(): f((yield 1), 1)
1193 def g(): f((yield from ()))
1194 def g(): f((yield from ()), 1)
David Cuthbertfd97d1f2018-09-21 18:31:15 -07001195 # Do not require parenthesis for tuple unpacking
1196 def g(): rest = 4, 5, 6; yield 1, 2, 3, *rest
Serhiy Storchaka4642d5f2018-10-05 21:09:56 +03001197 self.assertEqual(list(g()), [(1, 2, 3, 4, 5, 6)])
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001198 check_syntax_error(self, "def g(): f(yield 1)")
1199 check_syntax_error(self, "def g(): f(yield 1, 1)")
1200 check_syntax_error(self, "def g(): f(yield from ())")
1201 check_syntax_error(self, "def g(): f(yield from (), 1)")
1202 # Not allowed at top level
1203 check_syntax_error(self, "yield")
1204 check_syntax_error(self, "yield from")
1205 # Not allowed at class scope
Thomas Wouters89f507f2006-12-13 04:49:30 +00001206 check_syntax_error(self, "class foo:yield 1")
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001207 check_syntax_error(self, "class foo:yield from ()")
Pablo Galindob0544ba2021-04-21 12:41:19 +01001208 # Check annotation refleak on SyntaxError
1209 check_syntax_error(self, "def g(a:(yield)): pass")
Guido van Rossum3bead091992-01-27 17:00:37 +00001210
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001211 def test_yield_in_comprehensions(self):
1212 # Check yield in comprehensions
1213 def g(): [x for x in [(yield 1)]]
1214 def g(): [x for x in [(yield from ())]]
1215
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001216 check = self.check_syntax_error
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001217 check("def g(): [(yield x) for x in ()]",
1218 "'yield' inside list comprehension")
1219 check("def g(): [x for x in () if not (yield x)]",
1220 "'yield' inside list comprehension")
1221 check("def g(): [y for x in () for y in [(yield x)]]",
1222 "'yield' inside list comprehension")
1223 check("def g(): {(yield x) for x in ()}",
1224 "'yield' inside set comprehension")
1225 check("def g(): {(yield x): x for x in ()}",
1226 "'yield' inside dict comprehension")
1227 check("def g(): {x: (yield x) for x in ()}",
1228 "'yield' inside dict comprehension")
1229 check("def g(): ((yield x) for x in ())",
1230 "'yield' inside generator expression")
1231 check("def g(): [(yield from x) for x in ()]",
1232 "'yield' inside list comprehension")
1233 check("class C: [(yield x) for x in ()]",
1234 "'yield' inside list comprehension")
1235 check("[(yield x) for x in ()]",
1236 "'yield' inside list comprehension")
1237
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001238 def test_raise(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001239 # 'raise' test [',' test]
Collin Winter828f04a2007-08-31 00:04:24 +00001240 try: raise RuntimeError('just testing')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001241 except RuntimeError: pass
1242 try: raise KeyboardInterrupt
1243 except KeyboardInterrupt: pass
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00001244
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001245 def test_import(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001246 # 'import' dotted_as_names
1247 import sys
1248 import time, sys
1249 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
1250 from time import time
1251 from time import (time)
1252 # not testable inside a function, but already done at top of the module
1253 # from sys import *
1254 from sys import path, argv
1255 from sys import (path, argv)
1256 from sys import (path, argv,)
Tim Peters10fb3862001-02-09 20:17:14 +00001257
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001258 def test_global(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001259 # 'global' NAME (',' NAME)*
1260 global a
1261 global a, b
1262 global one, two, three, four, five, six, seven, eight, nine, ten
Thomas Wouters80d373c2001-09-26 12:43:39 +00001263
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001264 def test_nonlocal(self):
Benjamin Petersona933e522008-10-24 22:16:39 +00001265 # 'nonlocal' NAME (',' NAME)*
1266 x = 0
1267 y = 0
1268 def f():
1269 nonlocal x
1270 nonlocal x, y
1271
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001272 def test_assert(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +00001273 # assertTruestmt: 'assert' test [',' test]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001274 assert 1
1275 assert 1, 1
1276 assert lambda x:x
1277 assert 1, lambda x:x+1
Ezio Melotti6cc5bf72011-12-02 18:22:52 +02001278
1279 try:
1280 assert True
1281 except AssertionError as e:
1282 self.fail("'assert True' should not have raised an AssertionError")
1283
1284 try:
1285 assert True, 'this should always pass'
1286 except AssertionError as e:
1287 self.fail("'assert True, msg' should not have "
1288 "raised an AssertionError")
1289
1290 # these tests fail if python is run with -O, so check __debug__
1291 @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
tsukasa-aua8ef4572021-03-16 22:14:41 +11001292 def test_assert_failures(self):
Thomas Wouters80d373c2001-09-26 12:43:39 +00001293 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001294 assert 0, "msg"
Guido van Rossumb940e112007-01-10 16:19:56 +00001295 except AssertionError as e:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001296 self.assertEqual(e.args[0], "msg")
Thomas Wouters89f507f2006-12-13 04:49:30 +00001297 else:
Ezio Melotti6cc5bf72011-12-02 18:22:52 +02001298 self.fail("AssertionError not raised by assert 0")
1299
1300 try:
1301 assert False
1302 except AssertionError as e:
1303 self.assertEqual(len(e.args), 0)
1304 else:
1305 self.fail("AssertionError not raised by 'assert False'")
1306
tsukasa-aua8ef4572021-03-16 22:14:41 +11001307 def test_assert_syntax_warnings(self):
1308 # Ensure that we warn users if they provide a non-zero length tuple as
1309 # the assertion test.
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001310 self.check_syntax_warning('assert(x, "msg")',
1311 'assertion is always true')
tsukasa-aua8ef4572021-03-16 22:14:41 +11001312 self.check_syntax_warning('assert(False, "msg")',
1313 'assertion is always true')
1314 self.check_syntax_warning('assert(False,)',
1315 'assertion is always true')
1316
1317 with self.check_no_warnings(category=SyntaxWarning):
1318 compile('assert x, "msg"', '<testcase>', 'exec')
1319 compile('assert False, "msg"', '<testcase>', 'exec')
1320
1321 def test_assert_warning_promotes_to_syntax_error(self):
1322 # If SyntaxWarning is configured to be an error, it actually raises a
1323 # SyntaxError.
1324 # https://bugs.python.org/issue35029
Serhiy Storchakad31e7732018-10-21 10:09:39 +03001325 with warnings.catch_warnings():
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001326 warnings.simplefilter('error', SyntaxWarning)
tsukasa-aua8ef4572021-03-16 22:14:41 +11001327 try:
1328 compile('assert x, "msg" ', '<testcase>', 'exec')
1329 except SyntaxError:
1330 self.fail('SyntaxError incorrectly raised for \'assert x, "msg"\'')
1331 with self.assertRaises(SyntaxError):
1332 compile('assert(x, "msg")', '<testcase>', 'exec')
1333 with self.assertRaises(SyntaxError):
1334 compile('assert(False, "msg")', '<testcase>', 'exec')
1335 with self.assertRaises(SyntaxError):
1336 compile('assert(False,)', '<testcase>', 'exec')
Serhiy Storchakad31e7732018-10-21 10:09:39 +03001337
Thomas Wouters80d373c2001-09-26 12:43:39 +00001338
Thomas Wouters89f507f2006-12-13 04:49:30 +00001339 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
1340 # Tested below
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001342 def test_if(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001343 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
1344 if 1: pass
1345 if 1: pass
1346 else: pass
1347 if 0: pass
1348 elif 0: pass
1349 if 0: pass
1350 elif 0: pass
1351 elif 0: pass
1352 elif 0: pass
1353 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001354
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001355 def test_while(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001356 # 'while' test ':' suite ['else' ':' suite]
1357 while 0: pass
1358 while 0: pass
1359 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001360
Christian Heimes969fe572008-01-25 11:23:10 +00001361 # Issue1920: "while 0" is optimized away,
1362 # ensure that the "else" clause is still present.
1363 x = 0
1364 while 0:
1365 x = 1
1366 else:
1367 x = 2
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001368 self.assertEqual(x, 2)
Christian Heimes969fe572008-01-25 11:23:10 +00001369
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001370 def test_for(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001371 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
1372 for i in 1, 2, 3: pass
1373 for i, j, k in (): pass
1374 else: pass
1375 class Squares:
1376 def __init__(self, max):
1377 self.max = max
1378 self.sofar = []
1379 def __len__(self): return len(self.sofar)
1380 def __getitem__(self, i):
1381 if not 0 <= i < self.max: raise IndexError
1382 n = len(self.sofar)
1383 while n <= i:
1384 self.sofar.append(n*n)
1385 n = n+1
1386 return self.sofar[i]
1387 n = 0
1388 for x in Squares(10): n = n+x
1389 if n != 285:
1390 self.fail('for over growing sequence')
Guido van Rossum3bead091992-01-27 17:00:37 +00001391
Thomas Wouters89f507f2006-12-13 04:49:30 +00001392 result = []
1393 for x, in [(1,), (2,), (3,)]:
1394 result.append(x)
1395 self.assertEqual(result, [1, 2, 3])
Guido van Rossum3bead091992-01-27 17:00:37 +00001396
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001397 def test_try(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001398 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
1399 ### | 'try' ':' suite 'finally' ':' suite
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +03001400 ### except_clause: 'except' [expr ['as' NAME]]
Thomas Wouters89f507f2006-12-13 04:49:30 +00001401 try:
1402 1/0
1403 except ZeroDivisionError:
1404 pass
1405 else:
1406 pass
1407 try: 1/0
1408 except EOFError: pass
Guido van Rossumb940e112007-01-10 16:19:56 +00001409 except TypeError as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +00001410 except: pass
1411 else: pass
1412 try: 1/0
1413 except (EOFError, TypeError, ZeroDivisionError): pass
1414 try: 1/0
Guido van Rossumb940e112007-01-10 16:19:56 +00001415 except (EOFError, TypeError, ZeroDivisionError) as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +00001416 try: pass
1417 finally: pass
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +03001418 with self.assertRaises(SyntaxError):
1419 compile("try:\n pass\nexcept Exception as a.b:\n pass", "?", "exec")
1420 compile("try:\n pass\nexcept Exception as a[b]:\n pass", "?", "exec")
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +00001421
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001422 def test_suite(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001423 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
1424 if 1: pass
1425 if 1:
1426 pass
1427 if 1:
1428 #
1429 #
1430 #
1431 pass
1432 pass
1433 #
1434 pass
1435 #
Guido van Rossum3bead091992-01-27 17:00:37 +00001436
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001437 def test_test(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001438 ### and_test ('or' and_test)*
1439 ### and_test: not_test ('and' not_test)*
1440 ### not_test: 'not' not_test | comparison
1441 if not 1: pass
1442 if 1 and 1: pass
1443 if 1 or 1: pass
1444 if not not not 1: pass
1445 if not 1 and 1 and 1: pass
1446 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +00001447
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001448 def test_comparison(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001449 ### comparison: expr (comp_op expr)*
1450 ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
1451 if 1: pass
1452 x = (1 == 1)
1453 if 1 == 1: pass
1454 if 1 != 1: pass
1455 if 1 < 1: pass
1456 if 1 > 1: pass
1457 if 1 <= 1: pass
1458 if 1 >= 1: pass
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001459 if x is x: pass
1460 if x is not x: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +00001461 if 1 in (): pass
1462 if 1 not in (): pass
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001463 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in x is x is not x: pass
1464
1465 def test_comparison_is_literal(self):
1466 def check(test, msg='"is" with a literal'):
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001467 self.check_syntax_warning(test, msg)
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001468
1469 check('x is 1')
1470 check('x is "thing"')
1471 check('1 is x')
1472 check('x is y is 1')
1473 check('x is not 1', '"is not" with a literal')
1474
1475 with warnings.catch_warnings():
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001476 warnings.simplefilter('error', SyntaxWarning)
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001477 compile('x is None', '<testcase>', 'exec')
1478 compile('x is False', '<testcase>', 'exec')
1479 compile('x is True', '<testcase>', 'exec')
1480 compile('x is ...', '<testcase>', 'exec')
Guido van Rossum3bead091992-01-27 17:00:37 +00001481
Serhiy Storchaka62e44812019-02-16 08:12:19 +02001482 def test_warn_missed_comma(self):
1483 def check(test):
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001484 self.check_syntax_warning(test, msg)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02001485
1486 msg=r'is not callable; perhaps you missed a comma\?'
1487 check('[(1, 2) (3, 4)]')
1488 check('[(x, y) (3, 4)]')
1489 check('[[1, 2] (3, 4)]')
1490 check('[{1, 2} (3, 4)]')
1491 check('[{1: 2} (3, 4)]')
1492 check('[[i for i in range(5)] (3, 4)]')
1493 check('[{i for i in range(5)} (3, 4)]')
1494 check('[(i for i in range(5)) (3, 4)]')
1495 check('[{i: i for i in range(5)} (3, 4)]')
1496 check('[f"{x}" (3, 4)]')
1497 check('[f"x={x}" (3, 4)]')
1498 check('["abc" (3, 4)]')
1499 check('[b"abc" (3, 4)]')
1500 check('[123 (3, 4)]')
1501 check('[12.3 (3, 4)]')
1502 check('[12.3j (3, 4)]')
1503 check('[None (3, 4)]')
1504 check('[True (3, 4)]')
1505 check('[... (3, 4)]')
1506
1507 msg=r'is not subscriptable; perhaps you missed a comma\?'
1508 check('[{1, 2} [i, j]]')
1509 check('[{i for i in range(5)} [i, j]]')
1510 check('[(i for i in range(5)) [i, j]]')
1511 check('[(lambda x, y: x) [i, j]]')
1512 check('[123 [i, j]]')
1513 check('[12.3 [i, j]]')
1514 check('[12.3j [i, j]]')
1515 check('[None [i, j]]')
1516 check('[True [i, j]]')
1517 check('[... [i, j]]')
1518
1519 msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?'
1520 check('[(1, 2) [i, j]]')
1521 check('[(x, y) [i, j]]')
1522 check('[[1, 2] [i, j]]')
1523 check('[[i for i in range(5)] [i, j]]')
1524 check('[f"{x}" [i, j]]')
1525 check('[f"x={x}" [i, j]]')
1526 check('["abc" [i, j]]')
1527 check('[b"abc" [i, j]]')
1528
1529 msg=r'indices must be integers or slices, not tuple;'
1530 check('[[1, 2] [3, 4]]')
1531 msg=r'indices must be integers or slices, not list;'
1532 check('[[1, 2] [[3, 4]]]')
1533 check('[[1, 2] [[i for i in range(5)]]]')
1534 msg=r'indices must be integers or slices, not set;'
1535 check('[[1, 2] [{3, 4}]]')
1536 check('[[1, 2] [{i for i in range(5)}]]')
1537 msg=r'indices must be integers or slices, not dict;'
1538 check('[[1, 2] [{3: 4}]]')
1539 check('[[1, 2] [{i: i for i in range(5)}]]')
1540 msg=r'indices must be integers or slices, not generator;'
1541 check('[[1, 2] [(i for i in range(5))]]')
1542 msg=r'indices must be integers or slices, not function;'
1543 check('[[1, 2] [(lambda x, y: x)]]')
1544 msg=r'indices must be integers or slices, not str;'
1545 check('[[1, 2] [f"{x}"]]')
1546 check('[[1, 2] [f"x={x}"]]')
1547 check('[[1, 2] ["abc"]]')
1548 msg=r'indices must be integers or slices, not'
1549 check('[[1, 2] [b"abc"]]')
1550 check('[[1, 2] [12.3]]')
1551 check('[[1, 2] [12.3j]]')
1552 check('[[1, 2] [None]]')
1553 check('[[1, 2] [...]]')
1554
1555 with warnings.catch_warnings():
Serhiy Storchakae7a4bb52019-02-19 08:30:15 +02001556 warnings.simplefilter('error', SyntaxWarning)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02001557 compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')
1558 compile('[[1, 2] [i]]', '<testcase>', 'exec')
1559 compile('[[1, 2] [0]]', '<testcase>', 'exec')
1560 compile('[[1, 2] [True]]', '<testcase>', 'exec')
1561 compile('[[1, 2] [1:2]]', '<testcase>', 'exec')
1562 compile('[{(1, 2): 3} [i, j]]', '<testcase>', 'exec')
1563
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001564 def test_binary_mask_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001565 x = 1 & 1
1566 x = 1 ^ 1
1567 x = 1 | 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001568
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001569 def test_shift_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001570 x = 1 << 1
1571 x = 1 >> 1
1572 x = 1 << 1 >> 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001573
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001574 def test_additive_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001575 x = 1
1576 x = 1 + 1
1577 x = 1 - 1 - 1
1578 x = 1 - 1 + 1 - 1 + 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001579
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001580 def test_multiplicative_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001581 x = 1 * 1
1582 x = 1 / 1
1583 x = 1 % 1
1584 x = 1 / 1 * 1 % 1
Guido van Rossum3bead091992-01-27 17:00:37 +00001585
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001586 def test_unary_ops(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001587 x = +1
1588 x = -1
1589 x = ~1
1590 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
1591 x = -1*1/1 + 1*1 - ---1*1
Guido van Rossum3bead091992-01-27 17:00:37 +00001592
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001593 def test_selectors(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001594 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
1595 ### subscript: expr | [expr] ':' [expr]
Guido van Rossum3bead091992-01-27 17:00:37 +00001596
Thomas Wouters89f507f2006-12-13 04:49:30 +00001597 import sys, time
1598 c = sys.path[0]
1599 x = time.time()
1600 x = sys.modules['time'].time()
1601 a = '01234'
1602 c = a[0]
1603 c = a[-1]
1604 s = a[0:5]
1605 s = a[:5]
1606 s = a[0:]
1607 s = a[:]
1608 s = a[-5:]
1609 s = a[:-1]
1610 s = a[-4:-3]
1611 # A rough test of SF bug 1333982. http://python.org/sf/1333982
1612 # The testing here is fairly incomplete.
1613 # Test cases should include: commas with 1 and 2 colons
1614 d = {}
1615 d[1] = 1
1616 d[1,] = 2
1617 d[1,2] = 3
1618 d[1,2,3] = 4
1619 L = list(d)
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001620 L.sort(key=lambda x: (type(x).__name__, x))
Florent Xicluna9b86b9a2010-03-19 19:00:44 +00001621 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
Guido van Rossum3bead091992-01-27 17:00:37 +00001622
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001623 def test_atoms(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001624 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
1625 ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Guido van Rossum3bead091992-01-27 17:00:37 +00001626
Thomas Wouters89f507f2006-12-13 04:49:30 +00001627 x = (1)
1628 x = (1 or 2 or 3)
1629 x = (1 or 2 or 3, 2, 3)
Guido van Rossum3bead091992-01-27 17:00:37 +00001630
Thomas Wouters89f507f2006-12-13 04:49:30 +00001631 x = []
1632 x = [1]
1633 x = [1 or 2 or 3]
1634 x = [1 or 2 or 3, 2, 3]
1635 x = []
Guido van Rossum3bead091992-01-27 17:00:37 +00001636
Thomas Wouters89f507f2006-12-13 04:49:30 +00001637 x = {}
1638 x = {'one': 1}
1639 x = {'one': 1,}
1640 x = {'one' or 'two': 1 or 2}
1641 x = {'one': 1, 'two': 2}
1642 x = {'one': 1, 'two': 2,}
1643 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
Guido van Rossum3bead091992-01-27 17:00:37 +00001644
Thomas Wouters89f507f2006-12-13 04:49:30 +00001645 x = {'one'}
1646 x = {'one', 1,}
1647 x = {'one', 'two', 'three'}
1648 x = {2, 3, 4,}
1649
1650 x = x
1651 x = 'x'
1652 x = 123
1653
1654 ### exprlist: expr (',' expr)* [',']
1655 ### testlist: test (',' test)* [',']
1656 # These have been exercised enough above
1657
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001658 def test_classdef(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001659 # 'class' NAME ['(' [testlist] ')'] ':' suite
1660 class B: pass
1661 class B2(): pass
1662 class C1(B): pass
1663 class C2(B): pass
1664 class D(C1, C2, B): pass
1665 class C:
1666 def meth1(self): pass
1667 def meth2(self, arg): pass
1668 def meth3(self, a1, a2): pass
1669
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001670 # decorator: '@' namedexpr_test NEWLINE
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001671 # decorators: decorator+
1672 # decorated: decorators (classdef | funcdef)
1673 def class_decorator(x): return x
1674 @class_decorator
1675 class G: pass
1676
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001677 # Test expressions as decorators (PEP 614):
1678 @False or class_decorator
1679 class H: pass
1680 @d := class_decorator
1681 class I: pass
1682 @lambda c: class_decorator(c)
1683 class J: pass
1684 @[..., class_decorator, ...][1]
1685 class K: pass
1686 @class_decorator(class_decorator)(class_decorator)
1687 class L: pass
1688 @[class_decorator][0].__call__.__call__
1689 class M: pass
1690
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001691 def test_dictcomps(self):
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001692 # dictorsetmaker: ( (test ':' test (comp_for |
1693 # (',' test ':' test)* [','])) |
1694 # (test (comp_for | (',' test)* [','])) )
1695 nums = [1, 2, 3]
1696 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
1697
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001698 def test_listcomps(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001699 # list comprehension tests
1700 nums = [1, 2, 3, 4, 5]
1701 strs = ["Apple", "Banana", "Coconut"]
1702 spcs = [" Apple", " Banana ", "Coco nut "]
1703
1704 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
1705 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
1706 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
1707 self.assertEqual([(i, s) for i in nums for s in strs],
1708 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
1709 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
1710 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
1711 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
1712 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
1713 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
1714 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
1715 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
1716 (5, 'Banana'), (5, 'Coconut')])
1717 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
1718 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
1719
1720 def test_in_func(l):
1721 return [0 < x < 3 for x in l if x > 2]
1722
1723 self.assertEqual(test_in_func(nums), [False, False, False])
1724
1725 def test_nested_front():
1726 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
1727 [[1, 2], [3, 4], [5, 6]])
1728
1729 test_nested_front()
1730
1731 check_syntax_error(self, "[i, s for i in nums for s in strs]")
1732 check_syntax_error(self, "[x if y]")
1733
1734 suppliers = [
1735 (1, "Boeing"),
1736 (2, "Ford"),
1737 (3, "Macdonalds")
1738 ]
1739
1740 parts = [
1741 (10, "Airliner"),
1742 (20, "Engine"),
1743 (30, "Cheeseburger")
1744 ]
1745
1746 suppart = [
1747 (1, 10), (1, 20), (2, 20), (3, 30)
1748 ]
1749
1750 x = [
1751 (sname, pname)
1752 for (sno, sname) in suppliers
1753 for (pno, pname) in parts
1754 for (sp_sno, sp_pno) in suppart
1755 if sno == sp_sno and pno == sp_pno
1756 ]
1757
1758 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
1759 ('Macdonalds', 'Cheeseburger')])
1760
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001761 def test_genexps(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001762 # generator expression tests
1763 g = ([x for x in range(10)] for x in range(1))
Georg Brandla18af4e2007-04-21 15:47:16 +00001764 self.assertEqual(next(g), [x for x in range(10)])
Thomas Wouters89f507f2006-12-13 04:49:30 +00001765 try:
Georg Brandla18af4e2007-04-21 15:47:16 +00001766 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001767 self.fail('should produce StopIteration exception')
1768 except StopIteration:
1769 pass
1770
1771 a = 1
1772 try:
1773 g = (a for d in a)
Georg Brandla18af4e2007-04-21 15:47:16 +00001774 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001775 self.fail('should produce TypeError')
1776 except TypeError:
1777 pass
1778
1779 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
1780 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
1781
1782 a = [x for x in range(10)]
1783 b = (x for x in (y for y in a))
1784 self.assertEqual(sum(b), sum([x for x in range(10)]))
1785
1786 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
1787 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
1788 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
1789 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
1790 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
1791 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)]))
1792 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
1793 check_syntax_error(self, "foo(x for x in range(10), 100)")
1794 check_syntax_error(self, "foo(100, x for x in range(10))")
1795
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001796 def test_comprehension_specials(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001797 # test for outmost iterable precomputation
1798 x = 10; g = (i for i in range(x)); x = 5
1799 self.assertEqual(len(list(g)), 10)
1800
1801 # This should hold, since we're only precomputing outmost iterable.
1802 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
1803 x = 5; t = True;
1804 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
1805
1806 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
1807 # even though it's silly. Make sure it works (ifelse broke this.)
1808 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
1809 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
1810
1811 # verify unpacking single element tuples in listcomp/genexp.
1812 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
1813 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
1814
Benjamin Petersonf17ab892009-05-29 21:55:57 +00001815 def test_with_statement(self):
1816 class manager(object):
1817 def __enter__(self):
1818 return (1, 2)
1819 def __exit__(self, *args):
1820 pass
1821
1822 with manager():
1823 pass
1824 with manager() as x:
1825 pass
1826 with manager() as (x, y):
1827 pass
1828 with manager(), manager():
1829 pass
1830 with manager() as x, manager() as y:
1831 pass
1832 with manager() as x, manager():
1833 pass
1834
Pablo Galindo1ed83ad2020-06-11 17:30:46 +01001835 with (
1836 manager()
1837 ):
1838 pass
Pablo Galindo99db2a12020-05-06 22:54:34 +01001839
Pablo Galindo1ed83ad2020-06-11 17:30:46 +01001840 with (
1841 manager() as x
1842 ):
1843 pass
1844
1845 with (
1846 manager() as (x, y),
1847 manager() as z,
1848 ):
1849 pass
1850
1851 with (
1852 manager(),
1853 manager()
1854 ):
1855 pass
1856
1857 with (
1858 manager() as x,
1859 manager() as y
1860 ):
1861 pass
1862
1863 with (
1864 manager() as x,
1865 manager()
1866 ):
1867 pass
1868
1869 with (
1870 manager() as x,
1871 manager() as y,
1872 manager() as z,
1873 ):
1874 pass
1875
1876 with (
1877 manager() as x,
1878 manager() as y,
1879 manager(),
1880 ):
1881 pass
Pablo Galindo99db2a12020-05-06 22:54:34 +01001882
Benjamin Petersonc8507bf2011-05-30 10:52:48 -05001883 def test_if_else_expr(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +00001884 # Test ifelse expressions in various cases
1885 def _checkeval(msg, ret):
1886 "helper to check that evaluation of expressions is done correctly"
Victor Stinnerc6ec54d2016-04-12 18:33:41 +02001887 print(msg)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001888 return ret
1889
Nick Coghlan650f0d02007-04-15 12:05:43 +00001890 # the next line is not allowed anymore
1891 #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
Thomas Wouters89f507f2006-12-13 04:49:30 +00001892 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
1893 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])
1894 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
1895 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
1896 self.assertEqual((5 and 6 if 0 else 1), 1)
1897 self.assertEqual(((5 and 6) if 0 else 1), 1)
1898 self.assertEqual((5 and (6 if 1 else 1)), 6)
1899 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
1900 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
1901 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
1902 self.assertEqual((not 5 if 1 else 1), False)
1903 self.assertEqual((not 5 if 0 else 1), 1)
1904 self.assertEqual((6 + 1 if 1 else 2), 7)
1905 self.assertEqual((6 - 1 if 1 else 2), 5)
1906 self.assertEqual((6 * 2 if 1 else 4), 12)
1907 self.assertEqual((6 / 2 if 1 else 3), 3)
1908 self.assertEqual((6 < 4 if 0 else 2), 2)
Jeremy Hylton7b03bad2006-02-28 17:46:23 +00001909
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001910 def test_paren_evaluation(self):
1911 self.assertEqual(16 // (4 // 2), 8)
1912 self.assertEqual((16 // 4) // 2, 2)
1913 self.assertEqual(16 // 4 // 2, 2)
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02001914 x = 2
1915 y = 3
1916 self.assertTrue(False is (x is y))
1917 self.assertFalse((False is x) is y)
1918 self.assertFalse(False is x is y)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001919
Benjamin Petersond51374e2014-04-09 23:55:56 -04001920 def test_matrix_mul(self):
1921 # This is not intended to be a comprehensive test, rather just to be few
1922 # samples of the @ operator in test_grammar.py.
1923 class M:
1924 def __matmul__(self, o):
1925 return 4
1926 def __imatmul__(self, o):
1927 self.other = o
1928 return self
1929 m = M()
1930 self.assertEqual(m @ m, 4)
1931 m @= 42
1932 self.assertEqual(m.other, 42)
1933
Yury Selivanov75445082015-05-11 22:57:16 -04001934 def test_async_await(self):
Yury Selivanov75445082015-05-11 22:57:16 -04001935 async def test():
1936 def sum():
Yury Selivanov8fb307c2015-07-22 13:33:45 +03001937 pass
Yury Selivanov75445082015-05-11 22:57:16 -04001938 if 1:
1939 await someobj()
1940
1941 self.assertEqual(test.__name__, 'test')
1942 self.assertTrue(bool(test.__code__.co_flags & inspect.CO_COROUTINE))
1943
1944 def decorator(func):
1945 setattr(func, '_marked', True)
1946 return func
1947
1948 @decorator
1949 async def test2():
1950 return 22
1951 self.assertTrue(test2._marked)
1952 self.assertEqual(test2.__name__, 'test2')
1953 self.assertTrue(bool(test2.__code__.co_flags & inspect.CO_COROUTINE))
1954
1955 def test_async_for(self):
1956 class Done(Exception): pass
1957
1958 class AIter:
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001959 def __aiter__(self):
Yury Selivanov75445082015-05-11 22:57:16 -04001960 return self
1961 async def __anext__(self):
1962 raise StopAsyncIteration
1963
1964 async def foo():
1965 async for i in AIter():
1966 pass
1967 async for i, j in AIter():
1968 pass
1969 async for i in AIter():
1970 pass
1971 else:
1972 pass
1973 raise Done
1974
1975 with self.assertRaises(Done):
1976 foo().send(None)
1977
1978 def test_async_with(self):
1979 class Done(Exception): pass
1980
1981 class manager:
1982 async def __aenter__(self):
1983 return (1, 2)
1984 async def __aexit__(self, *exc):
1985 return False
1986
1987 async def foo():
1988 async with manager():
1989 pass
1990 async with manager() as x:
1991 pass
1992 async with manager() as (x, y):
1993 pass
1994 async with manager(), manager():
1995 pass
1996 async with manager() as x, manager() as y:
1997 pass
1998 async with manager() as x, manager():
1999 pass
2000 raise Done
2001
2002 with self.assertRaises(Done):
2003 foo().send(None)
2004
Guido van Rossum3bead091992-01-27 17:00:37 +00002005
Thomas Wouters89f507f2006-12-13 04:49:30 +00002006if __name__ == '__main__':
Zachary Ware38c707e2015-04-13 15:00:43 -05002007 unittest.main()