GH-90997: Wrap yield from/await in a virtual try/except StopIteration (GH-96010)

This commit is contained in:
Brandt Bucher 2022-08-19 12:33:44 -07:00 committed by GitHub
parent 2d9f252c0c
commit 5bfb3c372b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 106 deletions

View File

@ -567,6 +567,17 @@ the original TOS1.
.. versionchanged:: 3.11 .. versionchanged:: 3.11
Exception representation on the stack now consist of one, not three, items. Exception representation on the stack now consist of one, not three, items.
.. opcode:: CLEANUP_THROW
Handles an exception raised during a :meth:`~generator.throw` or
:meth:`~generator.close` call through the current frame. If TOS is an
instance of :exc:`StopIteration`, pop three values from the stack and push
its ``value`` member. Otherwise, re-raise TOS.
.. versionadded:: 3.12
.. opcode:: BEFORE_ASYNC_WITH .. opcode:: BEFORE_ASYNC_WITH
Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the Resolves ``__aenter__`` and ``__aexit__`` from the object on top of the
@ -1344,10 +1355,14 @@ iterations of the loop.
.. versionadded:: 3.11 .. versionadded:: 3.11
.. opcode:: SEND .. opcode:: SEND (delta)
Sends ``None`` to the sub-generator of this generator. Equivalent to ``TOS = TOS1.send(TOS)``. Used in ``yield from`` and ``await``
Used in ``yield from`` and ``await`` statements. statements.
If the call raises :exc:`StopIteration`, pop both items, push the
exception's ``value`` attribute, and increment the bytecode counter by
*delta*.
.. versionadded:: 3.11 .. versionadded:: 3.11

View File

