aboutsummaryrefslogtreecommitdiffstats
path: root/tools/execute_py_code.py
blob: ad9310cb0484a2afe4f89060eca6b9ab1ef92b19 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import io
import sys

def run(code: str):
    """Execute the python code.
    Args:
        code: Python code to execute, such as `print("hello world")`
    """
    old_stdout = sys.stdout
    new_stdout = io.StringIO()
    sys.stdout = new_stdout

    exec(code)

    sys.stdout = old_stdout
    return new_stdout.getvalue()