o
    ^i|                     @  s   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mZmZ d dl	m
Z
mZmZmZ d dlmZmZmZmZmZ G dd dejZdd	 Zd
d ZdS )    )annotationsN)adaptercommonlauncher)jsonlog	messagingsockets)clients
components	launchersserverssessionsc                      s  e Zd ZU dZejjZded< 	 G dd dejZG dd dejZ	 fdd	Z
d
d Zdd Zedd Zedd Zedd Zdd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Z fd(d)Zd*d+ Zd,d- Z  ZS ).Clientz+Handles the client side of a debug session.zset[servers.Connection]known_subprocessesc                   @  s   e Zd ZdddddddZdS )zClient.CapabilitiesF)supportsVariableTypesupportsVariablePagingsupportsRunInTerminalRequestsupportsMemoryReferences#supportsArgsCanBeInterpretedByShellsupportsStartDebuggingRequestN)__name__
__module____qualname__
PROPERTIES r   r   R/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/debugpy/adapter/clients.pyCapabilities   s    
r   c                   @  s$   e Zd ZdddejddddZdS )zClient.Expectationszen-USTpathoptional)localelinesStartAt1columnsStartAt1
pathFormatN)r   r   r   r   enumr   r   r   r   r   Expectations$   s    
r&   c                   s:  |dkr1t d|  d| _tj }ttjd t	_
}t|j ttjd t	_}t|j n	d| _tj|}t 5}t || d | _	 d| _	 d | _	 d| _	 d | _	 g | _	 d| _t | _| |_|  W d    n1 svw   Y  | jddd	d
t j!id | jdddd
t j!id t"  d S )Nstdioz"Connecting to client over stdio...TrwFoutput	telemetryptvsdpackageVersion)categoryr*   datadebugpy)#r   infousing_stdior   JsonIOStream
from_stdioopenosdevnullsysstdinatexitregisterclosestdoutfrom_socketr   Sessionsuper__init__	client_idhas_startedstart_requestrestart_requested_initialize_request_deferred_events_forward_terminate_requestsetr   clientchannel
send_eventr0   __version__report_sockets)selfsockstreamr9   r=   session	__class__r   r   rA   ,   sX   


&zClient.__init__c                 C  s6   | j d ur| j | td d S | jj| d S )NzPropagation deferred.)rG   appendr   debugrJ   rK   	propagaterO   eventr   r   r   propagate_after_starts   s   
zClient.propagate_after_startc                 C  sP   t d| j | jD ]}t d|  | jj| q
t d| j d | _d S )Nz%Propagating deferred events to {0}...zPropagating deferred {0}z&All deferred events propagated to {0}.)r   rV   rJ   rG   describerK   rW   r1   rX   r   r   r   _propagate_deferred_events~   s   

z!Client._propagate_deferred_eventsc                 C  s   | j r| j j| d S d S N)serverrK   rW   rX   r   r   r   rY      s   zClient.eventc                 C  s   | j j|S r]   )r^   rK   delegaterO   requestr   r   r   ra      s   zClient.requestc                 C  s   | j d ur
|d|dd| _| | || _| | || _|| _ dddddd	d
ddddddddg}i ddddddddddddddddddddddddddddddddd ddd|dd!S )"NzSession is already initializedclientID raisedzRaised ExceptionsFz'Break whenever any exception is raised.)filterlabeldefaultdescriptionuncaughtzUncaught ExceptionsTz=Break when the process is exiting due to unhandled exception.userUnhandledzUser Uncaught Exceptionsz/Break when exception escapes into library code.supportsCompletionsRequestsupportsConditionalBreakpoints supportsConfigurationDoneRequestsupportsDebuggerProperties supportsDelayedStackTraceLoadingsupportsEvaluateForHoverssupportsExceptionInfoRequestsupportsExceptionOptionssupportsFunctionBreakpoints!supportsHitConditionalBreakpointssupportsLogPointssupportsModulesRequestsupportsSetExpressionsupportsSetVariablesupportsValueFormattingOptionssupportsTerminateDebuggeesupportsTerminateRequest)supportsGotoTargetsRequestsupportsClipboardContextexceptionBreakpointFilterssupportsStepInTargetsRequest)rF   
isnt_validrB   r   capabilitiesr&   expectations)rO   ra   exception_breakpoint_filtersr   r   r   initialize_request   s|   

	
zClient.initialize_requestc                   s   t jj fdd}|S )Nc                   s  | ddsJ | jd u r|d| js| jr|d|dtd| j_| jjr/t	
  t|dtt | j_} | | |jd urId S | jr| j| j d | _|j}| jr|dd	k}d
