a
    
ak                     @   s   d dl mZmZmZ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mZmZ d dlmZ d dlmZmZmZ G dd dejZd	d
 Zdd ZdS )    )absolute_importdivisionprint_functionunicode_literalsN)adaptercommonlauncher)compatfmtjsonlog	messagingsockets)unicode)
componentsserverssessionsc                       s   e Zd ZdZejjZ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d&d' Z  ZS )(Clientz+Handles the client side of a debug session.c                   @   s   e Zd ZdddddZdS )zClient.CapabilitiesF)ZsupportsVariableTypeZsupportsVariablePagingZsupportsRunInTerminalRequestZsupportsMemoryReferencesN)__name__
__module____qualname__
PROPERTIES r   r   6lib/python3.9/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Zoptional)ZlocaleZlinesStartAt1ZcolumnsStartAt1Z
pathFormatN)r   r   r   r   enumr   r   r   r   r   Expectations   s
   r   c                    s  |dkr\t d|  tj }ttjd t_	}t
|j ttjd t_}t
|j ntj|}t V}tt| || d | _d| _d | _d | _g | _t | _| |_|  W d    n1 s0    Y  | jdddd	tjid
 | jdddd	tjid
 d S )NZstdioz"Connecting to client over stdio...rwFoutputZ	telemetryZptvsdZpackageVersion)categoryr!   datadebugpy) r   infor   ZJsonIOStreamZ
from_stdioopenosdevnullsysstdinatexitregisterclosestdoutZfrom_socketr   ZSessionsuperr   __init__	client_idhas_startedstart_request_initialize_request_deferred_eventsset_known_subprocessesclientchannel
send_eventr$   __version__)selfZsockstreamr*   r.   session	__class__r   r   r0   '   s@    

&zClient.__init__c                 C   s4   | j d ur"| j | td n| jj| d S )NzPropagation deferred.)r5   appendr   debugr8   r9   	propagater<   eventr   r   r   propagate_after_startg   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   rB   r8   r5   Zdescriber9   rC   r%   rD   r   r   r   _propagate_deferred_eventsr   s    
z!Client._propagate_deferred_eventsc                 C   s   | j r| j j| d S N)serverr9   rC   rD   r   r   r   rE   }   s    zClient.eventc                 C   s   | j j|S rH   )rI   r9   delegater<   requestr   r   r   rL      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g}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 initializedZclientID ZraisedzRaised ExceptionsF)filterZlabeldefaultZuncaughtzUncaught ExceptionsTZuserUnhandledzUser Uncaught Exceptions)ZsupportsCompletionsRequestZsupportsConditionalBreakpointsZ supportsConfigurationDoneRequestZsupportsDebuggerPropertiesZ supportsDelayedStackTraceLoadingZsupportsEvaluateForHoversZsupportsExceptionInfoRequestZsupportsExceptionOptionsZsupportsFunctionBreakpointsZ!supportsHitConditionalBreakpointsZsupportsLogPointsZsupportsModulesRequestZsupportsSetExpressionZsupportsSetVariableZsupportsValueFormattingOptionsZsupportsTerminateDebuggeeZsupportsGotoTargetsRequestZsupportsClipboardContextZexceptionBreakpointFiltersZsupportsStepInTargetsRequest)r4   
isnt_validr1   r   Zcapabilitiesr   Zexpectations)r<   rL   Zexception_breakpoint_filtersr   r   r   initialize_request   sD    



zClient.initialize_requestc                    s   t jj fdd}|S )Nc              
      s  | ddsJ | jd u r$|d| js0| jr:|d|dtd| j_| jjr^t	
  t|dtt | j_} | | |jd urd S | jrv| j| j d | _|j}| jrd|v rt|}t|dh |d< |d	rt|}|d	= z| jj|j| W nj tjyD   |i  | jtd
| j|j Y d S  tjyt } z|| W Y d }~n
d }~0 0 | jjr|| _d| _ |i  | !  d S ddh|@ rd}n(ddh|@ rd}nt"j#dkrdnd}| jjdddd|d | j$d || _tj%S )NZlaunchattachzSession is not initialized yetzSession is already startedZnoDebugFdebugOptionsZRedirectOutputZredirectOutput+{0} disconnected before responding to {1!j}TZWindowsClientZWindowsZWINDOWSZ
UnixClientZUNIXwin32ZsetDebuggerProperty)BaseException)	NameError)Z skipSuspendOnBreakpointExceptionZskipPrintBreakpointExceptionZmultiThreadsSingleNotificationZideOSZinitialized)&Z
is_requestr4   rP   r   rI   r   rO   r>   no_debugr   Zdont_wait_for_first_connectionr6   arrayr   debug_optionsresponseZ
initialize	argumentsdictlistgetr9   rL   commandr   NoMoreMessagesrespondfinalizer
   MessageHandlingErrorrC   r3   r2   rG   r)   platformr:   NO_RESPONSE)r<   rL   rZ   r\   excZclient_os_typefr   r   handle   s~    






 

