[Python-checkins] bpo-44180: Report generic syntax errors in the furthest position reached in the first parser pass (GH-26253) (GH-26281)

pablogsal webhook-mailer at python.org
Fri May 21 11:30:10 EDT 2021


https://github.com/python/cpython/commit/07dba474c582e61ca455846da7597d4346324e04
commit: 07dba474c582e61ca455846da7597d4346324e04
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-05-21T16:29:58+01:00
summary:

bpo-44180: Report generic syntax errors in the furthest position reached in the first parser pass (GH-26253) (GH-26281)

(cherry picked from commit b51081c1a8cf01b92ba0692173e1b9274a57f455)

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

files:
A Misc/NEWS.d/next/Core and Builtins/2021-05-19-20-33-36.bpo-44180.mQVaAs.rst
M Lib/test/test_exceptions.py
M Parser/pegen.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index aefdd7b769f289..5fb651f4c22e55 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -213,6 +213,7 @@ def testSyntaxErrorOffset(self):
         check('[file for str(file) in []\n])', 2, 2)
         check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
         check('[file for\n str(file) in []]', 2, 2)
+        check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
 
         # Errors thrown by compile.c
         check('class foo:return 1', 1, 11)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-19-20-33-36.bpo-44180.mQVaAs.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-19-20-33-36.bpo-44180.mQVaAs.rst
new file mode 100644
index 00000000000000..c67eb70b5823a2
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-19-20-33-36.bpo-44180.mQVaAs.rst	
@@ -0,0 +1,3 @@
+The parser doesn't report generic syntax errors that happen in a position
+further away that the one it reached in the first pass. Patch by Pablo
+Galindo
diff --git a/Parser/pegen.c b/Parser/pegen.c
index c2f25402ca4e13..406c4e809fd541 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -1281,6 +1281,7 @@ _PyPegen_run_parser(Parser *p)
 {
     void *res = _PyPegen_parse(p);
     if (res == NULL) {
+        Token *last_token = p->tokens[p->fill - 1];
         reset_parser_state(p);
         _PyPegen_parse(p);
         if (PyErr_Occurred()) {
@@ -1307,7 +1308,11 @@ _PyPegen_run_parser(Parser *p)
                 RAISE_INDENTATION_ERROR("unexpected unindent");
             }
             else {
-                RAISE_SYNTAX_ERROR("invalid syntax");
+                // Use the last token we found on the first pass to avoid reporting
+                // incorrect locations for generic syntax errors just because we reached
+                // further away when trying to find specific syntax errors in the second
+                // pass.
+                RAISE_SYNTAX_ERROR_KNOWN_LOCATION(last_token, "invalid syntax");
                 // _PyPegen_check_tokenizer_errors will override the existing
                 // generic SyntaxError we just raised if errors are found.
                 _PyPegen_check_tokenizer_errors(p);



More information about the Python-checkins mailing list