o
    imq                     @  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 G dd dejZdd	 Zd
d ZdS )    )annotationsN)adaptercommonlauncher)jsonlog	messagingsockets)
components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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ZdS )zClient.CapabilitiesF)supportsVariableTypesupportsVariablePagingsupportsRunInTerminalRequestsupportsMemoryReferences#supportsArgsCanBeInterpretedByShellN)__name__
__module____qualname__
PROPERTIES r   r   R/var/www/edux/Edux_v2/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r.t d|  tj }ttjd t_	}t
|j ttjd t_}t
|j ntj|}t 1}t || d | _	 d| _	 d | _	 d | _	 g | _	 d| _t | _| |_|  W d    n1 slw   Y  | jdddd	tjid
 | jdddd	tjid
 d S )Nstdioz"Connecting to client over stdio...rwFoutput	telemetryptvsdpackageVersion)categoryr'   datadebugpy) r   infor   JsonIOStream
from_stdioopenosdevnullsysstdinatexitregisterclosestdoutfrom_socketr   Sessionsuper__init__	client_idhas_startedstart_request_initialize_request_deferred_events_forward_terminate_requestsetr   clientchannel
send_eventr-   __version__)selfsockstreamr5   r9   session	__class__r   r   r=   +   sN   


!zClient.__init__c                 C  s6   | j d ur| j | td d S | jj| d S )NzPropagation deferred.)rB   appendr   debugrE   rF   	propagaterI   eventr   r   r   propagate_after_startj   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   rP   rE   rB   describerF   rQ   r.   rR   r   r   r   _propagate_deferred_eventsu   s   

z!Client._propagate_deferred_eventsc                 C  s   | j r| j j| d S d S N)serverrF   rQ   rR   r   r   r   rS      s   zClient.eventc                 C  s   | j j|S rW   )rX   rF   delegaterI   requestr   r   r   r[      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!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supportsTerminateRequestsupportsGotoTargetsRequest)supportsClipboardContextexceptionBreakpointFilterssupportsStepInTargetsRequest)rA   
isnt_validr>   r   capabilitiesr#   expectations)rI   r[   exception_breakpoint_filtersr   r   r   initialize_request   sz   

	
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_requestrA   ry   r   rX   r   ra   rL   no_debugr   dont_wait_for_first_connectionrD   arraystrdebug_optionsresponse
initialize	argumentsgetdictlistrF   r[   commandr   NoMoreMessagesrespondfinalizeformatreprMessageHandlingErrorrQ   r@   r?   rV   rG   NO_RESPONSE)rI   r[   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                   sH  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}d2d3}zt| W n ty } zd4| j|d }~ww || j|g|||
||||| d S )5Nr   )	launchers   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boolry   r   r   )	prop_name	flag_namevaluer   r[   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>V  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debugAdapterHost	127.0.0.1z4{0} couldn't create listener socket for servers: {1}) debugpy.adapterr   rL   idlenr   connectionscant_handlerD   r   r   r   ry   r4   
executabler   joinr"   ra   
isinstancerz   r2   r   dirnamer   platformrC   r   __file__serve	Exceptionspawn_debuggee)rI   r[   r   r   
python_keyr   launcher_pythonr   r   r   r   num_targetsr   console_titletarget_argsshell_expand_argsr   r   on_terminatelauncher_pathadapter_hostr   r   r   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
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|dkrt	 r|d|dd}|dt}d t
_t||\}}nt	 st  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r1dd }|dkr.tjnd }
nfdd}d}
jd||d  tj ||
}|d u rrdkre|i  j d! d S ||
rnd"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 supportedr   z$"processId" must be parseable as intdebugpyArgsc                   s    j d| |d d S )Nr'   r+   r'   )rF   rG   r   )rI   r   r   	on_output   s   z(Client.attach_request.<locals>.on_outputz&Error when trying to attach to PID:
%sc                   
   | j  kS rW   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   rW   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.)!rL   r   ry   r   intr   r   rC   r   
is_servingr   access_tokenr   listenergetsocknamer   r   r   r   injectr   swallow_exceptionr   r   PROCESS_SPAWN_TIMEOUTrF   rG   wait_for_connectionr   r   r   attach_to_session
ValueError)rI   r[   r   r   r   r   r   debugpy_argsr   etimeoutpredr   r   )r   rI   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   )r@   r?   r   rX   rF   rY   r   r   r   rL   r   r   r   r   r   r   rV   r   r   r   r   ppidr   notify_of_subprocess)rI   r[   resultr   r   r   r   r   configurationDone_request8  sT   



z Client.configurationDone_requestc                   s*   | j j } fdd}|| tjS )Nc                   s     | j d S rW   )r   body)r   r[   r   r   handle_responsee  s   z0Client.evaluate_request.<locals>.handle_response)rX   rF   rQ   on_responser   r   )rI   r[   propagated_requestr  r   r
  r   evaluate_requesta  s   
zClient.evaluate_requestc                 C  s   d|j d< | jj|S )N*threadId)r   rX   rF   rY   rZ   r   r   r   pause_requestl  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   rX   rF   rY   r   r   rZ   r   r   r   continue_requestq  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 )Nr-   versionpydevdSystemInfo)r-   rH   rX   rF   r[   r   update)rI   r[   r  pydevd_infor   r   r   debugpySystemInfo_request}  s   
z Client.debugpySystemInfo_requestc                 C  s(   | j r
| jj|S | jjddd i S )Nzclient requested "terminate"T)terminate_debuggee)rC   rX   rF   rY   rL   r   rZ   r   r   r   terminate_request  s   zClient.terminate_requestc                 C  s,   |dt dd}|dkrd }| jd| i S )NterminateDebuggeeTr   r   zclient requested "disconnect")r   rL   r   )rI   r[   r  r   r   r   disconnect_request  s
   zClient.disconnect_requestc                 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< d|d	< |j|d
< dD ]}|
|d  qy|
dd }|
dd }d|vri |d< d|d vr|d ur|nd|d d< d|d vr|d u rt \}}||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postDebugTaskzSubprocess {0}namer   r[   r   )r   r   r   r   r   r   r   debugpyAttach)r   r.   rL   r@   r   r   warningr   addnotify_changedpopr   r   r   r   rF   rG   )rI   r   r	  keyr   r   _r   r   r   r    sF   
zClient.notify_of_subprocess)r   r   r   __doc__r
   r   r   __annotations__r   r#   r=   rT   rV   rS   r[   r}   r   r   r  r  r  r  r  r  r  r  r  __classcell__r   r   rM   r   r      sL   
 	?


9P
 
 
(






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 d S  ty   tjdd Y d S w )Nr!  )level)r   r8   r   r   r   r   r   r   r   stop_serving  s
   r+  )
__future__r   r6   r2   r4   r-   r   r   r   debugpy.commonr   r   r   r	   r   r
   r   r   r   r   r   r+  r   r   r   r   <module>   s         ?