1"""Use a Windows event to interrupt a child process like SIGINT.
2
3The child needs to explicitly listen for this - see
4ipykernel.parentpoller.ParentPollerWindows for a Python implementation.
5"""
6
7import ctypes
8
9def create_interrupt_event():
10    """Create an interrupt event handle.
11
12    The parent process should call this to create the
13    interrupt event that is passed to the child process. It should store
14    this handle and use it with ``send_interrupt`` to interrupt the child
15    process.
16    """
17    # Create a security attributes struct that permits inheritance of the
18    # handle by new processes.
19    # FIXME: We can clean up this mess by requiring pywin32 for IPython.
20    class SECURITY_ATTRIBUTES(ctypes.Structure):
21        _fields_ = [ ("nLength", ctypes.c_int),
22                     ("lpSecurityDescriptor", ctypes.c_void_p),
23                     ("bInheritHandle", ctypes.c_int) ]
24    sa = SECURITY_ATTRIBUTES()
25    sa_p = ctypes.pointer(sa)
26    sa.nLength = ctypes.sizeof(SECURITY_ATTRIBUTES)
27    sa.lpSecurityDescriptor = 0
28    sa.bInheritHandle = 1
29
30    return ctypes.windll.kernel32.CreateEventA(
31        sa_p,  # lpEventAttributes
32        False, # bManualReset
33        False, # bInitialState
34        '')    # lpName
35
36def send_interrupt(interrupt_handle):
37    """ Sends an interrupt event using the specified handle.
38    """
39    ctypes.windll.kernel32.SetEvent(interrupt_handle)
40