Exemplo n.º 1
0
def test_Float_modulo():
    zero = 0.
    a = 10**1000  # for coverage: tests format_var_info for long variables

    try:
        a % 1 % zero
    except ZeroDivisionError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ZeroDivisionError: float modulo" in result
    if friendly.get_lang() == "en":
        assert "The following mathematical expression includes a division by zero" in result
        assert "done using the modulo operator" in result

    try:
        1 % zero
    except ZeroDivisionError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ZeroDivisionError: float modulo" in result
    if friendly.get_lang() == "en":
        assert "Using the modulo operator" in result
    return result, message
Exemplo n.º 2
0
def test_Object_is_not_subscriptable():
    try:
        a = 2 [1]
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'int' object is not subscriptable" in result
    if friendly.get_lang() == "en":
        assert "from `2`, an object of type `int`" in result

    def f():
        pass

    try:
        a = f[1]
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'function' object is not subscriptable" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `f(1)`" in result
    return result, message
Exemplo n.º 3
0
def test_Not_a_package():

    try:
        import os.xxx
    except ModuleNotFoundError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ModuleNotFoundError: No module named 'os.xxx'" in result
    if friendly.get_lang() == "en":
        assert "`xxx` cannot be imported" in result

    try:
        import os.open
    except ModuleNotFoundError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ModuleNotFoundError: No module named 'os.open'" in result
    if friendly.get_lang() == "en":
        assert "`from os import open`" in result

    try:
        import os.pathh
    except ModuleNotFoundError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ModuleNotFoundError: No module named 'os.pathh'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `import os.path`" in result
    return result, message
Exemplo n.º 4
0
def test_Typo_in_local():
    def test1():
        alpha1 = 1
        alpha2 += 1

    try:
        test1()
    except UnboundLocalError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "local variable 'alpha2' referenced before assignment" in result
    if friendly.get_lang() == "en":
        assert "similar name `alpha1` was found" in result

    def test2():
        alpha1 = 1
        alpha2 = 1
        alpha3 += 1

    try:
        test2()
    except UnboundLocalError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "local variable 'alpha3' referenced before assignment" in result
    if friendly.get_lang() == "en":
        assert "perhaps you meant one of the following" in result

    return result, message
Exemplo n.º 5
0
def test_Similar_names():
    first = {"alpha": 1, "beta": 2, "gamma": 3}
    try:
        a = first["alpha1"]
    except KeyError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "KeyError: 'alpha1'" in result
    if friendly.get_lang() == "en":
        expected = "Did you mean `'alpha'`?"
        ok, diff = expected_in_result(expected, result)
        assert ok, diff

    second = {"alpha0": 1, "alpha11": 2, "alpha12": 3}
    try:
        a = second["alpha"]
    except KeyError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "KeyError: 'alpha'" in result
    if friendly.get_lang() == "en":
        expected = "Did you mean `'alpha0'`?"
        ok, diff = expected_in_result(expected, result)
        assert ok, diff
        expected = "'alpha0', 'alpha12', 'alpha11'"
        ok, diff = expected_in_result(expected, result)
        assert ok, diff

    return result, message
Exemplo n.º 6
0
def test_Attribute_from_other_module():
    import math
    import keyword

    try:
        keyword.pi
    except AttributeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")

    result = friendly.get_output()
    assert "module 'keyword' has no attribute 'pi'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `math`?" in result

    import cmath
    try:
        keyword.pi
    except AttributeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")

    result = friendly.get_output()
    if friendly.get_lang() == "en":
        assert "Did you mean one of the following modules:" in result

    return result, message
Exemplo n.º 7
0
def test_Too_many_positional_argument():
    def fn(*, b=1):
        pass

    try:
        fn(1)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError" in result
    assert "fn() takes 0 positional arguments but 1 was given" in result
    if friendly.get_lang() == "en":
        assert "1 positional argument(s) while it requires 0" in result

    class A:
        def f(x):
            pass

    try:
        A().f(1)
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError" in result
    assert "f() takes 1 positional argument but 2 were given" in result
    if friendly.get_lang() == "en":
        assert "2 positional argument(s) while it requires 1" in result
        # assert "Perhaps you forgot `self`" in result
    return result, message
Exemplo n.º 8
0
def test_Module_attribute_typo():
    import string

    try:
        string.ascii_lowecase
    except AttributeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "AttributeError: module 'string' has no attribute 'ascii_lowecase'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `ascii_lowercase`" in result

    import math

    try:
        math.cost
    except AttributeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "AttributeError: module 'math' has no attribute 'cost'" in result
    if friendly.get_lang() == "en":
        assert (
            "Instead of writing `math.cost`, perhaps you meant to write one of"
            in result)
    assert "cos, cosh" in result
    assert not "acosh" in result
    return result, message
