]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-47186: Replace JUMP_IF_NOT_EG_MATCH by CHECK_EG_MATCH + jump (GH-32309)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 5 Apr 2022 11:06:22 +0000 (12:06 +0100)
committerGitHub <noreply@github.com>
Tue, 5 Apr 2022 11:06:22 +0000 (12:06 +0100)
Doc/library/dis.rst
Doc/whatsnew/3.11.rst
Include/opcode.h
Lib/importlib/_bootstrap_external.py
Lib/opcode.py
Misc/NEWS.d/next/Core and Builtins/2022-04-04-17-41-10.bpo-47186.aQWoSh.rst [new file with mode: 0644]
Objects/frameobject.c
Python/ceval.c
Python/compile.c
Python/opcode_targets.h

index b364e3f031b3d35c035590521b5985fc82dae56c..63d846715b165b1b7583408d06ca29ddd1ac392a 100644 (file)
@@ -635,6 +635,28 @@ iterations of the loop.
 
    .. versionadded:: 3.11
 
+.. opcode:: CHECK_EG_MATCH
+
+   Performs exception matching for ``except*``. Applies ``split(TOS)`` on
+   the exception group representing TOS1.
+
+   In case of a match, pops two items from the stack and pushes the
+   non-matching subgroup (``None`` in case of full match) followed by the
+   matching subgroup. When there is no match, pops one item (the match
+   type) and pushes ``None``.
+
+   .. versionadded:: 3.11
+
+.. opcode:: PREP_RERAISE_STAR
+
+   Combines the raised and reraised exceptions list from TOS, into an exception
+   group to propagate from a try-except* block. Uses the original exception
+   group from TOS1 to reconstruct the structure of reraised exceptions. Pops
+   two items from the stack and pushes the exception to reraise or ``None``
+   if there isn't one.
+
+   .. versionadded:: 3.11
+
 .. opcode:: WITH_EXCEPT_START
 
     Calls the function in position 4 on the stack with arguments (type, val, tb)
@@ -922,18 +944,6 @@ iterations of the loop.
    .. versionadded:: 3.1
 
 
-.. opcode:: JUMP_IF_NOT_EG_MATCH (target)
-
-   Performs exception matching for ``except*``. Applies ``split(TOS)`` on
-   the exception group representing TOS1. Jumps if no match is found.
-
-   Pops one item from the stack (the match type). If a match was found,
-   next item (the exception) and pushes the non-matching part of the
-   exception group followed by the matching part.
-
-   .. versionadded:: 3.11
-
-
 .. opcode:: POP_JUMP_IF_NOT_NONE (target)
 
    If TOS is not none, sets the bytecode counter to *target*.  TOS is popped.
@@ -948,17 +958,6 @@ iterations of the loop.
    .. versionadded:: 3.11
 
 
-.. opcode:: PREP_RERAISE_STAR
-
-   Combines the raised and reraised exceptions list from TOS, into an exception
-   group to propagate from a try-except* block. Uses the original exception
-   group from TOS1 to reconstruct the structure of reraised exceptions. Pops
-   two items from the stack and pushes the exception to reraise or ``None``
-   if there isn't one.
-
-   .. versionadded:: 3.11
-
-
 .. opcode:: JUMP_IF_TRUE_OR_POP (target)
 
    If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
index c607aadb4457c34d7a023cdcb5e16b164571d7bb..fae85d867f91b37b30e3e9cca17cf3c4ace83e8c 100644 (file)
@@ -523,6 +523,9 @@ CPython bytecode changes
 * Replaced :opcode:`JUMP_IF_NOT_EXC_MATCH` by :opcode:`CHECK_EXC_MATCH` which
   performs the check but does not jump.
 
+* Replaced :opcode:`JUMP_IF_NOT_EG_MATCH` by :opcode:`CHECK_EG_MATCH` which
+  performs the check but does not jump.
+
 * Replaced :opcode:`JUMP_ABSOLUTE` by the relative :opcode:`JUMP_BACKWARD`.
 
 Deprecated
