1 | """Script used to test os.kill on Windows, for issue #1220212
|
---|
2 |
|
---|
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.
|
---|
6 |
|
---|
7 | See http://msdn.microsoft.com/en-us/library/ms685049%28v=VS.85%29.aspx for a
|
---|
8 | similar example in C.
|
---|
9 | """
|
---|
10 |
|
---|
11 | from ctypes import wintypes, WINFUNCTYPE
|
---|
12 | import signal
|
---|
13 | import ctypes
|
---|
14 | import mmap
|
---|
15 | import sys
|
---|
16 |
|
---|
17 | # Function prototype for the handler function. Returns BOOL, takes a DWORD.
|
---|
18 | HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)
|
---|
19 |
|
---|
20 | def _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 |
|
---|
30 | ctrl_handler = HandlerRoutine(_ctrl_handler)
|
---|
31 |
|
---|
32 |
|
---|
33 | SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
|
---|
34 | SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
|
---|
35 | SetConsoleCtrlHandler.restype = wintypes.BOOL
|
---|
36 |
|
---|
37 | if __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
|
---|