Exemple #1
0
def test_try_except_else_finally():
    stmt = try_(
        var.x.store(1)
    ).except_(var.TypeError)[
        var.y.store(2)
    ].except_(var.ValueError)[
        var.y.store(3)
    ].except_(var.OSError, var.RuntimeError, as_='foo')[
        pass_,
    ].else_(
        var.x.iadd(1)
    ).finally_(
        var.print(1, 2, 3)
    )
    expected = """\
try:
    x = 1
except TypeError:
    y = 2
except ValueError:
    y = 3
except (OSError, RuntimeError) as foo:
    pass
else:
    x += 1
finally:
    print(1, 2, 3)"""
    result = sourcify(stmt)
    assert result == expected
Exemple #2
0
def test_try_except():
    stmt = try_(
        var.print(2)
    ).except_(var.TypeError)[
        var.print(1)
    ]
    result = sourcify(stmt)
    expected = """\
try:
    print(2)
except TypeError:
    print(1)"""
    assert result == expected
Exemple #3
0
def test_try_finally():
    stmt = try_(
        var.print(2)
    ).finally_(
        var.print(1)
    )
    result = sourcify(stmt)
    expected = """\
try:
    print(2)
finally:
    print(1)"""
    assert result == expected
Exemple #4
0
def test_try_except_finally():
    stmt = try_(
        var.x.store(1)
    ).except_(var.TypeError)[
        var.y.store(2)
    ].finally_(
        return_(3)
    )
    result = sourcify(stmt)
    expected = """\
try:
    x = 1
except TypeError:
    y = 2
finally:
    return 3"""
    assert result == expected