From 8bc65deec5f1454728329c3c15b490178a902ca5 Mon Sep 17 00:00:00 2001 From: FherStk Date: Sun, 19 Jan 2025 17:11:48 +0000 Subject: [PATCH] New "exception" checker, for expected exceptions. --- dmoj/checkers/__init__.py | 1 + dmoj/checkers/__init__.pyi | 1 + dmoj/checkers/exception.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 dmoj/checkers/exception.py diff --git a/dmoj/checkers/__init__.py b/dmoj/checkers/__init__.py index 094395408..776935aae 100644 --- a/dmoj/checkers/__init__.py +++ b/dmoj/checkers/__init__.py @@ -8,6 +8,7 @@ floatsrel, identical, linecount, + exception, linematches, rstripped, sorted, diff --git a/dmoj/checkers/__init__.pyi b/dmoj/checkers/__init__.pyi index 65168c9ad..d802a6256 100644 --- a/dmoj/checkers/__init__.pyi +++ b/dmoj/checkers/__init__.pyi @@ -32,6 +32,7 @@ floatsabs: Checker floatsrel: Checker identical: Checker linecount: Checker +exception: Checker linematches: Checker rstripped: Checker sorted: Checker diff --git a/dmoj/checkers/exception.py b/dmoj/checkers/exception.py new file mode 100644 index 000000000..9c09dcb2b --- /dev/null +++ b/dmoj/checkers/exception.py @@ -0,0 +1,30 @@ +from re import split as resplit +from typing import Union + +from dmoj.result import CheckerResult +from dmoj.utils.unicode import utf8bytes + +verdict = '\u2717\u2713' + + +def check( + process_output: bytes, judge_output: bytes, point_value: float = 1, exceptionType: str = '', **kwargs +) -> Union[CheckerResult, bool]: + found = kwargs['result'].feedback.split('.')[-1] + passed = (found == exceptionType or len(found) > 0 and len(exceptionType) == 0) + + if kwargs['result'].output != '': + extended_feedback = ('Any type of exception' if exceptionType == '' else 'An exception of type "' + exceptionType) + '" was expected, but none has been raised:\nYour output: ' + kwargs['result'].output + else: + extended_feedback = None if passed else 'An exception was raised, but another exception type was expected:\nRaised: ' + found + '\nExpected: ' + exceptionType + + kwargs['result'].proc_output = '' + kwargs['result'].result_flag = 0 if passed else 1 + + return CheckerResult( + passed=passed, + points=point_value, + extended_feedback=extended_feedback + ) + +check.run_on_error = True # type: ignore