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

Delta Between Two Patch Sets: Lib/test/test_grammar.py

Issue 21669: Custom error messages when print & exec are used as statements
Left Patch Set: Created 5 years, 10 months ago
Right Patch Set: Created 5 years, 10 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Objects/exceptions.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 386
387 def test_print_statement_refers_to_builtin(self): 387 # Check the heuristic for print & exec covers significant cases
388 with self.assertRaisesRegex(SyntaxError, "call to 'print'"): 388 # As well as placing some limits on false positives
389 exec("print foo") 389 def test_former_statements_refer_to_builtins(self):
390 with self.assertRaisesRegex(SyntaxError, "invalid syntax"): 390 keywords = "print", "exec"
391 exec("print (foo.)") 391 # Cases where we want the custom error
392 392 cases = [
393 def test_exec_statement_refers_to_builtin(self): 393 "{} foo",
394 with self.assertRaisesRegex(SyntaxError, "call to 'exec'"): 394 "{} {{1:foo}}",
395 exec("exec foo") 395 "if 1: {} foo",
396 with self.assertRaisesRegex(SyntaxError, "invalid syntax"): 396 "if 1: {} {{1:foo}}",
397 exec("exec (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)
398 411
399 def test_del_stmt(self): 412 def test_del_stmt(self):
400 # 'del' exprlist 413 # 'del' exprlist
401 abc = [1,2,3] 414 abc = [1,2,3]
402 x, y, z = abc 415 x, y, z = abc
403 xyz = x, y, z 416 xyz = x, y, z
404 417
405 del abc 418 del abc
406 del x, y, (z, xyz) 419 del x, y, (z, xyz)
407 420
(...skipping 602 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 self.assertEqual(m @ m, 4) 1023 self.assertEqual(m @ m, 4)
1011 m @= 42 1024 m @= 42
1012 self.assertEqual(m.other, 42) 1025 self.assertEqual(m.other, 42)
1013 1026
1014 1027
1015 def test_main(): 1028 def test_main():
1016 run_unittest(TokenTests, GrammarTests) 1029 run_unittest(TokenTests, GrammarTests)
1017 1030
1018 if __name__ == '__main__': 1031 if __name__ == '__main__':
1019 test_main() 1032 test_main()
LEFTRIGHT

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