Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 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 | |
Brian Curtin | 926f0da | 2010-09-29 14:51:42 +0000 | [diff] [blame] | 11 | from ctypes import wintypes, WINFUNCTYPE |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 12 | import signal |
| 13 | import ctypes |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 14 | import mmap |
| 15 | import sys |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 16 | |
| 17 | # Function prototype for the handler function. Returns BOOL, takes a DWORD. |
Brian Curtin | 926f0da | 2010-09-29 14:51:42 +0000 | [diff] [blame] | 18 | HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD) |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 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 | |
Hirokazu Yamamoto | 5280275 | 2010-12-09 11:13:30 +0000 | [diff] [blame] | 43 | # Awake main process |
Hirokazu Yamamoto | 54c950f | 2010-10-08 08:38:15 +0000 | [diff] [blame] | 44 | m = mmap.mmap(-1, 1, sys.argv[1]) |
| 45 | m[0] = 1 |
| 46 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 47 | # Do nothing but wait for the signal |
| 48 | while True: |
| 49 | pass |