aboutsummaryrefslogtreecommitdiffstats
path: root/tools/execute_py_code.py
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-26 08:26:52 +0800
committerGitHub <noreply@github.com>2024-10-26 08:26:52 +0800
commite1a26cb4f2032dfb44908ecb73b9039ca1c7dc04 (patch)
tree7cabfa29d7c5a0ccbbfff822ffb3bd4b607bd563 /tools/execute_py_code.py
parentfbeaa9cb2c5b7a8e0d9114a62c270c6fe8024c31 (diff)
downloadllm-functions-docker-e1a26cb4f2032dfb44908ecb73b9039ca1c7dc04.tar.gz
refactor: execute js/py code (#118)
Diffstat (limited to 'tools/execute_py_code.py')
-rw-r--r--tools/execute_py_code.py12
1 files changed, 11 insertions, 1 deletions
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