Mercurial > ~astiob > upreckon > hgweb
annotate win32.py @ 81:24752db487c5
Fixed errors in the win32 module
author | Oleg Oshmyan <chortos@inbox.lv> |
---|---|
date | Wed, 16 Feb 2011 15:30:57 +0000 |
parents | 809b77302b21 |
children | 06356af50bf9 |
rev | line source |
---|---|
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
1 # Copyright (c) 2011 Chortos-2 <chortos@inbox.lv> |
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 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
5 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
6 from compat import * |
81
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
7 from testcases import (TimeLimitExceeded, MemoryLimitExceeded, |
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
8 CanceledByUser, CannotStartTestee) |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
9 except ImportError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
10 import __main__ |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
11 __main__.import_error(sys.exc_info()[1]) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
12 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
13 from __main__ import clock |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
14 from ctypes import * |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
15 from ctypes.wintypes import * |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
16 from subprocess import Popen |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
17 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
18 # 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
|
19 INFINITE = -1 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
20 STD_INPUT_HANDLE = -10 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
21 WAIT_OBJECT_0 = 0 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
22 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
23 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
24 from _subprocess import * |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
25 except ImportError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
26 pass |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
27 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
28 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
29 from numbers import Integral |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
30 except ImportError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
31 Integral = int, long |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
32 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
33 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
34 from collections import namedtuple |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
35 except ImportError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
36 from operator import itemgetter |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
37 class ProcessTimes(tuple): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
38 __slots__ = () |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
39 __new__ = lambda cls, kernel, user: tuple.__new__(cls, (kernel, user)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
40 __getnewargs__ = lambda self: tuple(self) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
41 kernel, user = (property(itemgetter(i)) for i in (0, 1)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
42 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
43 ProcessTimes = namedtuple('ProcessTimes', 'kernel user') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
44 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
45 __all__ = 'call', 'kill', 'terminate' |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
46 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
47 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
48 # Automatically convert _subprocess handle objects into low-level HANDLEs |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
49 # and replicate their functionality for our own use |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
50 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
51 _subprocess_handle = type(GetCurrentProcess()) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
52 except NameError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
53 _subprocess_handle = Integral |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
54 class Handle(object): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
55 @staticmethod |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
56 def from_param(handle): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
57 if isinstance(handle, (_subprocess_handle, Integral)): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
58 return HANDLE(int(handle)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
59 elif isinstance(handle, Handle): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
60 return HANDLE(handle.handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
61 elif isinstance(handle, HANDLE): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
62 return handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
63 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
64 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
|
65 type(handle).__name__) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
66 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
67 __slots__ = 'handle' |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
68 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
69 def __init__(self, handle): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
70 if isinstance(handle, Integral): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
71 self.handle = handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
72 elif isinstance(handle, HANDLE): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
73 self.handle = handle.value |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
74 elif isinstance(handle, Handle): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
75 self.handle = handle.handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
76 elif isinstance(handle, _subprocess_handle): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
77 handle = HANDLE(int(handle)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
78 flags = DWORD() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
79 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
80 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
|
81 flags = flags.value |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
82 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
83 flags = 0 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
84 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
85 # 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
|
86 flags = 0 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
87 proc = HANDLE(int(GetCurrentProcess())) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
88 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
|
89 self.handle = handle.Detach() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
90 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
91 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
|
92 type(handle).__name__) |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
93 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
94 def __int__(self): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
95 return int(self.handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
96 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
97 def Detach(self): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
98 handle = self.handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
99 self.handle = None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
100 return handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
101 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
102 # 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
|
103 def Close(self, _CloseHandle=windll.kernel32.CloseHandle, _HANDLE=HANDLE): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
104 if self.handle: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
105 _CloseHandle(_HANDLE(self.handle)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
106 self.handle = None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
107 __del__ = Close |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
108 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
109 CHAR = c_char |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
110 INVALID_HANDLE_VALUE = HANDLE(-1).value |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
111 LPDWORD = POINTER(DWORD) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
112 LPFILETIME = POINTER(FILETIME) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
113 SIZE_T = ULONG_PTR = WPARAM |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
114 ULONGLONG = c_ulonglong |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
115 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
116 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
117 unicode |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
118 except NameError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
119 LPCTSTR = LPCWSTR |
81
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
120 UNISUFFIX = 'W' |
80
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 LPCTSTR = LPCSTR |
81
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
123 UNISUFFIX = 'A' |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
124 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
125 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
126 prototype = WINFUNCTYPE(BOOL, Handle, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
127 LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
128 flags = ((1, 'process'), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
129 (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
|
130 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
131 GetProcessTimes = prototype(('GetProcessTimes', windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
132 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
133 # 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
|
134 GetProcessTimes = None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
135 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
136 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
137 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
138 ftimes = [t.dwHighDateTime << 32 | t.dwLowDateTime for t in args[3:]] |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
139 kernel = ftimes[0] / 10000000 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
140 user = ftimes[1] / 10000000 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
141 return ProcessTimes(kernel, user) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
142 GetProcessTimes.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
143 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
144 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
145 class PROCESS_MEMORY_COUNTERS(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
146 _fields_ = (('cb', DWORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
147 ('PageFaultCount', DWORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
148 ('PeakWorkingSetSize', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
149 ('WorkingSetSize', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
150 ('QuotaPeakPagedPoolUsage', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
151 ('QuotaPagedPoolUsage', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
152 ('QuotaPeakNonPagedPoolUsage', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
153 ('QuotaNonPagedPoolUsage', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
154 ('PagefileUsage', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
155 ('PeakPagefileUsage', SIZE_T)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
156 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
157 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
|
158 flags = ((1, 'process'), (2, 'counters'), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
159 (5, 'cb', sizeof(PROCESS_MEMORY_COUNTERS))) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
160 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
161 GetProcessMemoryInfo = prototype(('GetProcessMemoryInfo', windll.psapi), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
162 flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
163 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
164 # 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
|
165 GetProcessMemoryInfo = None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
166 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
167 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
168 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
169 return args |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
170 GetProcessMemoryInfo.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
171 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
172 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
173 class _uChar_union(Union): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
174 _fields_ = (('UnicodeChar', WCHAR), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
175 ('AsciiChar', CHAR)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
176 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
177 class KEY_EVENT_RECORD(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
178 _fields_ = (('bKeyDown', BOOL), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
179 ('wRepeatCount', WORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
180 ('wVirtualKeyCode', WORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
181 ('wVirtualScanCode', WORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
182 ('uChar', _uChar_union), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
183 ('dwControlKeyState', DWORD)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
184 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
185 RIGHT_ALT_PRESSED = 0x001 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
186 LEFT_ALT_PRESSED = 0x002 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
187 RIGHT_CTRL_PRESSED = 0x004 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
188 LEFT_CTRL_PRESSED = 0x008 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
189 SHIFT_PRESSED = 0x010 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
190 NUMLOCK_ON = 0x020 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
191 SCROLLLOCK_ON = 0x040 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
192 CAPSLOCK_ON = 0x080 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
193 ENHANCED_KEY = 0x100 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
194 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
195 class _Event_union(Union): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
196 _fields_ = ('KeyEvent', KEY_EVENT_RECORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
197 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
198 class INPUT_RECORD(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
199 _fields_ = (('EventType', WORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
200 ('Event', _Event_union)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
201 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
202 KEY_EVENT = 0x01 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
203 MOUSE_EVENT = 0x02 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
204 WINDOW_BUFFER_SIZE_EVENT = 0x04 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
205 MENU_EVENT = 0x08 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
206 FOCUS_EVENT = 0x10 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
207 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
208 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
|
209 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
|
210 ReadConsoleInput = prototype(('ReadConsoleInputA', windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
211 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
212 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
213 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
|
214 ReadConsoleInput.errcheck = errcheck |
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 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
217 prototype = WINFUNCTYPE(BOOL, Handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
218 flags = (1, 'input'), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
219 FlushConsoleInputBuffer = prototype(('FlushConsoleInputBuffer', |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
220 windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
221 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
222 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
223 FlushConsoleInputBuffer.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
224 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
225 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
226 prototype = WINFUNCTYPE(BOOL, Handle, DWORD) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
227 flags = (1, 'console'), (1, 'mode') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
228 SetConsoleMode = prototype(('SetConsoleMode', windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
229 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
230 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
231 SetConsoleMode.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
232 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
233 ENABLE_PROCESSED_INPUT = 0x001 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
234 ENABLE_LINE_INPUT = 0x002 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
235 ENABLE_ECHO_INPUT = 0x004 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
236 ENABLE_WINDOW_INPUT = 0x008 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
237 ENABLE_MOUSE_INPUT = 0x010 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
238 ENABLE_INSERT_MODE = 0x020 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
239 ENABLE_QUICK_EDIT_MODE = 0x040 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
240 ENABLE_EXTENDED_FLAGS = 0x080 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
241 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
242 ENABLE_PROCESSED_OUTPUT = 1 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
243 ENABLE_WRAP_AT_EOL_OUTPUT = 2 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
244 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
245 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
246 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
|
247 flags = (5, 'attributes'), (1, 'name') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
248 try: |
81
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
249 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
|
250 flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
251 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
252 # 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
|
253 CreateJobObject = lambda name: None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
254 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
255 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
256 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
257 return Handle(result) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
258 CreateJobObject.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
259 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
260 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
261 prototype = WINFUNCTYPE(BOOL, Handle, Handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
262 flags = (1, 'job'), (1, 'handle') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
263 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
264 AssignProcessToJobObject = prototype(('AssignProcessToJobObject', |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
265 windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
266 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
267 # 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
|
268 AssignProcessToJobObject = lambda job, handle: None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
269 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
270 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
271 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
272 AssignProcessToJobObject.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
273 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
274 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
275 class JOBOBJECT_BASIC_LIMIT_INFORMATION(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
276 _fields_ = (('PerProcessUserTimeLimit', LARGE_INTEGER), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
277 ('PerJobUserTimeLimit', LARGE_INTEGER), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
278 ('LimitFlags', DWORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
279 ('MinimumWorkingSetSize', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
280 ('MaximumWorkingSetSize', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
281 ('ActiveProcessLimit', DWORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
282 ('Affinity', ULONG_PTR), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
283 ('PriorityClass', DWORD), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
284 ('SchedulingClass', DWORD)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
285 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
286 JOB_OBJECT_LIMIT_WORKINGSET = 0x0001 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
287 JOB_OBJECT_LIMIT_PROCESS_TIME = 0x0002 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
288 JOB_OBJECT_LIMIT_JOB_TIME = 0x0004 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
289 JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x0008 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
290 JOB_OBJECT_LIMIT_AFFINITY = 0x0010 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
291 JOB_OBJECT_LIMIT_PRIORITY_CLASS = 0x0020 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
292 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
|
293 JOB_OBJECT_LIMIT_SCHEDULING_CLASS = 0x0080 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
294 JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x0100 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
295 JOB_OBJECT_LIMIT_JOB_MEMORY = 0x0200 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
296 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
|
297 JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x0800 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
298 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
|
299 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
|
300 JOB_OBJECT_LIMIT_SUBSET_AFFINITY = 0x4000 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
301 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
302 class IO_COUNTERS(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
303 _fields_ = (('ReadOperationCount', ULONGLONG), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
304 ('WriteOperationCount', ULONGLONG), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
305 ('OtherOperationCount', ULONGLONG), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
306 ('ReadTransferCount', ULONGLONG), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
307 ('WriteTransferCount', ULONGLONG), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
308 ('OtherTransferCount', ULONGLONG)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
309 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
310 class JOBOBJECT_EXTENDED_LIMIT_INFORMATION(Structure): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
311 _fields_ = (('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
312 ('IoInfo', IO_COUNTERS), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
313 ('ProcessMemoryLimit', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
314 ('JobMemoryLimit', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
315 ('PeakProcessMemoryUsed', SIZE_T), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
316 ('PeakJobMemoryUsed', SIZE_T)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
317 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
318 prototype = WINFUNCTYPE(BOOL, HANDLE, c_int, c_void_p, DWORD) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
319 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
|
320 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
321 _setjobinfo = prototype(('SetInformationJobObject',windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
322 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
323 # 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
|
324 SetInformationJobObject = lambda job, infoclass, info: None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
325 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
326 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
327 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
328 _setjobinfo.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
329 def SetInformationJobObject(job, infoclass, info): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
330 return _setjobinfo(job, infoclass, info, sizeof(info)) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
331 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
332 ( |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
333 JobObjectBasicAccountingInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
334 JobObjectBasicLimitInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
335 JobObjectBasicProcessIdList, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
336 JobObjectBasicUIRestrictions, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
337 JobObjectSecurityLimitInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
338 JobObjectEndOfJobTimeInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
339 JobObjectAssociateCompletionPortInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
340 JobObjectBasicAndIoAccountingInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
341 JobObjectExtendedLimitInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
342 JobObjectJobSetInformation, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
343 MaxJobObjectInfoClass |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
344 ) = range(1, 12) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
345 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
346 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
347 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
|
348 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
|
349 _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
|
350 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
351 if result == WAIT_FAILED: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
352 return args |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
353 _wait_multiple.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
354 def WaitForMultipleObjects(handles, wait_all, timeout): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
355 n = len(handles) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
356 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
|
357 timeout = ceil(timeout * 1000) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
358 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
|
359 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
360 # 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
|
361 WAIT_ABANDONED_0 = 0x00000080 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
362 WAIT_TIMEOUT = 0x00000102 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
363 WAIT_FAILED = 0xFFFFFFFF |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
364 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
365 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
366 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
367 _wait_single = WaitForSingleObject |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
368 except NameError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
369 prototype = WINFUNCTYPE(DWORD, Handle, DWORD) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
370 flags = (1, 'handle'), (1, 'milliseconds') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
371 _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
|
372 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
373 if result == WAIT_FAILED: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
374 return args |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
375 _wait_single.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
376 def WaitForSingleObject(handle, timeout): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
377 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
|
378 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
379 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
380 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
381 GetStdHandle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
382 except NameError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
383 prototype = WINFUNCTYPE(HANDLE, DWORD) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
384 flags = (1, 'which'), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
385 GetStdHandle = prototype(('GetStdHandle', windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
386 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
387 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
|
388 return args if result else None |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
389 GetStdHandle.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
390 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
391 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
392 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
393 TerminateProcess |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
394 except NameError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
395 prototype = WINFUNCTYPE(BOOL, Handle, UINT) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
396 flags = (1, 'process'), (1, 'exitcode') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
397 TerminateProcess = prototype(('TerminateProcess', windll.kernel32), flags) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
398 def errcheck(result, func, args): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
399 if not result: raise WinError() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
400 TerminateProcess.errcheck = errcheck |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
401 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
402 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
403 # 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
|
404 try: |
81
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
405 errmode = windll.kernel32.GetErrorMode() |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
406 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
407 # 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
|
408 errmode = windll.kernel32.SetErrorMode(0) |
24752db487c5
Fixed errors in the win32 module
Oleg Oshmyan <chortos@inbox.lv>
parents:
80
diff
changeset
|
409 windll.kernel32.SetErrorMode(errmode | 0x8003) |
80
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
410 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
411 stdin = GetStdHandle(STD_INPUT_HANDLE) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
412 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
413 SetConsoleMode(stdin, ENABLE_PROCESSED_INPUT) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
414 except WindowsError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
415 console_input = False |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
416 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
417 console_input = True |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
418 FlushConsoleInputBuffer(stdin) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
419 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
420 def kill(process): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
421 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
422 process.terminate() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
423 except AttributeError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
424 TerminateProcess(process._handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
425 terminate = kill |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
426 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
427 def call(*args, **kwargs): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
428 case = kwargs.pop('case') |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
429 job = CreateJobObject(None) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
430 flags = 0 |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
431 if case.maxcputime: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
432 flags |= JOB_OBJECT_LIMIT_PROCESS_TIME |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
433 if case.maxmemory: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
434 flags |= JOB_OBJECT_LIMIT_PROCESS_MEMORY |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
435 limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION( |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
436 JOBOBJECT_BASIC_LIMIT_INFORMATION( |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
437 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
|
438 LimitFlags=flags, |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
439 ), |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
440 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
|
441 ) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
442 SetInformationJobObject(job, JobObjectExtendedLimitInformation, limits) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
443 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
444 case.process = Popen(*args, **kwargs) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
445 except OSError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
446 raise CannotStartTestee(sys.exc_info()[1]) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
447 case.time_started = clock() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
448 AssignProcessToJobObject(job, case.process._handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
449 if not console_input: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
450 if case.maxwalltime: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
451 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
|
452 WAIT_OBJECT_0): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
453 raise TimeLimitExceeded |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
454 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
455 case.process.wait() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
456 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
457 handles = stdin, case.process._handle |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
458 if case.maxwalltime: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
459 time_end = clock() + case.maxwalltime |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
460 while case.process.poll() is None: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
461 remaining = time_end - clock() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
462 if remaining > 0: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
463 if (WaitForMultipleObjects(handles, False, remaining) == |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
464 WAIT_OBJECT_0): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
465 ir = ReadConsoleInput(stdin) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
466 if (ir and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
467 ir.EventType == 1 and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
468 ir.Event.KeyEvent.bKeyDown and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
469 ir.Event.KeyEvent.wVirtualKeyCode == 27): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
470 raise CanceledByUser |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
471 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
472 raise TimeLimitExceeded |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
473 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
474 while case.process.poll() is None: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
475 if (WaitForMultipleObjects(handles, False, INFINITE) == |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
476 WAIT_OBJECT_0): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
477 ir = ReadConsoleInput(stdin) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
478 if (ir and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
479 ir.EventType == 1 and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
480 ir.Event.KeyEvent.bKeyDown and |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
481 ir.Event.KeyEvent.wVirtualKeyCode == 27): |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
482 raise CanceledByUser |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
483 case.time_stopped = clock() |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
484 if case.maxcputime and GetProcessTimes: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
485 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
486 times = GetProcessTimes(case.process._handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
487 except WindowsError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
488 pass |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
489 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
490 if times.kernel + times.user > case.maxcputime: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
491 raise TimeLimitExceeded |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
492 if case.maxmemory and GetProcessMemoryInfo: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
493 try: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
494 counters = GetProcessMemoryInfo(case.process._handle) |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
495 except WindowsError: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
496 pass |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
497 else: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
498 if counters.PeakPagefileUsage > case.maxmemory * 1048576: |
809b77302b21
Win32-specific module with memory and CPU time limits
Oleg Oshmyan <chortos@inbox.lv>
parents:
diff
changeset
|
499 raise MemoryLimitExceeded |