o
    "ifg                     @  s   d Z ddlm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	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ G dd dejZG dd dZeZd!ddZd"ddZd#dd ZdS )$zhGateway code for initiating popen, socket and ssh connections.

(c) 2004-2013, Holger Krekel and others
    )annotationsN)TYPE_CHECKING)Any)Callable   )gateway_base)IO)Channel)Message)Group)XSpecc                      s   e Zd ZU dZded< d/ fd
dZed0ddZd0ddZd1ddZ		d2d3ddZ
d4d5ddZd6ddZd7d!d"Zd8d(d)Zd9d:d-d.Z  ZS );Gatewayz0Gateway to a local or remote Python Interpreter.r   _groupior   specr   returnNonec                   s&   t  j||jdd || _|   dS )z	:private:r   )r   id_startcountN)super__init__r   r   _initreceive)selfr   r   	__class__ H/var/www/html/corbot_env/lib/python3.10/site-packages/execnet/gateway.pyr      s   zGateway.__init__strc                 C  s   | j jS N)_ioremoteaddressr   r   r   r   r    #   s   zGateway.remoteaddressc                 C  sp   z|   rdpd}tt| j }W n ty    d}d}Y nw d| jj d| jd| d| j	j
 d	| d
S )z.A string representing gateway type and status.zreceive-liveznot-receivinguninitializedno<z id= , z model, z active channels>)hasreceiverr   len_channelfactorychannelsAttributeErrorr   __name__r   	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yQ } z|  d |  d|  W Y d}~dS d}~w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_writez-io-error: could not send termination sequencez exception: %r)_tracer   _unregister_sendr
   GATEWAY_TERMINATEr   close_write
ValueErrorEOFErrorOSError)r   excr   r   r   exit2   s   





zGateway.exitTFpy2str_as_py3strboolpy3str_as_py2strc                 C  s*   ||f| _ t| j }| jtj|d dS )zSet 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)
_strconfigr   dumps_internalr4   r
   RECONFIGURE)r   r<   r>   r?   r   r   r   reconfigureF   s   
zGateway.reconfigureupdateRInfoc                 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_sourcerE   receiverF   	waitclose)r   rD   chr   r   r   _rinfoR   s   

zGateway._rinfoc                 C  s   | j  dkS )z(Whether gateway is able to receive data.r   )_receivepoolactive_countr!   r   r   r   r'   \   s   zGateway.hasreceiverRemoteStatusc                 C  s6   |   }| tj|j | }| j|j t|S )z5Obtain information about the remote execution status.)	
newchannelr4   r
   STATUSr   rJ   r)   _local_closerP   )r   channel
statusdictr   r   r   remote_status`   s
   zGateway.remote_statussourceCstr | types.FunctionType | Callable[..., object] | types.ModuleTypekwargsobjectr	   c              
   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dedentr   	TypeErrorrQ   r4   r
   CHANNEL_EXECr   r   rA   )r   rW   rY   	call_name	file_namerT   r   r   r   rH   j   s(   



zGateway.remote_execNnum
int | Nonec                 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   rk   r   r   r   remote_init_threads   s   zGateway.remote_init_threads)r   r   r   r   r   r   r   r   r   r   )TF)r<   r=   r>   r=   r   r   )F)rD   r=   r   rE   )r   r=   )r   rP   )rW   rX   rY   rZ   r   r	   r   )rk   rl   r   r   )r,   
__module____qualname____doc____annotations__r   propertyr    r1   r;   rC   rM   r'   rV   rH   rn   __classcell__r   r   r   r   r      s   
 







,r   c                   @  s2   e Zd ZdddZdddZerdd
dZdS dS )rE   r   r   c                 C  s   | j | d S r   )__dict__rD   )r   rY   r   r   r   r      s   zRInfo.__init__r   c                 C  s&   d dd t| j D }d| S )Nr&   c                 s  s"    | ]\}}| d | V  qdS )=Nr   ).0kvr   r   r   	<genexpr>   s     z!RInfo.__repr__.<locals>.<genexpr>z
<RInfo %r>)joinsortedrw   items)r   infor   r   r   r1      s   zRInfo.__repr__namer   c                 C  s   d S r   r   )r   r   r   r   r   __getattr__   s    zRInfo.__getattr__Nrp   ro   )r   r   r   r   )r,   rq   rr   r   r1   r   r   r   r   r   r   rE      s    

rE   r   r   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)	ossyssenddictr   r   r   getcwdgetpid)rT   r   r   r   r   r   rI      s   rI   rW   r   codeobjtypes.CodeType	list[str]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   rw   )ry   nodeastbuiltinsvarsr   r   
<listcomp>   s    

z-_find_non_builtin_globals.<locals>.<listcomp>)r   r   r   fromkeysco_varnameswalkparse)rW   r   r   r   r   _find_non_builtin_globals   s   r   function*types.FunctionType | Callable[..., object]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
yX } ztd|  |d }~ww t|}t||}|rjtd|d	|jd
  }|| S )Nz<lambda>z can't evaluate lambda functions'r   rT   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,   r7   r^   getfullargspecr+   
getargspecargs__closure____code__rb   r9   re   rf   r   co_firstlineno)	r   sigr   closurer   rW   eused_globals
leading_wsr   r   r   rd      s4   



rd   rp   )rW   r   r   r   r   r   )r   r   r   r   )rs   
__future__r   r^   r`   re   r\   typingr   r   r    r   r   r	   r
   multir   xspecr   BaseGatewayr   rE   rP   rI   r   rd   r   r   r   r   <module>   s,     

