From 99f896ebdd40f723a7f8bfd10af9a619fe8310ad Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 24 Oct 2018 13:40:56 +0100 Subject: [PATCH] Catch syntax errors with .py/.py2 --- modules/eval_python.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/eval_python.py b/modules/eval_python.py index 13361e42..1a9fb306 100644 --- a/modules/eval_python.py +++ b/modules/eval_python.py @@ -4,7 +4,15 @@ from src import ModuleManager, utils EVAL_TEMPLATE = """ 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 stdout = io.StringIO() @@ -13,8 +21,7 @@ sys.stdout = stdout try: result = eval(compiled) except Exception as e: - old_stdout.write(json.dumps({"success": False, "out": str(e)})) - sys.exit() + self.fail(str(e)) stdout.write("\\n") if not result == None: