diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2014-06-16 09:48:02 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2014-06-16 09:48:02 (GMT) |
commit | 5b1fdc1e377df88078f7788bf85dfebd353dc178 (patch) | |
tree | 922b50792ecc56d09650996971ac112971dafd9a /Lib/test/test_grammar.py | |
parent | b6d1f48c14b7a3bc7047138dc0bf5aadfe6be480 (diff) | |
download | cpython-5b1fdc1e377df88078f7788bf85dfebd353dc178.zip cpython-5b1fdc1e377df88078f7788bf85dfebd353dc178.tar.gz cpython-5b1fdc1e377df88078f7788bf85dfebd353dc178.tar.bz2 |
Issue #21669: Special case print & exec syntax errors
Diffstat (limited to 'Lib/test/test_grammar.py')
-rw-r--r-- | Lib/test/test_grammar.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index f012de1..70d85b1 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -390,6 +390,31 @@ class GrammarTests(unittest.TestCase): check_syntax_error(self, "x + 1 = 1") check_syntax_error(self, "a + 1 = b + 2") + # Check the heuristic for print & exec covers significant cases + # As well as placing some limits on false positives + def test_former_statements_refer_to_builtins(self): + keywords = "print", "exec" + # Cases where we want the custom error + cases = [ + "{} foo", + "{} {{1:foo}}", + "if 1: {} foo", + "if 1: {} {{1:foo}}", + "if 1:\n {} foo", + "if 1:\n {} {{1:foo}}", + ] + for keyword in keywords: + custom_msg = "call to '{}'".format(keyword) + for case in cases: + source = case.format(keyword) + with self.subTest(source=source): + with self.assertRaisesRegex(SyntaxError, custom_msg): + exec(source) + source = source.replace("foo", "(foo.)") + with self.subTest(source=source): + with self.assertRaisesRegex(SyntaxError, "invalid syntax"): + exec(source) + def test_del_stmt(self): # 'del' exprlist abc = [1,2,3] |