Mercurial > ~astiob > upreckon > hgweb
annotate testcases.py @ 62:593ad09cd69b
Multiple exit code handling fixes
call() on POSIX now always gets the exit code of the process.
force_zero_exitcode now has no effect if the process got killed by a signal.
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 22 Dec 2010 15:03:47 +0200 |
parents | 24f144e11b5e |
children | fb9d0223a871 |
rev | line source |
---|---|
21 | 1 #! /usr/bin/env python |
16 | 2 # Copyright (c) 2010 Chortos-2 <chortos@inbox.lv> |
3 | |
43 | 4 # TODO: copy the ansfile if not options.erase even if no validator is used |
5 | |
21 | 6 from __future__ import division, with_statement |
7 | |
8 try: | |
9 from compat import * | |
10 import files, problem, config | |
11 except ImportError: | |
12 import __main__ | |
13 __main__.import_error(sys.exc_info()[1]) | |
14 else: | |
15 from __main__ import clock, options | |
16 | |
17 import glob, re, sys, tempfile, time | |
18 from subprocess import Popen, PIPE, STDOUT | |
19 | |
20 import os | |
21 devnull = open(os.path.devnull, 'w+') | |
22 | |
23 try: | |
24 from signal import SIGTERM, SIGKILL | |
25 except ImportError: | |
26 SIGTERM = 15 | |
27 SIGKILL = 9 | |
28 | |
16 | 29 try: |
21 | 30 from _subprocess import TerminateProcess |
31 except ImportError: | |
32 # CPython 2.5 does define _subprocess.TerminateProcess even though it is | |
33 # not used in the subprocess module, but maybe something else does not | |
34 try: | |
35 import ctypes | |
36 TerminateProcess = ctypes.windll.kernel32.TerminateProcess | |
37 except (ImportError, AttributeError): | |
38 TerminateProcess = None | |
39 | |
22 | 40 |
41 # Do the hacky-wacky dark magic needed to catch presses of the Escape button. | |
42 # If only Python supported forcible termination of threads... | |
43 if not sys.stdin.isatty(): | |
44 canceled = init_canceled = lambda: False | |
45 pause = None | |
46 else: | |
47 try: | |
48 # Windows has select() too, but it is not the select() we want | |
49 import msvcrt | |
50 except ImportError: | |
51 try: | |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
52 from select import select |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
53 import termios, tty, atexit |
22 | 54 except ImportError: |
55 # It cannot be helped! | |
56 # Silently disable support for killing the program being tested | |
57 canceled = init_canceled = lambda: False | |
58 pause = None | |
59 else: | |
60 def cleanup(old=termios.tcgetattr(sys.stdin.fileno())): | |
61 termios.tcsetattr(sys.stdin.fileno(), termios.TCSAFLUSH, old) | |
62 atexit.register(cleanup) | |
63 del cleanup | |
64 tty.setcbreak(sys.stdin.fileno()) | |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
65 def canceled(select=select, stdin=sys.stdin, read=sys.stdin.read): |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
66 while select((stdin,), (), (), 0)[0]: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
67 if read(1) == '\33': |
22 | 68 return True |
69 return False | |
70 def init_canceled(): | |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
71 while select((sys.stdin,), (), (), 0)[0]: |
22 | 72 sys.stdin.read(1) |
73 def pause(): | |
74 sys.stdin.read(1) | |
75 else: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
76 def canceled(kbhit=msvcrt.kbhit, getch=msvcrt.getch): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
77 while kbhit(): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
78 c = getch() |
22 | 79 if c == '\33': |
80 return True | |
81 elif c == '\0': | |
82 # Let's hope no-one is fiddling with this | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
83 getch() |
22 | 84 return False |
85 def init_canceled(): | |
86 while msvcrt.kbhit(): | |
87 msvcrt.getch() | |
88 def pause(): | |
89 msvcrt.getch() | |
90 | |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
91 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
92 from signal import SIGCHLD, signal, SIG_DFL |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
93 from select import select, error as select_error |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
94 from errno import EINTR |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
95 except ImportError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
96 try: |
61
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
97 from _subprocess import WAIT_OBJECT_0, STD_INPUT_HANDLE, INFINITE |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
98 except ImportError: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
99 WAIT_OBJECT_0 = 0 |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
100 STD_INPUT_HANDLE = -10 |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
101 INFINITE = -1 |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
102 try: |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
103 import ctypes |
61
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
104 SetConsoleMode = ctypes.windll.kernel32.SetConsoleMode |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
105 FlushConsoleInputBuffer = ctypes.windll.kernel32.FlushConsoleInputBuffer |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
106 WaitForMultipleObjects = ctypes.windll.kernel32.WaitForMultipleObjects |
61
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
107 ReadConsoleInputA = ctypes.windll.kernel32.ReadConsoleInputA |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
108 try: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
109 from _subprocess import GetStdHandle |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
110 except ImportError: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
111 GetStdHandle = ctypes.windll.kernel32.GetStdHandle |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
112 except (ImportError, AttributeError): |
61
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
113 console_input = False |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
114 else: |
61
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
115 hStdin = GetStdHandle(STD_INPUT_HANDLE) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
116 console_input = bool(SetConsoleMode(hStdin, 1)) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
117 if console_input: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
118 FlushConsoleInputBuffer(hStdin) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
119 class KEY_EVENT_RECORD(ctypes.Structure): |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
120 _fields_ = (("bKeyDown", ctypes.c_int), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
121 ("wRepeatCount", ctypes.c_ushort), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
122 ("wVirtualKeyCode", ctypes.c_ushort), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
123 ("wVirtualScanCode", ctypes.c_ushort), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
124 ("UnicodeChar", ctypes.c_wchar), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
125 ("dwControlKeyState", ctypes.c_uint)) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
126 class INPUT_RECORD(ctypes.Structure): |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
127 _fields_ = (("EventType", ctypes.c_int), |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
128 ("KeyEvent", KEY_EVENT_RECORD)) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
129 # Memory limits (currently) are not supported |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
130 def call(*args, **kwargs): |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
131 case = kwargs.pop('case') |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
132 try: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
133 case.process = Popen(*args, **kwargs) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
134 except OSError: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
135 raise CannotStartTestee(sys.exc_info()[1]) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
136 case.time_started = clock() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
137 if not console_input: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
138 if case.maxtime: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
139 if WaitForSingleObject(case.process._handle, int(case.maxtime * 1000)) != WAIT_OBJECT_0: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
140 raise TimeLimitExceeded |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
141 else: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
142 case.process.wait() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
143 else: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
144 ir = INPUT_RECORD() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
145 n = ctypes.c_int() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
146 lpHandles = (ctypes.c_int * 2)(hStdin, case.process._handle) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
147 if case.maxtime: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
148 time_end = clock() + case.maxtime |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
149 while case.process.poll() is None: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
150 remaining = time_end - clock() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
151 if remaining > 0: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
152 if WaitForMultipleObjects(2, lpHandles, False, int(remaining * 1000)) == WAIT_OBJECT_0: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
153 ReadConsoleInputA(hStdin, ctypes.byref(ir), 1, ctypes.byref(n)) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
154 if ir.EventType == 1 and ir.KeyEvent.bKeyDown and ir.KeyEvent.wVirtualKeyCode == 27: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
155 raise CanceledByUser |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
156 else: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
157 raise TimeLimitExceeded |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
158 else: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
159 while case.process.poll() is None: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
160 if WaitForMultipleObjects(2, lpHandles, False, INFINITE) == WAIT_OBJECT_0: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
161 ReadConsoleInputA(hStdin, ctypes.byref(ir), 1, ctypes.byref(n)) |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
162 if ir.EventType == 1 and ir.KeyEvent.bKeyDown and ir.KeyEvent.wVirtualKeyCode == 27: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
163 raise CanceledByUser |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
164 case.time_stopped = clock() |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
165 if not console_input: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
166 try: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
167 try: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
168 from _subprocess import WaitForSingleObject |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
169 except ImportError: |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
170 import ctypes |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
171 WaitForSingleObject = ctypes.windll.kernel32.WaitForSingleObject |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
172 except (ImportError, AttributeError): |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
173 # TODO: move the default implementation here |
24f144e11b5e
Accurate run-time reporting on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents:
58
diff
changeset
|
174 call = None |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
175 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
176 # Make SIGCHLD interrupt sleep() and select() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
177 def bury_child(signum, frame): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
178 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
179 bury_child.case.time_stopped = clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
180 except Exception: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
181 pass |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
182 signal(SIGCHLD, bury_child) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
183 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
184 # If you want this to work, don't set any stdio argument to PIPE |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
185 def call_real(*args, **kwargs): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
186 bury_child.case = case = kwargs.pop('case') |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
187 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
188 case.process = Popen(*args, **kwargs) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
189 except OSError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
190 raise CannotStartTestee(sys.exc_info()[1]) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
191 case.time_started = clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
192 if pause is None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
193 if case.maxtime: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
194 time.sleep(case.maxtime) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
195 if case.process.poll() is None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
196 raise TimeLimitExceeded |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
197 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
198 case.process.wait() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
199 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
200 if not case.maxtime: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
201 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
202 while case.process.poll() is None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
203 if select((sys.stdin,), (), ())[0]: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
204 if sys.stdin.read(1) == '\33': |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
205 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
206 except select_error: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
207 if sys.exc_info()[1].args[0] != EINTR: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
208 raise |
62
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
209 else: |
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
210 case.process.poll() |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
211 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
212 time_end = clock() + case.maxtime |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
213 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
214 while case.process.poll() is None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
215 remaining = time_end - clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
216 if remaining > 0: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
217 if select((sys.stdin,), (), (), remaining)[0]: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
218 if sys.stdin.read(1) == '\33': |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
219 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
220 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
221 raise TimeLimitExceeded |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
222 except select_error: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
223 if sys.exc_info()[1].args[0] != EINTR: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
224 raise |
62
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
225 else: |
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
226 case.process.poll() |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
227 del bury_child.case |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
228 def call(*args, **kwargs): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
229 if 'preexec_fn' in kwargs: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
230 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
231 return call_real(*args, **kwargs) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
232 except MemoryError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
233 # If there is not enough memory for the forked test.py, |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
234 # opt for silent dropping of the limit |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
235 # TODO: show a warning somewhere |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
236 del kwargs['preexec_fn'] |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
237 return call_real(*args, **kwargs) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
238 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
239 return call_real(*args, **kwargs) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
240 |
22 | 241 |
21 | 242 __all__ = ('TestCase', 'load_problem', 'TestCaseNotPassed', |
22 | 243 'TimeLimitExceeded', 'CanceledByUser', 'WrongAnswer', |
244 'NonZeroExitCode', 'CannotStartTestee', | |
245 'CannotStartValidator', 'CannotReadOutputFile', | |
246 'CannotReadInputFile', 'CannotReadAnswerFile') | |
21 | 247 |
248 | |
249 | |
250 # Exceptions | |
251 | |
252 class TestCaseNotPassed(Exception): __slots__ = () | |
253 class TimeLimitExceeded(TestCaseNotPassed): __slots__ = () | |
22 | 254 class CanceledByUser(TestCaseNotPassed): __slots__ = () |
21 | 255 |
256 class WrongAnswer(TestCaseNotPassed): | |
257 __slots__ = 'comment' | |
258 def __init__(self, comment=''): | |
259 self.comment = comment | |
260 | |
261 class NonZeroExitCode(TestCaseNotPassed): | |
262 __slots__ = 'exitcode' | |
263 def __init__(self, exitcode): | |
264 self.exitcode = exitcode | |
265 | |
266 class ExceptionWrapper(TestCaseNotPassed): | |
267 __slots__ = 'upstream' | |
268 def __init__(self, upstream): | |
269 self.upstream = upstream | |
270 | |
271 class CannotStartTestee(ExceptionWrapper): __slots__ = () | |
272 class CannotStartValidator(ExceptionWrapper): __slots__ = () | |
273 class CannotReadOutputFile(ExceptionWrapper): __slots__ = () | |
274 class CannotReadInputFile(ExceptionWrapper): __slots__ = () | |
275 class CannotReadAnswerFile(ExceptionWrapper): __slots__ = () | |
276 | |
277 | |
278 | |
22 | 279 # Helper context managers |
280 | |
281 class CopyDeleting(object): | |
282 __slots__ = 'case', 'file', 'name' | |
283 | |
284 def __init__(self, case, file, name): | |
285 self.case = case | |
286 self.file = file | |
287 self.name = name | |
288 | |
289 def __enter__(self): | |
290 if self.name: | |
291 try: | |
292 self.file.copy(self.name) | |
293 except: | |
294 try: | |
295 self.__exit__(None, None, None) | |
296 except: | |
297 pass | |
298 raise | |
299 | |
300 def __exit__(self, exc_type, exc_val, exc_tb): | |
301 if self.name: | |
302 self.case.files_to_delete.append(self.name) | |
303 | |
304 | |
305 class Copying(object): | |
306 __slots__ = 'file', 'name' | |
307 | |
308 def __init__(self, file, name): | |
309 self.file = file | |
310 self.name = name | |
311 | |
312 def __enter__(self): | |
313 if self.name: | |
314 self.file.copy(self.name) | |
315 | |
316 def __exit__(self, exc_type, exc_val, exc_tb): | |
317 pass | |
318 | |
319 | |
320 | |
21 | 321 # Test case types |
16 | 322 |
323 class TestCase(object): | |
21 | 324 __slots__ = ('problem', 'id', 'isdummy', 'infile', 'outfile', 'points', |
325 'process', 'time_started', 'time_stopped', 'time_limit_string', | |
22 | 326 'realinname', 'realoutname', 'maxtime', 'maxmemory', |
327 'has_called_back', 'files_to_delete') | |
21 | 328 |
329 if ABCMeta: | |
330 __metaclass__ = ABCMeta | |
16 | 331 |
21 | 332 def __init__(case, prob, id, isdummy, points): |
16 | 333 case.problem = prob |
21 | 334 case.id = id |
335 case.isdummy = isdummy | |
336 case.points = points | |
337 case.maxtime = case.problem.config.maxtime | |
338 case.maxmemory = case.problem.config.maxmemory | |
339 if case.maxtime: | |
340 case.time_limit_string = '/%.3f' % case.maxtime | |
341 else: | |
342 case.time_limit_string = '' | |
343 if not isdummy: | |
344 case.realinname = case.problem.config.testcaseinname | |
345 case.realoutname = case.problem.config.testcaseoutname | |
346 else: | |
347 case.realinname = case.problem.config.dummyinname | |
348 case.realoutname = case.problem.config.dummyoutname | |
349 | |
350 @abstractmethod | |
351 def test(case): raise NotImplementedError | |
16 | 352 |
22 | 353 def __call__(case, callback): |
354 case.has_called_back = False | |
355 case.files_to_delete = [] | |
21 | 356 try: |
22 | 357 return case.test(callback) |
21 | 358 finally: |
22 | 359 now = clock() |
360 if not getattr(case, 'time_started', None): | |
361 case.time_started = case.time_stopped = now | |
362 elif not getattr(case, 'time_stopped', None): | |
363 case.time_stopped = now | |
364 if not case.has_called_back: | |
365 callback() | |
21 | 366 case.cleanup() |
367 | |
368 def cleanup(case): | |
369 #if getattr(case, 'infile', None): | |
370 # case.infile.close() | |
371 #if getattr(case, 'outfile', None): | |
372 # case.outfile.close() | |
373 if getattr(case, 'process', None): | |
374 # Try killing after three unsuccessful TERM attempts in a row | |
375 # (except on Windows, where TERMing is killing) | |
376 for i in range(3): | |
377 try: | |
378 try: | |
379 case.process.terminate() | |
380 except AttributeError: | |
381 # Python 2.5 | |
382 if TerminateProcess and hasattr(proc, '_handle'): | |
383 # Windows API | |
384 TerminateProcess(proc._handle, 1) | |
385 else: | |
386 # POSIX | |
387 os.kill(proc.pid, SIGTERM) | |
388 except Exception: | |
389 time.sleep(0) | |
390 case.process.poll() | |
391 else: | |
22 | 392 case.process.wait() |
21 | 393 break |
394 else: | |
395 # If killing the process is unsuccessful three times in a row, | |
396 # just silently stop trying | |
397 for i in range(3): | |
398 try: | |
399 try: | |
400 case.process.kill() | |
401 except AttributeError: | |
402 # Python 2.5 | |
403 if TerminateProcess and hasattr(proc, '_handle'): | |
404 # Windows API | |
405 TerminateProcess(proc._handle, 1) | |
406 else: | |
407 # POSIX | |
408 os.kill(proc.pid, SIGKILL) | |
409 except Exception: | |
410 time.sleep(0) | |
411 case.process.poll() | |
412 else: | |
22 | 413 case.process.wait() |
21 | 414 break |
22 | 415 if case.files_to_delete: |
416 for name in case.files_to_delete: | |
417 try: | |
418 os.remove(name) | |
419 except Exception: | |
420 # It can't be helped | |
421 pass | |
21 | 422 |
423 def open_infile(case): | |
424 try: | |
425 case.infile = files.File('/'.join((case.problem.name, case.realinname.replace('$', case.id)))) | |
426 except IOError: | |
427 e = sys.exc_info()[1] | |
428 raise CannotReadInputFile(e) | |
429 | |
430 def open_outfile(case): | |
431 try: | |
432 case.outfile = files.File('/'.join((case.problem.name, case.realoutname.replace('$', case.id)))) | |
433 except IOError: | |
434 e = sys.exc_info()[1] | |
435 raise CannotReadAnswerFile(e) | |
436 | |
16 | 437 |
21 | 438 class ValidatedTestCase(TestCase): |
439 __slots__ = 'validator' | |
440 | |
441 def __init__(case, *args): | |
442 TestCase.__init__(case, *args) | |
443 if not case.problem.config.tester: | |
444 case.validator = None | |
445 else: | |
446 case.validator = case.problem.config.tester | |
447 | |
448 def validate(case, output): | |
449 if not case.validator: | |
450 # Compare the output with the reference output | |
451 case.open_outfile() | |
452 with case.outfile.open() as refoutput: | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
453 for line, refline in zip_longest(output, refoutput): |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
454 if refline is not None and not isinstance(refline, basestring): |
21 | 455 line = bytes(line, sys.getdefaultencoding()) |
456 if line != refline: | |
22 | 457 raise WrongAnswer |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
458 return 1 |
21 | 459 elif callable(case.validator): |
460 return case.validator(output) | |
461 else: | |
462 # Call the validator program | |
463 output.close() | |
23 | 464 if case.problem.config.ansname: |
465 case.open_outfile() | |
466 case.outfile.copy(case.problem.config.ansname) | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
467 try: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
468 case.process = Popen(case.validator, stdin=devnull, stdout=PIPE, stderr=STDOUT, universal_newlines=True, bufsize=-1) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
469 except OSError: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
470 raise CannotStartValidator(sys.exc_info()[1]) |
21 | 471 comment = case.process.communicate()[0].strip() |
26 | 472 match = re.match(r'(?i)(ok|(?:correct|wrong)(?:(?:\s|_)*answer)?)(?:$|\s+|[.,!:]+\s*)', comment) |
21 | 473 if match: |
474 comment = comment[match.end():] | |
475 if not case.problem.config.maxexitcode: | |
476 if case.process.returncode: | |
477 raise WrongAnswer(comment) | |
478 else: | |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
479 return 1, comment |
21 | 480 else: |
24
c23d81f4a1a3
Score returned by TestCase.__call__() is now normalized to 0..1
Oleg Oshmyan <chortos@inbox.lv>
parents:
23
diff
changeset
|
481 return case.process.returncode / case.problem.config.maxexitcode, comment |
21 | 482 |
483 | |
484 class BatchTestCase(ValidatedTestCase): | |
485 __slots__ = () | |
486 | |
22 | 487 def test(case, callback): |
488 init_canceled() | |
21 | 489 if sys.platform == 'win32' or not case.maxmemory: |
490 preexec_fn = None | |
491 else: | |
492 def preexec_fn(): | |
493 try: | |
494 import resource | |
495 maxmemory = int(case.maxmemory * 1048576) | |
496 resource.setrlimit(resource.RLIMIT_AS, (maxmemory, maxmemory)) | |
497 # I would also set a CPU time limit but I do not want the time | |
498 # that passes between the calls to fork and exec to be counted in | |
499 except MemoryError: | |
500 # We do not have enough memory for ourselves; | |
501 # let the parent know about this | |
502 raise | |
503 except Exception: | |
504 # Well, at least we tried | |
505 pass | |
506 case.open_infile() | |
507 case.time_started = None | |
508 if case.problem.config.stdio: | |
54 | 509 if options.erase and not case.validator or not case.problem.config.inname: |
22 | 510 # TODO: re-use the same file name if possible |
21 | 511 # FIXME: 2.5 lacks the delete parameter |
512 with tempfile.NamedTemporaryFile(delete=False) as f: | |
22 | 513 inputdatafname = f.name |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
514 contextmgr = CopyDeleting(case, case.infile, inputdatafname) |
21 | 515 else: |
516 inputdatafname = case.problem.config.inname | |
25
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
517 contextmgr = Copying(case.infile, inputdatafname) |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
518 with contextmgr: |
b500e117080e
Bug fixes and overhead reduction
Oleg Oshmyan <chortos@inbox.lv>
parents:
24
diff
changeset
|
519 # FIXME: this U doesn't do anything good for the child process, does it? |
22 | 520 with open(inputdatafname, 'rU') as infile: |
521 with tempfile.TemporaryFile('w+') if options.erase and not case.validator else open(case.problem.config.outname, 'w+') as outfile: | |
57
855bdfeb32a6
NameErrors within call() are now reported
Oleg Oshmyan <chortos@inbox.lv>
parents:
56
diff
changeset
|
522 if call is not None: |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
523 call(case.problem.config.path, case=case, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1, preexec_fn=preexec_fn) |
57
855bdfeb32a6
NameErrors within call() are now reported
Oleg Oshmyan <chortos@inbox.lv>
parents:
56
diff
changeset
|
524 else: |
22 | 525 try: |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
526 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
527 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1, preexec_fn=preexec_fn) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
528 except MemoryError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
529 # If there is not enough memory for the forked test.py, |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
530 # opt for silent dropping of the limit |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
531 # TODO: show a warning somewhere |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
532 case.process = Popen(case.problem.config.path, stdin=infile, stdout=outfile, stderr=devnull, universal_newlines=True, bufsize=-1) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
533 except OSError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
534 raise CannotStartTestee(sys.exc_info()[1]) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
535 case.time_started = clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
536 time_next_check = case.time_started + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
537 if not case.maxtime: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
538 while True: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
539 exitcode, now = case.process.poll(), clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
540 if exitcode is not None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
541 case.time_stopped = now |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
542 break |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
543 # For some reason (probably Microsoft's fault), |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
544 # msvcrt.kbhit() is slow as hell |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
545 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
546 if now >= time_next_check: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
547 if canceled(): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
548 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
549 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
550 time_next_check = now + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
551 time.sleep(.001) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
552 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
553 time_end = case.time_started + case.maxtime |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
554 while True: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
555 exitcode, now = case.process.poll(), clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
556 if exitcode is not None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
557 case.time_stopped = now |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
558 break |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
559 elif now >= time_end: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
560 raise TimeLimitExceeded |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
561 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
562 if now >= time_next_check: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
563 if canceled(): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
564 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
565 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
566 time_next_check = now + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
567 time.sleep(.001) |
62
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
568 if config.globalconf.force_zero_exitcode and case.process.returncode or case.process.returncode < 0: |
22 | 569 raise NonZeroExitCode(case.process.returncode) |
570 callback() | |
571 case.has_called_back = True | |
572 outfile.seek(0) | |
573 return case.validate(outfile) | |
21 | 574 else: |
22 | 575 case.infile.copy(case.problem.config.inname) |
57
855bdfeb32a6
NameErrors within call() are now reported
Oleg Oshmyan <chortos@inbox.lv>
parents:
56
diff
changeset
|
576 if call is not None: |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
577 call(case.problem.config.path, case=case, stdin=devnull, stdout=devnull, stderr=STDOUT, preexec_fn=preexec_fn) |
57
855bdfeb32a6
NameErrors within call() are now reported
Oleg Oshmyan <chortos@inbox.lv>
parents:
56
diff
changeset
|
578 else: |
21 | 579 try: |
56
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
580 try: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
581 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT, preexec_fn=preexec_fn) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
582 except MemoryError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
583 # If there is not enough memory for the forked test.py, |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
584 # opt for silent dropping of the limit |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
585 # TODO: show a warning somewhere |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
586 case.process = Popen(case.problem.config.path, stdin=devnull, stdout=devnull, stderr=STDOUT) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
587 except OSError: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
588 raise CannotStartTestee(sys.exc_info()[1]) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
589 case.time_started = clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
590 time_next_check = case.time_started + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
591 if not case.maxtime: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
592 while True: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
593 exitcode, now = case.process.poll(), clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
594 if exitcode is not None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
595 case.time_stopped = now |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
596 break |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
597 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
598 if now >= time_next_check: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
599 if canceled(): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
600 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
601 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
602 time_next_check = now + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
603 time.sleep(.001) |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
604 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
605 time_end = case.time_started + case.maxtime |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
606 while True: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
607 exitcode, now = case.process.poll(), clock() |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
608 if exitcode is not None: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
609 case.time_stopped = now |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
610 break |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
611 elif now >= time_end: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
612 raise TimeLimitExceeded |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
613 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
614 if now >= time_next_check: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
615 if canceled(): |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
616 raise CanceledByUser |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
617 else: |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
618 time_next_check = now + .15 |
693a938bdeee
Accurate run-time reporting on POSIX
Oleg Oshmyan <chortos@inbox.lv>
parents:
55
diff
changeset
|
619 time.sleep(.001) |
62
593ad09cd69b
Multiple exit code handling fixes
Oleg Oshmyan <chortos@inbox.lv>
parents:
61
diff
changeset
|
620 if config.globalconf.force_zero_exitcode and case.process.returncode or case.process.returncode < 0: |
21 | 621 raise NonZeroExitCode(case.process.returncode) |
22 | 622 callback() |
623 case.has_called_back = True | |
21 | 624 with open(case.problem.config.outname, 'rU') as output: |
625 return case.validate(output) | |
626 | |
627 | |
628 # This is the only test case type not executing any programs to be tested | |
629 class OutputOnlyTestCase(ValidatedTestCase): | |
630 __slots__ = () | |
631 def cleanup(case): pass | |
632 | |
633 class BestOutputTestCase(ValidatedTestCase): | |
634 __slots__ = () | |
635 | |
636 # This is the only test case type executing two programs simultaneously | |
637 class ReactiveTestCase(TestCase): | |
638 __slots__ = () | |
639 # The basic idea is to launch the program to be tested and the grader | |
640 # and to pipe their standard I/O from and to each other, | |
641 # and then to capture the grader's exit code and use it | |
26 | 642 # like the exit code of an output validator is used. |
21 | 643 |
644 | |
645 def load_problem(prob, _types={'batch' : BatchTestCase, | |
646 'outonly' : OutputOnlyTestCase, | |
647 'bestout' : BestOutputTestCase, | |
648 'reactive': ReactiveTestCase}): | |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
649 # We will need to iterate over these configuration variables twice |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
650 try: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
651 len(prob.config.dummies) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
652 except Exception: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
653 prob.config.dummies = tuple(prob.config.dummies) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
654 try: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
655 len(prob.config.tests) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
656 except Exception: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
657 prob.config.tests = tuple(prob.config.tests) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
658 |
23 | 659 if options.legacy: |
660 prob.config.usegroups = False | |
58 | 661 newtests = [] |
23 | 662 for i, name in enumerate(prob.config.tests): |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
663 # Same here; we'll need to iterate over them twice |
23 | 664 try: |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
665 l = len(name) |
23 | 666 except Exception: |
667 try: | |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
668 name = tuple(name) |
23 | 669 except TypeError: |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
670 name = (name,) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
671 l = len(name) |
58 | 672 if l > 1: |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
673 prob.config.usegroups = True |
58 | 674 newtests.append(name) |
675 if prob.config.usegroups: | |
676 prob.config.tests = newtests | |
677 del newtests | |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
678 |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
679 # First get prob.cache.padoutput right, |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
680 # then yield the actual test cases |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
681 for i in prob.config.dummies: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
682 s = 'sample ' + str(i).zfill(prob.config.paddummies) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
683 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) |
16 | 684 if prob.config.usegroups: |
39
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
685 for group in prob.config.tests: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
686 for i in group: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
687 s = str(i).zfill(prob.config.padtests) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
688 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
689 for i in prob.config.dummies: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
690 s = str(i).zfill(prob.config.paddummies) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
691 yield _types[prob.config.kind](prob, s, True, 0) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
692 for group in prob.config.tests: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
693 yield problem.TestGroup() |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
694 for i in group: |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
695 s = str(i).zfill(prob.config.padtests) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
696 yield _types[prob.config.kind](prob, s, False, prob.config.pointmap.get(i, prob.config.pointmap.get(None, prob.config.maxexitcode if prob.config.maxexitcode else 1))) |
2b459f9743b4
Test groups are now supported
Oleg Oshmyan <chortos@inbox.lv>
parents:
27
diff
changeset
|
697 yield problem.test_context_end |
16 | 698 else: |
699 for i in prob.config.tests: | |
21 | 700 s = str(i).zfill(prob.config.padtests) |
701 prob.cache.padoutput = max(prob.cache.padoutput, len(s)) | |
702 for i in prob.config.dummies: | |
703 s = str(i).zfill(prob.config.paddummies) | |
704 yield _types[prob.config.kind](prob, s, True, 0) | |
705 for i in prob.config.tests: | |
706 s = str(i).zfill(prob.config.padtests) | |
707 yield _types[prob.config.kind](prob, s, False, prob.config.pointmap.get(i, prob.config.pointmap.get(None, prob.config.maxexitcode if prob.config.maxexitcode else 1))) |