Python_Code_Assistance / interpreter.py
valiant21's picture
Upload 5 files
64eae68 verified
raw
history blame
432 Bytes
import io
import sys
from logger import logger
def run_code(code):
# Redirect stdout to capture code output
old_stdout = sys.stdout
redirected_output = sys.stdout = io.StringIO()
try:
exec(code)
except Exception as e:
logger.error(f"Execution error: {e}")
return f"Error: {e}"
finally:
# Reset stdout
sys.stdout = old_stdout
return redirected_output.getvalue()