Ejemplo n.º 1
0
def create_tracebacks(target, intro_text, format="pre", messages=None):
    with open(target, "w", encoding="utf8") as out:
        with redirect_stderr(out):
            write(intro_text)

            for title in all_imports:
                function = None
                name, function = all_imports[title]
                make_title(title, format=format)
                try:
                    mod = __import__(name)
                    if function is not None:
                        result, message = getattr(mod, function)()
                        save_messages[function] = message
                        write(result)
                        if "debug_warning" in result:
                            print("debug_warning in ", name, function)
                except Exception as e:
                    friendly_traceback.explain_traceback()

    if messages:
        with open(messages, "w", encoding="utf8") as out:
            out.write("messages = {\n")
            for key in save_messages:
                out.write(f"    {repr(key)}: {repr(save_messages[key])},\n")
            out.write("}\n")
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
Ejemplo n.º 3
0
    def runcode(self, code):
        """Execute a code object.

        When an exception occurs, friendly_traceback.explain_traceback() is called to
        display a traceback.  All exceptions are caught except
        SystemExit, which, unlike the case for the original version in the
        standard library, cleanly exists the program. This is done
        so as to avoid our Friendly-traceback's exception hook to intercept
        it and confuse the users.

        A note about KeyboardInterrupt: this exception may occur
        elsewhere in this code, and may not always be caught.  The
        caller should be prepared to deal with it.
        """
        _ = current_lang.translate
        try:
            exec(code, self.locals)
        except SystemExit:
            os._exit(1)  # noqa -pycharm
        except Exception:
            try:
                friendly_traceback.explain_traceback()
            except Exception:
                print("Friendly-traceback Internal Error")
                print("-" * 60)
                traceback.print_exc()
                print("-" * 60)

        self.check_for_builtins_changes()
        self.check_for_annotations()
        self.old_locals = copy.copy(self.locals)
Ejemplo n.º 4
0
def test_type_error15a():
    try:
        _ = [1, 2](3, 4)
    except Exception as e:
        message = str(e)
        friendly_traceback.explain_traceback(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, message
def test_index_error1():
    a = (1, 2, 3)
    b = [1, 2, 3]
    try:
        print(a[3], b[2])
    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 "IndexError: tuple index out of range" in result
    return result, message
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
Ejemplo n.º 7
0
def test_type_error15():
    try:
        _ = (1, 2)(3, 4)
    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 "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, message
def test_import_error():
    try:
        from math import Pi
    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 "ImportError: cannot import name 'Pi'" in result
    if friendly_traceback.get_lang() == "en":
        assert "Did you mean `pi`" in result
    return result, message
Ejemplo n.º 9
0
def test_flush():
    try:
        b = c
    except Exception:
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output(flush=False)
    assert "NameError: name 'c' is not defined" in result
    result1 = friendly_traceback.get_output()  # flushes
    assert "NameError: name 'c' is not defined" in result1
    result2 = friendly_traceback.get_output()  # returns empty string
    assert not result2
    return result, result2
def test_name_error3():
    try:
        y = x
    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 "NameError: name 'x' is not defined" in result
    if friendly_traceback.get_lang() == "en":
        assert "x = 3" in result
    return result, message
Ejemplo n.º 11
0
def test_type_error16():
    try:
        raise "exception"
    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 "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, message
def test_index_error2():
    a = list(range(40))
    b = tuple(range(50))
    try:
        print(a[50], b[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 "IndexError: list index out of range" in result
    return result, message
def test_name_error():
    try:
        this = something
    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 "NameError: name 'something' is not defined" in result
    if friendly_traceback.get_lang() == "en":
        assert "In your program, `something` is an unknown name." in result
    return result, message
def test_name_error4():
    try:
        cost  # wrote from math import * above
    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 "NameError: name 'cost' is not defined" in result
    if friendly_traceback.get_lang() == "en":
        assert "perhaps you meant one of the following" in result
    return result, message
def test_module_not_found_error():
    try:
        import Tkinter
    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 "ModuleNotFoundError: No module named 'Tkinter'" in result
    if friendly_traceback.get_lang() == "en":
        assert "Did you mean `tkinter`" in result
    return result, message
def test_function_unknown_error():
    try:
        raise MyException(
            "Some informative message about an unknown exception.")
    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 "Some informative message" in result
    if friendly_traceback.get_lang() == "en":
        assert "No information is available about this exception." in result
    return result, message
Ejemplo n.º 17
0
def test_type_error12():
    a = (1, 2, 3)
    try:
        a[0] = 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 "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, message
def test_name_error2():
    nabs = 1
    try:
        x = babs(-1)
    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 "NameError: name 'babs' is not defined" in result
    if friendly_traceback.get_lang() == "en":
        assert "perhaps you meant one of the following" in result
    return result, message
Ejemplo n.º 19
0
def test_type_error11b():
    try:
        a = ~(1, 2, 3)
        print(a)
    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 "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, message
def test_key_error():
    d = {"a": 1, "b": 2}
    try:
        d["c"]
    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 "KeyError: 'c'" in result
    if friendly_traceback.get_lang() == "en":
        assert "that cannot be found is `'c'`." in result
    return result, message
def test_attribute_error2():
    try:
        a = [1, 2, 3]
        a.appendh(4)
    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: 'list' object has no attribute 'appendh'" in result
    if friendly_traceback.get_lang() == "en":
        assert "Did you mean `append`" in result
    return result, message
Ejemplo n.º 22
0
def test_type_error1b():
    try:
        a_tuple = (1, 2, 3)
        a_list = [1, 2, 3]
        result = a_tuple + a_list
    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 "TypeError: can only concatenate" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `tuple` and a `list`" in result
    return result, message
def test_misspelled_module_attribute():
    import string

    try:
        string.ascii_lowecase
    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: module 'string' has no attribute 'ascii_lowecase'" in result
    if friendly_traceback.get_lang() == "en":
        assert "Did you mean `ascii_lowercase`" in result
    return result, message
def test_nonetype():
    a = None
    try:
        a.b
    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 "'NoneType' object has no attribute 'b'" in result
    if friendly_traceback.get_lang() == "en":
        assert "for a variable whose value is `None`" in result

    return result, message
Ejemplo n.º 25
0
def test_type_error9a():
    try:
        a = "a"
        b = 2
        a @= b
    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 "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, message
Ejemplo n.º 26
0
def test_type_error17():
    try:
        "a" * "2"
    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 "TypeError: can't multiply sequence by non-int of type 'str'" in result
    if friendly_traceback.get_lang() == "en":
        assert (
            "Perhaps you forgot to convert a string into an integer using `int()`."
            in result
        )
Ejemplo n.º 27
0
def test_type_error2a():
    try:
        one = 1
        two = "two"
        one += two
    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 "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, message
Ejemplo n.º 28
0
def test_type_error3():
    try:
        a = (1, 2)
        b = [3, 4]
        result = a - b
    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 "TypeError: unsupported operand type(s) for -:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `tuple` and a `list`" in result
    return result, message
Ejemplo n.º 29
0
def test_type_error10():
    try:
        a = "a"
        b = 42
        b < a
    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 "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, message
def test_circular_import():
    try:
        import circular_a
    except Exception as e:
        message = str(e)
        friendly_traceback.explain_traceback(redirect="capture")
    result = friendly_traceback.get_output()
    # The actual message varies a lot depending on Python version.
    assert not "debug_warning" in result, "Internal error found."
    assert "ImportError" in result
    if friendly_traceback.get_lang() == "en":
        assert "what is known as a 'circular import'" in result

    return result, message