o
    1 i                     @   s   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
eZdZdd Ze Zdd Zd	d
 Ze	dddZdd Zdd Zdd Zdd ZdS )    N)build_address)DeveloperAPIZpost_mortem_error_uuidc               	   C   sN   zt d} t| dr| jdk rt | W S  ttfy&   td Y d S w )Ndebugpy__version__z1.8.0zModule 'debugpy>=1.8.0' cannot be loaded. Ray Debugpy Debugger will not work without 'debugpy>=1.8.0' installed. Install this module using 'pip install debugpy==1.8.0' )	importlibimport_modulehasattrr   ImportErrorModuleNotFoundErrorlogerror)r    r   \/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/util/debugpy.py_try_import_debugpy   s   
r   c                  C   s   t t_t t_ddl} t | _dS )a>  
    This method overrides the breakpoint() function to set_trace()
    so that other threads can reuse the same setup logic.
    This is based on: https://github.com/microsoft/debugpy/blob/ef9a67fe150179ee4df9997f9273723c26687fab/src/debugpy/_vendored/pydevd/pydev_sitecustomize/sitecustomize.py#L87 # noqa: E501
    r   N)	set_tracesys__breakpointhook__breakpointhookbuiltins
breakpoint)__builtin__r   r   r   _override_breakpoint_hooks#   s   
r   c                  C   s   t P t } | s	 W d   dS tjjjj}|s:| tjjjjdf\}}tjjj	| t
dt||  nt
d|  W d   dS W d   dS 1 sUw   Y  dS )zk
    This is a thread safe method that ensure that the debugger port
    is open, and if not, open it.
    Nr   zRay debugger is listening on z Ray debugger is already open on )debugger_port_lockr   ray_privateworkerglobal_workerdebugger_portlistenZnode_ip_addressZset_debugger_portr   infor   )r   r   hostportr   r   r   &_ensure_debugger_port_open_thread_safe0   s    "r"   c                 C   s   t  }|sdS t  t  tjjj  d}t	| |
  W d   n1 s*w   Y  t	d | tkr=t  dS t  dS )zrInterrupt the flow of the program and drop into the Ray debugger.
    Can be used within a Ray task or actor.
    NzuWaiting for debugger to attach (see https://docs.ray.io/en/latest/ray-observability/ray-distributed-debugger.html)...zDebugger client is connected)r   r"   r   r   r   r   r   Zworker_paused_by_debuggerr   r   Zwait_for_clientPOST_MORTEM_ERROR_UUID_debugpy_excepthook_debugpy_breakpoint)Zbreakpoint_uuidr   msgr   r   r   r   H   s   


	

r   c                  C   s   ddl } | jt jd dS )z:
    Drop the user into the debugger on a breakpoint.
    r   N)Zstop_at_frame)pydevdsettracer   	_getframef_back)r'   r   r   r   r%   g   s   r%   c               	   C   s   ddl } ddl}| }|  }||}| jd7  _z#t }||||| t	|d |d |d  W | jd8  _dS | jd8  _w )zD
    Drop the user into the debugger on an unhandled exception.
    r   N      )
	threadingr'   Zget_global_debuggercurrent_threadZset_additional_thread_info
is_tracingr   exc_infoZstop_on_unhandled_exception
excepthook)r-   r'   Zpy_dbthreadZadditional_infor   r   r   r   r$   p   s   
"r$   c                   C   s   t jdddkS )NZRAY_DEBUG_POST_MORTEM01)osenvirongetr   r   r   r   $_is_ray_debugger_post_mortem_enabled   s   r8   c                   C   s   t tS N)r   r#   r   r   r   r   _post_mortem   s   r:   r9   )r   loggingr5   r   r-   r   Zray._common.network_utilsr   Zray.util.annotationsr   	getLogger__name__r   r#   r   Lockr   r   r"   r   r%   r$   r8   r:   r   r   r   r   <module>   s(    
	