Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Side by Side Diff: Lib/test/test_grammar.py

Issue 21669: Custom error messages when print & exec are used as statements
Patch Set: Created 5 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Objects/exceptions.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Python test set -- part 1, grammar. 1 # Python test set -- part 1, grammar.
2 # This just tests whether the parser accepts them all. 2 # This just tests whether the parser accepts them all.
3 3
4 from test.support import run_unittest, check_syntax_error 4 from test.support import run_unittest, check_syntax_error
5 import unittest 5 import unittest
6 import sys 6 import sys
7 # testing import * 7 # testing import *
8 from sys import * 8 from sys import *
9 9
10 10
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 1 376 1
377 1, 2, 3 377 1, 2, 3
378 x = 1 378 x = 1
379 x = 1, 2, 3 379 x = 1, 2, 3
380 x = y = z = 1, 2, 3 380 x = y = z = 1, 2, 3
381 x, y, z = 1, 2, 3 381 x, y, z = 1, 2, 3
382 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) 382 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
383 383
384 check_syntax_error(self, "x + 1 = 1") 384 check_syntax_error(self, "x + 1 = 1")
385 check_syntax_error(self, "a + 1 = b + 2") 385 check_syntax_error(self, "a + 1 = b + 2")
386
387 # Check the heuristic for print & exec covers significant cases
388 # As well as placing some limits on false positives
389 def test_former_statements_refer_to_builtins(self):
390 keywords = "print", "exec"
391 # Cases where we want the custom error
392 cases = [
393 "{} foo",
394 "{} {{1:foo}}",
395 "if 1: {} foo",
396 "if 1: {} {{1:foo}}",
397 "if 1:\n {} foo",
398 "if 1:\n {} {{1:foo}}",
399 ]
400 for keyword in keywords:
401 custom_msg = "call to '{}'".format(keyword)
402 for case in cases:
403 source = case.format(keyword)
404 with self.subTest(source=source):
405 with self.assertRaisesRegex(SyntaxError, custom_msg):
406 exec(source)
407 source = source.replace("foo", "(foo.)")
408 with self.subTest(source=source):
409 with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
410 exec(source)
386 411
387 def test_del_stmt(self): 412 def test_del_stmt(self):
388 # 'del' exprlist 413 # 'del' exprlist
389 abc = [1,2,3] 414 abc = [1,2,3]
390 x, y, z = abc 415 x, y, z = abc
391 xyz = x, y, z 416 xyz = x, y, z
392 417
393 del abc 418 del abc
394 del x, y, (z, xyz) 419 del x, y, (z, xyz)
395 420
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 self.assertEqual(m @ m, 4) 1023 self.assertEqual(m @ m, 4)
999 m @= 42 1024 m @= 42
1000 self.assertEqual(m.other, 42) 1025 self.assertEqual(m.other, 42)
1001 1026
1002 1027
1003 def test_main(): 1028 def test_main():
1004 run_unittest(TokenTests, GrammarTests) 1029 run_unittest(TokenTests, GrammarTests)
1005 1030
1006 if __name__ == '__main__': 1031 if __name__ == '__main__':
1007 test_main() 1032 test_main()
OLDNEW
« no previous file with comments | « no previous file | Objects/exceptions.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+