ó
Ű¤[c           @   sä   y d  d l  Z  Wn d Z  n Xd  d l Z d  d l Z d  d l Z d  d l Z y d  d l m Z Wn! e k
 r d  d l	 m Z n Xd  d l
 m Z d  d l m Z d  d l Z d e f d     YZ d e f d     YZ d S(	   i˙˙˙˙N(   t   interrupt_main(   t   Thread(   t
   get_loggert   ParentPollerUnixc           B   s    e  Z d  Z d   Z d   Z RS(   sy    A Unix-specific daemon thread that terminates the program immediately
    when the parent process no longer exists.
    c         C   s    t  t |   j   t |  _ d  S(   N(   t   superR   t   __init__t   Truet   daemon(   t   self(    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyR      s    c         C   s   d d l  m } x{ t r yC t j   d k rN t   j d  t j d  n  t j	 d  Wq t
 k
 r } | j  | k r q n    q Xq Wd  S(   Ni˙˙˙˙(   t   EINTRi   s-   Parent appears to have exited, shutting down.g      đ?(   t   errnoR	   R   t   ost   getppidR   t   warningt   _exitt   timet   sleept   OSError(   R   R	   t   e(    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyt   run   s    	(   t   __name__t
   __module__t   __doc__R   R   (    (    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyR      s   	t   ParentPollerWindowsc           B   s&   e  Z d  Z d d d  Z d   Z RS(   sÇ    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.
    c         C   s_   | s | s t   t t |   j   t d k r@ t d   n  t |  _ | |  _	 | |  _
 d S(   s˝   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.
        s#   ParentPollerWindows requires ctypesN(   t   AssertionErrorR   R   R   t   ctypest   Nonet   ImportErrorR   R   t   interrupt_handlet   parent_handle(   R   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyR   4   s    		c         C   sŤ  y d d l  m } m } Wn' t k
 rC d d l m } m } n Xg  } |  j rf | j |  j  n  |  j r | j |  j  n  t j	   d } | j
 d  rŞ t j n t j } xń t rŚt j j j t |  | t |  |   t |  } | | k ot |  k  n r| | | } | |  j k rTt t j t j   rt   qqŁ| |  j k rŁt   j d  t j d  qŁqś | d k  rś t j d  d Sqś Wd S(	   s7    Run the poll loop. This method never returns.
        i˙˙˙˙(   t   WAIT_OBJECT_0t   INFINITEi    t   64s-   Parent appears to have exited, shutting down.i   sŃ   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.orgN(   t   _winapiR   R   R   t   _subprocessR   t   appendR   t   platformt   architecturet
   startswithR   t   c_int64t   c_intR   t   windllt   kernel32t   WaitForMultipleObjectst   lent   Falset   callablet   signalt	   getsignalt   SIGINTR    R   R   R   R   t   warningst   warn(   R   R   R   t   handlest   archR(   t   resultt   handle(    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyR   J   s:    		!			"N(   R   R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyR   .   s   (   R   R   R   R$   R/   R   t   _threadR    R   t   threadt	   threadingR   t   traitlets.logR   R2   R   R   (    (    (    s5   lib/python2.7/site-packages/ipykernel/parentpoller.pyt   <module>   s    
