Catch syntax errors with .py/.py2

This commit is contained in:
jesopo 2018-10-24 13:40:56 +01:00
parent ae0825650e
commit 99f896ebdd

View file

@ -4,7 +4,15 @@ from src import ModuleManager, utils
EVAL_TEMPLATE = """ EVAL_TEMPLATE = """
import io, json, sys import io, json, sys
compiled = compile(sys.stdin.read(), "code", "single") def fail(s):
old_stdout.write(json.dumps({"success": False, "out": str(e)}))
sys.exit()
try:
compiled = compile(sys.stdin.read(), "code", "single")
except Exception as e:
self.fail(str(e))
old_stdout = sys.stdout old_stdout = sys.stdout
stdout = io.StringIO() stdout = io.StringIO()
@ -13,8 +21,7 @@ sys.stdout = stdout
try: try:
result = eval(compiled) result = eval(compiled)
except Exception as e: except Exception as e:
old_stdout.write(json.dumps({"success": False, "out": str(e)})) self.fail(str(e))
sys.exit()
stdout.write("\\n") stdout.write("\\n")
if not result == None: if not result == None: