From 45f091f78cf2b48f50d1ce8cbf97ff5927e6c6ef Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 25 Nov 2024 08:13:48 +0800 Subject: [PATCH 1/2] fix: execute js/py code --- tools/execute_js_code.js | 7 +++++-- tools/execute_py_code.py | 26 +++++++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/tools/execute_js_code.js b/tools/execute_js_code.js index 6f04ade..0b7557b 100644 --- a/tools/execute_js_code.js +++ b/tools/execute_js_code.js @@ -12,8 +12,11 @@ exports.run = function run({ code }) { if (callback) callback(); }; - eval(code); - + const value = eval(code); + if (value !== undefined) { + output += value; + } + process.stdout.write = oldStdoutWrite; return output; } diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py index a8cfe48..5d8211c 100644 --- a/tools/execute_py_code.py +++ b/tools/execute_py_code.py @@ -1,16 +1,32 @@ +import ast import io import sys +from contextlib import redirect_stdout def run(code: str): """Execute the python code. Args: code: Python code to execute, such as `print("hello world")` """ - old_stdout = sys.stdout output = io.StringIO() - sys.stdout = output + with redirect_stdout(output): + value = exec_with_return(code, {}, {}) - exec(code) + if value is not None: + output.write(str(value)) - sys.stdout = old_stdout - return output.getvalue() \ No newline at end of file + return output.getvalue() + +def exec_with_return(code: str, globals: dict, locals: dict): + a = ast.parse(code) + last_expression = None + if a.body: + if isinstance(a_last := a.body[-1], ast.Expr): + last_expression = ast.unparse(a.body.pop()) + elif isinstance(a_last, ast.Assign): + last_expression = ast.unparse(a_last.targets[0]) + elif isinstance(a_last, (ast.AnnAssign, ast.AugAssign)): + last_expression = ast.unparse(a_last.target) + exec(ast.unparse(a), globals, locals) + if last_expression: + return eval(last_expression, globals, locals) \ No newline at end of file From bc20661fdaf2719d31b797d574d5c641a18136ac Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 25 Nov 2024 08:19:13 +0800 Subject: [PATCH 2/2] format --- tools/execute_py_code.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py index 5d8211c..71d66e1 100644 --- a/tools/execute_py_code.py +++ b/tools/execute_py_code.py @@ -1,8 +1,8 @@ import ast import io -import sys from contextlib import redirect_stdout + def run(code: str): """Execute the python code. Args: @@ -13,10 +13,11 @@ def run(code: str): value = exec_with_return(code, {}, {}) if value is not None: - output.write(str(value)) + output.write(str(value)) return output.getvalue() + def exec_with_return(code: str, globals: dict, locals: dict): a = ast.parse(code) last_expression = None @@ -29,4 +30,4 @@ def exec_with_return(code: str, globals: dict, locals: dict): last_expression = ast.unparse(a_last.target) exec(ast.unparse(a), globals, locals) if last_expression: - return eval(last_expression, globals, locals) \ No newline at end of file + return eval(last_expression, globals, locals)