z-Client._start_message_handler.<locals>.handle)r   	Componentmessage_handler)ri   rj   r   rh   r   _start_message_handler   s    XzClient._start_message_handlerc                    s  ddl m} | jjdks$tt r.dtdt	
t  fdd}d}|v rpd	v r|d
nd	v r|d	}|t	j
tddd}t|sttjg}|dt	j
tdd7 }|dd  jd< |jd< dtdd}|dkr|d }d } }}	dv r*dt}|g}
|jd< dv rP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n|dkrʈddt	jd d!dd}|d kr|
d"t	
t7 }
jd"d  d#tdd}|dkrD|dkr2d ntj|pBd }d$t	jd%d&d'dd}d(t	d)}t|d*d+}|rtjd,krd-d.tjtj}d/d0}zt| W n6 ty } zd1| j|W Y d }~n
d }~0 0 || j|g|||
||||
 d S )2Nr   )	launchers   z"attach" expectedrS   c                    s\   | d   r|d  sJ | tdd}|dkr6d }| v rX|du rTd| |d}|S )Nr   Tr   r   Fz={0!j}:false and "debugOptions":[{1!j}] are mutually exclusive)islowerisupperboolrP   )Z	prop_nameZ	flag_namevaluerZ   rL   r   r   property_or_debug_option  s    z7Client.launch_request.<locals>.property_or_debug_optionpythonZ
pythonPathz2"pythonPath" is not valid if "python" is specifiedT)r   )Z	vectorizesize
pythonArgs)rw   ZdebugLauncherPythonr   r   programprocessNamemodulez-mcode)ro   z-c
c                 S   s   g | ]}|d kr|qS )r   r   ).0xr   r   r   
<listcomp>T      z)Client.launch_request.<locals>.<listcomp>z7either "program", "module", or "code" must be specifiedz6"program", "module", and "code" are mutually exclusiveZargsExpansionshellZnoneargscwdconsoleZinternalConsoleZintegratedTerminalZexternalTerminalZconsoleTitlezPython Debug ConsolesudoZSudorU   z("sudo":true is not supported on Windows.ZdebugLauncherPathZdebugAdapterHost	127.0.0.1z4{0} couldn't create listener socket for servers: {1}) debugpy.adapterrn   r>   idlenr   connectionscant_handler6   r   rY   r   rP   r	   filenamer)   
executabler\   joinr   popr'   r   dirnamerO   rr   re   r   __file__serve	ExceptionZspawn_debuggee)r<   rL   rn   ru   Z
python_keyrv   Zlauncher_pythonry   r{   r|   r   Znum_targetsZargs_expansionr   r   Zconsole_titler   Zlauncher_pathZadapter_hostrg   r   rt   r   launch_request  s    













 	

zClient.launch_requestc           
         s  | 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
ksz|d
kr|d
kr|d|d
kr|d|d
kr|d
kr|d d
kr|dd
kr|d d
krd
kr|d|d
kr,|dd}|dt}d t_t	||\}}nt	 \}} d
krt
 tszzt  W n tyx   |dY n0 |dtt}t | tj} fdd}n8d
krdd }|d
krtjnd }nfdd}d}| jd||d t| j ||}	|	d u rPd
kr:|i  | j td d S ||rHdndz|	| j  W n  ty   |d|	 Y n0 d S )Nz'"noDebug" is not supported for "attach"hostTr   portlistenconnect	processIdsubProcessIdr   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 exclusiver   z$"processId" must be parseable as intZdebugpyArgsc                    s
   | j  kS rH   pidconnr   r   r   <lambda>  r   z'Client.attach_request.<locals>.<lambda>c                 S   s   dS )NTr   r   r   r   r   r     r   c                    s
   | j  kS rH   r   r   )sub_pidr   r   r     r   r   Z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.)r>   rX   rP   r   intr]   r   Zaccess_tokenr   r   
