script used to test os kill on windows for issue 1220212 this script is started as a subprocess in testos and is used to test the ctrlcevent and ctrlbreakevent signals which requires a custom handler to be written into the kill target see http msdn microsoft comenuslibraryms68504928vvs 8529 aspx for a similar example in c function prototype for the handler function returns bool takes a dword handle a sig event and return 0 to terminate the process if sig signal ctrlcevent pass elif sig signal ctrlbreakevent pass else printunknown event return 0 ctrlhandler handlerroutinectrlhandler setconsolectrlhandler ctypes windll kernel32 setconsolectrlhandler setconsolectrlhandler argtypes handlerroutine wintypes bool setconsolectrlhandler restype wintypes bool if name main add our console control handling function with value 1 if not setconsolectrlhandlerctrlhandler 1 printunable to add setconsolectrlhandler exit1 awake main process m mmap mmap1 1 sys argv1 m0 1 do nothing but wait for the signal while true pass function prototype for the handler function returns bool takes a dword handle a sig event and return 0 to terminate the process add our console control handling function with value 1 awake main process do nothing but wait for the signal
from ctypes import wintypes, WINFUNCTYPE
import signal
import ctypes
import mmap
import sys


HandlerRoutine = WINFUNCTYPE(wintypes.BOOL, wintypes.DWORD)

def _ctrl_handler(sig):
    
    if sig == signal.CTRL_C_EVENT:
        pass
    elif sig == signal.CTRL_BREAK_EVENT:
        pass
    else:
        print("UNKNOWN EVENT")
    return 0

ctrl_handler = HandlerRoutine(_ctrl_handler)


SetConsoleCtrlHandler = ctypes.windll.kernel32.SetConsoleCtrlHandler
SetConsoleCtrlHandler.argtypes = (HandlerRoutine, wintypes.BOOL)
SetConsoleCtrlHandler.restype = wintypes.BOOL

if __name__ == "__main__":
    
    if not SetConsoleCtrlHandler(ctrl_handler, 1):
        print("Unable to add SetConsoleCtrlHandler")
        exit(-1)

    
    m = mmap.mmap(-1, 1, sys.argv[1])
    m[0] = 1

    
    while True:
        pass