]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-99153: set location on SyntaxError for try with both except and except...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Mon, 7 Nov 2022 09:41:20 +0000 (09:41 +0000)
committerGitHub <noreply@github.com>
Mon, 7 Nov 2022 09:41:20 +0000 (09:41 +0000)
Grammar/python.gram
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2022-11-06-13-25-01.gh-issue-99153.uE3CVL.rst [new file with mode: 0644]
Parser/parser.c

index 51f846a57f404bbabac0b7fee1ea7fe286668573..e32008ca80d22726866b555d9086438227097c2e 100644 (file)
@@ -1247,8 +1247,8 @@ invalid_try_stmt:
     | a='try' ':' NEWLINE !INDENT {
         RAISE_INDENTATION_ERROR("expected an indented block after 'try' statement on line %d", a->lineno) }
     | 'try' ':' block !('except' | 'finally') { RAISE_SYNTAX_ERROR("expected 'except' or 'finally' block") }
-    | 'try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
-        RAISE_SYNTAX_ERROR("cannot have both 'except' and 'except*' on the same 'try'") }
+    | a='try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block* {
+        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot have both 'except' and 'except*' on the same 'try'") }
 invalid_except_stmt:
     | 'except' '*'? a=expression ',' expressions ['as' NAME ] ':' {
         RAISE_SYNTAX_ERROR_STARTING_FROM(a, "multiple exception types must be parenthesized") }
index ae1066924b3cf513bd83060d5a044c8dd4c0b45d..d72f41f0d5a8d2038c0afe8e6df26d1e412fae66 100644 (file)
@@ -1985,6 +1985,16 @@ class SyntaxTestCase(unittest.TestCase):
                           "Generator expression must be parenthesized",
                           lineno=1, end_lineno=1, offset=11, end_offset=53)
 
+    def test_except_then_except_star(self):
+        self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass",
+                          r"cannot have both 'except' and 'except\*' on the same 'try'",
+                          lineno=1, end_lineno=1, offset=1, end_offset=4)
+
+    def test_except_star_then_except(self):
+        self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass",
+                          r"cannot have both 'except' and 'except\*' on the same 'try'",
+                          lineno=1, end_lineno=1, offset=1, end_offset=4)
+
     def test_empty_line_after_linecont(self):
         # See issue-40847
         s = r"""\
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-11-06-13-25-01.gh-issue-99153.uE3CVL.rst b/Misc/NEWS.d/next/Core and Builtins/2022-11-06-13-25-01.gh-issue-99153.uE3CVL.rst
new file mode 100644 (file)
index 0000000..0445afb
--- /dev/null
@@ -0,0 +1 @@
+Fix location of :exc:`SyntaxError` for a :keyword:`try` block with both  :keyword:`except` and :keyword:`except* <except_star>`.
index 3fc12e50833c566c1ef1d77a0977f7b109a44951..91fdfb198b11f0dbf45cef12b5f8f3f69c398aed 100644 (file)
@@ -21892,13 +21892,13 @@ invalid_try_stmt_rule(Parser *p)
             return NULL;
         }
         D(fprintf(stderr, "%*c> invalid_try_stmt[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block*"));
-        Token * _keyword;
         Token * _literal;
         asdl_seq * _loop0_203_var;
         asdl_seq * _loop0_205_var;
         void *_tmp_204_var;
+        Token * a;
         if (
-            (_keyword = _PyPegen_expect_token(p, 618))  // token='try'
+            (a = _PyPegen_expect_token(p, 618))  // token='try'
             &&
             (_literal = _PyPegen_expect_token(p, 11))  // token=':'
             &&
@@ -21910,7 +21910,7 @@ invalid_try_stmt_rule(Parser *p)
         )
         {
             D(fprintf(stderr, "%*c+ invalid_try_stmt[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'try' ':' block* ((except_block+ except_star_block) | (except_star_block+ except_block)) block*"));
-            _res = RAISE_SYNTAX_ERROR ( "cannot have both 'except' and 'except*' on the same 'try'" );
+            _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "cannot have both 'except' and 'except*' on the same 'try'" );
             if (_res == NULL && PyErr_Occurred()) {
                 p->error_indicator = 1;
                 p->level--;