|v rvt|}t|d
h |d< d}|drt|}|d= d}||d< z| jj|j| W n6 tjy   |i  | jd| jt|j Y d S  tjy } z|| W Y d }~nd }~ww | jjr|| _ d| _!|i  | "  d S | j#d || _ tj$S )NlaunchattachzSession is not initialized yetzSession is already startednoDebugFdebugOptionsconsoleinternalConsoleRedirectOutputTredirectOutputisOutputRedirected){0} disconnected before responding to {1}initialized)%
is_requestrF   r   r   r^   r   rg   rR   no_debugr   dont_wait_for_first_connectionrI   arraystrdebug_optionsresponse
initialize	argumentsgetdictlistrK   ra   commandr   NoMoreMessagesrespondfinalizeformatreprMessageHandlingErrorrW   rD   rC   r\   rL   NO_RESPONSE)rO   ra   r   r   redirectingexcfr   r   handle   sn   








z-Client._start_message_handler.<locals>.handle)r   	Componentmessage_handler)r   r   r   r   r   _start_message_handler   s   LzClient._start_message_handlerc                   sP  ddl m} | jjdkstt rdtdt	
t  fdd}d}|v r8d	v r7d
nd	v r>d	}|t	j
tddd}t|sQtjg}|dt	j
tdd7 }|dd  jd< |jd< dtdd}|dkrz|d }d } }}	dv rdt}|g}
|jd< dv rdt}d|g}
|jd< dv rdt	j
tddd}	dd|	g}
djd< tdd |||	fD }|dkrӈd|dkr܈ddt	jd d!d"dd}d#t	d$}d%t	j
tdd&}|
|7 }
t|dkotjd% t}|r%| jd' sd(|d kr%d)d*tdd}|dkrA|dkr8d ntj|p@d }t|d+d,}|rVtjd-krVd.d/tdd}|re|d0k| _d1tjtj}t }d2|}zt| W n t y } zd3| j|d }~ww |!| j|g|||
||||| d S )4Nr   )r      z"attach" expectedr   c                   sh   | d   r|d  sJ | tdd}|dkrd }| v r2|du r0dt| t|d}|S )Nr   Tr   r   Fz9{0}:false and "debugOptions":[{1}] are mutually exclusive)islowerisupperboolr   r   r   )	prop_name	flag_namevaluer   ra   r   r   property_or_debug_option*  s   z7Client.launch_request.<locals>.property_or_debug_optionpython
pythonPathz2"pythonPath" is not valid if "python" is specifiedT)r   )	vectorizesize
pythonArgs)r   debugLauncherPythonr   r   programprocessNamemodulez-mcode)r   z-c
c                 S  s   g | ]}|d kr|qS )r   r   ).0xr   r   r   
<listcomp>`  s    z)Client.launch_request.<locals>.<listcomp>z7either "program", "module", or "code" must be specifiedz6"program", "module", and "code" are mutually exclusiver   r   integratedTerminalexternalTerminalconsoleTitlezPython Debug Consoleargs)r   r   z8Shell expansion in "args" is not supported by the clientzJShell expansion in "args" is not available for "console":"internalConsole"cwdsudoSudowin32z("sudo":true is not supported on Windows.onTerminateKeyboardInterruptdebugLauncherPathdebugAdapterHostz4{0} couldn't create listener socket for servers: {1})"debugpy.adapterr   rR   idlenr   connectionscant_handlerI   r   r   r   r   r8   
executabler   joinr%   rg   
isinstancer   r6   r   dirnamer   platformrH   r   __file__r	   get_default_localhostserve	Exceptionspawn_debuggee)rO   ra   r   r   
python_keyr   launcher_pythonr   r   r   r   num_targetsr   console_titletarget_argsshell_expand_argsr   r   on_terminatelauncher_path	localhostadapter_hostr   r   r   r   launch_request  s   






	


 


zClient.launch_requestc              
     s2  j jr	|d|dtdd}|dtdd}|dtdd}|dtdd}|dttfdd |d	tdd|d
tdd}|rC|dk_|dksK|dkr]|dkrT|d|dkr]|d|dkr||dkrj|d dkrs|ddkr||d dkrdkr|dt	 }|dkrt
 r|d|d|}|dt}d t_|dd_t
||\}}nt
 st
| tt
j\}} dkr)t tszt  W n ty   |dw |dtt}fdd}	z	t
 ||	 W n# ty }
 zt  j dt|
f  W Y d }
~
d S d }
~
ww tj} fdd}ndkr=dd }|dkr:tjnd }nfdd}d}jd ||d! t
j ||}|d u r~dkrq|i  j d"  d S |!|rzd#d$z	|"j  W d S  t#y   |!d%| Y d S w )&Nz'"noDebug" is not supported for "attach"hostTr   portlistenconnect	processIdsubProcessIdr   r   r   z1"listen" and "host"/"port" are mutually exclusivez2"connect" and "host"/"port" are mutually exclusivez-"listen" and "connect" are mutually exclusivez/"listen" and "processId" are mutually exclusivez2"listen" and "subProcessId" are mutually exclusivez5"processId" and "subProcessId" are mutually exclusivez7Multiple concurrent "listen" sessions are not supportedrestartFz$"processId" must be parseable as intdebugpyArgsc                   s    j d| |d d S )Nr*   r.   r*   )rK   rL   r   rO   r   r   	on_output  s   z(Client.attach_request.<locals>.on_outputz&Error when trying to attach to PID:
%sc                   
   | j  kS r]   pidconnr   r   r   <lambda>      
 z'Client.attach_request.<locals>.<lambda>c                 S  s   dS )NTr   r   r   r   r   r   #  s    c                   r   r]   r   r   )sub_pidr   r   r   &  r   r   debugpyWaitingForServerr   r   z+No known subprocess with "subProcessId":{0}z.Timed out waiting for debug server to connect.z3There is no debug server connected to this adapter.z{0} is already being debugged.)$rR   r   r   r   intr   r   rH   r	   r   r   
is_servingr   access_tokenrE   r   get_addresslistenerr   r   r   r   injectr   swallow_exceptionr   r   PROCESS_SPAWN_TIMEOUTrK   rL   wait_for_connectionr   r   r   attach_to_session
ValueError)rO   ra   r   r   r   r   r   r   debugpy_argsr   etimeoutpredr   r   )r   rO   r   r   attach_request  s   







	



	zClient.attach_requestc                 C  sj  | j d u s| jr|d z{zMd| _z	| jj|}W n9 tjyT   |i  | j i  | j	
d| jt|j Y W W | j jd u rR| j i  |   d S d S w || W n tjyv } z| j t| W Y d }~nd }~ww W | j jd u r| j i  |   n| j jd u r| j i  |   w w t D ]}|jd u r|j| j	jkr| | qd S )NzX"configurationDone" is only allowed during handling of a "launch" or an "attach" requestTr   )rD   rC   r   r^   rK   r_   r   r   r   rR   r   r   r   r   r   r   r\   r   r   r   r   ppidr   notify_of_subprocess)rO   ra   resultr   r   r   r   r   configurationDone_requestE  sT   



z Client.configurationDone_requestc                   s*   | j j } fdd}|| tjS )Nc                   s     | j d S r]   )r   body)r   ra   r   r   handle_responser  s   z0Client.evaluate_request.<locals>.handle_response)r^   rK   rW   on_responser   r   )rO   ra   propagated_requestr  r   r  r   evaluate_requestn  s   
zClient.evaluate_requestc                 C  s   d|j d< | jj|S )N*threadId)r   r^   rK   r_   r`   r   r   r   pause_requesty  s   
zClient.pause_requestc                 C  s8   d|j d< z| jj|W S  tjy   ddi Y S w )Nr  r  allThreadsContinuedT)r   r^   rK   r_   r   r   r`   r   r   r   continue_request~  s   
zClient.continue_requestc                 C  sJ   ddt jii}| jr#z	| jjd}W n
 ty   Y |S w || |S )Nr0   versionpydevdSystemInfo)r0   rM   r^   rK   ra   r   update)rO   ra   r  pydevd_infor   r   r   debugpySystemInfo_request  s   
z Client.debugpySystemInfo_requestc                 C  s.   d| _ | jr| jj|S | jjddd i S )NFzclient requested "terminate"T)terminate_debuggee)rE   rH   r^   rK   r_   rR   r   r`   r   r   r   terminate_request  s
   zClient.terminate_requestc              	   C  s   d| _ |dtdd}|dkrd }| jd| |i  | jrDt  t	d|  t
 D ]}z|j  W q- tyC   t  Y q-w z| j  W d S  ty\   tjdd	 Y d S w )
