[Python-checkins] [3.10] bpo-46004: Fix error location for loops with invalid targets (GH-29959). (GH-29961)

pablogsal webhook-mailer at python.org
Tue Dec 7 10:23:42 EST 2021


https://github.com/python/cpython/commit/c52141200364898818956a73b955f7c04f634dc8
commit: c52141200364898818956a73b955f7c04f634dc8
branch: 3.10
author: Pablo Galindo Salgado <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-12-07T15:23:33Z
summary:

[3.10] bpo-46004: Fix error location for loops with invalid targets (GH-29959). (GH-29961)

(cherry picked from commit 1c7a1c3be08ee911d347fffd2716f3911ba751f9)

Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-12-07-11-24-24.bpo-46004.TTEU1p.rst
M Lib/test/test_exceptions.py
M Parser/pegen.h

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index ad2864bc41637..bc5f83a5d2dc8 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 0000000000000..199bccf8166f0
--- /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.h b/Parser/pegen.h
index 04c7b9d19bb8a..29d48052e4bc9 100644
--- a/Parser/pegen.h
+++ b/Parser/pegen.h
@@ -321,8 +321,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;
 }
 
 void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);



More information about the Python-checkins mailing list