[Python-checkins] bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436)

pablogsal webhook-mailer at python.org
Wed Feb 3 18:29:33 EST 2021


https://github.com/python/cpython/commit/d4e6ed7e5fb43320ea714d7436bc11667c624d43
commit: d4e6ed7e5fb43320ea714d7436bc11667c624d43
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-02-03T23:29:26Z
summary:

bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-02-03-22-33-05.bpo-43121.jqcViq.rst
M Grammar/python.gram
M Lib/test/test_syntax.py
M Parser/parser.c

diff --git a/Grammar/python.gram b/Grammar/python.gram
index 22f2b41b11ef6..d1a36f0e4d094 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -694,7 +694,7 @@ invalid_primary:
 invalid_comprehension:
     | ('[' | '(' | '{') a=starred_expression for_if_clauses {
         RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "iterable unpacking cannot be used in comprehension") }
-    | ('[' | '{') a=star_named_expression ',' [star_named_expressions] {
+    | ('[' | '{') a=star_named_expression ',' [star_named_expressions] for_if_clauses {
         RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "did you forget parentheses around the comprehension target?") }
 invalid_dict_comprehension:
     | '{' a='**' bitwise_or for_if_clauses '}' {
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 6068dd9fc09e8..70dd22c62aa21 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -246,9 +246,25 @@
 Traceback (most recent call last):
 SyntaxError: did you forget parentheses around the comprehension target?
 
->>> {x,y: None for x,y in range(100)}
+# Missing commas in literals collections should not
+# produce special error messages regarding missing
+# parentheses
+
+>>> [1, 2 3]
 Traceback (most recent call last):
-SyntaxError: did you forget parentheses around the comprehension target?
+SyntaxError: invalid syntax
+
+>>> {1, 2 3}
+Traceback (most recent call last):
+SyntaxError: invalid syntax
+
+>>> {1:2, 2:5 3:12}
+Traceback (most recent call last):
+SyntaxError: invalid syntax
+
+>>> (1, 2 3)
+Traceback (most recent call last):
+SyntaxError: invalid syntax
 
 From compiler_complex_args():
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-02-03-22-33-05.bpo-43121.jqcViq.rst b/Misc/NEWS.d/next/Core and Builtins/2021-02-03-22-33-05.bpo-43121.jqcViq.rst
new file mode 100644
index 0000000000000..5030bda133c8d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-02-03-22-33-05.bpo-43121.jqcViq.rst	
@@ -0,0 +1,2 @@
+Fixed an incorrect :exc:`SyntaxError` message for missing comma in literals.
+Patch by Pablo Galindo.
diff --git a/Parser/parser.c b/Parser/parser.c
index c709e45dae565..f4501d3bca094 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -15217,7 +15217,7 @@ invalid_primary_rule(Parser *p)
 
 // invalid_comprehension:
 //     | ('[' | '(' | '{') starred_expression for_if_clauses
-//     | ('[' | '{') star_named_expression ',' star_named_expressions?
+//     | ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
 static void *
 invalid_comprehension_rule(Parser *p)
 {
@@ -15258,17 +15258,18 @@ invalid_comprehension_rule(Parser *p)
         D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
                   p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '(' | '{') starred_expression for_if_clauses"));
     }
-    { // ('[' | '{') star_named_expression ',' star_named_expressions?
+    { // ('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses
         if (p->error_indicator) {
             D(p->level--);
             return NULL;
         }
-        D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
+        D(fprintf(stderr, "%*c> invalid_comprehension[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
         Token * _literal;
         void *_opt_var;
         UNUSED(_opt_var); // Silence compiler warnings
         void *_tmp_132_var;
         expr_ty a;
+        asdl_comprehension_seq* for_if_clauses_var;
         if (
             (_tmp_132_var = _tmp_132_rule(p))  // '[' | '{'
             &&
@@ -15277,9 +15278,11 @@ invalid_comprehension_rule(Parser *p)
             (_literal = _PyPegen_expect_token(p, 12))  // token=','
             &&
             (_opt_var = star_named_expressions_rule(p), 1)  // star_named_expressions?
+            &&
+            (for_if_clauses_var = for_if_clauses_rule(p))  // for_if_clauses
         )
         {
-            D(fprintf(stderr, "%*c+ invalid_comprehension[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
+            D(fprintf(stderr, "%*c+ invalid_comprehension[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
             _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "did you forget parentheses around the comprehension target?" );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
@@ -15290,7 +15293,7 @@ invalid_comprehension_rule(Parser *p)
         }
         p->mark = _mark;
         D(fprintf(stderr, "%*c%s invalid_comprehension[%d-%d]: %s failed!\n", p->level, ' ',
-                  p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions?"));
+                  p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "('[' | '{') star_named_expression ',' star_named_expressions? for_if_clauses"));
     }
     _res = NULL;
   done:



More information about the Python-checkins mailing list