annotate upreckon/win32.py @ 157:5f9e6121161a

Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows Also fixed an indentation error in win32 apparently caused by a bug in my text editor.
author Oleg Oshmyan <chortos@inbox.lv>
date Wed, 08 Jun 2011 20:53:26 +0100
parents f8041e1e4d0d
children ebb35960b5bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
1 # Copyright (c) 2010-2011 Chortos-2 <chortos@inbox.lv>
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
2
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
3 from __future__ import division, with_statement
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
4
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
5 from .compat import *
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
6 from .exceptions import *
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
7
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
8 from ctypes import *
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
9 from ctypes.wintypes import *
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
10 from msvcrt import getch as pause
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
11 from time import clock
141
dfde0f5e0984 A couple of Win32 fixes
Oleg Oshmyan <chortos@inbox.lv>
parents: 140
diff changeset
12 import os, subprocess, sys, time
95
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
13
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
14 try:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
15 from _winreg import *
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
16 except ImportError:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
17 from winreg import *
85
741ae3391b61 Moved clock/time detection into platform-specific modules and testcases
Oleg Oshmyan <chortos@inbox.lv>
parents: 82
diff changeset
18
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
19 # Defaults that may be overwritten by values from _subprocess
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
20 INFINITE = -1
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
21 STD_INPUT_HANDLE = -10
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
22 WAIT_OBJECT_0 = 0
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
23
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
24 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
25 from _subprocess import *
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
26 except ImportError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
27 pass
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
28
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
29 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
30 from numbers import Integral
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
31 except ImportError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
32 Integral = int, long
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
33
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
34 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
35 from collections import namedtuple
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
36 except ImportError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
37 from operator import itemgetter
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
38 class ProcessTimes(tuple):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
39 __slots__ = ()
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
40 def __new__(cls, creation, exit, kernel, user):
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
41 return tuple.__new__(cls, (creation, exit, kernel, user))
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
42 __getnewargs__ = lambda self: tuple(self)
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
43 creation, exit, kernel, user = map(property, map(itemgetter, range(4)))
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
44 else:
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
45 ProcessTimes = namedtuple('ProcessTimes', 'creation exit kernel user')
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
46
155
f8041e1e4d0d win32 now includes its App-Path-aware Popen wrapper in its __all__
Oleg Oshmyan <chortos@inbox.lv>
parents: 148
diff changeset
47 __all__ = 'call', 'kill', 'pause', 'clock', 'Popen'
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
48
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
49
95
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
50 from functools import wraps
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
51 pathext = [''] + os.environ['PATHEXT'].split(';')
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
52 @wraps(subprocess.Popen)
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
53 def Popen(cmdline, *args, **kwargs):
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
54 try:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
55 return subprocess.Popen(cmdline, *args, **kwargs)
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
56 except WindowsError:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
57 for ext in pathext:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
58 path = cmdline[0] + ext
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
59 newcmdline = type(cmdline)((path,)) + cmdline[1:]
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
60 try:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
61 return subprocess.Popen(newcmdline, *args, **kwargs)
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
62 except WindowsError:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
63 pass
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
64 for branch in HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
65 try:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
66 path = (R'SOFTWARE\Microsoft\Windows\CurrentVersion'
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
67 R'\App Paths\%s%s' % (cmdline[0], ext))
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
68 path = QueryValue(branch, path)
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
69 break
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
70 except WindowsError:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
71 pass
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
72 else:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
73 continue
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
74 if path[0] == '"' == path[-1]:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
75 path = path[1:-1]
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
76 newcmdline = type(cmdline)((path,)) + cmdline[1:]
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
77 try:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
78 return subprocess.Popen(newcmdline, *args, **kwargs)
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
79 except WindowsError:
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
80 pass
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
81 # I'd like to transparently re-raise the exception generated
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
82 # on the very first try, but syntax differences preclude me from
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
83 # doing so in Python 2 and it can't be done at all in Python 3
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
84 raise
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
85
6c2997616bdf Added a search for the location of the executable for Popen on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 91
diff changeset
86
134
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
87 # Automatically convert _subprocess handle objects into low-level
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
88 # HANDLEs and replicate their functionality for our own use
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
89 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
90 _subprocess_handle = type(GetCurrentProcess())
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
91 except NameError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
92 _subprocess_handle = Integral
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
93 class Handle(object):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
94 @staticmethod
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
95 def from_param(handle):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
96 if isinstance(handle, (_subprocess_handle, Integral)):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
97 return HANDLE(int(handle))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
98 elif isinstance(handle, Handle):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
99 return HANDLE(handle.handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
100 elif isinstance(handle, HANDLE):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
101 return handle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
102 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
103 raise TypeError('cannot convert %s to a handle' %
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
104 type(handle).__name__)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
105
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
106 __slots__ = 'handle'
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
107
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
108 def __init__(self, handle):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
109 if isinstance(handle, Integral):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
110 self.handle = handle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
111 elif isinstance(handle, HANDLE):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
112 self.handle = handle.value
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
113 elif isinstance(handle, Handle):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
114 self.handle = handle.handle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
115 elif isinstance(handle, _subprocess_handle):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
116 handle = HANDLE(int(handle))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
117 flags = DWORD()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
118 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
119 if windll.kernel32.GetHandleInformation(handle, byref(flags)):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
120 flags = flags.value
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
121 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
122 flags = 0
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
123 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
124 # Available on NT 3.51 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
125 flags = 0
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
126 proc = HANDLE(int(GetCurrentProcess()))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
127 handle = DuplicateHandle(proc, handle, proc, 0, flags & 1, 2)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
128 self.handle = handle.Detach()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
129 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
130 raise TypeError("Handle() argument must be a handle, not '%s'" %
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
131 type(handle).__name__)
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
132
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
133 def __int__(self):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
134 return int(self.handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
135
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
136 def Detach(self):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
137 handle = self.handle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
138 self.handle = None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
139 return handle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
140
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
141 # This is also __del__, so only locals are accessed
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
142 def Close(self, _CloseHandle=windll.kernel32.CloseHandle, _HANDLE=HANDLE):
131
7923faf392fe win32.Handle.__del__ no longer raises if __init__ gets wrong arguments
Oleg Oshmyan <chortos@inbox.lv>
parents: 128
diff changeset
143 if getattr(self, 'handle', None):
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
144 _CloseHandle(_HANDLE(self.handle))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
145 self.handle = None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
146 __del__ = Close
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
147
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
148 CHAR = c_char
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
149 INVALID_HANDLE_VALUE = HANDLE(-1).value
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
150 LPDWORD = POINTER(DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
151 LPFILETIME = POINTER(FILETIME)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
152 SIZE_T = ULONG_PTR = WPARAM
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
153 ULONGLONG = c_ulonglong
148
aa343ff41c27 Attempted to work around unwarranted ERROR_ACCESS_DENIED on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
154 ERROR_ACCESS_DENIED = 5
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
155
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
156 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
157 unicode
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
158 except NameError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
159 LPCTSTR = LPCWSTR
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
160 UNISUFFIX = 'W'
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
161 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
162 LPCTSTR = LPCSTR
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
163 UNISUFFIX = 'A'
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
164
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
165
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
166 prototype = WINFUNCTYPE(BOOL, Handle,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
167 LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
168 flags = ((1, 'process'),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
169 (2, 'creation'), (2, 'exit'), (2, 'kernel'), (2, 'user'))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
170 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
171 GetProcessTimes = prototype(('GetProcessTimes', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
172 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
173 # Available on NT 3.5 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
174 GetProcessTimes = None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
175 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
176 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
177 if not result: raise WinError()
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
178 times = ((t.dwHighDateTime << 32 | t.dwLowDateTime) / 10000000
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
179 for t in args[1:])
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
180 return ProcessTimes(*times)
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
181 GetProcessTimes.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
182
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
183
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
184 class PROCESS_MEMORY_COUNTERS(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
185 _fields_ = (('cb', DWORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
186 ('PageFaultCount', DWORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
187 ('PeakWorkingSetSize', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
188 ('WorkingSetSize', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
189 ('QuotaPeakPagedPoolUsage', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
190 ('QuotaPagedPoolUsage', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
191 ('QuotaPeakNonPagedPoolUsage', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
192 ('QuotaNonPagedPoolUsage', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
193 ('PagefileUsage', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
194 ('PeakPagefileUsage', SIZE_T))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
195
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
196 prototype = WINFUNCTYPE(BOOL, Handle, POINTER(PROCESS_MEMORY_COUNTERS), DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
197 flags = ((1, 'process'), (2, 'counters'),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
198 (5, 'cb', sizeof(PROCESS_MEMORY_COUNTERS)))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
199 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
200 GetProcessMemoryInfo = prototype(('GetProcessMemoryInfo', windll.psapi),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
201 flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
202 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
203 # Available on NT 4.0 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
204 GetProcessMemoryInfo = None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
205 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
206 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
207 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
208 return args
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
209 GetProcessMemoryInfo.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
210
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
211
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
212 class _uChar_union(Union):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
213 _fields_ = (('UnicodeChar', WCHAR),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
214 ('AsciiChar', CHAR))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
215
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
216 class KEY_EVENT_RECORD(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
217 _fields_ = (('bKeyDown', BOOL),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
218 ('wRepeatCount', WORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
219 ('wVirtualKeyCode', WORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
220 ('wVirtualScanCode', WORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
221 ('uChar', _uChar_union),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
222 ('dwControlKeyState', DWORD))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
223
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
224 RIGHT_ALT_PRESSED = 0x001
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
225 LEFT_ALT_PRESSED = 0x002
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
226 RIGHT_CTRL_PRESSED = 0x004
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
227 LEFT_CTRL_PRESSED = 0x008
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
228 SHIFT_PRESSED = 0x010
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
229 NUMLOCK_ON = 0x020
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
230 SCROLLLOCK_ON = 0x040
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
231 CAPSLOCK_ON = 0x080
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
232 ENHANCED_KEY = 0x100
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
233
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
234 class _Event_union(Union):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
235 _fields_ = ('KeyEvent', KEY_EVENT_RECORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
236
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
237 class INPUT_RECORD(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
238 _fields_ = (('EventType', WORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
239 ('Event', _Event_union))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
240
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
241 KEY_EVENT = 0x01
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
242 MOUSE_EVENT = 0x02
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
243 WINDOW_BUFFER_SIZE_EVENT = 0x04
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
244 MENU_EVENT = 0x08
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
245 FOCUS_EVENT = 0x10
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
246
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
247 prototype = WINFUNCTYPE(BOOL, Handle, POINTER(INPUT_RECORD), DWORD, LPDWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
248 flags = (1, 'input'), (2, 'buffer'), (5, 'length', 1), (2, 'number_read')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
249 ReadConsoleInput = prototype(('ReadConsoleInputA', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
250 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
251 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
252 return args[1] if args[3] else None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
253 ReadConsoleInput.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
254
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
255
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
256 prototype = WINFUNCTYPE(BOOL, Handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
257 flags = (1, 'input'),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
258 FlushConsoleInputBuffer = prototype(('FlushConsoleInputBuffer',
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
259 windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
260 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
261 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
262 FlushConsoleInputBuffer.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
263
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
264
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
265 prototype = WINFUNCTYPE(BOOL, Handle, DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
266 flags = (1, 'console'), (1, 'mode')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
267 SetConsoleMode = prototype(('SetConsoleMode', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
268 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
269 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
270 SetConsoleMode.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
271
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
272 ENABLE_PROCESSED_INPUT = 0x001
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
273 ENABLE_LINE_INPUT = 0x002
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
274 ENABLE_ECHO_INPUT = 0x004
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
275 ENABLE_WINDOW_INPUT = 0x008
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
276 ENABLE_MOUSE_INPUT = 0x010
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
277 ENABLE_INSERT_MODE = 0x020
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
278 ENABLE_QUICK_EDIT_MODE = 0x040
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
279 ENABLE_EXTENDED_FLAGS = 0x080
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
280
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
281 ENABLE_PROCESSED_OUTPUT = 1
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
282 ENABLE_WRAP_AT_EOL_OUTPUT = 2
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
283
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
284
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
285 prototype = WINFUNCTYPE(HANDLE, c_void_p, LPCTSTR)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
286 flags = (5, 'attributes'), (1, 'name')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
287 try:
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
288 CreateJobObject = prototype(('CreateJobObject'+UNISUFFIX, windll.kernel32),
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
289 flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
290 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
291 # Available on 2000 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
292 CreateJobObject = lambda name: None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
293 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
294 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
295 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
296 return Handle(result)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
297 CreateJobObject.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
298
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
299
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
300 prototype = WINFUNCTYPE(BOOL, Handle, Handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
301 flags = (1, 'job'), (1, 'handle')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
302 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
303 AssignProcessToJobObject = prototype(('AssignProcessToJobObject',
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
304 windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
305 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
306 # Available on 2000 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
307 AssignProcessToJobObject = lambda job, handle: None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
308 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
309 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
310 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
311 AssignProcessToJobObject.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
312
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
313
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
314 class JOBOBJECT_BASIC_LIMIT_INFORMATION(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
315 _fields_ = (('PerProcessUserTimeLimit', LARGE_INTEGER),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
316 ('PerJobUserTimeLimit', LARGE_INTEGER),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
317 ('LimitFlags', DWORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
318 ('MinimumWorkingSetSize', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
319 ('MaximumWorkingSetSize', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
320 ('ActiveProcessLimit', DWORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
321 ('Affinity', ULONG_PTR),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
322 ('PriorityClass', DWORD),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
323 ('SchedulingClass', DWORD))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
324
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
325 JOB_OBJECT_LIMIT_WORKINGSET = 0x0001
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
326 JOB_OBJECT_LIMIT_PROCESS_TIME = 0x0002
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
327 JOB_OBJECT_LIMIT_JOB_TIME = 0x0004
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
328 JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x0008
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
329 JOB_OBJECT_LIMIT_AFFINITY = 0x0010
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
330 JOB_OBJECT_LIMIT_PRIORITY_CLASS = 0x0020
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
331 JOB_OBJECT_LIMIT_PRESERVE_JOB_TIME = 0x0040
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
332 JOB_OBJECT_LIMIT_SCHEDULING_CLASS = 0x0080
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
333 JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x0100
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
334 JOB_OBJECT_LIMIT_JOB_MEMORY = 0x0200
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
335 JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION = 0x0400
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
336 JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x0800
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
337 JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK = 0x1000
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
338 JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
339 JOB_OBJECT_LIMIT_SUBSET_AFFINITY = 0x4000
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
340
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
341 class IO_COUNTERS(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
342 _fields_ = (('ReadOperationCount', ULONGLONG),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
343 ('WriteOperationCount', ULONGLONG),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
344 ('OtherOperationCount', ULONGLONG),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
345 ('ReadTransferCount', ULONGLONG),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
346 ('WriteTransferCount', ULONGLONG),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
347 ('OtherTransferCount', ULONGLONG))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
348
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
349 class JOBOBJECT_EXTENDED_LIMIT_INFORMATION(Structure):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
350 _fields_ = (('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
351 ('IoInfo', IO_COUNTERS),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
352 ('ProcessMemoryLimit', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
353 ('JobMemoryLimit', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
354 ('PeakProcessMemoryUsed', SIZE_T),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
355 ('PeakJobMemoryUsed', SIZE_T))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
356
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
357 prototype = WINFUNCTYPE(BOOL, Handle, c_int, c_void_p, DWORD)
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
358 flags = (1, 'job'), (1, 'infoclass'), (1, 'info'), (1, 'infosize')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
359 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
360 _setjobinfo = prototype(('SetInformationJobObject',windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
361 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
362 # Available on 2000 and up, NT line only
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
363 SetInformationJobObject = lambda job, infoclass, info: None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
364 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
365 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
366 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
367 _setjobinfo.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
368 def SetInformationJobObject(job, infoclass, info):
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
369 return _setjobinfo(job, infoclass, byref(info), sizeof(info))
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
370
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
371 (
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
372 JobObjectBasicAccountingInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
373 JobObjectBasicLimitInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
374 JobObjectBasicProcessIdList,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
375 JobObjectBasicUIRestrictions,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
376 JobObjectSecurityLimitInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
377 JobObjectEndOfJobTimeInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
378 JobObjectAssociateCompletionPortInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
379 JobObjectBasicAndIoAccountingInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
380 JobObjectExtendedLimitInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
381 JobObjectJobSetInformation,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
382 MaxJobObjectInfoClass
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
383 ) = range(1, 12)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
384
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
385
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
386 prototype = WINFUNCTYPE(DWORD, DWORD, POINTER(HANDLE), BOOL, DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
387 flags = (1, 'count'), (1, 'handles'), (1, 'wait_all'), (1, 'milliseconds')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
388 _wait_multiple = prototype(('WaitForMultipleObjects', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
389 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
390 if result == WAIT_FAILED: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
391 return args
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
392 _wait_multiple.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
393 def WaitForMultipleObjects(handles, wait_all, timeout):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
394 n = len(handles)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
395 handles = (Handle.from_param(handle) for handle in handles)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
396 timeout = ceil(timeout * 1000)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
397 return _wait_multiple(n, (HANDLE * n)(*handles), wait_all, timeout)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
398
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
399 # WAIT_OBJECT_0 defined at the top of the file
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
400 WAIT_ABANDONED_0 = 0x00000080
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
401 WAIT_TIMEOUT = 0x00000102
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
402 WAIT_FAILED = 0xFFFFFFFF
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
403
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
404
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
405 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
406 _wait_single = WaitForSingleObject
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
407 except NameError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
408 prototype = WINFUNCTYPE(DWORD, Handle, DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
409 flags = (1, 'handle'), (1, 'milliseconds')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
410 _wait_single = prototype(('WaitForSingleObject', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
411 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
412 if result == WAIT_FAILED: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
413 return args
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
414 _wait_single.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
415 def WaitForSingleObject(handle, timeout):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
416 return _wait_single(handle, ceil(timeout * 1000))
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
417
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
418
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
419 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
420 GetStdHandle
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
421 except NameError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
422 prototype = WINFUNCTYPE(HANDLE, DWORD)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
423 flags = (1, 'which'),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
424 GetStdHandle = prototype(('GetStdHandle', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
425 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
426 if result == INVALID_HANDLE_VALUE: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
427 return args if result else None
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
428 GetStdHandle.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
429
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
430
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
431 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
432 TerminateProcess
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
433 except NameError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
434 prototype = WINFUNCTYPE(BOOL, Handle, UINT)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
435 flags = (1, 'process'), (1, 'exitcode')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
436 TerminateProcess = prototype(('TerminateProcess', windll.kernel32), flags)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
437 def errcheck(result, func, args):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
438 if not result: raise WinError()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
439 TerminateProcess.errcheck = errcheck
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
440
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
441
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
442 # Do not show error messages due to errors in the program being tested
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
443 try:
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
444 errmode = windll.kernel32.GetErrorMode()
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
445 except AttributeError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
446 # GetErrorMode is available on Vista/2008 and up
81
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
447 errmode = windll.kernel32.SetErrorMode(0)
24752db487c5 Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents: 80
diff changeset
448 windll.kernel32.SetErrorMode(errmode | 0x8003)
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
449
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
450 stdin = GetStdHandle(STD_INPUT_HANDLE)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
451 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
452 SetConsoleMode(stdin, ENABLE_PROCESSED_INPUT)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
453 except WindowsError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
454 console_input = False
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
455 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
456 console_input = True
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
457 FlushConsoleInputBuffer(stdin)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
458
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
459 def call(*args, **kwargs):
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
460 case = kwargs.pop('case')
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
461 job = CreateJobObject(None)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
462 flags = 0
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
463 if case.maxcputime:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
464 flags |= JOB_OBJECT_LIMIT_PROCESS_TIME
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
465 if case.maxmemory:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
466 flags |= JOB_OBJECT_LIMIT_PROCESS_MEMORY
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
467 limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION(
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
468 JOBOBJECT_BASIC_LIMIT_INFORMATION(
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
469 PerProcessUserTimeLimit=ceil((case.maxcputime or 0)*10000000),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
470 LimitFlags=flags,
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
471 ),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
472 ProcessMemoryLimit=ceil((case.maxmemory or 0)*1048576),
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
473 )
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
474 SetInformationJobObject(job, JobObjectExtendedLimitInformation, limits)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
475 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
476 case.process = Popen(*args, **kwargs)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
477 except OSError:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
478 raise CannotStartTestee(sys.exc_info()[1])
157
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
479 case.time_started = clock()
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
480 try:
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
481 AssignProcessToJobObject(job, case.process._handle)
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
482 except WindowsError:
148
aa343ff41c27 Attempted to work around unwarranted ERROR_ACCESS_DENIED on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
483 try:
157
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
484 if case.process.poll() is None:
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
485 raise CannotStartTestee(sys.exc_info()[1])
148
aa343ff41c27 Attempted to work around unwarranted ERROR_ACCESS_DENIED on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 146
diff changeset
486 except WindowsError:
157
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
487 raise CannotStartTestee(sys.exc_info()[1])
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
488 if not console_input:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
489 if case.maxwalltime:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
490 if (WaitForSingleObject(case.process._handle, case.maxwalltime) !=
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
491 WAIT_OBJECT_0):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
492 raise WallTimeLimitExceeded
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
493 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
494 case.process.wait()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
495 else:
121
72c02b640dcf Escape presses simultaneous with the testee exiting no longer cancel on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 120
diff changeset
496 handles = case.process._handle, stdin
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
497 if case.maxwalltime:
142
98bccf81db4d win32.call() now trusts its own case.time_started
Oleg Oshmyan <chortos@inbox.lv>
parents: 141
diff changeset
498 time_end = case.time_started + case.maxwalltime
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
499 while case.process.poll() is None:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
500 remaining = time_end - clock()
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
501 if remaining > 0:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
502 if (WaitForMultipleObjects(handles, False, remaining) ==
121
72c02b640dcf Escape presses simultaneous with the testee exiting no longer cancel on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 120
diff changeset
503 WAIT_OBJECT_0 + 1):
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
504 ir = ReadConsoleInput(stdin)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
505 if (ir and
120
08a1d6cc5948 Named a magic number that had sneaked into win32.py
Oleg Oshmyan <chortos@inbox.lv>
parents: 117
diff changeset
506 ir.EventType == KEY_EVENT and
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
507 ir.Event.KeyEvent.bKeyDown and
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
508 ir.Event.KeyEvent.wVirtualKeyCode == 27):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
509 raise CanceledByUser
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
510 else:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
511 raise WallTimeLimitExceeded
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
512 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
513 while case.process.poll() is None:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
514 if (WaitForMultipleObjects(handles, False, INFINITE) ==
121
72c02b640dcf Escape presses simultaneous with the testee exiting no longer cancel on Win32
Oleg Oshmyan <chortos@inbox.lv>
parents: 120
diff changeset
515 WAIT_OBJECT_0 + 1):
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
516 ir = ReadConsoleInput(stdin)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
517 if (ir and
120
08a1d6cc5948 Named a magic number that had sneaked into win32.py
Oleg Oshmyan <chortos@inbox.lv>
parents: 117
diff changeset
518 ir.EventType == KEY_EVENT and
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
519 ir.Event.KeyEvent.bKeyDown and
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
520 ir.Event.KeyEvent.wVirtualKeyCode == 27):
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
521 raise CanceledByUser
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
522 case.time_stopped = clock()
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
523 if GetProcessTimes:
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
524 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
525 times = GetProcessTimes(case.process._handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
526 except WindowsError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
527 pass
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
528 else:
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
529 if case.maxcputime or not case.maxwalltime:
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
530 cputime = times.kernel + times.user
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
531 case.time_stopped = cputime
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
532 case.time_started = 0
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
533 case.time_limit_string = case.cpu_time_limit_string
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
534 if case.maxcputime and cputime > case.maxcputime:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
535 raise CPUTimeLimitExceeded
140
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
536 else:
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
537 case.time_stopped = times.exit
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
538 case.time_started = times.creation
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
539 walltime = times.exit - times.creation
388ae061c915 The win32 module now trusts the wall-clock time reported by GetProcessTimes
Oleg Oshmyan <chortos@inbox.lv>
parents: 134
diff changeset
540 if case.maxwalltime and walltime > case.maxwalltime:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
541 raise WallTimeLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
542 if case.maxcputime and case.process.returncode == 1816:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
543 raise CPUTimeLimitExceeded
80
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
544 if case.maxmemory and GetProcessMemoryInfo:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
545 try:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
546 counters = GetProcessMemoryInfo(case.process._handle)
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
547 except WindowsError:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
548 pass
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
549 else:
809b77302b21 Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff changeset
550 if counters.PeakPagefileUsage > case.maxmemory * 1048576:
146
d5b6708c1955 Distutils support, reorganization and cleaning up
Oleg Oshmyan <chortos@inbox.lv>
parents: 142
diff changeset
551 raise MemoryLimitExceeded
82
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
552
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
553
06356af50bf9 Finished testcases reorganization and CPU time limit implementation
Oleg Oshmyan <chortos@inbox.lv>
parents: 81
diff changeset
554 def kill(process):
157
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
555 # Give up after three attempts
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
556 for i in range(3):
134
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
557 try:
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
558 try:
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
559 process.terminate()
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
560 except AttributeError:
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
561 TerminateProcess(process._handle, 1)
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
562 except WindowsError:
157
5f9e6121161a Second attempt to work around unwarranted ERROR_ACCESS_DENIED on Windows
Oleg Oshmyan <chortos@inbox.lv>
parents: 155
diff changeset
563 time.sleep(0)
134
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
564 else:
e84f33a60a5c Moved process killing logic into platform-specific modules
Oleg Oshmyan <chortos@inbox.lv>
parents: 131
diff changeset
565 break