blob: e7779b9363503b452116ea70181e7adfe7651b8f [file] [log] [blame]
Brian Curtineb24d742010-04-12 17:16:38 +00001"""Script used to test os.kill on Windows, for issue #1220212
2
3This script is started as a subprocess in test_os and is used to test the
4CTRL_C_EVENT and CTRL_BREAK_EVENT signals, which requires a custom handler
5to be written into the kill target.
6
7See http://msdn.microsoft.com/en-us/library/ms685049%28v=VS.85%29.aspx for a
8similar example in C.
9"""
10
Brian Curtin926f0da2010-09-29 14:51:42 +000011from ctypes import wintypes, WINFUNCTYPE
Brian Curtineb24d742010-04-12 17:16:38 +000012import signal
13import ctypes
Hirokazu Yamamoto54c950f2010-10-08 08:38:15 +000014import mmap
15import sys
Brian Curtineb24d742010-04-12 17:16:38 +000016
17# Function prototype for the handler function. Returns BOOL, takes a DWORD.
Brian Curtin926f0da2010-09-29 14:51:42 +000018HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
Brian Curtineb24d742010-04-12 17:16:38 +000019
20def _ctrl_handler(sig):
21 """Handle a sig event and return 0 to terminate the process"""
22 if sig == signal.CTRL_C_EVENT:
23 pass
24 elif sig == signal.CTRL_BREAK_EVENT:
25 pass
26 else:
27 print("UNKNOWN EVENT")
28 return 0
29
30ctrl_handler = HandlerRoutine(_ctrl_handler)
31
32
33SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
34SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
35SetConsoleCtrlHandler.restype = wintypes.BOOL
36
37if __name__ == "__main__":
38 # Add our console control handling function with value 1
39 if not SetConsoleCtrlHandler(ctrl_handler, 1):
40 print("Unable to add SetConsoleCtrlHandler")
41 exit(-1)
42
Hirokazu Yamamoto52802752010-12-09 11:13:30 +000043 # Awake main process
Hirokazu Yamamoto54c950f2010-10-08 08:38:15 +000044 m = mmap.mmap(-1, 1, sys.argv[1])
45 m[0] = 1
46
Brian Curtineb24d742010-04-12 17:16:38 +000047 # Do nothing but wait for the signal
48 while True:
49 pass