def try_statement_has_child_names_resolved(): _assert_children_resolved( lambda ref: nodes.try_([nodes.expression_statement(ref)]), ) _assert_children_resolved( lambda ref: nodes.try_([], handlers=[ nodes.except_(None, None, [nodes.expression_statement(ref)]) ]), ) _assert_children_resolved( lambda ref: nodes.try_([], handlers=[ nodes.except_(ref, None, []) ]), ) _assert_children_resolved( lambda ref: nodes.try_([], handlers=[ nodes.except_(nodes.ref("Exception"), ref, []) ]), other_names=["Exception"], ) _assert_children_resolved( lambda ref: nodes.try_([], finally_body=[ nodes.expression_statement(ref) ]), )
def exception_handler_targets_cannot_be_accessed_from_nested_function(): target_node = nodes.ref("error") ref_node = nodes.ref("error") body = [nodes.ret(ref_node)] func_node = nodes.func("f", nodes.arguments([]), body, type=None) try_node = nodes.try_( [], handlers=[ nodes.except_(nodes.none(), target_node, [func_node]) ], ) declaration = name_declaration.ExceptionHandlerTargetNode("error") references = References([ (target_node, declaration), (ref_node, declaration), (func_node, name_declaration.VariableDeclarationNode("f")), ]) try: _updated_bindings(try_node, references=references) assert False, "Expected error" except errors.UnboundLocalError as error: assert_equal(ref_node, error.node) assert_is("error", error.name)
def test_parse_try_finally(): expected = nodes.try_( [nodes.expression_statement(nodes.ref("x"))], finally_body=[nodes.expression_statement(nodes.ref("y"))], ) _assert_statement_parse(expected, "try:\n x\nfinally:\n y")
def continue_is_not_valid_in_try_finally_body(): continue_statement = nodes.continue_() node = nodes.try_([], finally_body=[continue_statement]) error = assert_raises(errors.TypeCheckError, lambda: check_loop_control(node, True)) assert_equal(continue_statement, error.node) assert_equal("'continue' not supported inside 'finally' clause", str(error))
def except_handler_target_is_defined_but_not_definitely_bound(): _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [], handlers=[ nodes.except_(nodes.none(), generate.target(), []) ], ) )
def except_handler_target_is_declared(): node = nodes.try_( [], handlers=[ nodes.except_(nodes.none(), nodes.ref("error"), []) ], ) declarations = find_declarations(node) assert_equal("error", declarations.declaration("error").name) assert isinstance(declarations.declaration("error"), name_declaration.VariableDeclarationNode)
def except_handler_targets_in_same_try_statement_can_share_their_name(): _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [], handlers=[ nodes.except_(nodes.none(), generate.target(), []), nodes.except_(nodes.none(), generate.target(), []), ], ) )
def test_parse_try_except_with_specific_type_and_identifier(): expected = nodes.try_( [nodes.expression_statement(nodes.ref("x"))], handlers=[ nodes.except_(nodes.ref("AssertionError"), "error", [ nodes.expression_statement(nodes.ref("y")) ]), ] ) _assert_statement_parse(expected, "try:\n x\nexcept AssertionError as error:\n y")
def target_of_exception_handler_is_removed_from_scope_after_exception_handler(): target_node = nodes.ref("error") node = nodes.try_( [], handlers=[ nodes.except_(nodes.none(), target_node, []) ], ) bindings = _updated_bindings(node, is_definitely_bound={"error": True}) assert not bindings.is_definitely_bound(target_node)
def test_parse_try_except_that_catches_all_exceptions(): expected = nodes.try_( [nodes.expression_statement(nodes.ref("x"))], handlers=[ nodes.except_(None, None, [ nodes.expression_statement(nodes.ref("y")) ]), ] ) _assert_statement_parse(expected, "try:\n x\nexcept:\n y")
def except_handler_type_must_be_exception_type(): type_node = nodes.ref("int") node = nodes.try_([], handlers=[ nodes.except_(type_node, None, []), ]) assert_type_mismatch( lambda: update_context(node, type_bindings={"int": types.int_meta_type}), expected="exception type", actual=types.int_meta_type, node=type_node, )
def declarations_in_body_and_handler_body_and_finally_body_of_try_statement_are_not_definitely_bound(): _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [generate.assignment()], handlers=[ nodes.except_(None, None, []) ], finally_body=[], ) ) _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [], handlers=[ nodes.except_(None, None, [generate.assignment()]) ], finally_body=[], ) ) _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [], handlers=[ nodes.except_(None, None, []) ], finally_body=[generate.assignment()], ) ) _assert_name_is_not_definitely_bound(lambda generate: nodes.try_( [generate.assignment()], handlers=[ nodes.except_(None, None, [generate.assignment()]) ], finally_body=[generate.assignment()], ) )
def test_statements_in_bodies_are_transformed(self): _assert_transform( nodes.try_( [nodes.ret(nodes.ref("x"))], handlers=[nodes.except_(nodes.ref("Exception"), nodes.ref("error"), [nodes.ref("y")])], finally_body=[nodes.ret(nodes.ref("z"))], ), cc.try_( [cc.ret(cc.ref("x"))], handlers=[cc.except_(cc.ref("Exception"), cc.ref("error"), [cc.ref("y")])], finally_body=[cc.ret(cc.ref("z"))], ), )
def except_handler_updates_type_of_error_target(): node = nodes.try_([], handlers=[ nodes.except_( nodes.ref("Exception"), "error", [nodes.expression_statement(nodes.ref("error"))] ), ]) type_bindings = { "Exception": types.meta_type(types.exception_type) } context = update_context(node, type_bindings=type_bindings) assert_equal(types.exception_type, context.lookup_name("error"))
def children_of_try_statement_are_checked(): _assert_child_statement_is_checked(lambda generate: nodes.try_([generate.unbound_ref_statement()]), ) _assert_child_statement_is_checked(lambda generate: nodes.try_([], handlers=[ nodes.except_(None, None, [generate.unbound_ref_statement()]) ]), ) _assert_child_expression_is_checked(lambda generate: nodes.try_([], handlers=[ nodes.except_(generate.unbound_ref(), None, []) ]), ) _assert_child_expression_is_checked(lambda generate: nodes.try_([], handlers=[ nodes.except_(nodes.none(), nodes.attr(generate.unbound_ref(), "blah"), []) ]), ) _assert_child_statement_is_checked(lambda generate: nodes.try_([], finally_body=[ generate.unbound_ref_statement() ]), )
def continue_is_valid_in_try_body(): node = nodes.try_([nodes.continue_()]) check_loop_control(node, True)
def break_is_valid_in_try_finally_body(): node = nodes.try_([], finally_body=[nodes.break_()]) check_loop_control(node, True)
def try_except_handler_body_is_type_checked(): assert_statement_is_type_checked( lambda bad_statement: nodes.try_([], handlers=[ nodes.except_(None, None, [bad_statement]), ]) )
def try_finally_body_is_type_checked(): assert_statement_is_type_checked( lambda bad_statement: nodes.try_([], finally_body=[bad_statement]) )