Spaces:
Sleeping
Sleeping
Create sandbox_runner.py
Browse files- sandbox_runner.py +23 -0
sandbox_runner.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
def sandbox_test(code):
|
| 5 |
+
try:
|
| 6 |
+
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") as tmp:
|
| 7 |
+
tmp.write(code)
|
| 8 |
+
tmp_path = tmp.name
|
| 9 |
+
|
| 10 |
+
result = subprocess.run(
|
| 11 |
+
["python3", tmp_path],
|
| 12 |
+
stdout=subprocess.PIPE,
|
| 13 |
+
stderr=subprocess.PIPE,
|
| 14 |
+
timeout=3
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
if result.returncode != 0:
|
| 18 |
+
return False, result.stderr.decode("utf8")
|
| 19 |
+
|
| 20 |
+
return True, None
|
| 21 |
+
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return False, str(e)
|