]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.10] bpo-45727: Only trigger the 'did you forgot a comma' error suggestion if insid...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Thu, 25 Nov 2021 01:01:40 +0000 (01:01 +0000)
committerGitHub <noreply@github.com>
Thu, 25 Nov 2021 01:01:40 +0000 (01:01 +0000)
Backport of GH-29757

Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Grammar/python.gram
Lib/test/test_exceptions.py
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2021-11-24-18-24-49.bpo-45727._xVbbo.rst [new file with mode: 0644]
Parser/parser.c
Parser/pegen.c
Parser/pegen.h
Parser/pegen_errors.c

index 20cccfd8368af3216fb7f0c3ab6a38f9cdd57419..f730658462147fb34bb000f61a975388de302167 100644 (file)
@@ -859,8 +859,8 @@ invalid_expression:
     # !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
     # Soft keywords need to also be ignored because they can be parsed as NAME NAME
    | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
-        _PyPegen_check_legacy_stmt(p, a) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b,
-        "invalid syntax. Perhaps you forgot a comma?") }
+        _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
+        RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
    | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
 
 invalid_named_expression:
index 4c18a59f6e92cb3dbd0389aa77a8e64a979664d3..ad2864bc41637fcba557bc9f5d9a31354771f1b5 100644 (file)
@@ -226,14 +226,14 @@ class ExceptionTests(unittest.TestCase):
         check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
         check('x = "a', 1, 5)
         check('lambda x: x = 2', 1, 1)
-        check('f{a + b + c}', 1, 1)
+        check('f{a + b + c}', 1, 2)
         check('[file for str(file) in []\n])', 1, 11)
         check('a = « hello » « world »', 1, 5)
         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)
         check('match ...:\n    case {**rest, "key": value}:\n        ...', 2, 19)
-        check("a b c d e f", 1, 1)
+        check("[a b c d e f]", 1, 2)
 
         # Errors thrown by compile.c
         check('class foo:return 1', 1, 11)
index 14a4c678fc909706b01d4099ab3f6df4550b0ca2..bd1ca943c7c09407828be82b9ee8bc01a5848ff2 100644 (file)
@@ -944,7 +944,7 @@ x = (
                              "Bf''",
                              "BF''",]
         double_quote_cases = [case.replace("'", '"') for case in single_quote_cases]
-        self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing',
+        self.assertAllRaise(SyntaxError, 'invalid syntax',
                             single_quote_cases + double_quote_cases)
 
     def test_leading_trailing_spaces(self):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-24-18-24-49.bpo-45727._xVbbo.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-24-18-24-49.bpo-45727._xVbbo.rst
new file mode 100644 (file)
index 0000000..d4b149d
--- /dev/null
@@ -0,0 +1,3 @@
+Refine the custom syntax error that suggests that a comma may be missing to
+trigger only when the expressions are detected between parentheses or
+brackets. Patch by Pablo Galindo
index b14092a483c32b373392bcd3f63c98002c81e1bb..0b6450c670bac867dc1ebdc505396d1abebe9ba9 100644 (file)
@@ -18288,7 +18288,7 @@ invalid_expression_rule(Parser *p)
         )
         {
             D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
-            _res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
+            _res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : p -> tokens [ p -> mark - 1 ] -> level == 0 ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 D(p->level--);
index 8946aa33145b8547380c57fdce73c6b2dba72738..e37aaf7975bdd690f4d8a6aaf9df4b35e9b529c4 100644 (file)
@@ -692,6 +692,8 @@ initialize_token(Parser *p, Token *token, const char *start, const char *end, in
         return -1;
     }
 
+    token->level = p->tok->level;
+
     const char *line_start = token_type == STRING ? p->tok->multi_line_start : p->tok->line_start;
     int lineno = token_type == STRING ? p->tok->first_lineno : p->tok->lineno;
     int end_lineno = p->tok->lineno;
@@ -1357,7 +1359,7 @@ _PyPegen_run_parser(Parser *p)
         if (p->fill == 0) {
             RAISE_SYNTAX_ERROR("error at start before reading any input");
         }
-        else if (p->tok->done == E_EOF) {
+        else if (last_token->type == ERRORTOKEN && p->tok->done == E_EOF) {
             if (p->tok->level) {
                 raise_unclosed_parentheses_error(p);
             } else {
index 40222d64499a9d6e1ffacb2cca65b367effe0a45..04c7b9d19bb8a0c0d3beb595ecb4345bae5ad24b 100644 (file)
@@ -33,6 +33,7 @@ typedef struct _memo {
 typedef struct {
     int type;
     PyObject *bytes;
+    int level;
     int lineno, col_offset, end_lineno, end_col_offset;
     Memo *memo;
 } Token;
index 694184a03b075ae6d667e5de92fb8adcd89c3b4d..93057d151db3865ee713db103280c365c918b371 100644 (file)
@@ -399,7 +399,7 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
         RAISE_SYNTAX_ERROR("error at start before reading any input");
     }
     // Parser encountered EOF (End of File) unexpectedtly
-    if (p->tok->done == E_EOF) {
+    if (last_token->type == ERRORTOKEN && p->tok->done == E_EOF) {
         if (p->tok->level) {
             raise_unclosed_parentheses_error(p);
         } else {
@@ -422,4 +422,4 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
     // _PyPegen_tokenize_full_source_to_check_for_errors will override the existing
     // generic SyntaxError we just raised if errors are found.
     _PyPegen_tokenize_full_source_to_check_for_errors(p);
-}
\ No newline at end of file
+}