3 .�]�P � @ s� d dl Z d dlZd dlZd dlZd dlZddlmZmZmZ ddl m Z mZmZ ej d dkZerfeneZG dd� de�ZG dd � d e�ZdS ) � N� )�ExceptionPexpect�EOF�TIMEOUT)�Expecter�searcher_string�searcher_re� c @ s, e Zd ZdZeddd��Zed dd��ZdS ) � _NullCoderzPass bytes through unchanged.Fc C s | S )N� )�b�finalr r �/usr/lib/python3.6/spawnbase.py�encode s z_NullCoder.encodec C s | S )Nr )r r r r r �decode s z_NullCoder.decodeN)F)F)�__name__� __module__�__qualname__�__doc__�staticmethodr r r r r r r s r c @ s� e Zd ZdZdZdZdZd0dd�Zd d � Zdd� Z d d� Z d1dd�Zdd� Zdd� Z d4dd�Zd7dd�Zd:dd�Zd=dd�Zd?dd�ZdAd d!�Zd"d#� ZdCd$d%�Zd&d'� Zd(d)� Zd*d+� Zd,d-� Zd.d/� ZdS )D� SpawnBasez�A base class providing the backwards-compatible spawn API for Pexpect. This should not be instantiated directly: use :class:`pexpect.spawn` or :class:`pexpect.fdpexpect.fdspawn`. NF� �� �strictc C s� t j| _t j| _t j| _d | _d| _d | _d | _d | _d | _ d| _ d | _d | _d | _ d| _|| _t| _|| _d | _d | _|| _|d kr�t� nt� | _|| _d| _d| _d| _d| _d| _dt| � d | _ d| _!|| _"|| _#|d k�rJt$� | _%| _&t| _'d | _(t)�r.tt*f| _+t,j-j.d �| _-dd� }|| _/nt0f| _+t,j-| _-t jj1| _/n\t2j3|�|�| _%t2j4|�|�| _&t| _'d | _(tf| _+t)�r�t,j-| _-nt,j-j5d �| _-t jj1| _/d | _6d S )NFTr g�������?g�������?g-C��6?�<�>s �asciic S s8 yt jjj| �S tk r2 t jj| jdd��S X d S )Nr �replace)�sys�stdout�buffer�write�AttributeErrorr )r r r r �write_to_stdout^ s z+SpawnBase.__init__.<locals>.write_to_stdoutz ���)7r �stdinr �stderr�searcher� ignorecase�before�after�matchZmatch_indexZ terminatedZ exitstatusZsignalstatusZstatus�child_fd�timeoutr � delimiter�logfile�logfile_read�logfile_send�maxread�bytes� text_typer �searchwindowsizeZdelaybeforesendZdelayaftercloseZdelayafterterminateZdelayafterreadZ softspace�repr�name�closed�encoding�codec_errorsr Z_encoder�_decoder�string_type�crlf�PY3�str�allowed_string_types�os�linesepr r# Z basestringr! �codecs�getincrementalencoder�getincrementaldecoderr Zasync_pw_transport)�selfr- r2 r5 r/ r9 r: r# r r r �__init__ sj zSpawnBase.__init__c C sR | j d k r | j j|� | j j� |dkr.| jn| j}|d k rN|j|� |j� d S )N�send)r/ r! �flushr1 r0 )rF �s� directionZ second_logr r r �_logy s zSpawnBase._logc C s$ | j d kr t|t� r |jd�S |S )Nr )r9 � isinstancer3 r )rF rJ r r r �_coerce_expect_string� s zSpawnBase._coerce_expect_stringc C s$ | j d kr t|t� r |jd�S |S )Nzutf-8)r9 rM r3 r )rF rJ r r r �_coerce_send_string� s zSpawnBase._coerce_send_stringr c C s� yt j| j|�}W nB tk rT } z&|jd tjkrBd| _td��� W Y dd}~X nX |dkrld| _td��| j j |dd�}| j|d � |S ) z�This reads data from the file descriptor. This is a simple implementation suitable for a regular file. Subclasses using ptys or pipes should override it. The timeout parameter is ignored. r Tz,End Of File (EOF). Exception style platform.N� z/End Of File (EOF). Empty string style platform.F)r �read)rA rQ r, �OSError�args�errnoZEIO�flag_eofr r; r rL )rF �sizer- rJ �errr r r �read_nonblocking� s zSpawnBase.read_nonblockingc C s. t djt|�|djdd� | jD ��d���d S )Nz`got {badtype} ({badobj!r}) as pattern, must be one of: {goodtypes}, pexpect.EOF, pexpect.TIMEOUTz, c S s g | ]}t |��qS r )r? )�.0Zastr r r � <listcomp>� s z/SpawnBase._pattern_type_err.<locals>.<listcomp>)ZbadtypeZbadobjZ goodtypes)� TypeError�format�type�joinr@ )rF �patternr r r �_pattern_type_err� s zSpawnBase._pattern_type_errc C s� |dkrg S t |t�s|g}tj}| jr2|tjB }g }x�t|�D ]�\}}t || j�rr| j|�}|j tj ||�� q@|tkr�|j t� q@|tkr�|j t� q@t |t tj d���r�|j |� q@| j|� q@W |S )a� This compiles a pattern-string or a list of pattern-strings. Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those. Patterns may also be None which results in an empty list (you might do this if waiting for an EOF or TIMEOUT condition without expecting any pattern). This is used by expect() when calling expect_list(). Thus expect() is nothing more than:: cpl = self.compile_pattern_list(pl) return self.expect_list(cpl, timeout) If you are using expect() within a loop it may be more efficient to compile the patterns first and then call expect_list(). This avoid calls in a loop to compile_pattern_list():: cpl = self.compile_pattern_list(my_pattern) while some_condition: ... i = self.expect_list(cpl, timeout) ... N� )rM �list�re�DOTALLr( � IGNORECASE� enumerater@ rN �append�compiler r r] r` )rF ZpatternsZ compile_flags�compiled_pattern_list�idx�pr r r �compile_pattern_list� s( zSpawnBase.compile_pattern_listc K s>