changeset 85:741ae3391b61

Moved clock/time detection into platform-specific modules and testcases
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 24 Feb 2011 23:59:48 +0000
parents 953a5baa406d
children 8cd7a732f2f3
files problem.py testcases.py unix.py upreckon-vcs win32.py
diffstat 5 files changed, 32 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/problem.py	Thu Feb 24 00:29:32 2011 +0000
+++ b/problem.py	Thu Feb 24 23:59:48 2011 +0000
@@ -10,7 +10,7 @@
 	import __main__
 	__main__.import_error(sys.exc_info()[1])
 else:
-	from __main__ import clock, options
+	from __main__ import options
 
 import os, re, sys
 
--- a/testcases.py	Thu Feb 24 00:29:32 2011 +0000
+++ b/testcases.py	Thu Feb 24 23:59:48 2011 +0000
@@ -12,7 +12,7 @@
 	import __main__
 	__main__.import_error(sys.exc_info()[1])
 else:
-	from __main__ import clock, options
+	from __main__ import options
 
 import glob, re, sys, tempfile, time
 from subprocess import Popen, PIPE, STDOUT
@@ -20,6 +20,21 @@
 import os
 devnull = open(os.path.devnull, 'w+')
 
+if options.autotime:
+	# This is really a dirty hack that assumes that sleep() does not spend
+	# the CPU time of the current process and that if clock() measures
+	# wall-clock time, then it is more precise than time() is. Both these
+	# assumptions are true on all platforms I have tested this on so far,
+	# but I am not aware of any guarantee that they will both be true
+	# on every other platform.
+	c = time.clock()
+	time.sleep(1)
+	c = time.clock() - c
+	if int(c + .5) == 1:
+		clock = time.clock
+	else:
+		clock = time.time
+
 try:
 	from win32 import *
 except Exception:
--- a/unix.py	Thu Feb 24 00:29:32 2011 +0000
+++ b/unix.py	Thu Feb 24 23:59:48 2011 +0000
@@ -10,17 +10,24 @@
 	import __main__
 	__main__.import_error(sys.exc_info()[1])
 
-from __main__ import clock
 from subprocess import Popen
 import os, sys
 
 try:
+	from testcases import clock
+except ImportError:
+	if sys.platform.startswith('java'):
+		from time import clock
+	else:
+		from time import time as clock
+
+try:
 	from signal import SIGTERM, SIGKILL
 except ImportError:
 	SIGTERM = 15
 	SIGKILL = 9
 
-__all__ = 'call', 'kill', 'terminate', 'pause'
+__all__ = 'call', 'kill', 'terminate', 'pause', 'clock'
 
 
 if not sys.stdin.isatty():
--- a/upreckon-vcs	Thu Feb 24 00:29:32 2011 +0000
+++ b/upreckon-vcs	Thu Feb 24 23:59:48 2011 +0000
@@ -64,25 +64,6 @@
 if options.legacy:
 	compat.pseudobuiltins += 'xrange',
 
-if options.autotime:
-	# This is really a dirty hack that assumes that sleep() does not spend
-	# the CPU time of the current process and that if clock() measures
-	# wall-clock time, then it is more precise than time() is. Both these
-	# assumptions are true on all platforms I have tested this on so far,
-	# but I am not aware of any guarantee that they will both be true
-	# on every other platform.
-	c = time.clock()
-	time.sleep(1)
-	c = time.clock() - c
-	if int(c + .5) == 1:
-		clock = time.clock
-	else:
-		clock = time.time
-elif sys.platform == 'win32':
-	clock = time.clock
-else:
-	clock = time.time
-
 try:
 	import testcases
 except ImportError:
--- a/win32.py	Thu Feb 24 00:29:32 2011 +0000
+++ b/win32.py	Thu Feb 24 23:59:48 2011 +0000
@@ -14,7 +14,11 @@
 from ctypes.wintypes import *
 from msvcrt import getch as pause
 from subprocess import Popen
-from __main__ import clock
+
+try:
+	from testcases import clock
+except ImportError:
+	from time import clock
 
 # Defaults that may be overwritten by values from _subprocess
 INFINITE = -1
@@ -43,7 +47,7 @@
 else:
 	ProcessTimes = namedtuple('ProcessTimes', 'kernel user')
 
-__all__ = 'call', 'kill', 'terminate', 'pause'
+__all__ = 'call', 'kill', 'terminate', 'pause', 'clock'
 
 
 # Automatically convert _subprocess handle objects into low-level HANDLEs