Exemplo n.º 9
0
def test_Not_enough_values_to_unpack():
    d = (1, )
    try:
        a, b, *c = d
    except ValueError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert (
        "ValueError: not enough values to unpack (expected at least 2, got 1)"
        in result)
    if friendly.get_lang() == "en":
        assert "a `tuple` of length 1" in result

    try:
        for x, y, z in enumerate(range(3)):
            pass
    except ValueError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "ValueError: not enough values to unpack (expected 3, got 2)" in result

    d = "ab"
    try:
        a, b, c = d
    except ValueError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "ValueError: not enough values to unpack (expected 3, got 2)" in result
    if friendly.get_lang() == "en":
        assert "a string (`str`) of length 2" in result
    return result, message
Exemplo n.º 10
0
def test_Indices_must_be_integers_or_slices():
    a = [1, 2, 3]

    try:
        a[1, 2]
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: list indices must be integers or slices" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `a[1:2]`" in result

    a = (1, 2, 3)
    try:
        a[2.0]
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: tuple indices must be integers or slices" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you forgot to convert `2.0` into an integer." in result

    try:
        [1, 2, 3]["2"]
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: list indices must be integers or slices" in result
    if friendly.get_lang() == "en":
        assert 'Perhaps you forgot to convert `"2"` into an integer.' in result
    return result, message
Exemplo n.º 11
0
def test_Generic():
    # Generic - no additional explanation
    class A:
        pass

    try:
        A.x  # testing type
    except AttributeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "AttributeError: type object 'A' has no attribute 'x'" in result
    if friendly.get_lang() == "en":
        assert "The object `A` has no attribute" in result

    try:
        a = A()
        a.x  # Testing instance
    except AttributeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "AttributeError: 'A' object has no attribute 'x'" in result
    if friendly.get_lang() == "en":
        assert "The object `a` has no attribute" in result
    return result, message
Exemplo n.º 12
0
def test_Cannot_multiply_by_non_int():

    try:
        "b" * "a"
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly.get_lang() == "en":
        assert "You can only multiply sequences, such as" in result

    try:
        "3" * "a"
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly.get_lang() == "en":
        assert 'Did you forget to convert `"3"` into an integer?' in result

    a = b = c = "2"
    try:
        d = a * b * c
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly.get_lang() == "en":
        assert "Did you forget to convert `a` and `b` into integers?" in result

    a = "abc"
    try:
        a *= c
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly.get_lang() == "en":
        assert "Did you forget to convert `c` into an integer?" in result

    try:
        "a" * "2"
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly.get_lang() == "en":
        assert 'Did you forget to convert `"2"` into an integer?' in result
    return result, message
Exemplo n.º 13
0
def test_Not_callable():
    try:
        _ = (1, 2)(3, 4)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'tuple' object is not callable" in result
    if friendly.get_lang() == "en":
        assert "you have a missing comma between the object" in result

    try:
        _ = 3 (4 + 4)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'int' object is not callable" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you forgot a multiplication operator" in result

    try:
        _ = [1, 2](3, 4)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "TypeError: 'list' object is not callable" in result
    if friendly.get_lang() == "en":
        assert "you have a missing comma between the object" in result

    # Test with dotted name
    class A:
        a_list = [1, 2, 3]

    try:
        b = A()
        b.a_list(3)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "TypeError: 'list' object is not callable" in result
    if friendly.get_lang() == "en":
        assert "b.a_list[3]" in result

    try:
        a, b = 3, 7
        _ = [1, 2](a + b)
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "TypeError: 'list' object is not callable" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you meant to use `[]` instead of `()`" in result
    return result, message
Exemplo n.º 14
0
def test_Bad_type_for_unary_operator():
    try:
        a = + "abc"
        print(a)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: bad operand type for unary +: 'str'" in result
    assert not "+=" in result
    if friendly.get_lang() == "en":
        assert "You tried to use the unary operator '+'" in result

    try:
        a = - [1, 2, 3]
        print(a)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: bad operand type for unary -: 'list'" in result
    if friendly.get_lang() == "en":
        assert "You tried to use the unary operator '-'" in result

    try:
        a = ~(1, 2, 3)
        print(a)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: bad operand type for unary ~: 'tuple'" in result
    if friendly.get_lang() == "en":
        assert "You tried to use the unary operator '~'" in result

    try:
        # fmt: off
        a = "abc"
        a = + "def"
        # fmt: on
        print(a)
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: bad operand type for unary +: 'str'" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you meant to write `+=`" in result
        assert "You tried to use the unary operator '+'" in result
    return result, message
Exemplo n.º 15
0
def no_suggestion():
    try:
        from math import alphabet_alphabet
    except ImportError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "ImportError: cannot import name 'alphabet_alphabet'" in result
    if friendly.get_lang() == "en":
        assert "could not be imported is `alphabet_alphabet`" in result
Exemplo n.º 16
0
def multiple_choices():
    try:
        from math import bsin
    except ImportError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "ImportError: cannot import name 'bsin'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean one of the following: `sin" in result
Exemplo n.º 17
0
def multiple_import_on_same_line():
    try:
        import circular_a, circular_b
    except ImportError:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "cannot import name 'a'" in result
    if friendly.get_lang() == "en":
        assert "likely caused by what is known as a 'circular import'." in result
