o
    $ºir/  ã                   @   sH  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m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)Úbuild_addressÚis_ipv6)Úray_constants)Ú_internal_kv_delÚ_internal_kv_put)ÚDeveloperAPIc                 C   s   t | |d | ¡  d S )N)Úfile)ÚprintÚflush)ÚmessageÚstderr© r   úJ/home/ubuntu/veenaModal/venv/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   3   s   z_LF2CRLF_FileWrapper.encodingc                 C   s
   | j  ¡ S r)   )r   Ú__iter__r*   r   r   r   r+   7   s   
z_LF2CRLF_FileWrapper.__iter__z?
c                 C   s   |  d|¡}|  |¡ d S )Nz
)Úsubr&   )r'   r   Únl_rexr   r   r   Úwrite:   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.   r1   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'   r9   r   r   r   r(   F   s   z_PdbWrap.__init__c                 C   ó   |   ¡  t | |¡S r)   )r:   r   Údo_continue©r'   Úargr   r   r   r<   J   ó   z_PdbWrap.do_continue)	r2   r3   r4   Ú__doc__r   r(   r<   Údo_cÚdo_contr   r   r   r   r8   C   s
    r8   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   s`   || _ || _|| _t t|ƒrtjntjtj¡| _| j 	tj
tjd¡ | j ||f¡ || _d S )NT)Ú_breakpoint_uuidÚ_quietÚ_patch_stdstreamsÚsocketr   ÚAF_INET6Ú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t| j| j ¡ d ƒ ƒ | j d¡ | j ¡ \}}| j s)td|› ƒ t|ƒ| _	t
j| d| j	| j	dgd g | _| jrXdD ]}| j |tt|ƒf¡ tt|| j	ƒ qC| t_d S )Nz;RemotePdb session open at %s, use 'ray debug' to connect...é   z#RemotePdb accepted connection from Útabzray.*)ÚcompletekeyÚstdinÚstdoutÚskip)r   r[   Ú
__stderr__Ú
__stdout__rZ   Ú	__stdin__)rE   r   r   rP   rK   ÚgetsocknameÚlistenÚacceptr   Úhandler   r(   ÚbackuprF   ÚappendÚgetattrÚsysÚsetattrrC   Úactive_instance)r'   r   ÚaddressÚnamer   r   r   ra   q   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 ...)	rd   rE   r   rh   rg   rc   r#   rC   ri   )r'   rk   r   r   r   r   Ú	__restore’   s   

z_RemotePdb.__restorec                 C   r;   r)   )Ú_RemotePdb__restorer   Údo_quitr=   r   r   r   rn   š   r?   z_RemotePdb.do_quitc                 C   s    |   ¡  | jj ¡  t | |¡S r)   )rm   rc   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)   )rg   Ú	_getframeÚf_backr   Ú	set_traceÚIOErrorÚerrnoÚ
ECONNRESET)r'   ÚframeÚexcr   r   r   rq   §   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é   )rg   Úexc_infoÚresetr   Úinteractionrr   rs   rt   )r'   Ú	tracebackÚtrv   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)rD   ÚrayÚ_privateÚworkerÚglobal_workerÚdebugger_breakpointÚjsonÚdumpsÚget_runtime_contextÚ
get_job_idr   Úformatr   ÚKV_NAMESPACE_PDBrm   rc   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.
        )rD   r   r‚   rƒ   r„   Údebugger_get_breakpointrm   rc   r   r#   r   r<   r=   r   r   r   Údo_getÐ   s
   
ÿz_RemotePdb.do_get)FFr)   )r2   r3   r4   r@   ri   r(   ra   rm   rn   Údo_qÚdo_exitr<   rA   rB   rq   r}   rŒ   rŽ   r   r   r   r   rC   Q   s     

ù!

	rC   Fc                 C   sb  |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 ¡ }t||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)rQ   rR   rS   rT   rU   rV   rW   rw   Ú
)Ú	proctitleÚpdb_addressÚfilenameÚlinenor{   Ú	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_addressrC   rK   r`   r   ÚinspectÚgetouterframesÚcurrentframeÚ_rayletÚgetproctitlerš   r›   Újoinr{   Úformat_exceptionrg   rx   Útimerˆ   r‰   Úget_node_idÚget_worker_idÚget_actor_idÚget_task_idr   rŠ   r†   r‡   r   r‹   ra   r   )rR   rS   rU   rV   rQ   Údebugger_externalrT   Ú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)rR   rS   rU   rV   rQ   r·   )ru   )r¢   r£   r¤   r   ÚutilÚray_debugpyrq   r‚   rƒ   r„   r…   rg   ro   rp   r»   ÚdecodeÚray_debugger_external)rQ   ru   r¸   r   r   r   rq     s    

úórq   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 )	zºThe 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Á   rq   r   r‚   rƒ   rÄ   r8   rg   ro   rp   )rÅ   Úpdbru   r   r   r   Ú_driver_set_trace4  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_enabledI  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)rR   rS   rU   rV   r·   )r¢   r£   r¤   r   rÀ   rÁ   Ú_post_mortemr»   r‚   rƒ   r„   rÃ   r}   )r¸   r   r   r   rÊ   M  s   
ûrÊ   c           
      C   s  t jdkr	dd l}t t| ƒrtjntjtj¡}| | |f¡ 	 t jdkrHt	 	|gg g d¡d }| 
¡ r:| t j¡ |sGt j ¡ sG| t j¡ nt	 	t j|gg g ¡\}}}|D ]*}||kru| d¡}|sg d S t j | ¡ ¡ t j ¡  qWt j ¡ }	| |	 ¡ ¡ qWq )NÚwin32r   TrW   i   )rg   ÚplatformÚmsvcrtrG   r   rH   rI   rJ   ÚconnectÚselectÚkbhitre   rZ   ÚisattyÚrecvr[   r.   rÂ   r   r!   Úsendr   )
rR   rS   rÍ   ÚsÚready_to_readÚwrite_socketsÚerror_socketsÚsockr   Úmsgr   r   r   Ú_connect_pdb_client[  s6   
ÿ
€
ÿ

ärÚ   )NNFNNFr)   )*rs   r«   r†   Úloggingr¢   r6   rÏ   rG   rg   r²   r{   r§   rÆ   r   Útypingr   r   Úray._common.network_utilsr   r   Úray._privater   Úray.experimental.internal_kvr   r   Úray.util.annotationsr	   Ú	getLoggerr2   Úlogr]   r   Úobjectr   r8   rC   r»   rq   rÇ   rÉ   rÊ   rÚ   r   r   r   r   Ú<module>   sL   
! 
ú@