o
    ci.                     @   s8  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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Zd dlmZ d dlmZmZ d dlmZ eeZejfddZG d	d
 d
eZG dd deZG dd deZ 						dddZ!edddZ"dd Z#dd Z$dd Z%dd Z&dS )    N)Pdb)Callable)ray_constants)_internal_kv_del_internal_kv_put)DeveloperAPIc                 C   s   t | |d |  d S )N)file)printflush)messagestderr r   A/home/ubuntu/.local/lib/python3.10/site-packages/ray/util/rpdb.py_cry   s   r   c                   @   sL   e Zd Zdd Zedd Zdd Zedfdd	Z	edfd
dZ
dS )_LF2CRLF_FileWrapperc                    sp    | _  d | _j| _j| _j| _j| _j| _j| _t	dr2 fdd| _
d S  j| _
d S )Nrwencodingc                    s     | jjddS )Nreplace)errors)sendallencoder   )data
connectionfhr   r   <lambda>,   s    z/_LF2CRLF_FileWrapper.__init__.<locals>.<lambda>)r   makefilestreamreadreadline	readlinescloser
   filenohasattr_sendr   )selfr   r   r   r   __init__"   s   
z_LF2CRLF_FileWrapper.__init__c                 C   s   | j jS N)r   r   r%   r   r   r   r   2   s   z_LF2CRLF_FileWrapper.encodingc                 C   s
   | j  S r'   )r   __iter__r(   r   r   r   r)   6   s   
z_LF2CRLF_FileWrapper.__iter__z?
c                 C   s   | d|}| | d S )Nz
)subr$   )r%   r   nl_rexr   r   r   write9   s   z_LF2CRLF_FileWrapper.writec                 C   s   |D ]}|  || qd S r'   )r,   )r%   linesr+   liner   r   r   
writelines=   s   z_LF2CRLF_FileWrapper.writelinesN)__name__
__module____qualname__r&   propertyr   r)   recompiler,   r/   r   r   r   r   r   !   s    
r   c                   @   s6   e Zd ZdZdeg df fddZdd Ze ZZdS )_PdbWrapz/Wrap PDB to run a custom exit hook on continue.	exit_hookNc                 C   s   || _ t|  d S r'   )
_exit_hookr   r&   )r%   r7   r   r   r   r&   E   s   z_PdbWrap.__init__c                 C      |    t| |S r'   )r8   r   do_continuer%   argr   r   r   r:   I      z_PdbWrap.do_continue)	r0   r1   r2   __doc__r   r&   r:   do_cdo_contr   r   r   r   r6   B   s
    r6   c                   @   sv   e Zd ZdZdZ		dddZdd Zdd	 Zd
d Ze Z	Z
dd Ze ZZdddZdddZdd Zdd ZdS )
_RemotePdbac  
    This will run pdb as a ephemeral telnet service. Once you connect no one
    else can connect. On construction this object will block execution till a
    client has connected.
    Based on https://github.com/tamentis/rpdb I think ...
    To use this::
        RemotePdb(host="0.0.0.0", port=4444).set_trace()
    Then run: telnet 127.0.0.1 4444
    NFc                 C   sR   || _ || _|| _ttjtj| _| jtjtj	d | j
||f || _d S )NT)_breakpoint_uuid_quiet_patch_stdstreamssocketAF_INETSOCK_STREAM_listen_socket
setsockopt
SOL_SOCKETSO_REUSEADDRbind_ip_address)r%   breakpoint_uuidhostport
ip_addresspatch_stdstreamsquietr   r   r   r&   ]   s   	
z_RemotePdb.__init__c                 C   s   | j std| j| j d f  | jd | j \}}| j s(td|  t|| _t	j
| d| j| jdgd g | _| jrWdD ]}| j|tt|f tt|| j qB| t_d S )Nz>RemotePdb session open at %s:%s, use 'ray debug' to connect...   z#RemotePdb accepted connection from tabzray.*)completekeystdinstdoutskip)r   rX   
__stderr__
__stdout__rW   	__stdin__)rC   r   rM   rH   getsocknamelistenacceptr   handler   r&   backuprD   appendgetattrsyssetattrrA   active_instance)r%   r   addressnamer   r   r   r^   n   s0   

z_RemotePdb.listenc                 C   sJ   | j r| jstd| j   | j D ]
\}}tt|| q| j  d t_d S )NzRestoring streams: %s ...)	ra   rC   r   re   rd   r`   r!   rA   rf   )r%   rh   r   r   r   r   	__restore   s   

z_RemotePdb.__restorec                 C   r9   r'   )_RemotePdb__restorer   do_quitr;   r   r   r   rk      r=   z_RemotePdb.do_quitc                 C   s    |    | jj  t| |S r'   )rj   r`   r   r!   r   r:   r;   r   r   r   r:      s   z_RemotePdb.do_continuec              
   C   sZ   |d u r	t  j}z	t| | W d S  ty, } z|jtjkr! W Y d }~d S d }~ww r'   )rd   	_getframef_backr   	set_traceIOErrorerrno
ECONNRESET)r%   frameexcr   r   r   rn      s   
z_RemotePdb.set_tracec              
   C   s^   zt  d }|   t| d | W d S  ty. } z|jtjkr# W Y d }~d S d }~ww )N   )rd   exc_inforesetr   interactionro   rp   rq   )r%   	tracebacktrs   r   r   r   post_mortem   s   z_RemotePdb.post_mortemc                 C   s\   | j tjjj_tdt 	 i}t
