From b028ee94a756e1e9755f66425f156a6f58a5d459 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 7 Dec 2021 11:24:45 +0000 Subject: [PATCH 1/2] bpo-46004: Fix error location for loops with invalid targets --- Lib/test/test_exceptions.py | 1 + .../Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst | 2 ++ Parser/pegen.c | 1 + Parser/pegen.h | 3 ++- 4 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index e4b7b8f0a6406f..a954ecb8f7c25c 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -234,6 +234,7 @@ def testSyntaxErrorOffset(self): check("ages = {'Alice'=22, 'Bob'=23}", 1, 16) check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19) check("[a b c d e f]", 1, 2) + check("for x yfff:", 1, 7) # Errors thrown by compile.c check('class foo:return 1', 1, 11) diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst new file mode 100644 index 00000000000000..199bccf8166f06 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst @@ -0,0 +1,2 @@ +Fix the :exc:`SyntaxError` location for errors involving for loops with +invalid targets. Patch by Pablo Galindo diff --git a/Parser/pegen.c b/Parser/pegen.c index ede281ac89cd9a..63ad8ab447cda9 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -381,6 +381,7 @@ _PyPegen_expect_token(Parser *p, int type) } Token *t = p->tokens[p->mark]; if (t->type != type) { + if (Py_DebugFlag) printf("Token = %s - Expected %d\n", PyBytes_AsString(t->bytes), type); return NULL; } p->mark += 1; diff --git a/Parser/pegen.h b/Parser/pegen.h index 78e75d7060cf1d..caba34e535b6ae 100644 --- a/Parser/pegen.h +++ b/Parser/pegen.h @@ -227,8 +227,9 @@ _RAISE_SYNTAX_ERROR_INVALID_TARGET(Parser *p, TARGETS_TYPE type, void *e) msg, _PyPegen_get_expr_name(invalid_target) ); + return RAISE_SYNTAX_ERROR_KNOWN_LOCATION(invalid_target, "invalid syntax"); } - return RAISE_SYNTAX_ERROR("invalid syntax"); + return NULL; } // Action utility functions From 85d1cd0b3b38bca92312e592342ba8cd345f9aa2 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 7 Dec 2021 11:58:31 +0000 Subject: [PATCH 2/2] Update Parser/pegen.c --- Parser/pegen.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Parser/pegen.c b/Parser/pegen.c index 63ad8ab447cda9..ede281ac89cd9a 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -381,7 +381,6 @@ _PyPegen_expect_token(Parser *p, int type) } Token *t = p->tokens[p->mark]; if (t->type != type) { - if (Py_DebugFlag) printf("Token = %s - Expected %d\n", PyBytes_AsString(t->bytes), type); return NULL; } p->mark += 1;