Debian Codegraph @

mypyc/primitives/exc_ops.py

raw
1 """Exception-related primitive ops."""
2
3 from __future__ import annotations
4
5 from mypyc.ir.ops import ERR_ALWAYS, ERR_FALSE, ERR_NEVER
6 from mypyc.ir.rtypes import bit_rprimitive, exc_rtuple, object_rprimitive, void_rtype
7 from mypyc.primitives.registry import custom_op, custom_primitive_op
8
9 # If the argument is a class, raise an instance of the class. Otherwise, assume
10 # that the argument is an exception object, and raise it.
11 raise_exception_op = custom_op(
12 arg_types=[object_rprimitive],
13 return_type=void_rtype,
14 c_function_name="CPy_Raise",
15 error_kind=ERR_ALWAYS,
16 )
17
18 # Raise StopIteration exception with the specified value (which can be NULL).
19 set_stop_iteration_value = custom_op(
20 arg_types=[object_rprimitive],
21 return_type=void_rtype,
22 c_function_name="CPyGen_SetStopIterationValue",
23 error_kind=ERR_ALWAYS,
24 )
25
26 # Raise exception with traceback.
27 # Arguments are (exception type, exception value, traceback).
28 raise_exception_with_tb_op = custom_op(
29 arg_types=[object_rprimitive, object_rprimitive, object_rprimitive],
30 return_type=void_rtype,
31 c_function_name="CPyErr_SetObjectAndTraceback",
32 error_kind=ERR_ALWAYS,
33 )
34
35 # Reraise the currently raised exception.
36 reraise_exception_op = custom_op(
37 arg_types=[], return_type=void_rtype, c_function_name="CPy_Reraise", error_kind=ERR_ALWAYS
38 )
39
40 # Propagate exception if the CPython error indicator is set (an exception was raised).
41 no_err_occurred_op = custom_op(
42 arg_types=[],
43 return_type=bit_rprimitive,
44 c_function_name="CPy_NoErrOccurred",
45 error_kind=ERR_FALSE,
46 )
47
48 err_occurred_op = custom_op(
49 arg_types=[],
50 return_type=object_rprimitive,
51 c_function_name="PyErr_Occurred",
52 error_kind=ERR_NEVER,
53 is_borrowed=True,
54 )
55
56 # Keep propagating a raised exception by unconditionally giving an error value.
57 # This doesn't actually raise an exception.
58 keep_propagating_op = custom_op(
59 arg_types=[],
60 return_type=bit_rprimitive,
61 c_function_name="CPy_KeepPropagating",
62 error_kind=ERR_FALSE,
63 )
64
65 # If argument is NULL, propagate currently raised exception (in this case
66 # an exception must have been raised). If this can be used, it's faster
67 # than using PyErr_Occurred().
68 propagate_if_error_op = custom_primitive_op(
69 "propagate_if_error",
70 arg_types=[object_rprimitive],
71 return_type=bit_rprimitive,
72 error_kind=ERR_FALSE,
73 )
74
75 # Catches a propagating exception and makes it the "currently
76 # handled exception" (by sticking it into sys.exc_info()). Returns the
77 # exception that was previously being handled, which must be restored
78 # later.
79 error_catch_op = custom_op(
80 arg_types=[], return_type=exc_rtuple, c_function_name="CPy_CatchError", error_kind=ERR_NEVER
81 )
82
83 # Restore an old "currently handled exception" returned from.
84 # error_catch (by sticking it into sys.exc_info())
85 restore_exc_info_op = custom_op(
86 arg_types=[exc_rtuple],
87 return_type=void_rtype,
88 c_function_name="CPy_RestoreExcInfo",
89 error_kind=ERR_NEVER,
90 )
91
92 # Checks whether the exception currently being handled matches a particular type.
93 exc_matches_op = custom_op(
94 arg_types=[object_rprimitive],
95 return_type=bit_rprimitive,
96 c_function_name="CPy_ExceptionMatches",
97 error_kind=ERR_NEVER,
98 )
99
100 # Get the value of the exception currently being handled.
101 get_exc_value_op = custom_op(
102 arg_types=[],
103 return_type=object_rprimitive,
104 c_function_name="CPy_GetExcValue",
105 error_kind=ERR_NEVER,
106 )
107
108 # Get exception info (exception type, exception instance, traceback object).
109 get_exc_info_op = custom_op(
110 arg_types=[], return_type=exc_rtuple, c_function_name="CPy_GetExcInfo", error_kind=ERR_NEVER
111 )