@@ -3090,6 +3090,7 @@ def check_signal(val):
3090
3090
@register_opcode_handler ("ASYNC_GEN_WRAP" , min_ver = (3 , 11 ), max_ver = (3 , 11 ))
3091
3091
def _async_gen_wrap_handler (inst : dis .Instruction , / , stack : InterpreterStack , ** kwargs ) -> None :
3092
3092
# the next thing will be to yield the value, but we delegate this along with the wrapping to thunder_interpreter_async_generator
3093
+ # update the intrinsic for 3.12+, too
3093
3094
pass
3094
3095
3095
3096
@@ -3685,6 +3686,45 @@ def _list_to_tuple_intrinsic(tos):
3685
3686
return res
3686
3687
3687
3688
3689
+ def _stopiteration_error_intrinsic (exc ):
3690
+ runtimectx : InterpreterRuntimeCtx = get_interpreterruntimectx ()
3691
+ co_flags = runtimectx .frame_stack [- 1 ].code .co_flags
3692
+
3693
+ assert wrapped_isinstance (exc , Exception )
3694
+ # CPython 3.12 asserts whether frame->owner == FRAME_OWNED_BY_GENERATOR
3695
+ assert co_flags & (inspect .CO_COROUTINE | inspect .CO_GENERATOR | inspect .CO_ASYNC_GENERATOR )
3696
+
3697
+ msg = None
3698
+ if wrapped_isinstance (exc , StopIteration ):
3699
+ msg = "generator raised StopIteration"
3700
+ if co_flags & inspect .CO_ASYNC_GENERATOR :
3701
+ msg = "async generator raised StopIteration"
3702
+ elif co_flags & inspect .CO_COROUTINE :
3703
+ msg = "coroutine raised StopIteration"
3704
+ elif (co_flags & inspect .CO_ASYNC_GENERATOR ) and wrapped_isinstance (exc , StopAsyncIteration ):
3705
+ msg = "async generator raised StopAsyncIteration"
3706
+
3707
+ if msg :
3708
+ compile_ctx : InterpreterCompileCtx = get_interpretercompilectx ()
3709
+ if compile_ctx ._with_provenance_tracking :
3710
+ msg = wrap_const (msg )
3711
+
3712
+ def impl (exc , msg ):
3713
+ error = RuntimeError (msg )
3714
+ error .__cause__ = exc .value
3715
+ return error
3716
+
3717
+ return _interpret_call (impl , exc , msg )
3718
+
3719
+ return exc
3720
+
3721
+
3722
+ def _async_gen_wrap_intrinsic (v ):
3723
+ # noop for now
3724
+ # see ASYNC_GEN_WRAP opcode for 3.11
3725
+ return v
3726
+
3727
+
3688
3728
# https://docs.python.org/3.12/library/dis.html#opcode-CALL_INTRINSIC_1
3689
3729
@register_opcode_handler ("CALL_INTRINSIC_1" , min_ver = (3 , 12 ))
3690
3730
def _call_intrinsic_1_handler (
@@ -3696,8 +3736,8 @@ def _call_intrinsic_1_handler(
3696
3736
"INTRINSIC_PRINT" : _print_intrinsic ,
3697
3737
"INTRINSIC_LIST_TO_TUPLE" : _list_to_tuple_intrinsic ,
3698
3738
"INTRINSIC_IMPORT_STAR" : _import_star_intrinsic ,
3699
- # INTRINSIC_STOPITERATION_ERROR
3700
- # INTRINSIC_ASYNC_GEN_WRAP
3739
+ " INTRINSIC_STOPITERATION_ERROR" : _stopiteration_error_intrinsic ,
3740
+ " INTRINSIC_ASYNC_GEN_WRAP" : _async_gen_wrap_intrinsic ,
3701
3741
"INTRINSIC_UNARY_POSITIVE" : _unary_positive_intrinsic ,
3702
3742
# INTRINSIC_TYPEVAR
3703
3743
# INTRINSIC_PARAMSPEC
@@ -4079,7 +4119,7 @@ def _end_for_handler(inst: dis.Instruction, /, stack: InterpreterStack, **kwargs
4079
4119
# https://docs.python.org/3.12/library/dis.html#opcode-END_SEND
4080
4120
@register_opcode_handler ("END_SEND" , min_ver = (3 , 12 ))
4081
4121
def _end_send_handler (inst : dis .Instruction , / , stack : InterpreterStack , ** kwargs ) -> None :
4082
- stack . pop_wrapped ()
4122
+ del stack [ - 2 ]
4083
4123
4084
4124
4085
4125
# https://docs.python.org/3.10/library/dis.html#opcode-EXTENDED_ARG
0 commit comments