Skip to content

Commit 2e9afec

Browse files
authored
[mypyc] Merge keep_propagating_op and remove unused assert_err_occurred_op (#9303)
This PR merges keep_propagating_op and removes unused assert_err_occurred_op. To keep the design unified, we turn to C wrapper function for now. This completes all exception-related ops.
1 parent c2c72da commit 2e9afec

File tree

6 files changed

+21
-28
lines changed

6 files changed

+21
-28
lines changed

mypyc/irbuild/statement.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def transform_try_except(builder: IRBuilder,
325325
# the exception.
326326
builder.activate_block(double_except_block)
327327
builder.call_c(restore_exc_info_op, [builder.read(old_exc)], line)
328-
builder.primitive_op(keep_propagating_op, [], NO_TRACEBACK_LINE_NO)
328+
builder.call_c(keep_propagating_op, [], NO_TRACEBACK_LINE_NO)
329329
builder.add(Unreachable())
330330

331331
# If present, compile the else body in the obvious way
@@ -463,7 +463,7 @@ def try_finally_resolve_control(builder: IRBuilder,
463463
# If there was an exception, restore again
464464
builder.activate_block(cleanup_block)
465465
finally_control.gen_cleanup(builder, -1)
466-
builder.primitive_op(keep_propagating_op, [], NO_TRACEBACK_LINE_NO)
466+
builder.call_c(keep_propagating_op, [], NO_TRACEBACK_LINE_NO)
467467
builder.add(Unreachable())
468468

469469
return out_block

mypyc/lib-rt/CPy.h

+3
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ static int CPy_NoErrOccured(void) {
407407
return PyErr_Occurred() == NULL;
408408
}
409409

410+
static inline bool CPy_KeepPropagating(void) {
411+
return 0;
412+
}
410413
// We want to avoid the public PyErr_GetExcInfo API for these because
411414
// it requires a bunch of spurious refcount traffic on the parts of
412415
// the triple we don't care about. Unfortunately the layout of the

mypyc/primitives/exc_ops.py

+5-15
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
from mypyc.ir.ops import ERR_NEVER, ERR_FALSE, ERR_ALWAYS
44
from mypyc.ir.rtypes import bool_rprimitive, object_rprimitive, void_rtype, exc_rtuple
5-
from mypyc.primitives.registry import (
6-
simple_emit, custom_op, c_custom_op
7-
)
5+
from mypyc.primitives.registry import c_custom_op
86

97
# If the argument is a class, raise an instance of the class. Otherwise, assume
108
# that the argument is an exception object, and raise it.
@@ -43,22 +41,14 @@
4341
c_function_name='CPy_NoErrOccured',
4442
error_kind=ERR_FALSE)
4543

46-
# Assert that the error indicator has been set.
47-
assert_err_occured_op = custom_op(
48-
arg_types=[],
49-
result_type=void_rtype,
50-
error_kind=ERR_NEVER,
51-
format_str='assert_err_occurred',
52-
emit=simple_emit('assert(PyErr_Occurred() != NULL && "failure w/o err!");'))
5344

5445
# Keep propagating a raised exception by unconditionally giving an error value.
5546
# This doesn't actually raise an exception.
56-
keep_propagating_op = custom_op(
47+
keep_propagating_op = c_custom_op(
5748
arg_types=[],
58-
result_type=bool_rprimitive,
59-
error_kind=ERR_FALSE,
60-
format_str='{dest} = keep_propagating',
61-
emit=simple_emit('{dest} = 0;'))
49+
return_type=bool_rprimitive,
50+
c_function_name='CPy_KeepPropagating',
51+
error_kind=ERR_FALSE)
6252

6353
# Catches a propagating exception and makes it the "currently
6454
# handled exception" (by sticking it into sys.exc_info()). Returns the

mypyc/test-data/analysis.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ L7:
752752
goto L10
753753
L8:
754754
CPy_RestoreExcInfo(r1)
755-
r7 = keep_propagating
755+
r7 = CPy_KeepPropagating()
756756
if not r7 goto L11 else goto L9 :: bool
757757
L9:
758758
unreachable

mypyc/test-data/exceptions.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ L5:
213213
L6:
214214
CPy_RestoreExcInfo(r4)
215215
dec_ref r4
216-
r10 = keep_propagating
216+
r10 = CPy_KeepPropagating()
217217
if not r10 goto L9 else goto L7 :: bool
218218
L7:
219219
unreachable
@@ -305,7 +305,7 @@ L15:
305305
CPy_RestoreExcInfo(r6)
306306
dec_ref r6
307307
L16:
308-
r16 = keep_propagating
308+
r16 = CPy_KeepPropagating()
309309
if not r16 goto L19 else goto L17 :: bool
310310
L17:
311311
unreachable

mypyc/test-data/irbuild-try.test

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ L3:
3434
goto L5
3535
L4: (handler for L2)
3636
CPy_RestoreExcInfo(r4)
37-
r10 = keep_propagating
37+
r10 = CPy_KeepPropagating()
3838
unreachable
3939
L5:
4040
return 1
@@ -87,7 +87,7 @@ L6:
8787
goto L8
8888
L7: (handler for L5)
8989
CPy_RestoreExcInfo(r6)
90-
r12 = keep_propagating
90+
r12 = CPy_KeepPropagating()
9191
unreachable
9292
L8:
9393
return 1
@@ -164,7 +164,7 @@ L6:
164164
goto L8
165165
L7: (handler for L3, L4, L5)
166166
CPy_RestoreExcInfo(r9)
167-
r20 = keep_propagating
167+
r20 = CPy_KeepPropagating()
168168
unreachable
169169
L8:
170170
goto L12
@@ -180,7 +180,7 @@ L10:
180180
goto L12
181181
L11: (handler for L9)
182182
CPy_RestoreExcInfo(r21)
183-
r27 = keep_propagating
183+
r27 = CPy_KeepPropagating()
184184
unreachable
185185
L12:
186186
return 1
@@ -250,7 +250,7 @@ L7:
250250
goto L9
251251
L8: (handler for L2, L3, L4, L5, L6)
252252
CPy_RestoreExcInfo(r0)
253-
r19 = keep_propagating
253+
r19 = CPy_KeepPropagating()
254254
unreachable
255255
L9:
256256
return 1
@@ -312,7 +312,7 @@ L10: (handler for L7, L8)
312312
L11:
313313
CPy_RestoreExcInfo(r5)
314314
L12:
315-
r13 = keep_propagating
315+
r13 = CPy_KeepPropagating()
316316
unreachable
317317
L13:
318318
return 1
@@ -378,7 +378,7 @@ L6:
378378
goto L8
379379
L7: (handler for L3, L4, L5)
380380
CPy_RestoreExcInfo(r13)
381-
r20 = keep_propagating
381+
r20 = CPy_KeepPropagating()
382382
unreachable
383383
L8:
384384
L9:
@@ -406,7 +406,7 @@ L17: (handler for L12, L13, L14, L15)
406406
L18:
407407
CPy_RestoreExcInfo(r21)
408408
L19:
409-
r26 = keep_propagating
409+
r26 = CPy_KeepPropagating()
410410
unreachable
411411
L20:
412412
return 1

0 commit comments

Comments
 (0)