aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-02-03 23:29:26 +0000
committerGitHub <noreply@github.com>2021-02-03 23:29:26 +0000
commitd4e6ed7e5fb43320ea714d7436bc11667c624d43 (patch)
tree1b7139c3a92eb9de5b91e61b5d5a7e92bed07bd7
parentFix typo (GH-23019) (diff)
downloadcpython-d4e6ed7e5fb43320ea714d7436bc11667c624d43.tar.gz
cpython-d4e6ed7e5fb43320ea714d7436bc11667c624d43.tar.bz2
cpython-d4e6ed7e5fb43320ea714d7436bc11667c624d43.zip
bpo-43121: Fix incorrect SyntaxError message for missing comma (GH-24436)
-rw-r--r--Grammar/python.gram2
-rw-r--r--Lib/test/test_syntax.py20
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2021-02-03-22-33-05.bpo-43121.jqcViq.rst2
-rw-r--r--Parser/parser.c13
4 files changed, 29 insertions, 8 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 22f2b41b11..d1a36f0e4d 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 6068dd9fc0..70dd22c62a 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -246,9 +246,25 @@ SyntaxError: did you forget parentheses around the comprehension target?
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 0000000000..5030bda133
--- /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 c709e45dae..f4501d3bca 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: