Nymbo commited on
Commit
3affea4
·
verified ·
1 Parent(s): b971e4a

Update Modules/Code_Interpreter.py

Browse files
Files changed (1) hide show
  1. Modules/Code_Interpreter.py +3 -40
Modules/Code_Interpreter.py CHANGED
@@ -1,16 +1,14 @@
1
  from __future__ import annotations
2
 
3
- import os
4
- import sys
5
- from io import StringIO
6
  from typing import Annotated
7
 
8
  import gradio as gr
9
  from ._docstrings import autodoc
10
- from .File_System import ROOT_DIR
11
 
12
  from app import _log_call_end, _log_call_start, _truncate_for_log
13
 
 
14
  # Single source of truth for the LLM-facing tool description
15
  TOOL_SUMMARY = (
16
  "Execute Python code from the tool root; returns captured stdout or the exception text."
@@ -22,42 +20,7 @@ TOOL_SUMMARY = (
22
  )
23
  def Code_Interpreter(code: Annotated[str, "Python source code to run; stdout is captured and returned."]) -> str:
24
  _log_call_start("Code_Interpreter", code=_truncate_for_log(code or "", 300))
25
- if code is None:
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)
55
- finally:
56
- sys.stdout = old_stdout
57
- try:
58
- os.chdir(old_cwd)
59
- except Exception:
60
- pass
61
  _log_call_end("Code_Interpreter", _truncate_for_log(result))
62
  return result
63
 
 
1
  from __future__ import annotations
2
 
 
 
 
3
  from typing import Annotated
4
 
5
  import gradio as gr
6
  from ._docstrings import autodoc
7
+ from ._core import sandboxed_exec
8
 
9
  from app import _log_call_end, _log_call_start, _truncate_for_log
10
 
11
+
12
  # Single source of truth for the LLM-facing tool description
13
  TOOL_SUMMARY = (
14
  "Execute Python code from the tool root; returns captured stdout or the exception text."
 
20
  )
21
  def Code_Interpreter(code: Annotated[str, "Python source code to run; stdout is captured and returned."]) -> str:
22
  _log_call_start("Code_Interpreter", code=_truncate_for_log(code or "", 300))
23
+ result = sandboxed_exec(code, ast_mode=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  _log_call_end("Code_Interpreter", _truncate_for_log(result))
25
  return result
26