Skip to content

Commit 4debbb8

Browse files
committed
2 failed, 232 passed, 7 skipped
1 parent 82f7416 commit 4debbb8

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

thunder/core/interpreter.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,6 +3090,7 @@ def check_signal(val):
30903090
@register_opcode_handler("ASYNC_GEN_WRAP", min_ver=(3, 11), max_ver=(3, 11))
30913091
def _async_gen_wrap_handler(inst: dis.Instruction, /, stack: InterpreterStack, **kwargs) -> None:
30923092
# 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
30933094
pass
30943095

30953096

@@ -3685,6 +3686,45 @@ def _list_to_tuple_intrinsic(tos):
36853686
return res
36863687

36873688

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+
36883728
# https://docs.python.org/3.12/library/dis.html#opcode-CALL_INTRINSIC_1
36893729
@register_opcode_handler("CALL_INTRINSIC_1", min_ver=(3, 12))
36903730
def _call_intrinsic_1_handler(
@@ -3696,8 +3736,8 @@ def _call_intrinsic_1_handler(
36963736
"INTRINSIC_PRINT": _print_intrinsic,
36973737
"INTRINSIC_LIST_TO_TUPLE": _list_to_tuple_intrinsic,
36983738
"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,
37013741
"INTRINSIC_UNARY_POSITIVE": _unary_positive_intrinsic,
37023742
# INTRINSIC_TYPEVAR
37033743
# INTRINSIC_PARAMSPEC
@@ -4079,7 +4119,7 @@ def _end_for_handler(inst: dis.Instruction, /, stack: InterpreterStack, **kwargs
40794119
# https://docs.python.org/3.12/library/dis.html#opcode-END_SEND
40804120
@register_opcode_handler("END_SEND", min_ver=(3, 12))
40814121
def _end_send_handler(inst: dis.Instruction, /, stack: InterpreterStack, **kwargs) -> None:
4082-
stack.pop_wrapped()
4122+
del stack[-2]
40834123

40844124

40854125
# https://docs.python.org/3.10/library/dis.html#opcode-EXTENDED_ARG

0 commit comments

Comments
 (0)