File size: 772 Bytes
0e0a1bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pathlib import Path
import os
import subprocess

def execute_code(code_content):
    """Executes the given Python code and returns output/errors."""
    temp_path = "temp_code.py"
    Path(temp_path).write_text(code_content, encoding="utf-8")
    
    try:
        output = subprocess.check_output(["python", temp_path], stderr=subprocess.STDOUT, text=True)
        os.remove(temp_path)
        return output, None
    except subprocess.CalledProcessError as e:
        os.remove(temp_path)
        return None, e.output
    

def format_code(user_code):
    if user_code.strip().startswith("def true_code()"):
        return user_code
    else:
        return f"def true_code():\n    " + "\n    ".join(user_code.splitlines()) + "\n\ntrue_code()"  # Wrap in function