Skip to content

Commit 1dfbcb4

Browse files
committed
more ctx
1 parent 93b4a3a commit 1dfbcb4

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

mypyc/irbuild/statement.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@
105105
get_exc_info_op,
106106
get_exc_value_op,
107107
keep_propagating_op,
108+
err_occurred_op,
108109
no_err_occurred_op,
109110
propagate_if_error_op,
110111
raise_exception_op,
111112
reraise_exception_op,
112113
restore_exc_info_op,
114+
error_clear_op,
113115
)
114116
from mypyc.primitives.generic_ops import iter_op, next_op, next_raw_op, py_delattr_op
115117
from mypyc.primitives.misc_ops import (
@@ -1018,24 +1020,47 @@ def try_body() -> None:
10181020
with_body()
10191021

10201022
# except Exception as e:
1021-
# exc = True
10221023
# try:
10231024
# gen.throw(e)
10241025
# except StopIteration as e2:
10251026
# if e2 is not e:
10261027
# raise
10271028
# return
10281029
# except RuntimeError:
1029-
# # TODO: some other stuff
1030-
# ...
1030+
# # TODO: check the traceback munging
1031+
# raise
10311032
# except BaseException:
1032-
# # TODO: some other stuff
1033-
# ...
1033+
# # approximately
1034+
# raise
10341035

10351036
def except_body() -> None:
1036-
builder.add(RaiseStandardError(RaiseStandardError.RUNTIME_ERROR, "TODO", line))
1037+
exc_original = builder.call_c(get_exc_value_op, [], line)
1038+
1039+
builder.py_call(builder.py_get_attr(gen, "throw", line), [], line)
1040+
err_occurred = builder.call_c(err_occurred_op, [], line)
1041+
1042+
error_block, no_error_block = BasicBlock(), BasicBlock()
1043+
builder.add(Branch(err_occurred, error_block, no_error_block, Branch.BOOL))
1044+
1045+
builder.activate_block(error_block)
1046+
1047+
stop_iteration = builder.call_c(check_stop_op, [], line)
1048+
is_same_exc = builder.binary_op(stop_iteration, exc_original, "is", line)
1049+
1050+
suppress_block, propagate_block = BasicBlock(), BasicBlock()
1051+
builder.add(Branch(is_same_exc, suppress_block, propagate_block, Branch.BOOL))
1052+
1053+
builder.activate_block(propagate_block)
1054+
builder.call_c(keep_propagating_op, [], line)
1055+
builder.add(Unreachable())
1056+
1057+
builder.activate_block(no_error_block)
1058+
builder.add(RaiseStandardError(RaiseStandardError.RUNTIME_ERROR, "generator didn't stop after throw()", line))
10371059
builder.add(Unreachable())
10381060

1061+
builder.activate_block(suppress_block)
1062+
builder.call_c(error_clear_op, [], -1)
1063+
10391064
# TODO: actually do the exceptions
10401065
handlers = [(None, None, except_body)]
10411066

@@ -1062,7 +1087,8 @@ def else_body() -> None:
10621087
builder.add(Unreachable())
10631088

10641089
builder.activate_block(stop_block)
1065-
builder.call_c(error_catch_op, [], -1)
1090+
# TODO: should check for StopIteration
1091+
builder.call_c(error_clear_op, [], -1)
10661092

10671093
transform_try_except(
10681094
builder,

mypyc/primitives/exc_ops.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
arg_types=[], return_type=exc_rtuple, c_function_name="CPy_CatchError", error_kind=ERR_NEVER
8181
)
8282

83+
# Clear the current exception.
84+
error_clear_op = custom_op(
85+
arg_types=[], return_type=void_rtype, c_function_name="PyErr_Clear", error_kind=ERR_NEVER
86+
)
87+
8388
# Restore an old "currently handled exception" returned from.
8489
# error_catch (by sticking it into sys.exc_info())
8590
restore_exc_info_op = custom_op(

0 commit comments

Comments
 (0)