Esempio n. 1
0
def test_completion():
    shell = Console({"a_variable": 7})
    assert shell.complete("a") == (
        [
            "and ",
            "as ",
            "assert ",
            "async ",
            "await ",
            "a_variable",
            "abs(",
            "all(",
            "any(",
            "ascii(",
            "aiter(",
            "anext(",
        ],
        0,
    )

    assert shell.complete("a = 0 ; print.__g") == (
        [
            "print.__ge__(",
            "print.__getattribute__(",
            "print.__gt__(",
        ],
        8,
    )
Esempio n. 2
0
def test_persistent_redirection(safe_sys_redirections):
    my_stdout = ""
    my_stderr = ""
    orig_stdout = sys.stdout
    orig_stderr = sys.stderr

    def stdout_callback(string):
        nonlocal my_stdout
        my_stdout += string

    def stderr_callback(string):
        nonlocal my_stderr
        my_stderr += string

    shell = Console(
        stdout_callback=stdout_callback,
        stderr_callback=stderr_callback,
        persistent_stream_redirection=True,
    )

    # std names
    assert sys.stdout.name == orig_stdout.name
    assert sys.stderr.name == orig_stderr.name

    # std redirections
    print("foo")
    assert my_stdout == "foo\n"
    print("bar", file=sys.stderr)
    assert my_stderr == "bar\n"
    my_stderr = ""

    async def get_result(input):
        res = shell.push(input)
        assert res.syntax_check == "complete"
        return await res

    async def test():
        assert await get_result("print('foobar')") is None
        assert my_stdout == "foo\nfoobar\n"

        assert await get_result("print('foobar')") is None
        assert my_stdout == "foo\nfoobar\nfoobar\n"

        assert await get_result("1+1") == 2
        assert my_stdout == "foo\nfoobar\nfoobar\n"

    asyncio.get_event_loop().run_until_complete(test())

    my_stderr = ""

    shell.persistent_restore_streams()
    my_stdout = ""
    my_stderr = ""
    print(sys.stdout, file=orig_stdout)
    print("bar")
    assert my_stdout == ""

    print("foo", file=sys.stdout)
    assert my_stderr == ""
Esempio n. 3
0
def test_top_level_await():
    from asyncio import Queue, sleep

    q: Queue[int] = Queue()
    shell = Console(locals())
    fut = shell.push("await q.get()")

    async def test():
        await sleep(0.3)
        assert not fut.done()
        await q.put(5)
        assert await fut == 5

    asyncio.get_event_loop().run_until_complete(test())
Esempio n. 4
0
def test_interactive_console():
    shell = Console()

    def assert_incomplete(input):
        res = shell.push(input)
        assert res.syntax_check == "incomplete"

    async def get_result(input):
        res = shell.push(input)
        assert res.syntax_check == "complete"
        return await res

    async def test():
        assert await get_result("x = 5") is None
        assert await get_result("x") == 5
        assert await get_result("x ** 2") == 25

        assert_incomplete("def f(x):")
        assert_incomplete("    return x*x + 1")
        assert await get_result("") is None
        assert await get_result("[f(x) for x in range(5)]") == [
            1, 2, 5, 10, 17
        ]

        assert_incomplete("def factorial(n):")
        assert_incomplete("    if n < 2:")
        assert_incomplete("        return 1")
        assert_incomplete("    else:")
        assert_incomplete("        return n * factorial(n - 1)")
        assert await get_result("") is None
        assert await get_result("factorial(10)") == 3628800

        assert await get_result("import pytz") is None
        assert await get_result("pytz.utc.zone") == "UTC"

        fut = shell.push("1+")
        assert fut.syntax_check == "syntax-error"
        assert fut.exception() is not None
        assert (
            fut.formatted_error ==
            '  File "<console>", line 1\n    1+\n      ^\nSyntaxError: invalid syntax\n'
        )

        fut = shell.push("raise Exception('hi')")
        try:
            await fut
        except Exception:
            assert (
                fut.formatted_error ==
                'Traceback (most recent call last):\n  File "<console>", line 1, in <module>\nException: hi\n'
            )

    asyncio.get_event_loop().run_until_complete(test())
Esempio n. 5
0
def test_nonpersistent_redirection(safe_sys_redirections):
    my_stdout = ""
    my_stderr = ""

    def stdin_callback() -> str:
        return ""

    def stdout_callback(string: str) -> None:
        nonlocal my_stdout
        my_stdout += string

    def stderr_callback(string: str) -> None:
        nonlocal my_stderr
        my_stderr += string

    async def get_result(input):
        res = shell.push(input)
        assert res.syntax_check == "complete"
        return await res

    shell = Console(
        stdin_callback=stdin_callback,
        stdout_callback=stdout_callback,
        stderr_callback=stderr_callback,
        persistent_stream_redirection=False,
    )

    print("foo")
    assert my_stdout == ""

    async def test():
        assert await get_result("print('foobar')") is None
        assert my_stdout == "foobar\n"

        print("bar")
        assert my_stdout == "foobar\n"

        assert await get_result("print('foobar')") is None
        assert my_stdout == "foobar\nfoobar\n"

        assert await get_result("import sys") is None
        assert await get_result("print('foobar', file=sys.stderr)") is None
        assert my_stderr == "foobar\n"

        assert await get_result("1+1") == 2

        assert await get_result("sys.stdin.isatty()")
        assert await get_result("sys.stdout.isatty()")
        assert await get_result("sys.stderr.isatty()")

    asyncio.get_event_loop().run_until_complete(test())