Spaces:
Sleeping
Sleeping
| import subprocess | |
| import tempfile | |
| def sandbox_test(code): | |
| try: | |
| with tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") as tmp: | |
| tmp.write(code) | |
| tmp_path = tmp.name | |
| result = subprocess.run( | |
| ["python3", tmp_path], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| timeout=3 | |
| ) | |
| if result.returncode != 0: | |
| return False, result.stderr.decode("utf8") | |
| return True, None | |
| except Exception as e: | |
| return False, str(e) |