jatinror commited on
Commit
f070999
·
verified ·
1 Parent(s): 40d2521

Update tools/python_runner.py

Browse files
Files changed (1) hide show
  1. tools/python_runner.py +10 -5
tools/python_runner.py CHANGED
@@ -4,6 +4,8 @@ import traceback
4
  import contextlib
5
  import signal
6
 
 
 
7
  class TimeoutException(Exception):
8
  pass
9
 
@@ -11,10 +13,6 @@ def timeout_handler(signum, frame):
11
  raise TimeoutException("Execution timed out.")
12
 
13
  def run_python(code: str, timeout: int = 5) -> Dict[str, Any]:
14
- """
15
- Safely execute Python code and capture output/errors.
16
- """
17
-
18
  safe_globals = {
19
  "__builtins__": {
20
  "print": print,
@@ -42,7 +40,6 @@ def run_python(code: str, timeout: int = 5) -> Dict[str, Any]:
42
  exec(code, safe_globals, {})
43
 
44
  signal.alarm(0)
45
-
46
  return {"status": "success", "output": stdout_buffer.getvalue()}
47
 
48
  except TimeoutException as e:
@@ -50,3 +47,11 @@ def run_python(code: str, timeout: int = 5) -> Dict[str, Any]:
50
 
51
  except Exception:
52
  return {"status": "error", "output": traceback.format_exc()}
 
 
 
 
 
 
 
 
 
4
  import contextlib
5
  import signal
6
 
7
+ # ---- your safe executor ----
8
+
9
  class TimeoutException(Exception):
10
  pass
11
 
 
13
  raise TimeoutException("Execution timed out.")
14
 
15
  def run_python(code: str, timeout: int = 5) -> Dict[str, Any]:
 
 
 
 
16
  safe_globals = {
17
  "__builtins__": {
18
  "print": print,
 
40
  exec(code, safe_globals, {})
41
 
42
  signal.alarm(0)
 
43
  return {"status": "success", "output": stdout_buffer.getvalue()}
44
 
45
  except TimeoutException as e:
 
47
 
48
  except Exception:
49
  return {"status": "error", "output": traceback.format_exc()}
50
+
51
+
52
+ # ---- THIS PART REGISTERS THE TOOL ----
53
+ def python_runner(code: str) -> Dict[str, Any]:
54
+ """
55
+ Executes safe Python code and returns stdout or errors.
56
+ """
57
+ return run_python(code)