diff --git a/judge/logging.py b/judge/logging.py new file mode 100644 index 0000000..8d9f154 --- /dev/null +++ b/judge/logging.py @@ -0,0 +1,7 @@ +import logging + +error_log = logging.getLogger("judge.errors") + + +def log_exception(msg): + error_log.exception(msg) diff --git a/judge/models/problem.py b/judge/models/problem.py index 9560be8..98e477b 100644 --- a/judge/models/problem.py +++ b/judge/models/problem.py @@ -556,7 +556,7 @@ class Problem(models.Model, PageVotable, Bookmarkable): def save(self, *args, **kwargs): super(Problem, self).save(*args, **kwargs) - if self.code != self.__original_code: + if self.__original_code and self.code != self.__original_code: if hasattr(self, "data_files") or self.pdf_description: try: problem_data_storage.rename(self.__original_code, self.code) diff --git a/judge/models/problem_data.py b/judge/models/problem_data.py index d61b528..b04d2ab 100644 --- a/judge/models/problem_data.py +++ b/judge/models/problem_data.py @@ -162,10 +162,10 @@ class ProblemData(models.Model): get_file_cachekey(file), ) cache.delete(cache_key) - except BadZipFile: + except (BadZipFile, FileNotFoundError): pass - if self.zipfile != self.__original_zipfile and self.__original_zipfile: - self.__original_zipfile.delete(save=False) + if self.zipfile != self.__original_zipfile: + self.__original_zipfile.delete(save=False) return super(ProblemData, self).save(*args, **kwargs) def has_yml(self): diff --git a/judge/utils/problem_data.py b/judge/utils/problem_data.py index 954c213..023c0be 100644 --- a/judge/utils/problem_data.py +++ b/judge/utils/problem_data.py @@ -12,6 +12,8 @@ from django.urls import reverse from django.utils.translation import gettext as _ from django.core.cache import cache +from judge.logging import log_exception + if os.altsep: def split_path_first( @@ -323,11 +325,13 @@ def get_problem_case(problem, files): settings.DMOJ_PROBLEM_DATA_ROOT, str(problem.data_files.zipfile) ) if not os.path.exists(archive_path): - raise Exception('archive file "%s" does not exist' % archive_path) + log_exception('archive file "%s" does not exist' % archive_path) + return {} try: archive = zipfile.ZipFile(archive_path, "r") except zipfile.BadZipfile: - raise Exception('bad archive: "%s"' % archive_path) + log_exception('bad archive: "%s"' % archive_path) + return {} for file in uncached_files: cache_key = "problem_archive:%s:%s" % (problem.code, get_file_cachekey(file)) diff --git a/judge/views/problem_data.py b/judge/views/problem_data.py index 121d570..0525395 100644 --- a/judge/views/problem_data.py +++ b/judge/views/problem_data.py @@ -56,6 +56,7 @@ from judge.utils.fine_uploader import ( FineUploadForm, ) from judge.views.problem import ProblemMixin +from judge.logging import log_exception mimetypes.init() mimetypes.add_type("application/x-yaml", ".yml") @@ -249,6 +250,9 @@ class ProblemDataView(TitleMixin, ProblemManagerMixin): return ZipFile(data.zipfile.path).namelist() except BadZipfile: return [] + except FileNotFoundError as e: + log_exception(e) + return [] return [] def get_context_data(self, **kwargs): diff --git a/judge/views/submission.py b/judge/views/submission.py index 61982cf..26300b0 100644 --- a/judge/views/submission.py +++ b/judge/views/submission.py @@ -195,8 +195,8 @@ def get_cases_data(submission): continue count += 1 problem_data[count] = { - "input": case_data[case.input_file] if case.input_file else "", - "answer": case_data[case.output_file] if case.output_file else "", + "input": case_data.get(case.input_file, "") if case.input_file else "", + "answer": case_data.get(case.output_file, "") if case.output_file else "", } return problem_data