def test_repr(safe_sys_redirections): sep = "..." for string in ("x" * 10**5, "x" * (10**5 + 1)): for limit in (9, 10, 100, 101): assert len(console.repr_shorten( string, limit=limit, separator=sep)) == 2 * (limit // 2) + len(sep) sys.stdout = io.StringIO() console.displayhook([0] * 100, lambda v: console.repr_shorten(v, 100, separator=sep)) assert len(sys.stdout.getvalue()) == 100 + len(sep) + 1 # for \n
def test_repr(safe_sys_redirections): sep = "..." for string in ("x" * 10 ** 5, "x" * (10 ** 5 + 1)): for limit in (9, 10, 100, 101): assert len( console.repr_shorten(string, limit=limit, separator=sep) ) == 2 * (limit // 2) + len(sep)
async def exec_code( code, syntax_check_passed, stdin_callback, stdout_callback, stderr_callback ): mod, last_expr = code_runner._split_and_compile( code, flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT # type: ignore ) syntax_check_passed() await sleep(0) with redirect_stdout(WriteStream(stdout_callback)), redirect_stderr( WriteStream(stderr_callback) ), redirect_stdin(ReadStream(stdin_callback)): # run first part if mod is not None: coro = eval(mod, code_runner.globals, code_runner.locals) if iscoroutine(coro): await coro # evaluate last expression if last_expr is not None: res = eval(last_expr, code_runner.globals, code_runner.locals) if iscoroutine(res): res = await res if res is not None: res = repr_shorten(res) return res
async def exec_code(code: str, syntax_check_passed, stdin_callback, stdout_callback, stderr_callback): pyconsole.stdin_callback = stdin_callback pyconsole.stdout_callback = stdout_callback pyconsole.stderr_callback = stderr_callback for line in code.splitlines(): fut = pyconsole.push(line) if fut.syntax_check == "syntax-error": return to_js([-1, fut.formatted_error]) syntax_check_passed() try: result = await fut repr_result = repr_shorten(result) if result is not None else None return to_js([0, repr_result]) except Exception: return to_js([-1, fut.formatted_error])