From 5bbdb6359b349bac297f3d06989e7ad21f26b78d Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Sat, 6 Oct 2018 01:02:50 -0500 Subject: [PATCH 1/2] disable exit() to be called from solution code --- course/page/code_runpy_backend.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/course/page/code_runpy_backend.py b/course/page/code_runpy_backend.py index a06b45fc..2832bda7 100644 --- a/course/page/code_runpy_backend.py +++ b/course/page/code_runpy_backend.py @@ -26,6 +26,7 @@ THE SOFTWARE. import sys import traceback +import re try: from .code_feedback import Feedback, GradingComplete @@ -172,12 +173,26 @@ def run_code(result, run_req): # }}} + # {{{ disable exit + + disable_exit_lines = re.sub("\n ", + "\n", r""" + def _monkey_patch_exit(): + print("Using exit() is not allowed") + + exit = _monkey_patch_exit + """) + + # }}} + # {{{ compile code if getattr(run_req, "setup_code", None): try: setup_code = compile( - run_req.setup_code, "[setup code]", 'exec') + run_req.setup_code, + "\n".join(["[setup_code]", disable_exit_lines]), + 'exec') except Exception: package_exception(result, "setup_compile_error") return -- GitLab From 25a149148571f880d3a5bf98cca46a4d12d4755d Mon Sep 17 00:00:00 2001 From: Kaushik Kulkarni Date: Sat, 6 Oct 2018 03:23:39 -0400 Subject: [PATCH 2/2] grammar correction --- course/page/code_runpy_backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/course/page/code_runpy_backend.py b/course/page/code_runpy_backend.py index 2832bda7..a9d5f37f 100644 --- a/course/page/code_runpy_backend.py +++ b/course/page/code_runpy_backend.py @@ -178,7 +178,7 @@ def run_code(result, run_req): disable_exit_lines = re.sub("\n ", "\n", r""" def _monkey_patch_exit(): - print("Using exit() is not allowed") + print("Using exit() is not allowed.") exit = _monkey_patch_exit """) -- GitLab