ยปCore Development>Code coverage>Lib/test/win_console_handler.py

Python code coverage for Lib/test/win_console_handler.py

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