From 6de4cf76d0dffb7648a8246df6cda5de5f3b8128 Mon Sep 17 00:00:00 2001 From: RangHo Lee Date: Mon, 8 Apr 2024 22:20:42 +0900 Subject: [PATCH] fix(COR1010): register SIGALRM only when they're available Closes: #21 --- COR1010/TA/grader.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/COR1010/TA/grader.py b/COR1010/TA/grader.py index 5c4cfc5..38691a5 100644 --- a/COR1010/TA/grader.py +++ b/COR1010/TA/grader.py @@ -11,12 +11,20 @@ import re # Signal library is only available on UNIX -if sys.platform != "win32": +if sys.platform == 'linux' or sys.platform == 'darwin': import signal def timeout_handler(signum, frame): raise TimeoutError("Student's code timed out!") + def set_alarm(seconds: int): + signal.alarm(seconds) + signal.signal(signal.SIGALRM, timeout_handler) +else: + def set_alarm(seconds: int): + log_info("Timeout feature is not supported on this platform.") + log_info("Student's code will run indefinitely.") + from dataclasses import dataclass from pathlib import Path @@ -179,7 +187,7 @@ def run(script_path: Path, input_path: Optional[Path] = None) -> RunResult: """Run a script and return its output.""" # Set up an alarm - signal.alarm(5) + set_alarm(5) # Of course, their code can set this computer on fire try: @@ -224,7 +232,7 @@ def run(script_path: Path, input_path: Optional[Path] = None) -> RunResult: output_str="Timeout" ) finally: - signal.alarm(0) + set_alarm(0) if __name__ == '__main__':