diff 2.00/problem.py @ 22:f07b7a431ea6

Further 2.00 work Testconfs in all supported kinds of archives should now work. Test runs are now cancelled by pressing Escape rather than Ctrl+C. Improved time control. Greatly improved temporary and helper file cleanup. The pause configuration variable can now be a callable and is now processed using subprocess rather than system().
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 22 Sep 2010 22:01:56 +0000
parents ec6f1a132109
children c1f52b5d80d6
line wrap: on
line diff
--- a/2.00/problem.py	Fri Aug 06 15:39:29 2010 +0000
+++ b/2.00/problem.py	Wed Sep 22 22:01:56 2010 +0000
@@ -10,9 +10,9 @@
 	import __main__
 	__main__.import_error(sys.exc_info()[1])
 else:
-	from __main__ import clock
+	from __main__ import clock, options
 
-import sys, re
+import os, re, sys
 
 try:
 	import signal
@@ -26,7 +26,7 @@
 	for name in dir(signal):
 		if re.match('SIG[A-Z]+$', name):
 			value = signal.__dict__[name]
-			if isinstance(value, int) and (value not in signalnames or signalnames[value][3:] not in unixnames):
+			if isinstance(value, int) and (value not in signalnames or name[3:] in unixnames):
 				signalnames[value] = name
 	del unixnames
 
@@ -58,99 +58,112 @@
 		raise NotImplementedError
 	
 	def test(prob):
-		real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
-		for case in prob.testcases:
-			ntotal += 1
-			max += case.points
-			if case.points: nvalued += 1
-			granted = 0
-			id = str(case.id)
-			if case.isdummy:
-				id = 'sample ' + id
-			say('%*s: ' % (prob.cache.padoutput, id), end='')
-			sys.stdout.flush()
-			try:
-				granted = case()
-			except KeyboardInterrupt:
-				if not hasattr(case, 'time_stopped'):
-					# Too quick! The testing has not even started!
-					raise
-				verdict = 'canceled by the user'
-			except testcases.TimeLimitExceeded:
-				verdict = 'time limit exceeded'
-			except testcases.WrongAnswer:
-				e = sys.exc_info()[1]
-				if e.comment:
-					verdict = 'wrong answer (%s)' % e.comment
-				else:
-					verdict = 'wrong answer'
-			except testcases.NonZeroExitCode:
-				e = sys.exc_info()[1]
-				if e.exitcode < 0:
-					if sys.platform == 'win32':
-						verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000)
-					elif -e.exitcode in signalnames:
-						verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode])
+		try:
+			real = max = ntotal = nvalued = ncorrect = ncorrectvalued = 0
+			for case in prob.testcases:
+				ntotal += 1
+				max += case.points
+				if case.points: nvalued += 1
+				granted = 0
+				id = str(case.id)
+				if case.isdummy:
+					id = 'sample ' + id
+				say('%*s: ' % (prob.cache.padoutput, id), end='')
+				sys.stdout.flush()
+				try:
+					granted = case(lambda: (say('%7.3f%s s, ' % (case.time_stopped - case.time_started, case.time_limit_string), end=''), sys.stdout.flush()))
+				except testcases.CanceledByUser:
+					verdict = 'canceled by the user'
+				except testcases.TimeLimitExceeded:
+					verdict = 'time limit exceeded'
+				except testcases.WrongAnswer:
+					e = sys.exc_info()[1]
+					if e.comment:
+						verdict = 'wrong answer (%s)' % e.comment
+					else:
+						verdict = 'wrong answer'
+				except testcases.NonZeroExitCode:
+					e = sys.exc_info()[1]
+					if e.exitcode < 0:
+						if sys.platform == 'win32':
+							verdict = 'terminated with error 0x%X' % (e.exitcode + 0x100000000)
+						elif -e.exitcode in signalnames:
+							verdict = 'terminated by signal %d (%s)' % (-e.exitcode, signalnames[-e.exitcode])
+						else:
+							verdict = 'terminated by signal %d' % -e.exitcode
 					else:
-						verdict = 'terminated by signal %d' % -e.exitcode
-				else:
-					verdict = 'non-zero return code %d' % e.exitcode
-			except testcases.CannotStartTestee:
-				e = sys.exc_info()[1]
-				if e.upstream.strerror:
-					verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower()
-				else:
-					verdict = 'cannot launch the program to test'
-			except testcases.CannotStartValidator:
-				e = sys.exc_info()[1]
-				if e.upstream.strerror:
-					verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower()
+						verdict = 'non-zero return code %d' % e.exitcode
+				except testcases.CannotStartTestee:
+					e = sys.exc_info()[1]
+					if e.upstream.strerror:
+						verdict = 'cannot launch the program to test (%s)' % e.upstream.strerror.lower()
+					else:
+						verdict = 'cannot launch the program to test'
+				except testcases.CannotStartValidator:
+					e = sys.exc_info()[1]
+					if e.upstream.strerror:
+						verdict = 'cannot launch the validator (%s)' % e.upstream.strerror.lower()
+					else:
+						verdict = 'cannot launch the validator'
+				except testcases.CannotReadOutputFile:
+					e = sys.exc_info()[1]
+					if e.upstream.strerror:
+						verdict = 'cannot read the output file (%s)' % e.upstream.strerror.lower()
+					else:
+						verdict = 'cannot read the output file'
+				except testcases.CannotReadInputFile:
+					e = sys.exc_info()[1]
+					if e.upstream.strerror:
+						verdict = 'cannot read the input file (%s)' % e.upstream.strerror.lower()
+					else:
+						verdict = 'cannot read the input file'
+				except testcases.CannotReadAnswerFile:
+					e = sys.exc_info()[1]
+					if e.upstream.strerror:
+						verdict = 'cannot read the reference output file (%s)' % e.upstream.strerror.lower()
+					else:
+						verdict = 'cannot read the reference output file'
+				except testcases.TestCaseNotPassed:
+					e = sys.exc_info()[1]
+					verdict = 'unspecified reason [this may be a bug in test.py] (%s)' % e
+				#except Exception:
+				#	e = sys.exc_info()[1]
+				#	verdict = 'unknown error [this may be a bug in test.py] (%s)' % e
 				else:
-					verdict = 'cannot launch the validator'
-			except testcases.CannotReadOutputFile:
-				e = sys.exc_info()[1]
-				if e.upstream.strerror:
-					verdict = 'cannot read the output file (%s)' % e.upstream.strerror.lower()
-				else:
-					verdict = 'cannot read the output file'
-			except testcases.CannotReadInputFile:
-				e = sys.exc_info()[1]
-				if e.upstream.strerror:
-					verdict = 'cannot read the input file (%s)' % e.upstream.strerror.lower()
-				else:
-					verdict = 'cannot read the input file'
-			except testcases.CannotReadAnswerFile:
-				e = sys.exc_info()[1]
-				if e.upstream.strerror:
-					verdict = 'cannot read the reference output file (%s)' % e.upstream.strerror.lower()
-				else:
-					verdict = 'cannot read the reference output file'
-			except testcases.TestCaseNotPassed:
-				e = sys.exc_info()[1]
-				verdict = 'unspecified reason [this may be a bug in test.py] (%s)' % e
-			#except Exception:
-			#	e = sys.exc_info()[1]
-			#	verdict = 'unknown error [this may be a bug in test.py] (%s)' % e
+					if hasattr(granted, '__iter__'):
+						granted, comment = granted
+						if comment:
+							comment = ' (%s)' % comment
+					else:
+						comment = ''
+					if granted == case.points:
+						ncorrect += 1
+						if granted: ncorrectvalued += 1
+						verdict = 'OK' + comment
+					elif not granted:
+						verdict = 'wrong answer' + comment
+					else:
+						verdict = 'partly correct' + comment
+				say('%g/%g, %s' % (granted, case.points, verdict))
+				real += granted
+			weighted = real * prob.config.taskweight / max if max else 0
+			if nvalued != ntotal:
+				say('Problem total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, ncorrectvalued, nvalued, real, max, weighted, prob.config.taskweight))
 			else:
-				if hasattr(granted, '__iter__'):
-					granted, comment = granted
-					if comment:
-						comment = ' (%s)' % comment
-				else:
-					comment = ''
-				if granted == case.points:
-					ncorrect += 1
-					if granted: ncorrectvalued += 1
-					verdict = 'OK' + comment
-				elif not granted:
-					verdict = 'wrong answer' + comment
-				else:
-					verdict = 'partly correct' + comment
-			say('%.3f%s s, %g/%g, %s' % (case.time_stopped - case.time_started, case.time_limit_string, granted, case.points, verdict))
-			real += granted
-		weighted = real * prob.config.taskweight / max if max else 0
-		if nvalued != ntotal:
-			say('Grand total: %d/%d tests (%d/%d valued); %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, ncorrectvalued, nvalued, real, max, weighted, prob.config.taskweight))
-		else:
-			say('Grand total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
-		return weighted, prob.config.taskweight
+				say('Problem total: %d/%d tests; %g/%g points; weighted score: %g/%g' % (ncorrect, ntotal, real, max, weighted, prob.config.taskweight))
+			return weighted, prob.config.taskweight
+		finally:
+			if options.erase and (not prob.config.stdio or case.validator):
+				for var in 'in', 'out':
+					name = getattr(prob.config, var + 'name')
+					if name:
+						try:
+							os.remove(name)
+						except Exception:
+							pass
+				if case.validator and not callable(case.validator):
+					if prob.config.ansname:
+						try:
+							os.remove(prob.config.ansname)
+						except Exception:
+							pass
\ No newline at end of file