d| j |tjd |   | jj  t| |S )z7remote
        Skip into the next remote call.
        job_idzRAY_PDB_CONTINUE_{}	namespace)rB   ray_privateworkerglobal_workerdebugger_breakpointjsondumpsget_runtime_context
get_job_idr   formatr   KV_NAMESPACE_PDBrj   r`   r   r!   r   r:   )r%   r<   r   r   r   r   	do_remote   s   
z_RemotePdb.do_remotec                 C   s.   | j tjjj_|   | jj	  t
| |S )z?get
        Skip to where the current task returns to.
        )rB   r~   r   r   r   debugger_get_breakpointrj   r`   r   r!   r   r:   r;   r   r   r   do_get   s
   
z_RemotePdb.do_get)FFr'   )r0   r1   r2   r>   rf   r&   r^   rj   rk   do_qdo_exitr:   r?   r@   rn   rz   r   r   r   r   r   r   rA   P   s     

!

	rA   Fc                 C   sd  |r| rJ dd} n| du rt jdd} |du r#tt jdd}|du r0tt jdd	}|s7t j}|r@tj	j
jj}nd
}t|| ||||d}|j }d||d }	tt d }
tj |	|
j|
jdtjt  t t  t   t ! t " t # d}t$d|t%&|dt'j(d |)  t*d|t'j(d |S )z5
    Opens a remote PDB on first available port.
    z.Cannot specify both host and debugger_externalz0.0.0.0NREMOTE_PDB_HOSTz	127.0.0.1REMOTE_PDB_PORT0REMOTE_PDB_QUIET 	localhost)rN   rO   rP   rQ   rR   rS   z{}:{}rT   rt   
)	proctitlepdb_addressfilenamelinenorx   	timestampr{   node_id	worker_idactor_idtask_idz
RAY_PDB_{}T)	overwriter}   r|   )+osenvirongetintbooluuiduuid4hexr~   r   r   r   node_ip_addressrA   rH   r]   r   inspectgetouterframescurrentframe_rayletgetproctitler   r   joinrx   format_exceptionrd   ru   timer   r   get_node_idget_worker_idget_actor_idget_task_idr   r   r   r   r   r^   r   )rO   rP   rR   rS   rN   debugger_externalrQ   rdbsocknamer   parentframeinfor   r   r   r   _connect_ray_pdb   s`   






r   c                 C   s   t jdddkrtjj| S t jdddkrBtjjj	j
dkrDt j}tdddd| r1|  ndtjjj	jd}|j|d dS dS dS )	zsInterrupt the flow of the program and drop into the Ray debugger.

    Can be used within a Ray task or actor.
    	RAY_DEBUG1legacy    NF)rO   rP   rR   rS   rN   r   )rr   )r   r   r   r~   utilray_debugpyrn   r   r   r   r   rd   rl   rm   r   decoderay_debugger_external)rN   rr   r   r   r   r   rn     s    

rn   c                  C   sn   t jdddkrtjj S t jdddkr5td dtjj	_
dd } t| }t j}|| dS dS )	zThe breakpoint hook to use for the driver.

    This disables Ray driver logs temporarily so that the PDB console is not
    spammed: https://github.com/ray-project/ray/issues/18172
    r   r   r   z-*** Temporarily disabling Ray worker logs ***Fc                   S   s   t d dtjj_d S )Nz#*** Re-enabling Ray worker logs ***T)r	   r~   r   r   _worker_logs_enabledr   r   r   r   enable_logging=  s   z)_driver_set_trace.<locals>.enable_loggingN)r   r   r   r~   r   r   rn   r	   r   r   r   r6   rd   rl   rm   )r   pdbrr   r   r   r   _driver_set_trace1  s   

r   c                   C   s   t jdddkS )NRAY_DEBUG_POST_MORTEMr   r   )r   r   r   r   r   r   r   $_is_ray_debugger_post_mortem_enabledF  s   r   c                  C   sD   t jdddkrtjj S td d dd tjj	j
jd} |   d S )Nr   r   F)rO   rP   rR   rS   r   )r   r   r   r~   r   r   _post_mortemr   r   r   r   r   rz   )r   r   r   r   r   J  s   
r   c           
      C   s   t jdkr	dd l}ttjtj}|| |f 	 t jdkrAt|gg g dd }| r3|	t j
 |s@t j
 s@|	t j
 ntt j
|gg g \}}}|D ]*}||krn|d}|s` d S t j|  t j  qPt j
 }	||	  qPq)Nwin32r   TrT   i   )rd   platformmsvcrtrE   rF   rG   connectselectkbhitrb   rW   isattyrecvrX   r,   r   r
   r   sendr   )
rO   rP   r   sready_to_readwrite_socketserror_socketssockr   msgr   r   r   _connect_pdb_clientX  s2   




r   )NNFNNFr'   )'rp   r   r   loggingr   r4   r   rE   rd   r   rx   r   r   r   typingr   r~   ray._privater   ray.experimental.internal_kvr   r   ray.util.annotationsr   	getLoggerr0   logrZ   r   objectr   r6   rA   r   rn   r   r   r   r   r   r   r   r   <module>   sJ   
! 
@