index c82d1fd1fae2bf6462c6c348be005af120edaf91..fd49dfed27ecd2fefc41939ed2c3f23b949589a5 100644 (file)
@@ -22,6 +22,7 @@ extern "C" {
 #define MATCH_KEYS                              33
 #define PUSH_EXC_INFO                           35
 #define CHECK_EXC_MATCH                         36
+#define CHECK_EG_MATCH                          37
 #define WITH_EXCEPT_START                       49
 #define GET_AITER                               50
 #define GET_ANEXT                               51
@@ -80,7 +81,6 @@ extern "C" {
 #define LOAD_FAST                              124
 #define STORE_FAST                             125
 #define DELETE_FAST                            126
-#define JUMP_IF_NOT_EG_MATCH                   127
 #define POP_JUMP_IF_NOT_NONE                   128
 #define POP_JUMP_IF_NONE                       129
 #define RAISE_VARARGS                          130
@@ -136,39 +136,39 @@ extern "C" {
 #define COMPARE_OP_INT_JUMP                     28
 #define COMPARE_OP_STR_JUMP                     29
 #define JUMP_BACKWARD_QUICK                     34
-#define LOAD_ATTR_ADAPTIVE                      37
-#define LOAD_ATTR_INSTANCE_VALUE                38
-#define LOAD_ATTR_MODULE                        39
-#define LOAD_ATTR_SLOT                          40
-#define LOAD_ATTR_WITH_HINT                     41
-#define LOAD_CONST__LOAD_FAST                   42
-#define LOAD_FAST__LOAD_CONST                   43
-#define LOAD_FAST__LOAD_FAST                    44
-#define LOAD_GLOBAL_ADAPTIVE                    45
-#define LOAD_GLOBAL_BUILTIN                     46
-#define LOAD_GLOBAL_MODULE                      47
-#define LOAD_METHOD_ADAPTIVE                    48
-#define LOAD_METHOD_CLASS                       55
-#define LOAD_METHOD_MODULE                      56
-#define LOAD_METHOD_NO_DICT                     57
-#define LOAD_METHOD_WITH_DICT                   58
-#define LOAD_METHOD_WITH_VALUES                 59
-#define PRECALL_ADAPTIVE                        62
-#define PRECALL_BOUND_METHOD                    63
-#define PRECALL_BUILTIN_CLASS                   64
-#define PRECALL_BUILTIN_FAST_WITH_KEYWORDS      65
-#define PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS  66
-#define PRECALL_NO_KW_BUILTIN_FAST              67
-#define PRECALL_NO_KW_BUILTIN_O                 72
-#define PRECALL_NO_KW_ISINSTANCE                73
-#define PRECALL_NO_KW_LEN                       76
-#define PRECALL_NO_KW_LIST_APPEND               77
-#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST    78
-#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS  79
-#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O       80
-#define PRECALL_NO_KW_STR_1                     81
-#define PRECALL_NO_KW_TUPLE_1                  113
-#define PRECALL_NO_KW_TYPE_1                   121
+#define LOAD_ATTR_ADAPTIVE                      38
+#define LOAD_ATTR_INSTANCE_VALUE                39
+#define LOAD_ATTR_MODULE                        40
+#define LOAD_ATTR_SLOT                          41
+#define LOAD_ATTR_WITH_HINT                     42
+#define LOAD_CONST__LOAD_FAST                   43
+#define LOAD_FAST__LOAD_CONST                   44
+#define LOAD_FAST__LOAD_FAST                    45
+#define LOAD_GLOBAL_ADAPTIVE                    46
+#define LOAD_GLOBAL_BUILTIN                     47
+#define LOAD_GLOBAL_MODULE                      48
+#define LOAD_METHOD_ADAPTIVE                    55
+#define LOAD_METHOD_CLASS                       56
+#define LOAD_METHOD_MODULE                      57
+#define LOAD_METHOD_NO_DICT                     58
+#define LOAD_METHOD_WITH_DICT                   59
+#define LOAD_METHOD_WITH_VALUES                 62
+#define PRECALL_ADAPTIVE                        63
+#define PRECALL_BOUND_METHOD                    64
+#define PRECALL_BUILTIN_CLASS                   65
+#define PRECALL_BUILTIN_FAST_WITH_KEYWORDS      66
+#define PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS  67
+#define PRECALL_NO_KW_BUILTIN_FAST              72
+#define PRECALL_NO_KW_BUILTIN_O                 73
+#define PRECALL_NO_KW_ISINSTANCE                76
+#define PRECALL_NO_KW_LEN                       77
+#define PRECALL_NO_KW_LIST_APPEND               78
+#define PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST    79
+#define PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS  80
+#define PRECALL_NO_KW_METHOD_DESCRIPTOR_O       81
+#define PRECALL_NO_KW_STR_1                    113
+#define PRECALL_NO_KW_TUPLE_1                  121
+#define PRECALL_NO_KW_TYPE_1                   127
 #define PRECALL_PYFUNC                         141
 #define RESUME_QUICK                           143
 #define STORE_ATTR_ADAPTIVE                    150
@@ -205,7 +205,7 @@ static const uint32_t _PyOpcode_Jump[8] = {
     0U,
     0U,
     536870912U,
-    2282602496U,
+    135118848U,
     4163U,
     0U,
     0U,
@@ -259,6 +259,7 @@ const uint8_t _PyOpcode_Deopt[256] = {
     [CALL_FUNCTION_EX] = CALL_FUNCTION_EX,
     [CALL_PY_EXACT_ARGS] = CALL,
     [CALL_PY_WITH_DEFAULTS] = CALL,
+    [CHECK_EG_MATCH] = CHECK_EG_MATCH,
     [CHECK_EXC_MATCH] = CHECK_EXC_MATCH,
     [COMPARE_OP] = COMPARE_OP,
     [COMPARE_OP_ADAPTIVE] = COMPARE_OP,
@@ -294,7 +295,6 @@ const uint8_t _PyOpcode_Deopt[256] = {
     [JUMP_BACKWARD_QUICK] = JUMP_BACKWARD,
     [JUMP_FORWARD] = JUMP_FORWARD,
     [JUMP_IF_FALSE_OR_POP] = JUMP_IF_FALSE_OR_POP,
-    [JUMP_IF_NOT_EG_MATCH] = JUMP_IF_NOT_EG_MATCH,
     [JUMP_IF_TRUE_OR_POP] = JUMP_IF_TRUE_OR_POP,
     [JUMP_NO_INTERRUPT] = JUMP_NO_INTERRUPT,
     [KW_NAMES] = KW_NAMES,
index 39a348aecef6c368ba2ea842429fae7f0e478565..189547cc16a8c776fda2668ff140071a602964eb 100644 (file)
@@ -398,6 +398,7 @@ _code_type = type(_write_atomic.__code__)
 #     Python 3.11a6 3488 (LOAD_GLOBAL can push additional NULL)
 #     Python 3.11a6 3489 (Add JUMP_BACKWARD, remove JUMP_ABSOLUTE)
 #     Python 3.11a6 3490 (remove JUMP_IF_NOT_EXC_MATCH, add CHECK_EXC_MATCH)
+#     Python 3.11a6 3491 (remove JUMP_IF_NOT_EG_MATCH, add CHECK_EG_MATCH)
 
 #     Python 3.12 will start with magic number 3500
 
@@ -412,7 +413,7 @@ _code_type = type(_write_atomic.__code__)
 # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
 # in PC/launcher.c must also be updated.
 
-MAGIC_NUMBER = (3490).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3491).to_bytes(2, 'little') + b'\r\n'
 _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little')  # For import.c
 
 _PYCACHE = '__pycache__'
index e993a5c1ff16a3aa75bb840ea15e92525310d0b4..23d98df5061f430b9eda4cab3a210151e7e04829 100644 (file)
@@ -77,6 +77,7 @@ def_op('MATCH_KEYS', 33)
 
 def_op('PUSH_EXC_INFO', 35)
 def_op('CHECK_EXC_MATCH', 36)
+def_op('CHECK_EG_MATCH', 37)
 
 def_op('WITH_EXCEPT_START', 49)
 def_op('GET_AITER', 50)
@@ -147,7 +148,6 @@ def_op('STORE_FAST', 125)       # Local variable number
 haslocal.append(125)
 def_op('DELETE_FAST', 126)      # Local variable number
 haslocal.append(126)
-jabs_op('JUMP_IF_NOT_EG_MATCH', 127)
 jabs_op('POP_JUMP_IF_NOT_NONE', 128)
 jabs_op('POP_JUMP_IF_NONE', 129)
 def_op('RAISE_VARARGS', 130)    # Number of raise arguments (1, 2, or 3)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-04-04-17-41-10.bpo-47186.aQWoSh.rst b/Misc/NEWS.d/next/Core and Builtins/2022-04-04-17-41-10.bpo-47186.aQWoSh.rst
new file mode 100644 (file)
index 0000000..2282c48
--- /dev/null
@@ -0,0 +1 @@
+Replace :opcode:`JUMP_IF_NOT_EG_MATCH` by :opcode:`CHECK_EG_MATCH` + jump.
index fe374bf0632cd97c4da970e265b6e4bbe3964917..c257c0a3b43f27f189ecfa4a86b0f95522731724 100644 (file)
@@ -207,7 +207,6 @@ mark_stacks(PyCodeObject *code_obj, int len)
                 case JUMP_IF_TRUE_OR_POP:
                 case POP_JUMP_IF_FALSE:
                 case POP_JUMP_IF_TRUE:
-                case JUMP_IF_NOT_EG_MATCH:
                 {
                     int64_t target_stack;
                     int j = get_arg(code, i);
@@ -215,13 +214,8 @@ mark_stacks(PyCodeObject *code_obj, int len)
                     if (stacks[j] == UNINITIALIZED && j < i) {
                         todo = 1;
                     }
-                    if (opcode == JUMP_IF_NOT_EG_MATCH)
-                    {
-                        next_stack = pop_value(pop_value(next_stack));
-                        target_stack = next_stack;
-                    }
-                    else if (opcode == JUMP_IF_FALSE_OR_POP ||
-                             opcode == JUMP_IF_TRUE_OR_POP)
+                    if (opcode == JUMP_IF_FALSE_OR_POP ||
+                        opcode == JUMP_IF_TRUE_OR_POP)
                     {
                         target_stack = next_stack;
                         next_stack = pop_value(next_stack);
index ce4abd5da2414bce0830509e6780f960cda1e674..f7e08c6ee813a648a7ddc06000d6428c2aa65fab 100644 (file)
@@ -3804,7 +3804,7 @@ handle_eval_breaker:
             DISPATCH();
         }
 
-        TARGET(JUMP_IF_NOT_EG_MATCH) {
+        TARGET(CHECK_EG_MATCH) {
             PyObject *match_type = POP();
             if (check_except_star_type_valid(tstate, match_type) < 0) {
                 Py_DECREF(match_type);
@@ -3825,15 +3825,11 @@ handle_eval_breaker:
                 assert(rest == NULL);
                 goto error;
             }
-
             if (Py_IsNone(match)) {
-                Py_DECREF(match);
+                PUSH(match);
                 Py_XDECREF(rest);
-                /* no match - jump to target */
-                JUMPTO(oparg);
             }
             else {
-
                 /* Total or partial match - update the stack from
                  * [val]
                  * to
@@ -3841,17 +3837,11 @@ handle_eval_breaker:
                  * (rest can be Py_None)
                  */
 
-                PyObject *exc = TOP();
-
                 SET_TOP(rest);
                 PUSH(match);
-
                 PyErr_SetExcInfo(NULL, Py_NewRef(match), NULL);
-
-                Py_DECREF(exc);
-
+                Py_DECREF(exc_value);
             }
-
             DISPATCH();
         }
 
index c3773a4f489296e633f21e76783f0e54ea3272e3..c92b267dd03a15c2566f189ba08828831caf3d43 100644 (file)
@@ -1000,8 +1000,8 @@ stack_effect(int opcode, int oparg, int jump)
             return -1;
         case CHECK_EXC_MATCH:
             return 0;
-        case JUMP_IF_NOT_EG_MATCH:
-            return jump > 0 ? -1 : 0;
+        case CHECK_EG_MATCH:
+            return 0;
         case IMPORT_NAME:
             return -1;
         case IMPORT_FROM:
@@ -3533,14 +3533,18 @@ compiler_try_except(struct compiler *c, stmt_ty s)
    []                                         POP_BLOCK
    []                                         JUMP                  L0
 
-   [exc]                            L1:       COPY 1      )  save copy of the original exception
+   [exc]                            L1:       COPY 1       )  save copy of the original exception
    [orig, exc]                                BUILD_LIST   )  list for raised/reraised excs ("result")
    [orig, exc, res]                           SWAP 2
 
    [orig, res, exc]                           <evaluate E1>
-   [orig, res, exc, E1]                       JUMP_IF_NOT_EG_MATCH L2
+   [orig, res, exc, E1]                       CHECK_EG_MATCH
+   [orig, red, rest/exc, match?]              COPY 1
+   [orig, red, rest/exc, match?, match?]      POP_JUMP_IF_NOT_NONE  H1
+   [orig, red, exc, None]                     POP_TOP
+   [orig, red, exc]                           JUMP L2
 
-   [orig, res, rest, match]                   <assign to V1>  (or POP if no V1)
+   [orig, res, rest, match]         H1:       <assign to V1>  (or POP if no V1)
 
    [orig, res, rest]                          SETUP_FINALLY         R1
    [orig, res, rest]                          <code for S1>
@@ -3622,6 +3626,10 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
         if (except == NULL) {
             return 0;
         }
+        basicblock *handle_match = compiler_new_block(c);
+        if (handle_match == NULL) {
+            return 0;
+        }
         if (i == 0) {
             /* Push the original EG into the stack */
             /*
@@ -3641,9 +3649,15 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
         }
         if (handler->v.ExceptHandler.type) {
             VISIT(c, expr, handler->v.ExceptHandler.type);
-            ADDOP_JUMP(c, JUMP_IF_NOT_EG_MATCH, except);
+            ADDOP(c, CHECK_EG_MATCH);
+            ADDOP_I(c, COPY, 1);
+            ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, handle_match);
+            ADDOP(c, POP_TOP);  // match
+            ADDOP_JUMP(c, JUMP, except);
         }
 
+        compiler_use_next_block(c, handle_match);
+
         basicblock *cleanup_end = compiler_new_block(c);
         if (cleanup_end == NULL) {
             return 0;
@@ -3657,7 +3671,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
             compiler_nameop(c, handler->v.ExceptHandler.name, Store);
         }
         else {
-            ADDOP(c, POP_TOP);  // exc
+            ADDOP(c, POP_TOP);  // match
         }
 
         /*
index 2eba5531723fb6dbc1fa2201476eea97a37bd29d..e71b3e2120f7a01f687a4ad46bf50ad9ea910b3a 100644 (file)
@@ -36,6 +36,7 @@ static void *opcode_targets[256] = {
     &&TARGET_JUMP_BACKWARD_QUICK,
     &&TARGET_PUSH_EXC_INFO,
     &&TARGET_CHECK_EXC_MATCH,
+    &&TARGET_CHECK_EG_MATCH,
     &&TARGET_LOAD_ATTR_ADAPTIVE,
     &&TARGET_LOAD_ATTR_INSTANCE_VALUE,
     &&TARGET_LOAD_ATTR_MODULE,
@@ -47,40 +48,39 @@ static void *opcode_targets[256] = {
     &&TARGET_LOAD_GLOBAL_ADAPTIVE,
     &&TARGET_LOAD_GLOBAL_BUILTIN,
     &&TARGET_LOAD_GLOBAL_MODULE,
-    &&TARGET_LOAD_METHOD_ADAPTIVE,
     &&TARGET_WITH_EXCEPT_START,
     &&TARGET_GET_AITER,
     &&TARGET_GET_ANEXT,
     &&TARGET_BEFORE_ASYNC_WITH,
     &&TARGET_BEFORE_WITH,
     &&TARGET_END_ASYNC_FOR,
+    &&TARGET_LOAD_METHOD_ADAPTIVE,
     &&TARGET_LOAD_METHOD_CLASS,
     &&TARGET_LOAD_METHOD_MODULE,
     &&TARGET_LOAD_METHOD_NO_DICT,
     &&TARGET_LOAD_METHOD_WITH_DICT,
-    &&TARGET_LOAD_METHOD_WITH_VALUES,
     &&TARGET_STORE_SUBSCR,
     &&TARGET_DELETE_SUBSCR,
+    &&TARGET_LOAD_METHOD_WITH_VALUES,
     &&TARGET_PRECALL_ADAPTIVE,
     &&TARGET_PRECALL_BOUND_METHOD,
     &&TARGET_PRECALL_BUILTIN_CLASS,
     &&TARGET_PRECALL_BUILTIN_FAST_WITH_KEYWORDS,
     &&TARGET_PRECALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS,
-    &&TARGET_PRECALL_NO_KW_BUILTIN_FAST,
     &&TARGET_GET_ITER,
     &&TARGET_GET_YIELD_FROM_ITER,
     &&TARGET_PRINT_EXPR,
     &&TARGET_LOAD_BUILD_CLASS,
+    &&TARGET_PRECALL_NO_KW_BUILTIN_FAST,
     &&TARGET_PRECALL_NO_KW_BUILTIN_O,
-    &&TARGET_PRECALL_NO_KW_ISINSTANCE,
     &&TARGET_LOAD_ASSERTION_ERROR,
     &&TARGET_RETURN_GENERATOR,
+    &&TARGET_PRECALL_NO_KW_ISINSTANCE,
     &&TARGET_PRECALL_NO_KW_LEN,
     &&TARGET_PRECALL_NO_KW_LIST_APPEND,
     &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_FAST,
     &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_NOARGS,
     &&TARGET_PRECALL_NO_KW_METHOD_DESCRIPTOR_O,
-    &&TARGET_PRECALL_NO_KW_STR_1,
     &&TARGET_LIST_TO_TUPLE,
     &&TARGET_RETURN_VALUE,
     &&TARGET_IMPORT_STAR,
@@ -112,7 +112,7 @@ static void *opcode_targets[256] = {
     &&TARGET_JUMP_FORWARD,
     &&TARGET_JUMP_IF_FALSE_OR_POP,
     &&TARGET_JUMP_IF_TRUE_OR_POP,
-    &&TARGET_PRECALL_NO_KW_TUPLE_1,
+    &&TARGET_PRECALL_NO_KW_STR_1,
     &&TARGET_POP_JUMP_IF_FALSE,
     &&TARGET_POP_JUMP_IF_TRUE,
     &&TARGET_LOAD_GLOBAL,
@@ -120,13 +120,13 @@ static void *opcode_targets[256] = {
     &&TARGET_CONTAINS_OP,
     &&TARGET_RERAISE,
     &&TARGET_COPY,
-    &&TARGET_PRECALL_NO_KW_TYPE_1,
+    &&TARGET_PRECALL_NO_KW_TUPLE_1,
     &&TARGET_BINARY_OP,
     &&TARGET_SEND,
     &&TARGET_LOAD_FAST,
     &&TARGET_STORE_FAST,
     &&TARGET_DELETE_FAST,
-    &&TARGET_JUMP_IF_NOT_EG_MATCH,
+    &&TARGET_PRECALL_NO_KW_TYPE_1,
     &&TARGET_POP_JUMP_IF_NOT_NONE,
     &&TARGET_POP_JUMP_IF_NONE,
     &&TARGET_RAISE_VARARGS,