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)
        ]),
    )
Example #2
0
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)
Example #3
0
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")
Example #4
0
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))
Example #5
0
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)
Example #7
0
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(), []),
            ],
        )
    )
Example #8
0
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")
Example #9
0
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)
Example #10
0
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")
Example #11
0
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,
    )
Example #12
0
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()],
        )
    )
Example #13
0
 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"))],
         ),
     )
Example #14
0
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"))
Example #15
0
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()
        ]),
    )
Example #16
0
def continue_is_valid_in_try_body():
    node = nodes.try_([nodes.continue_()])
    check_loop_control(node, True)
Example #17
0
def break_is_valid_in_try_finally_body():
    node = nodes.try_([], finally_body=[nodes.break_()])
    check_loop_control(node, True)
Example #18
0
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]),
        ])
    )
Example #19
0
def try_finally_body_is_type_checked():
    assert_statement_is_type_checked(
        lambda bad_statement: nodes.try_([], finally_body=[bad_statement])
    )