o
    iD                     @   s   d Z zddlZW n ey   dZY nw 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                       *   e Zd ZdZd 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.
    r   c                    s   t    || _d| _dS )zInitialize the poller.

        Parameters
        ----------
        parent_handle : int, optional
            If provided, the program will terminate immediately when
            process parent is no longer this original parent.
        TN)super__init__
parent_piddaemon)selfr	   	__class__ Q/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/ipykernel/parentpoller.pyr      s   
	
zParentPollerUnix.__init__c              
   C   s   ddl m} t }|| jkrd| _t dt| j| j 	 z*t }| j o+|dk}| jo3|| jk}|s8|rCt 	d t
d td W n tya } z|j |kr\W Y d}~q d}~ww q)	zRun the poller.r   )EINTRz6%s: poll for parent change with original parent pid=%dT   -Parent appears to have exited, shutting down.g      ?N)errnor   osgetppidr	   r   debugtype__name__warning_exittimesleepOSError)r   r   original_ppidppidparent_is_initparent_has_changeder   r   r   run&   s2   


zParentPollerUnix.run)r   r   
__module____qualname____doc__r   r#   __classcell__r   r   r   r   r      s    r   c                       r   )ParentPollerWindowszA 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   K   s   

zParentPollerWindows.__init__c                 C   s>  z
ddl m}m} W n ty   ddlm}m} Y nw g }| jr'|| j | jr0|| j t	 d }|
dr>tjntj}	 tjjt||t| | d|}||  kr`t|k rn n/|||  }tjj| || jkrtttjrt  n|| jkrt d td n|dk rtjdd	d
 dS qB)z-Run the poll loop. This method never returns.r   )INFINITEWAIT_OBJECT_064TFr   r   zParent 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)_winapir/   r0   r+   _subprocessr,   appendr-   platformarchitecture
startswithr*   c_int64c_intwindllkernel32WaitForMultipleObjectslen
ResetEventcallablesignal	getsignalSIGINTr   r   r   r   r   warningswarn)r   r/   r0   handlesarchr;   resulthandler   r   r   r#   a   sJ   


zParentPollerWindows.run)NNr$   r   r   r   r   r)   E   s    r)   )r'   r*   r+   r   r7   rB   r   rE   _threadr   	threadingr   traitlets.logr   r   r)   r   r   r   r   <module>   s     1