comparison win32.py @ 140:388ae061c915

The win32 module now trusts the wall-clock time reported by GetProcessTimes
author Oleg Oshmyan <chortos@inbox.lv>
date Tue, 24 May 2011 22:46:30 +0100
parents e84f33a60a5c
children dfde0f5e0984
comparison
equal deleted inserted replaced
139:2d465a8a679c 140:388ae061c915
39 from collections import namedtuple 39 from collections import namedtuple
40 except ImportError: 40 except ImportError:
41 from operator import itemgetter 41 from operator import itemgetter
42 class ProcessTimes(tuple): 42 class ProcessTimes(tuple):
43 __slots__ = () 43 __slots__ = ()
44 __new__ = lambda cls, kernel, user: tuple.__new__(cls, (kernel, user)) 44 def __new__(cls, creation, exit, kernel, user):
45 return tuple.__new__(cls, (creation, exit, kernel, user))
45 __getnewargs__ = lambda self: tuple(self) 46 __getnewargs__ = lambda self: tuple(self)
46 kernel, user = (property(itemgetter(i)) for i in (0, 1)) 47 creation, exit, kernel, user = map(property, map(itemgetter, range(4)))
47 else: 48 else:
48 ProcessTimes = namedtuple('ProcessTimes', 'kernel user') 49 ProcessTimes = namedtuple('ProcessTimes', 'creation exit kernel user')
49 50
50 __all__ = 'call', 'kill', 'pause', 'clock' 51 __all__ = 'call', 'kill', 'pause', 'clock'
51 52
52 53
53 from functools import wraps 54 from functools import wraps
175 # Available on NT 3.5 and up, NT line only 176 # Available on NT 3.5 and up, NT line only
176 GetProcessTimes = None 177 GetProcessTimes = None
177 else: 178 else:
178 def errcheck(result, func, args): 179 def errcheck(result, func, args):
179 if not result: raise WinError() 180 if not result: raise WinError()
180 ftimes = [t.dwHighDateTime << 32 | t.dwLowDateTime for t in args[3:]] 181 times = ((t.dwHighDateTime << 32 | t.dwLowDateTime) / 10000000
181 kernel = ftimes[0] / 10000000 182 for t in args[1:])
182 user = ftimes[1] / 10000000 183 return ProcessTimes(*times)
183 return ProcessTimes(kernel, user)
184 GetProcessTimes.errcheck = errcheck 184 GetProcessTimes.errcheck = errcheck
185 185
186 186
187 class PROCESS_MEMORY_COUNTERS(Structure): 187 class PROCESS_MEMORY_COUNTERS(Structure):
188 _fields_ = (('cb', DWORD), 188 _fields_ = (('cb', DWORD),
514 ir.EventType == KEY_EVENT and 514 ir.EventType == KEY_EVENT and
515 ir.Event.KeyEvent.bKeyDown and 515 ir.Event.KeyEvent.bKeyDown and
516 ir.Event.KeyEvent.wVirtualKeyCode == 27): 516 ir.Event.KeyEvent.wVirtualKeyCode == 27):
517 raise testcases.CanceledByUser 517 raise testcases.CanceledByUser
518 case.time_stopped = clock() 518 case.time_stopped = clock()
519 if GetProcessTimes and (case.maxcputime or not case.maxwalltime): 519 if GetProcessTimes:
520 try: 520 try:
521 times = GetProcessTimes(case.process._handle) 521 times = GetProcessTimes(case.process._handle)
522 except WindowsError: 522 except WindowsError:
523 pass 523 pass
524 else: 524 else:
525 time = times.kernel + times.user 525 if case.maxcputime or not case.maxwalltime:
526 case.time_stopped = time 526 cputime = times.kernel + times.user
527 case.time_started = 0 527 case.time_stopped = cputime
528 case.time_limit_string = case.cpu_time_limit_string 528 case.time_started = 0
529 if case.maxcputime and time > case.maxcputime: 529 case.time_limit_string = case.cpu_time_limit_string
530 raise testcases.CPUTimeLimitExceeded 530 if case.maxcputime and cputime > case.maxcputime:
531 raise testcases.CPUTimeLimitExceeded
532 else:
533 case.time_stopped = times.exit
534 case.time_started = times.creation
535 walltime = times.exit - times.creation
536 if case.maxwalltime and walltime > case.maxwalltime:
537 raise testcases.WallTimeLimitExceeded
531 if case.maxcputime and case.process.returncode == 1816: 538 if case.maxcputime and case.process.returncode == 1816:
532 raise testcases.CPUTimeLimitExceeded 539 raise testcases.CPUTimeLimitExceeded
533 if case.maxmemory and GetProcessMemoryInfo: 540 if case.maxmemory and GetProcessMemoryInfo:
534 try: 541 try:
535 counters = GetProcessMemoryInfo(case.process._handle) 542 counters = GetProcessMemoryInfo(case.process._handle)