Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (78 loc) · 2.96 KB
/
Copy pathapp.py
File metadata and controls
100 lines (78 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Python Sandbox Plugin for Noorle
This plugin provides a sandboxed Python execution environment that can:
- Execute arbitrary Python statements (exec)
- Evaluate Python expressions (eval)
Inspired by componentize-py sandbox example.
"""
import contextlib
import io
import json
import wit_world
from wit_world.types import Err
# Import additional stdlib modules to bundle them into WASM
# Just add .py files to additional_modules/ directory and rebuild!
import additional_modules
def handle_exception(e: Exception) -> Err[str]:
"""
Convert a Python exception to an Err result with a formatted error message.
Args:
e: The exception to convert
Returns:
Err result with the exception type and message
"""
message = str(e)
if message == "":
return Err(f"{type(e).__name__}")
else:
return Err(f"{type(e).__name__}: {message}")
class WitWorld(wit_world.WitWorld):
"""
Python Sandbox component that implements code execution capabilities.
"""
def eval(self, expression: str) -> str:
"""
Evaluate a Python expression and return the result as JSON.
This function evaluates a single Python expression and serializes
the result to JSON format. Useful for calculations and data transformations.
Args:
expression: Python expression to evaluate (e.g., "2 + 2", "[x**2 for x in range(5)]")
Returns:
JSON-serialized result
Raises:
Err with error message if evaluation fails
Examples:
eval("2 + 2") -> "4"
eval("[1, 2, 3]") -> "[1, 2, 3]"
eval("unknown_var") -> raises Err("NameError: name 'unknown_var' is not defined")
"""
try:
result = eval(expression)
return json.dumps(result)
except Exception as e:
raise handle_exception(e)
def exec(self, statements: str) -> str:
"""
Execute Python statements and capture stdout/stderr output.
This function executes arbitrary Python code (can be multiple statements)
and captures all output written to stdout and stderr. Useful for running
scripts and seeing their printed output.
Args:
statements: Python code to execute (can include multiple lines/statements)
Returns:
Captured output as string
Raises:
Err with error message if execution fails
Examples:
exec("print('Hello')") -> "Hello\n"
exec("for i in range(3):\n print(i)") -> "0\n1\n2\n"
exec("1/0") -> raises Err("ZeroDivisionError: division by zero")
"""
buffer = io.StringIO()
try:
# Redirect stdout and stderr to our buffer
with contextlib.redirect_stdout(buffer), contextlib.redirect_stderr(buffer):
exec(statements)
return buffer.getvalue()
except Exception as e:
raise handle_exception(e)