U
    pufÚ  ã                   @   st   d Z ddlZddlZddlZddlmZ ddlmZ edkrDej	Z
nejZ
ddd„Zdd	d
„ZG dd„ dejƒZdS )z¡Implementation of coroutines using ``async def``/``await`` keywords.

These keywords replaced ``@asyncio.coroutine`` and ``yield from`` from
Python 3.5 onwards.
é    N)Úversion_info)ÚEOF)é   é   c              
   ƒ   sÐ   |   ¡ }|d k	r|S | jjs\tƒ ‰ ˆ  | ¡ tƒ  ‡ fdd„| j¡I d H \}‰ ˆ |f| j_n| jj\‰ }ˆ  | ¡ | ¡  zt 	ˆ j
|¡I d H W S  tjk
rÊ } z| ¡  |  |¡ W Y ¢S d }~X Y nX d S )Nc                      s   ˆ S ©N© r   ©Zpattern_waiterr   ú:/tmp/pip-unpacked-wheel-v5brguv_/pexpect/_async_w_await.pyÚ<lambda>   ó    zexpect_async.<locals>.<lambda>)Zexisting_dataÚspawnZasync_pw_transportÚPatternWaiterÚset_expecterÚ_loop_getterZconnect_read_pipeZresume_readingÚasyncioÚwait_forÚfutÚTimeoutErrorÚpause_readingÚtimeout)Úexpecterr   ÚidxÚ	transportÚexcr   r   r	   Úexpect_async   s&    

 ÿ
r   éÿÿÿÿc                 Ã   s®   g }| j  |d ¡ |dd … D ]2}| j|ddI d H  | | j j¡ | j  |¡ q | j|ddI d H }|dkrš| j  tj¡ | jdddI d H  tdƒ‚d 	|| j jg ¡S )Nr   é   T)r   Zasync_z1Continuation prompt found - input was incomplete:Ú )
ÚchildZsendlineZ_expect_promptÚappendÚbeforeÚkillÚsignalÚSIGINTÚ
ValueErrorÚjoin)ÚreplZcmdlinesr   ÚresÚlineZ
prompt_idxr   r   r	   Úrepl_run_command_async-   s    r)   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )r   Nc                 C   s   || _ t ¡ | _d S r   )r   r   ZFuturer   )Úselfr   r   r   r	   r   B   s    zPatternWaiter.set_expecterc                 C   s$   | j  ¡ s | j  |¡ | j ¡  d S r   )r   ÚdoneZ
set_resultr   r   )r*   Úresultr   r   r	   ÚfoundF   s    
zPatternWaiter.foundc                 C   s$   | j  ¡ s | j  |¡ | j ¡  d S r   )r   r+   Zset_exceptionr   r   ©r*   r   r   r   r	   ÚerrorK   s    
zPatternWaiter.errorc                 C   s
   || _ d S r   )r   )r*   r   r   r   r	   Úconnection_madeP   s    zPatternWaiter.connection_madec              
   C   s¤   | j j}|j |¡}| |d¡ | j ¡ rF|j |¡ |j	 |¡ d S z"| j  
|¡}|d k	rf|  |¡ W n6 tk
rž } z| j  ¡  |  |¡ W 5 d }~X Y nX d S )NÚread)r   r   Ú_decoderÚdecodeÚ_logr   r+   Z_beforeÚwriteÚ_bufferZnew_datar-   Ú	ExceptionZerroredr/   )r*   Údatar   ÚsÚindexr   r   r   r	   Údata_receivedS   s    

zPatternWaiter.data_receivedc              
   C   sT   zd| j j_| j  ¡ }W n, tk
rD } z|  |¡ W 5 d }~X Y nX |  |¡ d S )NT)r   r   Zflag_eofÚeofr   r/   r-   )r*   r:   r   r   r   r	   Úeof_receivedf   s    
zPatternWaiter.eof_receivedc                 C   s6   t |tƒr |jtjkr |  ¡  n|d k	r2|  |¡ d S r   )Ú
isinstanceÚOSErrorÚerrnoZEIOr=   r/   r.   r   r   r	   Úconnection_lostq   s    
zPatternWaiter.connection_lost)Ú__name__Ú
__module__Ú__qualname__r   r   r-   r/   r0   r;   r=   rA   r   r   r   r	   r   ?   s   r   )N)r   )Ú__doc__r   r@   r"   Úsysr   Úpy_version_infoZpexpectr   Zget_running_loopr   Zget_event_loopr   r)   ÚProtocolr   r   r   r   r	   Ú<module>   s   

