From e1a26cb4f2032dfb44908ecb73b9039ca1c7dc04 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 26 Oct 2024 08:26:52 +0800 Subject: refactor: execute js/py code (#118) --- tools/execute_py_code.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tools/execute_py_code.py') diff --git a/tools/execute_py_code.py b/tools/execute_py_code.py index 3c6beb4..ad9310c 100644 --- a/tools/execute_py_code.py +++ b/tools/execute_py_code.py @@ -1,6 +1,16 @@ +import io +import sys + def run(code: str): """Execute the python code. Args: code: Python code to execute, such as `print("hello world")` """ - return eval(code) \ No newline at end of file + old_stdout = sys.stdout + new_stdout = io.StringIO() + sys.stdout = new_stdout + + exec(code) + + sys.stdout = old_stdout + return new_stdout.getvalue() \ No newline at end of file -- cgit v1.2.3