annotate testcases.py @ 63:fb9d0223a871

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