Exemplo n.º 18
0
def test_Not_an_integer():
    try:
        range([1, 2])
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'list' object cannot be interpreted as an integer" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you forgot to convert " not in result

    try:
        range("2")
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'str' object cannot be interpreted as an integer" in result
    if friendly.get_lang() == "en":
        assert 'Perhaps you forgot to convert `"2"` into an integer.' in result

    try:
        range(1.0)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'float' object cannot be interpreted as an integer" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you forgot to convert `1.0" in result

    c, d = "2", "3"
    try:
        range(c, d)
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: 'str' object cannot be interpreted as an integer" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you forgot to convert `c, d` into integers." in result
    return result, message
Exemplo n.º 19
0
def test_Can_only_concatenate():
    try:
        a = "2"
        one = 1
        result = a + one
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    py37 = "TypeError: can only concatenate" in result
    py36 = "must be str, not int" in result
    assert py37 or py36
    if friendly.get_lang() == "en":
        assert "a string (`str`) and an integer (`int`)" in result
        assert "Perhaps you forgot to convert the string" in result

    try:
        a = "a"
        a_list = [1, 2, 3]
        result = a + a_list
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    py37 = "TypeError: can only concatenate" in result
    py36 = "must be str, not list" in result
    assert py37 or py36
    if friendly.get_lang() == "en":
        assert "a string (`str`) and a `list`" in result

    try:
        a_tuple = (1, 2, 3)
        a_list = [1, 2, 3]
        result = a_tuple + a_list
    except TypeError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "TypeError: can only concatenate" in result
    if friendly.get_lang() == "en":
        assert "a `tuple` and a `list`" in result
    return result, message
Exemplo n.º 20
0
def back():
    """Removes the last recorded traceback item.

    The intention is to allow recovering from a typo when trying interactively
    to find out specific information about a given exception.
    """
    if not session.saved_info:
        session.write_err(
            _("Nothing to go back to: no exception recorded.") + "\n")
        return
    if not session.friendly:  # pragma: no cover
        debug_helper.log("Problem: saved info is not empty but friendly is")
    session.saved_info.pop()
    session.friendly.pop()
    if session.saved_info:
        info = session.saved_info[-1]
        if info["lang"] != friendly.get_lang():
            info["lang"] = friendly.get_lang()
            session.friendly[-1].recompile_info()
Exemplo n.º 21
0
def test_Mixed_operations():
    try:
        a = divmod(8, 1 // 2)
    except ZeroDivisionError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    assert "ZeroDivisionError: integer division or modulo by zero" in result
    if friendly.get_lang() == "en":
        assert "The following mathematical expression includes a division by zero" in result
    return result, message
def test_Standard_library_module():
    try:
        import Tkinter
    except ModuleNotFoundError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    
    assert "No module named 'Tkinter'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `tkinter`" in result
    return result, message
def test_Need_to_install_module():
    try:
        import alphabet
    except ModuleNotFoundError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "No module named 'alphabet'" in result
    if friendly.get_lang() == "en":
        assert "Perhaps you need to install it" in result
    return result, message
Exemplo n.º 24
0
def test_Simple_import_error():
    try:
        from math import Pi
    except ImportError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    
    assert "ImportError: cannot import name 'Pi'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `pi`" in result
    return result, message
def test_Not_a_package_similar_name():
    try:
        import os.pathh
    except ModuleNotFoundError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    
    assert "No module named 'os.pathh'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `import os.path`" in result
    return result, message
Exemplo n.º 26
0
def test_Object_is_not_iterable():
    try:
        list(42)
    except TypeError as e:
        friendly.explain_traceback(redirect="capture")
        message = str(e)
    result = friendly.get_output()

    assert "TypeError: 'int' object is not iterable" in result
    if friendly.get_lang() == "en":
        assert "An iterable is required here." in result
    return result, message
def test_Object_not_module():
    try:
        import os.open
    except ModuleNotFoundError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()

    assert "No module named 'os.open'" in result
    if friendly.get_lang() == "en":
        assert "Did you mean `from os import open`?" in result
    return result, message
Exemplo n.º 28
0
def test_Annotated_variable():
    try:
        y = x
    except NameError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    
    assert "NameError: name 'x' is not defined" in result
    if friendly.get_lang() == "en":
        assert "x = 3" in result
    return result, message
    def test_no_curses():
        try:
            import curses
        except ModuleNotFoundError as e:
            message = str(e)
            friendly.explain_traceback(redirect="capture")
        result = friendly.get_output()
        assert "No module named '_curses'" in result
        if friendly.get_lang() == "en":
            assert "The curses module is rarely installed with Python on Windows." in result

        return result, message
Exemplo n.º 30
0
def test_Generic():
    try:
        this = something
    except NameError as e:
        message = str(e)
        friendly.explain_traceback(redirect="capture")
    result = friendly.get_output()
    
    assert "NameError: name 'something' is not defined" in result
    if friendly.get_lang() == "en":
        assert "In your program, `something` is an unknown name." in result
    return result, message