Example #1
0
def test_run_code():
    # set-up
    bad_code_syntax = "True = 1"
    bad_code_exec = "a = b"  # Not a syntax error, but a NameError
    good_code = "c = 1"

    friendly.set_stream("capture")
    original_level = friendly.get_level()
    installed = friendly.is_installed()
    # ----- end of set-up

    # When a SyntaxError is raised, run_code returns False

    assert not friendly.run_code(source=bad_code_syntax)
    result = friendly.get_output()  # content is flushed
    assert "Python exception" in result
    assert "SyntaxError" in result

    assert not friendly.get_output()  # confirm that content was flushed

    assert not friendly.run_code(source=bad_code_exec)
    result = friendly.get_output()
    assert "Python exception" in result
    assert "NameError" in result

    assert friendly.run_code(source=good_code)
    assert not friendly.get_output()  # no new exceptions recorded

    try:
        exec(bad_code_syntax, {})
    except Exception:
        assert not friendly.get_output()

    # When friendly-traceback is not installed, a call to run_code
    # will end with level set to 0, which corresponds to normal Python
    # tracebacks
    friendly.uninstall()
    friendly.run_code(source=bad_code_syntax)
    assert friendly.get_level() == 0
    friendly.run_code(source=bad_code_syntax, level=4)
    assert friendly.get_level() == 0

    # When friendly-traceback is "installed", a call to run_code
    # leaves its level unchanged.
    friendly.install()

    friendly.set_level(3)
    friendly.run_code(source=bad_code_syntax)
    assert friendly.get_level() == 3
    friendly.run_code(source=bad_code_syntax, level=4)
    assert friendly.get_level() == 3

    # Clean up and restore for other tests
    friendly.get_output()
    friendly.set_stream(None)
    if installed:
        friendly.uninstall()
    friendly.set_level(original_level)
Example #2
0
def start_console(
    local_vars=None,
    use_rich=False,
    include="friendly_tb",
    lang="en",
    banner=None,
    theme="dark",
):
    """Starts a console; modified from code.interact"""
    from . import config

    if banner is None:
        banner = BANNER
    if theme != "light":
        theme = "dark"
    if use_rich:
        config.session.set_formatter("rich", theme=theme)

    console_defaults = {
        "explain": explain,
        "what": what,
        "where": where,
        "why": why,
        "more": more,
        "get_lang": friendly_traceback.get_lang,
        "set_lang": friendly_traceback.set_lang,
        "get_include": friendly_traceback.get_include,
        "set_include": friendly_traceback.set_include,
        "hint": hint,
        "friendly_tb": friendly_tb,
        "python_tb": python_tb,
        "debug_tb": debug_tb,
        "debug": debug,
        "show_paths": path_utils.show_paths,
        "_info": _info,
    }
    if not friendly_traceback.is_installed():
        friendly_traceback.install(include=include, lang=lang)
    if local_vars is None:
        local_vars = console_defaults
    else:
        local_vars.update(console_defaults)

    console = FriendlyConsole(locals=local_vars,
                              use_rich=use_rich,
                              theme=theme)
    console.interact(banner=banner)
Example #3
0
def start_console(local_vars=None,
                  use_rich=False,
                  include="friendly_tb",
                  lang="en",
                  banner=None):
    """Starts a console; modified from code.interact"""
    # from . import config

    if banner is None:
        banner = BANNER

    if not friendly_traceback.is_installed():
        friendly_traceback.install(include=include, lang=lang)

    source_cache.idle_get_lines = None

    if local_vars is not None:
        # Make sure we don't overwrite with our own functions
        helpers.update(local_vars)

    console = FriendlyConsole(locals=helpers, use_rich=use_rich)
    console.interact(banner=banner)
