File size: 556 Bytes
ea51bb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)