isinstancer   r   rY   Zinjectr   ZPROCESS_SPAWN_TIMEOUTr9   r:   Zwait_for_connectionrb   rc   r
   r   Zattach_to_session
ValueError)
r<   rL   r   r   r   r   Zdebugpy_argsZtimeoutZpredr   r   )r   r   r   attach_request  s    








	zClient.attach_requestc              
   C   sh  | j d u s| jr|d zzd| _z| jj|}W nh tjy   |i  | j i  | j	
td| j|j Y W W | j jd u r| j i  |   d S 0 || W n6 tjy } z| j t| W Y d }~n
d }~0 0 W | j jd u r.| j i  |   n$| j jd u r,| j i  |   0 t D ],}|jd u r6|j| j	jkr6| | q6d S )NzX"configurationDone" is only allowed during handling of a "launch" or an "attach" requestTrT   )r3   r2   r   rI   r9   rJ   r   ra   rb   r>   rc   r
   r`   r[   rG   rd   strr   r   Zppidr   notify_of_subprocess)r<   rL   resultrg   r   r   r   r   configurationDone_request
  sF    
(

z Client.configurationDone_requestc                    s*   | j j } fdd}|| tjS )Nc                    s     | j d S rH   )rb   body)r[   rL   r   r   handle_response8  s    z0Client.evaluate_request.<locals>.handle_response)rI   r9   rC   Zon_responser   rf   )r<   rL   Zpropagated_requestr   r   r   r   evaluate_request4  s    
zClient.evaluate_requestc                 C   s   d|j d< | jj|S )N*threadId)r\   rI   r9   rJ   rK   r   r   r   pause_request?  s    
zClient.pause_requestc                 C   s<   d|j d< z| jj|W S  tjy6   ddi Y S 0 d S )Nr   r   ZallThreadsContinuedT)r\   rI   r9   rJ   r   ra   rK   r   r   r   continue_requestD  s
    
zClient.continue_requestc                 C   sH   ddt jii}| jrDz| jjd}W n ty8   Y n0 || |S )Nr$   versionZpydevdSystemInfo)r$   r;   rI   r9   rL   r   update)r<   rL   r   Zpydevd_infor   r   r   debugpySystemInfo_requestP  s    
z Client.debugpySystemInfo_requestc                 C   s   | j jddd i S )Nzclient requested "terminate"T)terminate_debuggee)r>   rc   rK   r   r   r   terminate_request^  s    zClient.terminate_requestc                 C   s,   |dt dd}|dkrd }| jd| i S )NZterminateDebuggeeTr   r   zclient requested "disconnect")rr   r>   rc   )r<   rL   r   r   r   r   disconnect_requestc  s
    zClient.disconnect_requestc                 C   s|  | j  | jd u s|| jv r,W d    d S d| jjv rVtd| j  W d    d S td| | t| jj}| j| W d    n1 s0    Y  dD ]}|	|d  qt
d|j|d< d|d< |j|d	< d
D ]}|	|d  q|	dd }|	dd }d|vri |d< d|d vr:|d ur.|nd|d d< d|d vrj|d u r^t \}}||d d< | jd| d S )Nr   zrNot reporting subprocess for {0}, because the parent process was attached to using "processId" rather than "port".zNotifying {0} about {1}.)r   r   ZpreLaunchTaskZpostDebugTaskzSubprocess {0}namerR   rL   r   )r   rz   rx   r   r   r   r   ZdebugpyAttach)r>   r3   r7   r\   r   warningr%   r]   addr   r
   r   listenergetsocknamer9   r:   )r<   r   r   keyr   r   _r   r   r   r   k  s<    *


zClient.notify_of_subprocess)r   r   r   __doc__r   rk   rl   r   r   r0   rF   rG   rE   rL   rQ   rm   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r?   r   r      sD   @


.\
 
u
)






r   c                 C   s   t dt| |at S )Nr   )r   r   r   r   r   r   r   r   r   r     s    r   c                   C   s0   zt   W n ty*   tjdd Y n0 d S )Nr   )level)r   r-   r   r   Zswallow_exceptionr   r   r   r   stop_serving  s    r   )Z
__future__r   r   r   r   r+   r'   r)   r$   r   r   r   Zdebugpy.commonr	   r
   r   r   r   r   Zdebugpy.common.compatr   r   r   r   r   rk   r   r   r   r   r   r   r   <module>   s          