• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""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
11from ctypes import wintypes, WINFUNCTYPE
12import signal
13import ctypes
14import mmap
15import sys
16
17# Function prototype for the handler function. Returns BOOL, takes a DWORD.
18HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
19
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
43    # Awaken mail process
44    m = mmap.mmap(-1, 1, sys.argv[1])
45    m[0] = '1'
46
47    # Do nothing but wait for the signal
48    while True:
49        pass
50