ó
Û¤[c           @   sÊ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 d d l m Z d d l m Z d d l m Z d	 Z d
 Z e d „  ƒ Z d „  Z d „  Z d „  Z d S(   s   test IPython.embed_kernel()iÿÿÿÿN(   t   contextmanager(   t   Popent   PIPE(   t   BlockingKernelClient(   t   paths(   t	   py3compat(   t   unicode_typei<   i   c         c   s}  t  t j d |  g d t d t ƒ} t j j t j ƒ  d | j	 ƒ } t
 j
 ƒ  } xL t j j | ƒ r  | j ƒ  d	 k r  t
 j
 ƒ  | t k  r  t
 j d ƒ qU W| j ƒ  d	 k	 rç | j ƒ  \ } } t j | ƒ } t d | ƒ ‚ n  t j j | ƒ s+| j ƒ  d	 k r| j ƒ  n  t d | ƒ ‚ n  t d | ƒ } | j ƒ  | j ƒ  | j ƒ  z	 | VWd	 | j ƒ  | j ƒ  Xd	 S(
   s˜   start an embedded kernel in a subprocess, and wait for it to be ready

    Returns
    -------
    kernel_manager: connected KernelManager instance
    s   -ct   stdoutt   stderrs   kernel-%i.jsongš™™™™™¹?s   Kernel failed to start:
%ss    Connection file %r never arrivedt   connection_fileN(   R   t   syst
   executableR   t   ost   patht   joinR   t   jupyter_runtime_dirt   pidt   timet   existst   pollt   Nonet   SETUP_TIMEOUTt   sleept   communicateR   t   cast_unicodet   IOErrort	   terminateR   t   load_connection_filet   start_channelst   wait_for_readyt   stop_channels(   t   cmdt   kernelR	   t   tict   ot   et   client(    (    s@   lib/python2.7/site-packages/ipykernel/tests/test_embed_kernel.pyt   setup_kernel   s2    $		


	
c       	   C   s&  d j  d d d d d d d g ƒ }  t |  ƒ ð } | j d	 ƒ } | j d
 t d t ƒ } | d } | d st t ‚ | j d ƒ } | j d
 t d t ƒ } | d } | d d k s» t ‚ | j d ƒ } | j d
 t d t ƒ } | d } | d sü t ‚ | d d } d | k st ‚ Wd QXd S(   s.   IPython.embed_kernel() is basically functionals   
s    from IPython import embed_kernels	   def go():s       a=5s       b="hi there"s       embed_kernel()s   go()t    t   at   blockt   timeoutt   contentt   founds   c=a*2t   statusu   okt   ct   datas
   text/plaint   10N(   R   R%   t   inspectt   get_shell_msgt   Truet   TIMEOUTt   AssertionErrort   execute(   R   R$   t   msg_idt   msgR*   t   text(    (    s@   lib/python2.7/site-packages/ipykernel/tests/test_embed_kernel.pyt   test_embed_kernel_basic@   s.    


c       	   C   sA  d j  d d d d d d d g ƒ }  t |  ƒ } | j d	 ƒ } | j d
 t d t ƒ } | d } | d st t ‚ | d d } d | k s” t ‚ | j d ƒ } | j d
 t d t ƒ } | d } | d sÕ t ‚ | d d } d | k sõ t ‚ | j d ƒ } | j d
 t d t ƒ } | d } | d s7t ‚ Wd QXd S(   s1   IPython.embed_kernel() inherits calling namespaces   
s    from IPython import embed_kernels	   def go():s       a=5s       b="hi there"s       embed_kernel()s   go()R&   R'   R(   R)   R*   R+   R.   s
   text/plainu   5t   bu   hi thereR-   N(   R   R%   R0   R1   R2   R3   R4   (   R   R$   R6   R7   R*   R8   (    (    s@   lib/python2.7/site-packages/ipykernel/tests/test_embed_kernel.pyt   test_embed_kernel_namespace`   s2    


c       
   C   só   d j  d d d d d d d d	 d g	 ƒ }  t |  ƒ · } x­ t d
 ƒ D]Ÿ } | j d ƒ } | j d t d t ƒ } | d } | d s t ‚ | d d } t | ƒ | k s³ t ‚ | j	 d ƒ | j d t d t ƒ } t
 j d ƒ qF WWd QXd S(   s3   IPython.embed_kernel() can be called multiple timess   
s    from IPython import embed_kernels	   count = 0s	   def go():s       global counts       embed_kernel()s       count = count + 1R&   s   while True:    go()i   t   countR(   R)   R*   R+   R.   s
   text/plains   get_ipython().exit_now = Truegš™™™™™É?N(   R   R%   t   rangeR0   R1   R2   R3   R4   R   R5   R   R   (   R   R$   t   iR6   R7   R*   R8   (    (    s@   lib/python2.7/site-packages/ipykernel/tests/test_embed_kernel.pyt   test_embed_kernel_reentrantƒ   s*    
(   t   __doc__R   R
   R   t
   contextlibR    t
   subprocessR   R   t   jupyter_clientR   t   jupyter_coreR   t   ipython_genutilsR   t   ipython_genutils.py3compatR   R   R3   R%   R9   R;   R?   (    (    (    s@   lib/python2.7/site-packages/ipykernel/tests/test_embed_kernel.pyt   <module>   s   )	 	#