def test_check_syntax():
    # set-up
    bad_code_syntax = "True = 1"
    bad_code_exec = "a = b"  # Not a syntax error, but a NameError
    good_code = "c = 1"

    friendly.set_stream("capture")
    original_verbosity = friendly.get_verbosity()
    installed = friendly.is_installed()
    # ----- end of set-up

    # When a SyntaxError is raised, check_syntax returns False

    assert not friendly.advanced_check_syntax(source=bad_code_syntax)
    result = friendly.get_output()  # content is flushed
    assert "Python exception" in result
    assert "SyntaxError" in result

    assert not friendly.get_output()  # confirm that content was flushed

    # When no SyntaxError is raised, check_syntax returns a tuple
    # containing a code object and a file name
    assert friendly.advanced_check_syntax(source=bad_code_exec)
    assert friendly.advanced_check_syntax(source=good_code)
    assert not friendly.get_output()  # no new exceptions recorded

    try:
        exec(bad_code_syntax, {})
    except Exception:
        assert not friendly.get_output()

    # When friendly-traceback is not installed, a call to check_syntax
    # will end with verbosity set to 0, which corresponds to normal Python
    # tracebacks
    friendly.uninstall()
    friendly.advanced_check_syntax(source=bad_code_syntax)
    assert friendly.get_verbosity() == 0
    friendly.advanced_check_syntax(source=bad_code_syntax, verbosity=4)
    assert friendly.get_verbosity() == 0

    # When friendly-traceback is "installed", a call to check_syntax
    # leaves its verbosity unchanged.
    friendly.install(redirect="capture")

    friendly.set_verbosity(3)
    friendly.advanced_check_syntax(source=bad_code_syntax)
    assert friendly.get_verbosity() == 3
    friendly.advanced_check_syntax(source=bad_code_syntax, verbosity=4)
    assert friendly.get_verbosity() == 3

    # A call to advanced_code_syntax, with a language specified as an argument
    # should leave the previous language unchanged.

    friendly.set_lang("en")
    assert not friendly.advanced_check_syntax(source=bad_code_syntax, lang="fr")
    result = friendly.get_output()
    assert "Exception Python" in result  # French heading
    assert friendly.get_lang() == "en"

    # Clean up and restore for other tests
    friendly.get_output()
    friendly.set_stream(None)
    if installed:
        friendly.uninstall()
    friendly.set_verbosity(original_verbosity)
def test_exec_code():
    # set-up
    bad_code_syntax = "True = 1"
    bad_code_exec = "a = b"  # Not a syntax error, but a NameError
    good_code = "c = 1"

    friendly.set_stream("capture")
    original_include = friendly.get_include()
    installed = friendly.is_installed()
    # ----- end of set-up

    # When a SyntaxError is raised, exec_code returns False

    assert not friendly.editors_helper.exec_code(source=bad_code_syntax)
    result = friendly.get_output()  # content is flushed
    assert "SyntaxError" in result

    assert not friendly.get_output()  # confirm that content was flushed

    friendly.editors_helper.exec_code(source=bad_code_exec)
    result = friendly.get_output()
    assert "NameError" in result

    assert friendly.editors_helper.exec_code(source=good_code)
    assert not friendly.get_output()  # no new exceptions recorded

    try:
        exec(bad_code_syntax, {})
    except Exception:
        assert not friendly.get_output()

    # Ensure that a call to exec_code only install() temporarily
    # if it was not installed before.
    friendly.uninstall()
    friendly.editors_helper.exec_code(source=bad_code_syntax)
    assert not friendly.is_installed()
    friendly.editors_helper.exec_code(source=bad_code_syntax, include="no_tb")
    assert not friendly.is_installed()

    # When friendly-traceback is "installed", a call to exec_code
    # leaves its include unchanged.
    friendly.install(redirect="capture")

    friendly.set_include("friendly_tb")
    friendly.editors_helper.exec_code(source=bad_code_syntax)
    assert friendly.get_include() == "friendly_tb"
    friendly.editors_helper.exec_code(source=bad_code_syntax, include="no_tb")
    assert friendly.get_include() == "friendly_tb"

    # A call to exec_code, with a language specified as an argument
    # should leave the previous language unchanged.

    friendly.set_lang("en")
    friendly.editors_helper.exec_code(source=bad_code_exec, lang="fr", include="explain")
    result = friendly.get_output()
    assert "Une exception `NameError` indique" in result
    assert friendly.get_lang() == "en"

    # Clean up and restore for other tests
    friendly.get_output()
    friendly.set_stream(None)
    if installed:
        friendly.uninstall()
    friendly.set_include(original_include)