Update Modules/Code_Interpreter.py
Browse files- Modules/Code_Interpreter.py +18 -1
Modules/Code_Interpreter.py
CHANGED
|
@@ -26,12 +26,29 @@ def Code_Interpreter(code: Annotated[str, "Python source code to run; stdout is
|
|
| 26 |
result = "No code provided."
|
| 27 |
_log_call_end("Code_Interpreter", result)
|
| 28 |
return result
|
|
|
|
|
|
|
| 29 |
old_stdout = sys.stdout
|
| 30 |
old_cwd = os.getcwd()
|
| 31 |
redirected_output = sys.stdout = StringIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
os.chdir(ROOT_DIR)
|
| 34 |
-
exec(code)
|
| 35 |
result = redirected_output.getvalue()
|
| 36 |
except Exception as exc: # pylint: disable=broad-except
|
| 37 |
result = str(exc)
|
|
|
|
| 26 |
result = "No code provided."
|
| 27 |
_log_call_end("Code_Interpreter", result)
|
| 28 |
return result
|
| 29 |
+
from .File_System import safe_open
|
| 30 |
+
|
| 31 |
old_stdout = sys.stdout
|
| 32 |
old_cwd = os.getcwd()
|
| 33 |
redirected_output = sys.stdout = StringIO()
|
| 34 |
+
|
| 35 |
+
# Create safe builtins
|
| 36 |
+
safe_builtins = None
|
| 37 |
+
if isinstance(__builtins__, dict):
|
| 38 |
+
safe_builtins = __builtins__.copy()
|
| 39 |
+
else:
|
| 40 |
+
safe_builtins = vars(__builtins__).copy()
|
| 41 |
+
safe_builtins["open"] = safe_open
|
| 42 |
+
|
| 43 |
+
env = {
|
| 44 |
+
"open": safe_open,
|
| 45 |
+
"__builtins__": safe_builtins,
|
| 46 |
+
"print": print,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
os.chdir(ROOT_DIR)
|
| 51 |
+
exec(code, env)
|
| 52 |
result = redirected_output.getvalue()
|
| 53 |
except Exception as exc: # pylint: disable=broad-except
|
| 54 |
result = str(exc)
|