move sections
[python/dscho.git] / Lib / test / win_console_handler.py
blob17bbe1af48a8085f2c7c3b46a44672bd5e55b660
1 """Script used to test os.kill on Windows, for issue #1220212
3 This script is started as a subprocess in test_os and is used to test the
4 CTRL_C_EVENT and CTRL_BREAK_EVENT signals, which requires a custom handler
5 to be written into the kill target.
7 See http://msdn.microsoft.com/en-us/library/ms685049%28v=VS.85%29.aspx for a
8 similar example in C.
9 """
11 from ctypes import wintypes
12 import signal
13 import ctypes
15 # Function prototype for the handler function. Returns BOOL, takes a DWORD.
16 HandlerRoutine = wintypes.WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
18 def _ctrl_handler(sig):
19 """Handle a sig event and return 0 to terminate the process"""
20 if sig == signal.CTRL_C_EVENT:
21 pass
22 elif sig == signal.CTRL_BREAK_EVENT:
23 pass
24 else:
25 print("UNKNOWN EVENT")
26 return 0
28 ctrl_handler = HandlerRoutine(_ctrl_handler)
31 SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
32 SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
33 SetConsoleCtrlHandler.restype = wintypes.BOOL
35 if __name__ == "__main__":
36 # Add our console control handling function with value 1
37 if not SetConsoleCtrlHandler(ctrl_handler, 1):
38 print("Unable to add SetConsoleCtrlHandler")
39 exit(-1)
41 # Do nothing but wait for the signal
42 while True:
43 pass