From 80433234dba5c8504e79fc829d05f1ef68a82f9f Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Wed, 24 Feb 2021 15:18:55 +0100 Subject: Move most of the unittest template inside of the handler --- resources/unittest_template.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'resources/unittest_template.py') diff --git a/resources/unittest_template.py b/resources/unittest_template.py index 4c9b0bb..7919b5c 100644 --- a/resources/unittest_template.py +++ b/resources/unittest_template.py @@ -18,17 +18,6 @@ class RunnerTestCase(unittest.TestCase): ### UNIT CODE -# Fake file object not writing anything -DEVNULL = SimpleNamespace(write=lambda *_: None, flush=lambda *_: None) - -RESULT = io.StringIO() -ORIGINAL_STDOUT = sys.stdout - -# stdout/err is patched in order to control what is outputted by the runner -sys.stdout = DEVNULL -sys.stderr = DEVNULL - - def _exit_sandbox(code: int) -> NoReturn: """ Exit the sandbox by printing the result to the actual stdout and exit with the provided code. @@ -74,10 +63,20 @@ def _main() -> None: try: + # Fake file object not writing anything + DEVNULL = SimpleNamespace(write=lambda *_: None, flush=lambda *_: None) + + RESULT = io.StringIO() + ORIGINAL_STDOUT = sys.stdout + + # stdout/err is patched in order to control what is outputted by the runner + sys.stdout = DEVNULL + sys.stderr = DEVNULL + # Load the user code as a global module variable module = _load_user_module() _main() except Exception: print("Uncaught exception:\n", file=RESULT) traceback.print_exc(file=RESULT) - _exit_sandbox(99) + _exit_sandbox(99) \ No newline at end of file -- cgit v1.2.3