NFterminateDebuggeeTr   r   zclient requested "disconnect"zB{0} disconnected from stdio; closing remaining server connections.warninglevel)rE   r   rR   r   r   r2   r   stop_servingr   r1   r   rK   r<   r   r  )rO   ra   r"  r   r   r   r   disconnect_request  s(   
zClient.disconnect_requestc                   s   t    d S r]   )r@   
disconnectr   rS   r   r   r*    s   zClient.disconnectc                 C  s0   dd t jtjtjfD }| jdd|i d S )Nc                 S  s4   g | ]}|d urt |\}}|||tjudqS )N)r   r   internal)r	   r  r
   r  )r   r  r   r   r   r   r   r     s    z)Client.report_sockets.<locals>.<listcomp>debugpySocketsr	   )r
   r  r   r   rK   rL   )rO   socksr   r   r   rN     s   
zClient.report_socketsc                 C  s  t d| | | jI | jd u s|| jv r	 W d    d S d| jjv r4t d| j 	 W d    d S t d| | t| jj}| j| | j	  W d    n1 sVw   Y  dD ]}|
|d  q]d|j|d< |j|d< d	D ]}|
|d  qu|
d
d }|
dd }d|vri |d< d
|d vrt }|p||d d
< d|d vr|d u rtt\}}||d d< | jd r| jdd|d d S d|d< | jd| d S )Nz{1} is a subprocess of {0}.r   zrNot reporting subprocess for {0}, because the parent process was attached to using "processId" rather than "port".zNotifying {0} about {1}.)r   r   preLaunchTaskpostDebugTaskra   r   zSubprocess {0}namer   )r   r   r   r   r   r   r   startDebuggingr   )ra   configurationra   debugpyAttach)r   r1   rR   rD   r   r   r%  r   addnotify_changedpopr   r   r	   r   r  r  r   rK   ra   rL   )rO   r   r  keyr   r   r   _r   r   r   r    sR   

zClient.notify_of_subprocess)r   r   r   __doc__r   r   r   __annotations__r   r&   rA   rZ   r\   rY   ra   r   r   r   r  r  r  r  r  r!  r#  r)  r*  rN   r  __classcell__r   r   rS   r   r      sP   
 
G


:P
 
 
(






r   c                 C  s"   t dt| |at  t tS )Nr   )r	   r   r   r  r   rN   r  r   r   r   r   r     s   
r   c                   C  sD   t d urzt   W n ty   tjdd Y nw d a t  d S )Nr%  r&  )r  r<   r   r   r  r   rN   r   r   r   r   r(    s   r(  )
__future__r   r:   r6   r8   r0   r   r   r   debugpy.commonr   r   r   r	   r   r
   r   r   r   r   r   r   r   r(  r   r   r   r   <module>   s"         