diff --git a/course/page/code_runpy_backend.py b/course/page/code_runpy_backend.py index a06b45fc4be49a50db600dbc5accae5b82ae2e17..a9d5f37fb92be4dd38112db25ca1b82aaada5b82 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