Hi, welcome to my blog post #5!
Today we will see how are exception messages are displayed while working with Reformulations in Nuitka. The following is an example try block which is trying to assign iter()
to the arguments if supported else throw an type error.
makeTryExceptSingleHandlerNode(
tried=makeStatementsSequence(
statements=[
StatementAssignmentVariable(
variable=zip_iter_variable,
source=ExpressionBuiltinIter1(
value=ExpressionTempVariableRef(
variable=zip_arg_variable, source_ref=source_ref
),
source_ref=source_ref,
),
source_ref=source_ref,
)
for i, zip_iter_variable, zip_arg_variable in zip(
range(len(call_args)), zip_iter_variables, zip_arg_variables
)
],
allow_none=False,
source_ref=source_ref,
),
exception_name="TypeError",
handler_body=StatementRaiseException(
exception_type=ExpressionBuiltinExceptionRef(
exception_name="TypeError", source_ref=source_ref
),
exception_value=makeConstantRefNode(
constant="zip argument #%d must support iteration" % (i + 1),
source_ref=source_ref,
),
exception_trace=None,
exception_cause=None,
source_ref=source_ref,
),
source_ref=source_ref,
)
tried=makeStatementsSequence(
statements=[
StatementAssignmentVariable(
variable=zip_iter_variable,
source=ExpressionBuiltinIter1(
value=ExpressionTempVariableRef(
variable=zip_arg_variable, source_ref=source_ref
),
source_ref=source_ref,
),
source_ref=source_ref,
)
for i, zip_iter_variable, zip_arg_variable in zip(
range(len(call_args)), zip_iter_variables, zip_arg_variables
)
],
allow_none=False,
source_ref=source_ref,
),
exception_name="TypeError",
handler_body=StatementRaiseException(
exception_type=ExpressionBuiltinExceptionRef(
exception_name="TypeError", source_ref=source_ref
),
exception_value=makeConstantRefNode(
constant="zip argument #%d must support iteration" % (i + 1),
source_ref=source_ref,
),
exception_trace=None,
exception_cause=None,
source_ref=source_ref,
),
source_ref=source_ref,
)
You can see there that I'm calling create a StatementRaiseException
with a template i.e "zip argument #%d must support iteration"
in our case, which is basically to handle cases like for example: zip(1, [1,2,3]).
Thanks for reading