Better handling of eval errors in modules/eval_python.py

This commit is contained in:
jesopo 2018-10-17 15:02:04 +01:00
parent 11944e348b
commit 3d34896beb

View file

@ -2,12 +2,24 @@ import html, socket
from src import ModuleManager, utils from src import ModuleManager, utils
EVAL_TEMPLATE = """ EVAL_TEMPLATE = """
import sys import StringIO, sys
compiled = compile(sys.stdin.read(), "code", "single") compiled = compile(sys.stdin.read(), "code", "single")
old_stdout = sys.stdout
stdout = StringIO.StringIO()
sys.stdout = stdout
try:
result = eval(compiled) result = eval(compiled)
print("") except Exception as e:
old_stdout.write(json.dumps({"success" False, "out": str(e)}))
sys.exit()
stdout.write("\n")
if not result == None: if not result == None:
sys.stdout.write(str(result)) stdout.write(str(result)+"\n")
old_stdout.write(json.dumps({"success" True, "out": stdout.getvalue()}))
""" """
EVAL_URL = "https://tpcg.tutorialspoint.com/tpcg.php" EVAL_URL = "https://tpcg.tutorialspoint.com/tpcg.php"