a
    ø£Beµ  ã                   @   s˜   d Z zddlZW n ey&   dZY n0 ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ G dd„ deƒZG dd	„ d	eƒZdS )
zA parent poller for unix.é    N)Úinterrupt_main)ÚThread)Ú
get_loggerc                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚParentPollerUnixzxA Unix-specific daemon thread that terminates the program immediately
    when the parent process no longer exists.
    c                    s   t ƒ  ¡  d| _dS )zInitialize the poller.TN)ÚsuperÚ__init__Údaemon)Úself©Ú	__class__© úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/parentpoller.pyr      s    
zParentPollerUnix.__init__c              
   C   s€   ddl m} z0t ¡ dkr0tƒ  d¡ t d¡ t d¡ W q t	yx } z$|j |krbW Y d}~q‚ W Y d}~qd}~0 0 qdS )zRun the poller.r   )ÚEINTRé   ú-Parent appears to have exited, shutting down.g      ð?N)
Úerrnor   ÚosÚgetppidr   ÚwarningÚ_exitÚtimeÚsleepÚOSError)r	   r   Úer   r   r   Úrun   s    

zParentPollerUnix.run©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__classcell__r   r   r
   r   r      s   r   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )ÚParentPollerWindowszÆA Windows-specific daemon thread that listens for a special event that
    signals an interrupt and, optionally, terminates the program immediately
    when the parent process no longer exists.
    Nc                    s@   |s|sJ ‚t ƒ  ¡  tdu r*d}t|ƒ‚d| _|| _|| _dS )a»  Create the poller. At least one of the optional parameters must be
        provided.

        Parameters
        ----------
        interrupt_handle : HANDLE (int), optional
            If provided, the program will generate a Ctrl+C event when this
            handle is signaled.
        parent_handle : HANDLE (int), optional
            If provided, the program will terminate immediately when this
            handle is signaled.
        Nz#ParentPollerWindows requires ctypesT)r   r   ÚctypesÚImportErrorr   Úinterrupt_handleÚparent_handle)r	   r$   r%   Úmsgr
   r   r   r   5   s    
zParentPollerWindows.__init__c                 C   s6  zddl m}m} W n" ty6   ddlm}m} Y n0 g }| jrN| | j¡ | jr`| | j¡ t 	¡ d }| 
d¡r|tjntj}tjj t|ƒ|t|ƒ |Ž d|¡}||  krÀt|ƒk �rn nR|||  }|| jkròtt tj¡ƒrðtƒ  n"|| jk�r0tƒ  d¡ t d¡ q‚|dk r‚tjddd	� d
S q‚d
S )z-Run the poll loop. This method never returns.r   )ÚINFINITEÚWAIT_OBJECT_0Z64Fr   r   zÑParent poll failed.  If the frontend dies,
                the kernel may be left running.  Please let us know
                about your system (bitness, Python, etc.) at
                ipython-dev@scipy.orgé   )Ú
stacklevelN)Z_winapir'   r(   r#   Z_subprocessr$   Úappendr%   ÚplatformÚarchitectureÚ
startswithr"   Zc_int64Úc_intZwindllZkernel32ZWaitForMultipleObjectsÚlenÚcallableÚsignalÚ	getsignalÚSIGINTr   r   r   r   r   ÚwarningsÚwarn)r	   r'   r(   ZhandlesÚarchr/   ÚresultÚhandler   r   r   r   K   s>    ü
ûzParentPollerWindows.run)NNr   r   r   r
   r   r!   /   s   r!   )r   r"   r#   r   r,   r2   r   r5   Ú_threadr   Ú	threadingr   Ztraitlets.logr   r   r!   r   r   r   r   Ú<module>   s   
