]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Script used to test os.kill on Windows, for issue #1220212\r |
2 | \r | |
3 | This script is started as a subprocess in test_os and is used to test the\r | |
4 | CTRL_C_EVENT and CTRL_BREAK_EVENT signals, which requires a custom handler\r | |
5 | to be written into the kill target.\r | |
6 | \r | |
7 | See http://msdn.microsoft.com/en-us/library/ms685049%28v=VS.85%29.aspx for a\r | |
8 | similar example in C.\r | |
9 | """\r | |
10 | \r | |
11 | from ctypes import wintypes, WINFUNCTYPE\r | |
12 | import signal\r | |
13 | import ctypes\r | |
14 | import mmap\r | |
15 | import sys\r | |
16 | \r | |
17 | # Function prototype for the handler function. Returns BOOL, takes a DWORD.\r | |
18 | HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)\r | |
19 | \r | |
20 | def _ctrl_handler(sig):\r | |
21 | """Handle a sig event and return 0 to terminate the process"""\r | |
22 | if sig == signal.CTRL_C_EVENT:\r | |
23 | pass\r | |
24 | elif sig == signal.CTRL_BREAK_EVENT:\r | |
25 | pass\r | |
26 | else:\r | |
27 | print("UNKNOWN EVENT")\r | |
28 | return 0\r | |
29 | \r | |
30 | ctrl_handler = HandlerRoutine(_ctrl_handler)\r | |
31 | \r | |
32 | \r | |
33 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler\r | |
34 | SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)\r | |
35 | SetConsoleCtrlHandler.restype = wintypes.BOOL\r | |
36 | \r | |
37 | if __name__ == "__main__":\r | |
38 | # Add our console control handling function with value 1\r | |
39 | if not SetConsoleCtrlHandler(ctrl_handler, 1):\r | |
40 | print("Unable to add SetConsoleCtrlHandler")\r | |
41 | exit(-1)\r | |
42 | \r | |
43 | # Awaken mail process\r | |
44 | m = mmap.mmap(-1, 1, sys.argv[1])\r | |
45 | m[0] = '1'\r | |
46 | \r | |
47 | # Do nothing but wait for the signal\r | |
48 | while True:\r | |
49 | pass\r |