o
    jZh                     @   s   d Z ddlZddlZ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
 ejejejZG dd de	jZG dd	 d	ZeZd
d Zdd Zdd ZdS )zh
gateway code for initiating popen, socket and ssh connections.
(c) 2004-2013, Holger Krekel and others
    N   )gateway_base)Messagec                       sr   e Zd ZdZ fddZedd Zdd Zdd	 ZdddZ	dddZ
dd Zdd Zdd ZdddZ  ZS )Gatewayz0Gateway to a local or remote Python Interpreter.c                    s&   t  j||jdd || _|   d S )Nr   )ioidZ_startcount)super__init__r   specZ_initreceive)selfr   r
   	__class__ F/var/www/html/lang_env/lib/python3.10/site-packages/execnet/gateway.pyr	      s   zGateway.__init__c                 C   s   | j jS N)_ioremoteaddressr   r   r   r   r      s   zGateway.remoteaddressc                 C   sZ   z|   rdpd}t| j }W n ty   d}d}Y nw d| jj| j|| j	j
|S )z3return string representing gateway type and status.zreceive-liveznot-receivingZuninitializednoz-<{} id={!r} {}, {} model, {} active channels>)hasreceiverlen_channelfactoryZchannelsAttributeErrorformatr   __name__r   Z	execmodelbackend)r   rir   r   r   __repr__    s   zGateway.__repr__c              
   C   s   |  d | | jvr|  d dS | j|  z|  d | tj |  d | j  W dS  tt	t
fyN   t d }|  d |  d|  Y dS w )	ztrigger gateway exit.  Defer waiting for finishing
        of receiver-thread and subprocess activity to when
        group.terminate() is called.
        zgateway.exit() calledz'gateway already unregistered with groupNz--> sending GATEWAY_TERMINATEz--> io.close_writer   z-io-error: could not send termination sequencez exception: %r)_trace_groupZ_unregister_sendr   ZGATEWAY_TERMINATEr   Zclose_write
ValueErrorEOFErrorOSErrorsysexc_info)r   vr   r   r   exit,   s   





zGateway.exitTFc                 C   s*   ||f| _ t| j }| jtj|d dS )z
        set the string coercion for this gateway
        the default is to try to convert py2 str as py3 str,
        but not to try and convert py3 str to py2 str
        )dataN)Z
_strconfigr   dumps_internalr!   r   ZRECONFIGURE)r   Zpy2str_as_py3strZpy3str_as_py2strr)   r   r   r   reconfigure@   s   
zGateway.reconfigurec                 C   sH   |st | ds!| t}zt| | _W |  | jS |  w | jS )z,return some sys/env information from remote._cache_rinfo)hasattrremote_execrinfo_sourceRInforeceiver,   Z	waitclose)r   updatechr   r   r   _rinfoJ   s   

zGateway._rinfoc                 C   s   | j  dkS )z/return True if gateway is able to receive data.r   )Z_receivepoolactive_countr   r   r   r   r   T   s   zGateway.hasreceiverc                 C   s6   |   }| tj|j | }| j|j t|S )z8return information object about remote execution status.)	
newchannelr!   r   ZSTATUSr   r1   r   Z_local_closeRemoteStatus)r   channelZ
statusdictr   r   r   remote_statusX   s
   zGateway.remote_statusc              
   K   s   d}d}t |tjrt|}t| t|}nt |tjr-|j	}t|}t
|}ntt|}|s<|r<td|  }| tj|jt||||f |S )a  return channel object and connect it to a remote
        execution thread where the given ``source`` executes.

        * ``source`` is a string: execute source string remotely
          with a ``channel`` put into the global namespace.
        * ``source`` is a pure function: serialize source and
          call function with ``**kwargs``, adding a
          ``channel`` object to the keyword arguments.
        * ``source`` is a pure module: execute source of module
          with a ``channel`` in its global namespace

        In all cases the binding ``__name__='__channelexec__'``
        will be available in the global namespace of the remotely
        executing code.
        Nz-can't pass kwargs to non-function remote_exec)
isinstancetypes
ModuleTypeinspectgetsourcefile	linecacheupdatecache	getsourceFunctionTyper   _source_of_functiontextwrapdedentstr	TypeErrorr6   r!   r   ZCHANNEL_EXECr   r   r*   )r   sourcekwargsZ	call_name	file_namer8   r   r   r   r.   b   s(   



zGateway.remote_execNc                 C   s   t d dS )z1DEPRECATED.  Is currently a NO-OPERATION already.z?WARNING: remote_init_threads() is a no-operation in execnet-1.2N)print)r   numr   r   r   remote_init_threads   s   zGateway.remote_init_threads)TF)Fr   )r   
__module____qualname____doc__r	   propertyr   r   r(   r+   r4   r   r9   r.   rM   __classcell__r   r   r   r   r      s    





(r   c                   @   s   e Zd Zdd Zdd ZdS )r0   c                 C   s   | j | d S r   )__dict__r2   )r   rI   r   r   r   r	      s   zRInfo.__init__c                 C   s&   d dd t| j D }d| S )Nz, c                 s   s    | ]}d | V  qdS )z%s=%sNr   ).0itemr   r   r   	<genexpr>   s    z!RInfo.__repr__.<locals>.<genexpr>z
<RInfo %r>)joinsortedrS   items)r   infor   r   r   r      s   zRInfo.__repr__N)r   rN   rO   r	   r   r   r   r   r   r0      s    r0   c              	   C   sB   dd l }dd l}| t|j|jd d |j| | d d S )Nr      )
executableversion_infoplatformcwdpid)	r%   ossenddictr\   r]   r^   getcwdgetpid)r8   r%   ra   r   r   r   r/      s   r/   c                    s>   dd l  dd lt|j fdd  | D S )Nr   c                    s4   g | ]}t | jr|jvr|jjvr|jqS r   )r:   Namer   rS   )rT   nodeastbuiltinsvarsr   r   
<listcomp>   s    

z-_find_non_builtin_globals.<locals>.<listcomp>)ri   rj   rc   fromkeysco_varnameswalkparse)rH   codeobjr   rh   r   _find_non_builtin_globals   s   rr   c                 C   s   | j dkr	tdzt| }W n ty    t| d }Y nw |j}|r,|d dkr0td| j}| j}|d ur>tdzt	| }W n t
yR   td|  w t|}t||}|rdtd|d	|jd
  }|| S )Nz<lambda>z can't evaluate lambda functions'r   r8   z0expected first function argument to be `channel`z'functions with closures can't be passedzcan't find source file for %sz.the use of non-builtin globals isn't supported
r   )r   r"   r=   getfullargspecr   
getargspecargs__closure____code__rA   r$   rD   rE   rr   co_firstlineno)functionsigrv   closurerq   rH   Zused_globalsZ
leading_wsr   r   r   rC      s2   



rC   )rP   r=   r?   ra   r%   rD   r;   Zexecnet r   r   pathdirname__file__Z	importdirZBaseGatewayr   r0   r7   r/   rr   rC   r   r   r   r   <module>   s"    {	