@ -104,6 +104,7 @@ const uint8_t _PyOpcode_Deopt[256] = {
[CALL_PY_WITH_DEFAULTS] = CALL, [CALL_PY_WITH_DEFAULTS] = CALL,
[CHECK_EG_MATCH] = CHECK_EG_MATCH, [CHECK_EG_MATCH] = CHECK_EG_MATCH,
[CHECK_EXC_MATCH] = CHECK_EXC_MATCH, [CHECK_EXC_MATCH] = CHECK_EXC_MATCH,
[CLEANUP_THROW] = CLEANUP_THROW,
[COMPARE_OP] = COMPARE_OP, [COMPARE_OP] = COMPARE_OP,
[COMPARE_OP_ADAPTIVE] = COMPARE_OP, [COMPARE_OP_ADAPTIVE] = COMPARE_OP,
[COMPARE_OP_FLOAT_JUMP] = COMPARE_OP, [COMPARE_OP_FLOAT_JUMP] = COMPARE_OP,
@ -298,38 +299,38 @@ static const char *const _PyOpcode_OpName[267] = {
[BEFORE_ASYNC_WITH] = "BEFORE_ASYNC_WITH", [BEFORE_ASYNC_WITH] = "BEFORE_ASYNC_WITH",
[BEFORE_WITH] = "BEFORE_WITH", [BEFORE_WITH] = "BEFORE_WITH",
[END_ASYNC_FOR] = "END_ASYNC_FOR", [END_ASYNC_FOR] = "END_ASYNC_FOR",
[CLEANUP_THROW] = "CLEANUP_THROW",
[CALL_NO_KW_TYPE_1] = "CALL_NO_KW_TYPE_1", [CALL_NO_KW_TYPE_1] = "CALL_NO_KW_TYPE_1",
[COMPARE_OP_ADAPTIVE] = "COMPARE_OP_ADAPTIVE", [COMPARE_OP_ADAPTIVE] = "COMPARE_OP_ADAPTIVE",
[COMPARE_OP_FLOAT_JUMP] = "COMPARE_OP_FLOAT_JUMP", [COMPARE_OP_FLOAT_JUMP] = "COMPARE_OP_FLOAT_JUMP",
[COMPARE_OP_INT_JUMP] = "COMPARE_OP_INT_JUMP", [COMPARE_OP_INT_JUMP] = "COMPARE_OP_INT_JUMP",
[COMPARE_OP_STR_JUMP] = "COMPARE_OP_STR_JUMP",
[STORE_SUBSCR] = "STORE_SUBSCR", [STORE_SUBSCR] = "STORE_SUBSCR",
[DELETE_SUBSCR] = "DELETE_SUBSCR", [DELETE_SUBSCR] = "DELETE_SUBSCR",
[COMPARE_OP_STR_JUMP] = "COMPARE_OP_STR_JUMP",
[EXTENDED_ARG_QUICK] = "EXTENDED_ARG_QUICK", [EXTENDED_ARG_QUICK] = "EXTENDED_ARG_QUICK",
[FOR_ITER_ADAPTIVE] = "FOR_ITER_ADAPTIVE", [FOR_ITER_ADAPTIVE] = "FOR_ITER_ADAPTIVE",
[FOR_ITER_LIST] = "FOR_ITER_LIST", [FOR_ITER_LIST] = "FOR_ITER_LIST",
[FOR_ITER_RANGE] = "FOR_ITER_RANGE", [FOR_ITER_RANGE] = "FOR_ITER_RANGE",
[JUMP_BACKWARD_QUICK] = "JUMP_BACKWARD_QUICK", [JUMP_BACKWARD_QUICK] = "JUMP_BACKWARD_QUICK",
[LOAD_ATTR_ADAPTIVE] = "LOAD_ATTR_ADAPTIVE",
[GET_ITER] = "GET_ITER", [GET_ITER] = "GET_ITER",
[GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER", [GET_YIELD_FROM_ITER] = "GET_YIELD_FROM_ITER",
[PRINT_EXPR] = "PRINT_EXPR", [PRINT_EXPR] = "PRINT_EXPR",
[LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS", [LOAD_BUILD_CLASS] = "LOAD_BUILD_CLASS",
[LOAD_ATTR_ADAPTIVE] = "LOAD_ATTR_ADAPTIVE",
[LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS", [LOAD_ATTR_CLASS] = "LOAD_ATTR_CLASS",
[LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
[LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR", [LOAD_ASSERTION_ERROR] = "LOAD_ASSERTION_ERROR",
[RETURN_GENERATOR] = "RETURN_GENERATOR", [RETURN_GENERATOR] = "RETURN_GENERATOR",
[LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN] = "LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN",
[LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE", [LOAD_ATTR_INSTANCE_VALUE] = "LOAD_ATTR_INSTANCE_VALUE",
[LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE", [LOAD_ATTR_MODULE] = "LOAD_ATTR_MODULE",
[LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY", [LOAD_ATTR_PROPERTY] = "LOAD_ATTR_PROPERTY",
[LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT", [LOAD_ATTR_SLOT] = "LOAD_ATTR_SLOT",
[LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT", [LOAD_ATTR_WITH_HINT] = "LOAD_ATTR_WITH_HINT",
[LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT",
[LIST_TO_TUPLE] = "LIST_TO_TUPLE", [LIST_TO_TUPLE] = "LIST_TO_TUPLE",
[RETURN_VALUE] = "RETURN_VALUE", [RETURN_VALUE] = "RETURN_VALUE",
[IMPORT_STAR] = "IMPORT_STAR", [IMPORT_STAR] = "IMPORT_STAR",
[SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS", [SETUP_ANNOTATIONS] = "SETUP_ANNOTATIONS",
[LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT", [LOAD_ATTR_METHOD_LAZY_DICT] = "LOAD_ATTR_METHOD_LAZY_DICT",
[ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP", [ASYNC_GEN_WRAP] = "ASYNC_GEN_WRAP",
[PREP_RERAISE_STAR] = "PREP_RERAISE_STAR", [PREP_RERAISE_STAR] = "PREP_RERAISE_STAR",
[POP_EXCEPT] = "POP_EXCEPT", [POP_EXCEPT] = "POP_EXCEPT",
@ -356,7 +357,7 @@ static const char *const _PyOpcode_OpName[267] = {
[JUMP_FORWARD] = "JUMP_FORWARD", [JUMP_FORWARD] = "JUMP_FORWARD",
[JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP", [JUMP_IF_FALSE_OR_POP] = "JUMP_IF_FALSE_OR_POP",
[JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP", [JUMP_IF_TRUE_OR_POP] = "JUMP_IF_TRUE_OR_POP",
[LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT", [LOAD_ATTR_METHOD_NO_DICT] = "LOAD_ATTR_METHOD_NO_DICT",
[POP_JUMP_FORWARD_IF_FALSE] = "POP_JUMP_FORWARD_IF_FALSE", [POP_JUMP_FORWARD_IF_FALSE] = "POP_JUMP_FORWARD_IF_FALSE",
[POP_JUMP_FORWARD_IF_TRUE] = "POP_JUMP_FORWARD_IF_TRUE", [POP_JUMP_FORWARD_IF_TRUE] = "POP_JUMP_FORWARD_IF_TRUE",
[LOAD_GLOBAL] = "LOAD_GLOBAL", [LOAD_GLOBAL] = "LOAD_GLOBAL",
@ -364,7 +365,7 @@ static const char *const _PyOpcode_OpName[267] = {
[CONTAINS_OP] = "CONTAINS_OP", [CONTAINS_OP] = "CONTAINS_OP",
[RERAISE] = "RERAISE", [RERAISE] = "RERAISE",
[COPY] = "COPY", [COPY] = "COPY",
[LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES", [LOAD_ATTR_METHOD_WITH_DICT] = "LOAD_ATTR_METHOD_WITH_DICT",
[BINARY_OP] = "BINARY_OP", [BINARY_OP] = "BINARY_OP",
[SEND] = "SEND", [SEND] = "SEND",
[LOAD_FAST] = "LOAD_FAST", [LOAD_FAST] = "LOAD_FAST",
@ -384,9 +385,9 @@ static const char *const _PyOpcode_OpName[267] = {
[STORE_DEREF] = "STORE_DEREF", [STORE_DEREF] = "STORE_DEREF",
[DELETE_DEREF] = "DELETE_DEREF", [DELETE_DEREF] = "DELETE_DEREF",
[JUMP_BACKWARD] = "JUMP_BACKWARD", [JUMP_BACKWARD] = "JUMP_BACKWARD",
[LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST", [LOAD_ATTR_METHOD_WITH_VALUES] = "LOAD_ATTR_METHOD_WITH_VALUES",
[CALL_FUNCTION_EX] = "CALL_FUNCTION_EX", [CALL_FUNCTION_EX] = "CALL_FUNCTION_EX",
[LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST", [LOAD_CONST__LOAD_FAST] = "LOAD_CONST__LOAD_FAST",
[EXTENDED_ARG] = "EXTENDED_ARG", [EXTENDED_ARG] = "EXTENDED_ARG",
[LIST_APPEND] = "LIST_APPEND", [LIST_APPEND] = "LIST_APPEND",
[SET_ADD] = "SET_ADD", [SET_ADD] = "SET_ADD",
@ -396,30 +397,31 @@ static const char *const _PyOpcode_OpName[267] = {
[YIELD_VALUE] = "YIELD_VALUE", [YIELD_VALUE] = "YIELD_VALUE",
[RESUME] = "RESUME", [RESUME] = "RESUME",
[MATCH_CLASS] = "MATCH_CLASS", [MATCH_CLASS] = "MATCH_CLASS",
[LOAD_FAST__LOAD_CONST] = "LOAD_FAST__LOAD_CONST",
[LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST", [LOAD_FAST__LOAD_FAST] = "LOAD_FAST__LOAD_FAST",
[LOAD_GLOBAL_ADAPTIVE] = "LOAD_GLOBAL_ADAPTIVE",
[FORMAT_VALUE] = "FORMAT_VALUE", [FORMAT_VALUE] = "FORMAT_VALUE",
[BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP", [BUILD_CONST_KEY_MAP] = "BUILD_CONST_KEY_MAP",
[BUILD_STRING] = "BUILD_STRING", [BUILD_STRING] = "BUILD_STRING",
[LOAD_GLOBAL_ADAPTIVE] = "LOAD_GLOBAL_ADAPTIVE",
[LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN", [LOAD_GLOBAL_BUILTIN] = "LOAD_GLOBAL_BUILTIN",
[LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE", [LOAD_GLOBAL_MODULE] = "LOAD_GLOBAL_MODULE",
[RESUME_QUICK] = "RESUME_QUICK", [RESUME_QUICK] = "RESUME_QUICK",
[STORE_ATTR_ADAPTIVE] = "STORE_ATTR_ADAPTIVE",
[LIST_EXTEND] = "LIST_EXTEND", [LIST_EXTEND] = "LIST_EXTEND",
[SET_UPDATE] = "SET_UPDATE", [SET_UPDATE] = "SET_UPDATE",
[DICT_MERGE] = "DICT_MERGE", [DICT_MERGE] = "DICT_MERGE",
[DICT_UPDATE] = "DICT_UPDATE", [DICT_UPDATE] = "DICT_UPDATE",
[STORE_ATTR_ADAPTIVE] = "STORE_ATTR_ADAPTIVE",
[STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE", [STORE_ATTR_INSTANCE_VALUE] = "STORE_ATTR_INSTANCE_VALUE",
[STORE_ATTR_SLOT] = "STORE_ATTR_SLOT", [STORE_ATTR_SLOT] = "STORE_ATTR_SLOT",
[STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT", [STORE_ATTR_WITH_HINT] = "STORE_ATTR_WITH_HINT",
[STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST", [STORE_FAST__LOAD_FAST] = "STORE_FAST__LOAD_FAST",
[STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST",
[CALL] = "CALL", [CALL] = "CALL",
[KW_NAMES] = "KW_NAMES", [KW_NAMES] = "KW_NAMES",
[POP_JUMP_BACKWARD_IF_NOT_NONE] = "POP_JUMP_BACKWARD_IF_NOT_NONE", [POP_JUMP_BACKWARD_IF_NOT_NONE] = "POP_JUMP_BACKWARD_IF_NOT_NONE",
[POP_JUMP_BACKWARD_IF_NONE] = "POP_JUMP_BACKWARD_IF_NONE", [POP_JUMP_BACKWARD_IF_NONE] = "POP_JUMP_BACKWARD_IF_NONE",
[POP_JUMP_BACKWARD_IF_FALSE] = "POP_JUMP_BACKWARD_IF_FALSE", [POP_JUMP_BACKWARD_IF_FALSE] = "POP_JUMP_BACKWARD_IF_FALSE",
[POP_JUMP_BACKWARD_IF_TRUE] = "POP_JUMP_BACKWARD_IF_TRUE", [POP_JUMP_BACKWARD_IF_TRUE] = "POP_JUMP_BACKWARD_IF_TRUE",
[STORE_FAST__STORE_FAST] = "STORE_FAST__STORE_FAST",
[STORE_SUBSCR_ADAPTIVE] = "STORE_SUBSCR_ADAPTIVE", [STORE_SUBSCR_ADAPTIVE] = "STORE_SUBSCR_ADAPTIVE",
[STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT", [STORE_SUBSCR_DICT] = "STORE_SUBSCR_DICT",
[STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT", [STORE_SUBSCR_LIST_INT] = "STORE_SUBSCR_LIST_INT",
@ -427,7 +429,6 @@ static const char *const _PyOpcode_OpName[267] = {
[UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST", [UNPACK_SEQUENCE_LIST] = "UNPACK_SEQUENCE_LIST",
[UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE", [UNPACK_SEQUENCE_TUPLE] = "UNPACK_SEQUENCE_TUPLE",
[UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE", [UNPACK_SEQUENCE_TWO_TUPLE] = "UNPACK_SEQUENCE_TWO_TUPLE",
[184] = "<184>",
[185] = "<185>", [185] = "<185>",
[186] = "<186>", [186] = "<186>",
[187] = "<187>", [187] = "<187>",
@ -514,7 +515,6 @@ static const char *const _PyOpcode_OpName[267] = {
#endif #endif
#define EXTRA_CASES \ #define EXTRA_CASES \
case 184: \
case 185: \ case 185: \
case 186: \ case 186: \
case 187: \ case 187: \

85
Include/opcode.h generated
View File

@ -32,6 +32,7 @@ extern "C" {
#define BEFORE_ASYNC_WITH 52 #define BEFORE_ASYNC_WITH 52
#define BEFORE_WITH 53 #define BEFORE_WITH 53
#define END_ASYNC_FOR 54 #define END_ASYNC_FOR 54
#define CLEANUP_THROW 55
#define STORE_SUBSCR 60 #define STORE_SUBSCR 60
#define DELETE_SUBSCR 61 #define DELETE_SUBSCR 61
#define GET_ITER 68 #define GET_ITER 68
@ -164,48 +165,48 @@ extern "C" {
#define CALL_NO_KW_METHOD_DESCRIPTOR_O 46 #define CALL_NO_KW_METHOD_DESCRIPTOR_O 46
#define CALL_NO_KW_STR_1 47 #define CALL_NO_KW_STR_1 47
#define CALL_NO_KW_TUPLE_1 48 #define CALL_NO_KW_TUPLE_1 48
#define CALL_NO_KW_TYPE_1 55 #define CALL_NO_KW_TYPE_1 56
#define COMPARE_OP_ADAPTIVE 56 #define COMPARE_OP_ADAPTIVE 57
#define COMPARE_OP_FLOAT_JUMP 57 #define COMPARE_OP_FLOAT_JUMP 58
#define COMPARE_OP_INT_JUMP 58 #define COMPARE_OP_INT_JUMP 59
#define COMPARE_OP_STR_JUMP 59 #define COMPARE_OP_STR_JUMP 62
#define EXTENDED_ARG_QUICK 62 #define EXTENDED_ARG_QUICK 63
#define FOR_ITER_ADAPTIVE 63 #define FOR_ITER_ADAPTIVE 64
#define FOR_ITER_LIST 64 #define FOR_ITER_LIST 65
#define FOR_ITER_RANGE 65 #define FOR_ITER_RANGE 66
#define JUMP_BACKWARD_QUICK 66 #define JUMP_BACKWARD_QUICK 67
#define LOAD_ATTR_ADAPTIVE 67 #define LOAD_ATTR_ADAPTIVE 72
#define LOAD_ATTR_CLASS 72 #define LOAD_ATTR_CLASS 73
#define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 73 #define LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN 76
#define LOAD_ATTR_INSTANCE_VALUE 76 #define LOAD_ATTR_INSTANCE_VALUE 77
#define LOAD_ATTR_MODULE 77 #define LOAD_ATTR_MODULE 78
#define LOAD_ATTR_PROPERTY 78 #define LOAD_ATTR_PROPERTY 79
#define LOAD_ATTR_SLOT 79 #define LOAD_ATTR_SLOT 80
#define LOAD_ATTR_WITH_HINT 80 #define LOAD_ATTR_WITH_HINT 81
#define LOAD_ATTR_METHOD_LAZY_DICT 81 #define LOAD_ATTR_METHOD_LAZY_DICT 86
#define LOAD_ATTR_METHOD_NO_DICT 86 #define LOAD_ATTR_METHOD_NO_DICT 113
#define LOAD_ATTR_METHOD_WITH_DICT 113 #define LOAD_ATTR_METHOD_WITH_DICT 121
#define LOAD_ATTR_METHOD_WITH_VALUES 121 #define LOAD_ATTR_METHOD_WITH_VALUES 141
#define LOAD_CONST__LOAD_FAST 141 #define LOAD_CONST__LOAD_FAST 143
#define LOAD_FAST__LOAD_CONST 143 #define LOAD_FAST__LOAD_CONST 153
#define LOAD_FAST__LOAD_FAST 153 #define LOAD_FAST__LOAD_FAST 154
#define LOAD_GLOBAL_ADAPTIVE 154 #define LOAD_GLOBAL_ADAPTIVE 158
#define LOAD_GLOBAL_BUILTIN 158 #define LOAD_GLOBAL_BUILTIN 159
#define LOAD_GLOBAL_MODULE 159 #define LOAD_GLOBAL_MODULE 160
#define RESUME_QUICK 160 #define RESUME_QUICK 161
#define STORE_ATTR_ADAPTIVE 161 #define STORE_ATTR_ADAPTIVE 166
#define STORE_ATTR_INSTANCE_VALUE 166 #define STORE_ATTR_INSTANCE_VALUE 167
#define STORE_ATTR_SLOT 167 #define STORE_ATTR_SLOT 168
#define STORE_ATTR_WITH_HINT 168 #define STORE_ATTR_WITH_HINT 169
#define STORE_FAST__LOAD_FAST 169 #define STORE_FAST__LOAD_FAST 170
#define STORE_FAST__STORE_FAST 170 #define STORE_FAST__STORE_FAST 177
#define STORE_SUBSCR_ADAPTIVE 177 #define STORE_SUBSCR_ADAPTIVE 178
#define STORE_SUBSCR_DICT 178 #define STORE_SUBSCR_DICT 179
#define STORE_SUBSCR_LIST_INT 179 #define STORE_SUBSCR_LIST_INT 180
#define UNPACK_SEQUENCE_ADAPTIVE 180 #define UNPACK_SEQUENCE_ADAPTIVE 181
#define UNPACK_SEQUENCE_LIST 181 #define UNPACK_SEQUENCE_LIST 182
#define UNPACK_SEQUENCE_TUPLE 182 #define UNPACK_SEQUENCE_TUPLE 183
#define UNPACK_SEQUENCE_TWO_TUPLE 183 #define UNPACK_SEQUENCE_TWO_TUPLE 184
#define DO_TRACING 255 #define DO_TRACING 255
#define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\ #define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\

View File

@ -411,10 +411,10 @@ _code_type = type(_write_atomic.__code__)
# Python 3.12a1 3505 (Specialization/Cache for FOR_ITER) # Python 3.12a1 3505 (Specialization/Cache for FOR_ITER)
# Python 3.12a1 3506 (Add BINARY_SLICE and STORE_SLICE instructions) # Python 3.12a1 3506 (Add BINARY_SLICE and STORE_SLICE instructions)
# Python 3.12a1 3507 (Set lineno of module's RESUME to 0) # Python 3.12a1 3507 (Set lineno of module's RESUME to 0)
# Python 3.12a1 3508 (Add CLEANUP_THROW)
# Python 3.13 will start with 3550 # Python 3.13 will start with 3550
#
# MAGIC must change whenever the bytecode emitted by the compiler may no # MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually # longer be understood by older implementations of the eval loop (usually
# due to the addition of new opcodes). # due to the addition of new opcodes).
@ -424,7 +424,7 @@ _code_type = type(_write_atomic.__code__)
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated. # in PC/launcher.c must also be updated.
MAGIC_NUMBER = (3507).to_bytes(2, 'little') + b'\r\n' MAGIC_NUMBER = (3508).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

View File

@ -104,6 +104,7 @@ def_op('GET_ANEXT', 51)
def_op('BEFORE_ASYNC_WITH', 52) def_op('BEFORE_ASYNC_WITH', 52)
def_op('BEFORE_WITH', 53) def_op('BEFORE_WITH', 53)
def_op('END_ASYNC_FOR', 54) def_op('END_ASYNC_FOR', 54)
def_op('CLEANUP_THROW', 55)
def_op('STORE_SUBSCR', 60) def_op('STORE_SUBSCR', 60)
def_op('DELETE_SUBSCR', 61) def_op('DELETE_SUBSCR', 61)

View File

@ -507,26 +507,31 @@ dis_asyncwith = """\
LOAD_CONST 0 (None) LOAD_CONST 0 (None)
RETURN_VALUE RETURN_VALUE
%3d >> PUSH_EXC_INFO %3d >> CLEANUP_THROW
JUMP_BACKWARD 24 (to 22)
>> CLEANUP_THROW
JUMP_BACKWARD 9 (to 56)
>> PUSH_EXC_INFO
WITH_EXCEPT_START WITH_EXCEPT_START
GET_AWAITABLE 2 GET_AWAITABLE 2
LOAD_CONST 0 (None) LOAD_CONST 0 (None)
>> SEND 3 (to 82) >> SEND 4 (to 92)
YIELD_VALUE 6 YIELD_VALUE 6
RESUME 3 RESUME 3
JUMP_BACKWARD_NO_INTERRUPT 4 (to 74) JUMP_BACKWARD_NO_INTERRUPT 4 (to 82)
>> POP_JUMP_FORWARD_IF_TRUE 1 (to 86) >> CLEANUP_THROW
>> POP_JUMP_FORWARD_IF_TRUE 1 (to 96)
RERAISE 2 RERAISE 2
>> POP_TOP >> POP_TOP
POP_EXCEPT POP_EXCEPT
POP_TOP POP_TOP
POP_TOP POP_TOP
JUMP_BACKWARD 19 (to 58) JUMP_BACKWARD 24 (to 58)
>> COPY 3 >> COPY 3
POP_EXCEPT POP_EXCEPT
RERAISE 1 RERAISE 1
ExceptionTable: ExceptionTable:
2 rows 6 rows
""" % (_asyncwith.__code__.co_firstlineno, """ % (_asyncwith.__code__.co_firstlineno,
_asyncwith.__code__.co_firstlineno + 1, _asyncwith.__code__.co_firstlineno + 1,
_asyncwith.__code__.co_firstlineno + 2, _asyncwith.__code__.co_firstlineno + 2,

View File

@ -0,0 +1,3 @@
Compile virtual :keyword:`try`/:keyword:`except` blocks to handle exceptions
raised during :meth:`~generator.close` or :meth:`~generator.throw` calls
through a suspended frame.

View File

@ -485,26 +485,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
} }
Py_DECREF(yf); Py_DECREF(yf);
if (!ret) { if (!ret) {
PyObject *val; ret = gen_send_ex(gen, Py_None, 1, 0);
/* Pop subiterator from stack */
assert(gen->gi_frame_state < FRAME_CLEARED);
ret = _PyFrame_StackPop((_PyInterpreterFrame *)gen->gi_iframe);
assert(ret == yf);
Py_DECREF(ret);
// XXX: Performing this jump ourselves is awkward and problematic.
// See https://github.com/python/cpython/pull/31968.
/* Termination repetition of SEND loop */
assert(_PyInterpreterFrame_LASTI(frame) >= 0);
/* Backup to SEND */
assert(_Py_OPCODE(frame->prev_instr[-1]) == SEND);
int jump = _Py_OPARG(frame->prev_instr[-1]);
frame->prev_instr += jump - 1;
if (_PyGen_FetchStopIterationValue(&val) == 0) {
ret = gen_send(gen, val);
Py_DECREF(val);
} else {
ret = gen_send_ex(gen, Py_None, 1, 0);
}
} }
return ret; return ret;
} }

View File

@ -2668,6 +2668,9 @@ handle_eval_breaker:
} }
TARGET(YIELD_VALUE) { TARGET(YIELD_VALUE) {
// NOTE: It's important that YIELD_VALUE never raises an exception!
// The compiler treats any exception raised here as a failed close()
// or throw() call.
assert(oparg == STACK_LEVEL()); assert(oparg == STACK_LEVEL());
assert(frame->is_entry); assert(frame->is_entry);
PyObject *retval = POP(); PyObject *retval = POP();
@ -2746,6 +2749,26 @@ handle_eval_breaker:
} }
} }
TARGET(CLEANUP_THROW) {
assert(throwflag);
PyObject *exc_value = TOP();
assert(exc_value && PyExceptionInstance_Check(exc_value));
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
PyObject *value = ((PyStopIterationObject *)exc_value)->value;
Py_INCREF(value);
Py_DECREF(POP()); // The StopIteration.
Py_DECREF(POP()); // The last sent value.
Py_DECREF(POP()); // The delegated sub-iterator.
PUSH(value);
DISPATCH();
}
Py_INCREF(exc_value);
PyObject *exc_type = Py_NewRef(Py_TYPE(exc_value));
PyObject *exc_traceback = PyException_GetTraceback(exc_value);
_PyErr_Restore(tstate, exc_type, exc_value, exc_traceback);
goto exception_unwind;
}
TARGET(LOAD_ASSERTION_ERROR) { TARGET(LOAD_ASSERTION_ERROR) {
PyObject *value = PyExc_AssertionError; PyObject *value = PyExc_AssertionError;
Py_INCREF(value); Py_INCREF(value);

View File

@ -1234,6 +1234,8 @@ stack_effect(int opcode, int oparg, int jump)
return 0; return 0;
case END_ASYNC_FOR: case END_ASYNC_FOR:
return -2; return -2;
case CLEANUP_THROW:
return -2;
case FORMAT_VALUE: case FORMAT_VALUE:
/* If there's a fmt_spec on the stack, we go from 2->1, /* If there's a fmt_spec on the stack, we go from 2->1,
else 1->1. */ else 1->1. */
@ -1946,17 +1948,22 @@ compiler_call_exit_with_nones(struct compiler *c) {
static int static int
compiler_add_yield_from(struct compiler *c, int await) compiler_add_yield_from(struct compiler *c, int await)
{ {
NEW_JUMP_TARGET_LABEL(c, start); NEW_JUMP_TARGET_LABEL(c, send);
NEW_JUMP_TARGET_LABEL(c, resume); NEW_JUMP_TARGET_LABEL(c, fail);
NEW_JUMP_TARGET_LABEL(c, exit); NEW_JUMP_TARGET_LABEL(c, exit);
USE_LABEL(c, start); USE_LABEL(c, send);
ADDOP_JUMP(c, SEND, exit); ADDOP_JUMP(c, SEND, exit);
// Set up a virtual try/except to handle when StopIteration is raised during
USE_LABEL(c, resume); // a close or throw call. The only way YIELD_VALUE raises if they do!
ADDOP_JUMP(c, SETUP_FINALLY, fail);
ADDOP_I(c, YIELD_VALUE, 0); ADDOP_I(c, YIELD_VALUE, 0);
ADDOP_NOLINE(c, POP_BLOCK);
ADDOP_I(c, RESUME, await ? 3 : 2); ADDOP_I(c, RESUME, await ? 3 : 2);
ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start); ADDOP_JUMP(c, JUMP_NO_INTERRUPT, send);
USE_LABEL(c, fail);
ADDOP(c, CLEANUP_THROW);
USE_LABEL(c, exit); USE_LABEL(c, exit);
return 1; return 1;

View File

@ -54,38 +54,38 @@ static void *opcode_targets[256] = {
&&TARGET_BEFORE_ASYNC_WITH, &&TARGET_BEFORE_ASYNC_WITH,
&&TARGET_BEFORE_WITH, &&TARGET_BEFORE_WITH,
&&TARGET_END_ASYNC_FOR, &&TARGET_END_ASYNC_FOR,
&&TARGET_CLEANUP_THROW,
&&TARGET_CALL_NO_KW_TYPE_1, &&TARGET_CALL_NO_KW_TYPE_1,
&&TARGET_COMPARE_OP_ADAPTIVE, &&TARGET_COMPARE_OP_ADAPTIVE,
&&TARGET_COMPARE_OP_FLOAT_JUMP, &&TARGET_COMPARE_OP_FLOAT_JUMP,
&&TARGET_COMPARE_OP_INT_JUMP, &&TARGET_COMPARE_OP_INT_JUMP,
&&TARGET_COMPARE_OP_STR_JUMP,
&&TARGET_STORE_SUBSCR, &&TARGET_STORE_SUBSCR,
&&TARGET_DELETE_SUBSCR, &&TARGET_DELETE_SUBSCR,
&&TARGET_COMPARE_OP_STR_JUMP,
&&TARGET_EXTENDED_ARG_QUICK, &&TARGET_EXTENDED_ARG_QUICK,
&&TARGET_FOR_ITER_ADAPTIVE, &&TARGET_FOR_ITER_ADAPTIVE,
&&TARGET_FOR_ITER_LIST, &&TARGET_FOR_ITER_LIST,
&&TARGET_FOR_ITER_RANGE, &&TARGET_FOR_ITER_RANGE,
&&TARGET_JUMP_BACKWARD_QUICK, &&TARGET_JUMP_BACKWARD_QUICK,
&&TARGET_LOAD_ATTR_ADAPTIVE,
&&TARGET_GET_ITER, &&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER, &&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_PRINT_EXPR, &&TARGET_PRINT_EXPR,
&&TARGET_LOAD_BUILD_CLASS, &&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_ATTR_ADAPTIVE,
&&TARGET_LOAD_ATTR_CLASS, &&TARGET_LOAD_ATTR_CLASS,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_ASSERTION_ERROR, &&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_RETURN_GENERATOR, &&TARGET_RETURN_GENERATOR,
&&TARGET_LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN,
&&TARGET_LOAD_ATTR_INSTANCE_VALUE, &&TARGET_LOAD_ATTR_INSTANCE_VALUE,
&&TARGET_LOAD_ATTR_MODULE, &&TARGET_LOAD_ATTR_MODULE,
&&TARGET_LOAD_ATTR_PROPERTY, &&TARGET_LOAD_ATTR_PROPERTY,
&&TARGET_LOAD_ATTR_SLOT, &&TARGET_LOAD_ATTR_SLOT,
&&TARGET_LOAD_ATTR_WITH_HINT, &&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_LIST_TO_TUPLE, &&TARGET_LIST_TO_TUPLE,
&&TARGET_RETURN_VALUE, &&TARGET_RETURN_VALUE,
&&TARGET_IMPORT_STAR, &&TARGET_IMPORT_STAR,
&&TARGET_SETUP_ANNOTATIONS, &&TARGET_SETUP_ANNOTATIONS,
&&TARGET_LOAD_ATTR_METHOD_NO_DICT, &&TARGET_LOAD_ATTR_METHOD_LAZY_DICT,
&&TARGET_ASYNC_GEN_WRAP, &&TARGET_ASYNC_GEN_WRAP,
&&TARGET_PREP_RERAISE_STAR, &&TARGET_PREP_RERAISE_STAR,
&&TARGET_POP_EXCEPT, &&TARGET_POP_EXCEPT,
@ -112,7 +112,7 @@ static void *opcode_targets[256] = {
&&TARGET_JUMP_FORWARD, &&TARGET_JUMP_FORWARD,
&&TARGET_JUMP_IF_FALSE_OR_POP, &&TARGET_JUMP_IF_FALSE_OR_POP,
&&TARGET_JUMP_IF_TRUE_OR_POP, &&TARGET_JUMP_IF_TRUE_OR_POP,
&&TARGET_LOAD_ATTR_METHOD_WITH_DICT, &&TARGET_LOAD_ATTR_METHOD_NO_DICT,
&&TARGET_POP_JUMP_FORWARD_IF_FALSE, &&TARGET_POP_JUMP_FORWARD_IF_FALSE,
&&TARGET_POP_JUMP_FORWARD_IF_TRUE, &&TARGET_POP_JUMP_FORWARD_IF_TRUE,
&&TARGET_LOAD_GLOBAL, &&TARGET_LOAD_GLOBAL,
@ -120,7 +120,7 @@ static void *opcode_targets[256] = {
&&TARGET_CONTAINS_OP, &&TARGET_CONTAINS_OP,
&&TARGET_RERAISE, &&TARGET_RERAISE,
&&TARGET_COPY, &&TARGET_COPY,
&&TARGET_LOAD_ATTR_METHOD_WITH_VALUES, &&TARGET_LOAD_ATTR_METHOD_WITH_DICT,
&&TARGET_BINARY_OP, &&TARGET_BINARY_OP,
&&TARGET_SEND, &&TARGET_SEND,
&&TARGET_LOAD_FAST, &&TARGET_LOAD_FAST,
@ -140,9 +140,9 @@ static void *opcode_targets[256] = {
&&TARGET_STORE_DEREF, &&TARGET_STORE_DEREF,
&&TARGET_DELETE_DEREF, &&TARGET_DELETE_DEREF,
&&TARGET_JUMP_BACKWARD, &&TARGET_JUMP_BACKWARD,
&&TARGET_LOAD_CONST__LOAD_FAST, &&TARGET_LOAD_ATTR_METHOD_WITH_VALUES,
&&TARGET_CALL_FUNCTION_EX, &&TARGET_CALL_FUNCTION_EX,
&&TARGET_LOAD_FAST__LOAD_CONST, &&TARGET_LOAD_CONST__LOAD_FAST,
&&TARGET_EXTENDED_ARG, &&TARGET_EXTENDED_ARG,
&&TARGET_LIST_APPEND, &&TARGET_LIST_APPEND,
&&TARGET_SET_ADD, &&TARGET_SET_ADD,
@ -152,30 +152,31 @@ static void *opcode_targets[256] = {
&&TARGET_YIELD_VALUE, &&TARGET_YIELD_VALUE,
&&TARGET_RESUME, &&TARGET_RESUME,
&&TARGET_MATCH_CLASS, &&TARGET_MATCH_CLASS,
&&TARGET_LOAD_FAST__LOAD_CONST,
&&TARGET_LOAD_FAST__LOAD_FAST, &&TARGET_LOAD_FAST__LOAD_FAST,
&&TARGET_LOAD_GLOBAL_ADAPTIVE,
&&TARGET_FORMAT_VALUE, &&TARGET_FORMAT_VALUE,
&&TARGET_BUILD_CONST_KEY_MAP, &&TARGET_BUILD_CONST_KEY_MAP,
&&TARGET_BUILD_STRING, &&TARGET_BUILD_STRING,
&&TARGET_LOAD_GLOBAL_ADAPTIVE,
&&TARGET_LOAD_GLOBAL_BUILTIN, &&TARGET_LOAD_GLOBAL_BUILTIN,
&&TARGET_LOAD_GLOBAL_MODULE, &&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_RESUME_QUICK, &&TARGET_RESUME_QUICK,
&&TARGET_STORE_ATTR_ADAPTIVE,
&&TARGET_LIST_EXTEND, &&TARGET_LIST_EXTEND,
&&TARGET_SET_UPDATE, &&TARGET_SET_UPDATE,
&&TARGET_DICT_MERGE, &&TARGET_DICT_MERGE,
&&TARGET_DICT_UPDATE, &&TARGET_DICT_UPDATE,
&&TARGET_STORE_ATTR_ADAPTIVE,
&&TARGET_STORE_ATTR_INSTANCE_VALUE, &&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_ATTR_SLOT, &&TARGET_STORE_ATTR_SLOT,
&&TARGET_STORE_ATTR_WITH_HINT, &&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_STORE_FAST__LOAD_FAST, &&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_STORE_FAST__STORE_FAST,
&&TARGET_CALL, &&TARGET_CALL,
&&TARGET_KW_NAMES, &&TARGET_KW_NAMES,
&&TARGET_POP_JUMP_BACKWARD_IF_NOT_NONE, &&TARGET_POP_JUMP_BACKWARD_IF_NOT_NONE,
&&TARGET_POP_JUMP_BACKWARD_IF_NONE, &&TARGET_POP_JUMP_BACKWARD_IF_NONE,
&&TARGET_POP_JUMP_BACKWARD_IF_FALSE, &&TARGET_POP_JUMP_BACKWARD_IF_FALSE,
&&TARGET_POP_JUMP_BACKWARD_IF_TRUE, &&TARGET_POP_JUMP_BACKWARD_IF_TRUE,
&&TARGET_STORE_FAST__STORE_FAST,
&&TARGET_STORE_SUBSCR_ADAPTIVE, &&TARGET_STORE_SUBSCR_ADAPTIVE,
&&TARGET_STORE_SUBSCR_DICT, &&TARGET_STORE_SUBSCR_DICT,
&&TARGET_STORE_SUBSCR_LIST_INT, &&TARGET_STORE_SUBSCR_LIST_INT,
@ -253,6 +254,5 @@ static void *opcode_targets[256] = {
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode, &&_unknown_opcode,
&&_unknown_opcode,
&&TARGET_DO_TRACING &&TARGET_DO_TRACING
}; };