def test_attribute_error():
    class A:
        pass

    try:
        A.x  # testing type
    except Exception as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()
    assert not "debug_warning" in result, "Internal error found."
    assert "AttributeError: type object 'A' has no attribute 'x'" in result
    if friendly_traceback.get_lang() == "en":
        assert "The object of type `A` has no attribute" in result

    try:
        a = A()
        a.x  # Testing instance
    except Exception as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()
    assert not "debug_warning" in result, "Internal error found."
    assert "AttributeError: 'A' object has no attribute 'x'" in result
    if friendly_traceback.get_lang() == "en":
        assert "The object `a` has no attribute" in result
    return result, message
 def set_lang():
     """Sets the language used by Friendly-traceback to be also
        used by the custom exceptions."""
     # Only required for exceptions that are translated like is
     # the case for this custom example
     lang = friendly_traceback.get_lang()
     demo_lang.install(lang=lang)
示例#3
0
def test_type_error15a():
    try:
        _ = [1, 2](3, 4)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'list' object is not callable" in result
    if friendly_traceback.get_lang() == "en":
        assert "I suspect that you had an object of this type, a `list`," in result
    return result
示例#4
0
def test_type_error16():
    try:
        raise "exception"
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: exceptions must derive from BaseException" in result
    if friendly_traceback.get_lang() == "en":
        assert "In Python 3, exceptions must be derived from BaseException." in result
    return result
def test_module_not_found_error():
    try:
        import does_not_exist
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ModuleNotFoundError: No module named 'does_not_exist'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "module that cannot be found is 'does_not_exist'." in result
    return result
示例#6
0
def test_overflow_error():
    try:
        2. ** 1600
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "OverflowError: (34, 'Result too large')" in result
    if friendly_traceback.get_lang() == 'en':
        assert "OverflowError is raised when the result" in result
    return result
def test_type_error15():
    try:
        _ = (1, 2)(3, 4)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'tuple' object is not callable" in result
    if friendly_traceback.get_lang() == 'en':
        assert "I suspect that you had an object of this type, <a tuple>," in result
    return result
def test_zero_division_error():
    try:
        1 / 0
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ZeroDivisionError: division by zero" in result
    if friendly_traceback.get_lang() == "en":
        assert "A ZeroDivisionError occurs when" in result
    return result
def test_unknown_error():
    try:
        raise MyException("Some informative message about an unknown exception.")
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "Some informative message" in result
    if friendly_traceback.get_lang() == "en":
        assert "Please report this example" in result
    return result
def test_import_error():
    try:
        from math import Pi
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ImportError: cannot import name 'Pi'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "The object that could not be imported is 'Pi'." in result
    return result
示例#11
0
def test_name_error():
    try:
        b = c
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "NameError: name 'c' is not defined" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In your program, the unknown name is 'c'." in result
    return result
def test_type_error11a():
    try:
        a = - [1, 2, 3]
        print(a)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: bad operand type for unary -: 'list'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "You tried to use the unary operator '-'" in result
    return result
def test_zero_division_error2():
    zero = 0
    try:
        1 % zero
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ZeroDivisionError: integer division or modulo by zero" in result
    if friendly_traceback.get_lang() == "en":
        assert "A ZeroDivisionError occurs when" in result
    return result
def test_overflow_error():
    try:
        2.0**1600
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert ("OverflowError: (34, 'Result too large')" in result or
            "OverflowError: (34, 'Numerical result out of range')" in result)
    if friendly_traceback.get_lang() == "en":
        assert "OverflowError is raised when the result" in result
    return result
示例#15
0
def test_type_error12():
    a = (1, 2, 3)
    try:
        a[0] = 0
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'tuple' object does not support item assignment" in result
    if friendly_traceback.get_lang() == "en":
        assert "In Python, some objects are known as immutable:" in result
    return result
def test_key_error():
    d = {'a': 1, 'b': 2}
    try:
        d['c']
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "KeyError: 'c'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "that cannot be found is 'c'." in result
    return result
示例#17
0
def test_type_error11b():
    try:
        a = ~(1, 2, 3)
        print(a)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: bad operand type for unary ~: 'tuple'" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to use the unary operator '~'" in result
    return result
示例#18
0
def test_type_error9a():
    try:
        a = "a"
        b = 2
        a @= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for @=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to use the operator @" in result
    return result
示例#19
0
def test_type_error5c():
    try:
        a = {1: 1, 2: 2}
        b = 3.1416
        b //= a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for //=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a dictionary (`dict`)"
    return result
def test_zero_division_error():
    try:
        1 / 0
    except Exception as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()
    assert not "debug_warning" in result, "Internal error found."
    assert "ZeroDivisionError: division by zero" in result
    if friendly_traceback.get_lang() == "en":
        assert "A `ZeroDivisionError` occurs when" in result
    return result, message
示例#21
0
def test_type_error3a():
    try:
        a = (1, 2)
        b = [3, 4]
        b -= a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for -=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `list` and a `tuple`" in result
    return result
示例#22
0
def test_type_error2a():
    try:
        one = 1
        two = "two"
        one += two
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for +=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a string (`str`)" in result
    return result
示例#23
0
def test_type_error2():
    try:
        one = 1
        none = None
        result = one + none
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for +:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a variable equal to None (`NoneType`)" in result
    return result
示例#24
0
def test_type_error6():
    try:
        a = "a"
        b = 2
        result = a & b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for &:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a string (`str`) and an integer (`int`)" in result
    return result
示例#25
0
def test_type_error7a():
    try:
        a = {1: 1, 2: 2}
        b = 3.1416
        a **= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for ** or pow():" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to exponentiate (raise to a power)" in result
    return result
示例#26
0
def test_type_error1b():
    try:
        a_tuple = (1, 2, 3)
        a_list = [1, 2, 3]
        result = a_tuple + a_list
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: can only concatenate" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `tuple` and a `list`" in result
    return result
示例#27
0
def test_type_error8a():
    try:
        a = "a"
        b = 42
        a >>= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for >>=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to perform the bit shifting operation >>" in result
    return result
def test_index_error2():
    a = list(range(40))
    b = tuple(range(50))
    try:
        print(a[50], b[0])
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "IndexError: list index out of range" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In this case, the sequence is a list." in result
    return result
def test_index_error1():
    a = (1, 2, 3)
    b = [1, 2, 3]
    try:
        print(a[3], b[2])
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "IndexError: tuple index out of range" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In this case, the sequence is a tuple." in result
    return result
示例#30
0
def test_type_error10():
    try:
        a = "a"
        b = 42
        b < a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: '<' not supported between instances of 'int' and 'str'" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to do an order comparison (<)" in result
    return result