o
    1 iù.  ã                   @   sD  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 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)Úray_constants)Ú_internal_kv_delÚ_internal_kv_put)ÚDeveloperAPIc                 C   s   t | |d | ¡  d S )N)Úfile)ÚprintÚflush)ÚmessageÚstderr© r   úY/home/app/PaddleOCR-VL-test/.venv_paddleocr/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-   r0   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&   r8   r   r   r   r'   F   s   z_PdbWrap.__init__c                 C   ó   |   ¡  t | |¡S r(   )r9   r   Údo_continue©r&   Úargr   r   r   r;   J   ó   z_PdbWrap.do_continue)	r1   r2   r3   Ú__doc__r   r'   r;   Údo_cÚdo_contr   r   r   r   r7   C   s
    r7   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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.*)ZcompletekeyÚstdinÚstdoutÚskip)r   rX   Ú
__stderr__Ú
__stdout__rW   Ú	__stdin__)rD   r   r   rN   rI   ÚgetsocknameÚlistenÚacceptr   Úhandler   r'   ÚbackuprE   ÚappendÚgetattrÚsysÚsetattrrB   Úactive_instance)r&   r   ÚaddressÚnamer   r   r   r^   o   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   rD   r   re   rd   r`   r"   rB   rf   )r&   rh   r   r   r   r   Z	__restore   s   

z_RemotePdb.__restorec                 C   r:   r(   )Ú_RemotePdb__restorer   Údo_quitr<   r   r   r   rj   ˜   r>   z_RemotePdb.do_quitc                 C   s    |   ¡  | jj ¡  t | |¡S r(   )ri   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   rm   ¥   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   Zinteractionrn   ro   rp   )r&   Ú	tracebackÚtrr   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)rC   ÚrayÚ_privateÚworkerÚglobal_workerÚdebugger_breakpointÚjsonÚdumpsÚget_runtime_contextÚ
get_job_idr   Úformatr   ÚKV_NAMESPACE_PDBri   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.
        )rC   r|   r}   r~   r   Zdebugger_get_breakpointri   r`   r   r"   r   r;   r<   r   r   r   Údo_getÎ   s
   
ÿz_RemotePdb.do_get)FFr(   )r1   r2   r3   r?   rf   r'   r^   ri   rj   Zdo_qZdo_exitr;   r@   rA   rm   rx   r‡   rˆ   r   r   r   r   rB   Q   s     

ù!

	rB   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.0NZREMOTE_PDB_HOSTz	127.0.0.1ZREMOTE_PDB_PORTÚ0ZREMOTE_PDB_QUIETÚ Ú	localhost)rO   rP   rQ   rR   rS   rT   z{}:{}rU   rs   Ú
)Z	proctitleÚpdb_addressÚfilenameÚlinenorv   Ú	timestampry   Znode_idZ	worker_idZactor_idÚtask_idz
RAY_PDB_{}T)Ú	overwriter{   rz   )+ÚosÚenvironÚgetÚintÚboolÚuuidÚuuid4Úhexr|   r}   r~   r   Znode_ip_addressrB   rI   r]   r…   ÚinspectÚgetouterframesÚcurrentframeZ_rayletZgetproctitlerŽ   r   Újoinrv   Úformat_exceptionrd   rt   Útimerƒ   r„   Zget_node_idZget_worker_idZget_actor_idZget_task_idr   r   r‚   r   r†   r^   r   )rP   rQ   rS   rT   rO   Údebugger_externalrR   ÚrdbÚsocknamer   Z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)rP   rQ   rS   rT   rO   r¡   )rq   )r“   r”   r•   r|   ÚutilÚray_debugpyrm   r}   r~   r   r€   rd   rk   rl   r¤   ÚdecodeÚray_debugger_external)rO   rq   r¢   r   r   r   rm     s    

úórm   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ª   rm   r
   r}   r~   r­   r7   rd   rk   rl   )r®   Úpdbrq   r   r   r   Ú_driver_set_trace2  s   

ör°   c                   C   s   t j dd¡dkS )NZRAY_DEBUG_POST_MORTEMr‰   r¦   )r“   r”   r•   r   r   r   r   Ú$_is_ray_debugger_post_mortem_enabledG  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)rP   rQ   rS   rT   r¡   )r“   r”   r•   r|   r©   rª   Ú_post_mortemr¤   r}   r~   r   r¬   rx   )r¢   r   r   r   r²   K  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   TrU   i   )rd   ÚplatformÚmsvcrtrF   rG   rH   ÚconnectÚselectZkbhitrb   rW   ÚisattyÚrecvrX   r-   r«   r   r    Úsendr   )
rP   rQ   rµ   ÚsZready_to_readZwrite_socketsZerror_socketsÚsockr   Úmsgr   r   r   Ú_connect_pdb_clientY  s2   

€
ÿ

är¾   )NNFNNFr(   ))ro   r›   r   Úloggingr“   r5   r·   rF   rd   r    rv   r˜   r¯   r   Útypingr   r|   Zray._common.network_utilsr   Zray._privater   Zray.experimental.internal_kvr   r   Zray.util.annotationsr   Ú	getLoggerr1   ÚlogrZ   r   Úobjectr   r7   rB   r¤   rm   r°   r±   r²   r¾   r   r   r   r   Ú<module>   sL   
! 
ú@