Skip to content

Commit 8257887

Browse files
authored
Prekinitev izvajanja kode po določenem času (#287)
Dodan timeout pri preverjanju nalog
1 parent 3c84e90 commit 8257887

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

web/problems/templates/python/attempt.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ def update_attempts(old_parts, response):
209209
] = "{{ token }}"
210210
try:
211211
{{ part.validation|default:"pass"|indent:" "|safe }}
212-
except:
212+
except TimeoutError:
213+
Check.error("Dovoljen čas izvajanja presežen")
214+
except Exception:
213215
Check.error(
214216
"Testi sprožijo izjemo\n {0}",
215217
"\n ".join(traceback.format_exc().split("\n"))[:-2],

web/problems/templates/python/check.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def readline(self, size=None):
1414
return line
1515

1616

17+
class TimeoutError(Exception):
18+
pass
19+
20+
1721
class Check:
1822
parts = None
1923
current_part = None
@@ -394,3 +398,21 @@ def set_stringio(stringio):
394398
else:
395399
with Check.set(stringio=stringio):
396400
yield
401+
402+
@staticmethod
403+
@contextmanager
404+
def time_limit(timeout_seconds=1):
405+
from signal import SIGINT, raise_signal
406+
from threading import Timer
407+
408+
def interrupt_main():
409+
raise_signal(SIGINT)
410+
411+
timer = Timer(timeout_seconds, interrupt_main)
412+
timer.start()
413+
try:
414+
yield
415+
except KeyboardInterrupt:
416+
raise TimeoutError
417+
finally:
418+
timer.cancel()

0 commit comments

Comments
 (0)