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