o
    ii                  
   @   s  d 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Zddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ zdd	lmZ W n eyQ   dd
lmZ Y nw ddlmZmZmZ zddlmZ ddlmZ ddlm Z m!Z! dZ"W n' ey   dZ"Y n e#y Z$ ze$j%j&dkrdZ"ne$W Y dZ$[$ndZ$[$ww e'eddpej(Z)G dd dZ*G dd dZ+G dd dZ,G dd dZ-G dd dZ.G dd dZ/G d d! d!Z0dS )"z/Debugger implementation for the IPython kernel.    N)Path)get_ipython)leading_empty_lines)Event)Queue)jsonapi)json_default)date_default   )get_file_nameget_tmp_directoryget_tmp_hash_seed)api)pydevd_frame_utils)SuspendedFramesManager_FramesTrackerTFDebuggerInitializationError
ROUTING_IDc                   @      e Zd ZdZdd ZdS )	_FakeCodezFake code class.c                 C   s   || _ || _dS Init.N)co_filenameco_name)selfr   r    r   M/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/ipykernel/debugger.py__init__4   s   
z_FakeCode.__init__N__name__
__module____qualname____doc__r   r   r   r   r   r   1       r   c                   @   r   )
_FakeFramezFake frame class.c                 C   s   || _ || _|| _d| _dS r   )f_code	f_globalsf_localsf_back)r   r%   r&   r'   r   r   r   r   =   s   
z_FakeFrame.__init__Nr   r   r   r   r   r$   :   r#   r$   c                   @   r   )
_DummyPyDBzFake PyDb class.c                 C   s   ddl m} | | _dS )r   r   )	PyDevdAPIN)_pydevd_bundle.pydevd_apir*   VariablePresentationvariable_presentation)r   r*   r   r   r   r   H   s   z_DummyPyDB.__init__Nr   r   r   r   r   r)   E   r#   r)   c                   @   s2   e Zd ZdZdd Zdd Zdd Zdd	d
ZdS )VariableExplorerzA variable explorer.c                 C   s*   t  | _t | _t| j| j| _d| _dS )zInitialize the explorer.N)r   suspended_frame_managerr)   py_dbr   trackerframer   r   r   r   r   R   s   
zVariableExplorer.__init__c                 C   s:   t  j}ttdtd||| _| jdt	| j dS )zStart tracking.<module>zsys._getframe()thread1N)
r   user_nsr$   r   r   r2   r1   trackr   create_frames_list_from_frame)r   varr   r   r   r7   Y   s   zVariableExplorer.trackc                 C   s   | j   dS )zStop tracking.N)r1   untrack_allr3   r   r   r   r:   _   s   zVariableExplorer.untrack_allNc                 C   s0   |}|s	t | j}| j|}dd | D S )z1Get the child variables for a variable reference.c                 S   s   g | ]}|  qS r   )get_var_data).0xr   r   r   
<listcomp>i   s    z;VariableExplorer.get_children_variables.<locals>.<listcomp>)idr2   r/   get_variableget_children_variables)r   variable_refvar_ref	variablesr   r   r   rA   c   s
   
z'VariableExplorer.get_children_variablesN)r   r    r!   r"   r   r7   r:   rA   r   r   r   r   r.   O   s    r.   c                   @   sH   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
d Z
dd Zdd ZdS )DebugpyMessageQueuezA debugpy message queue.zContent-Length:    z

   c                 C   s&   d| _ |   || _t | _|| _dS )zInit the queue. N)
tcp_buffer_reset_tcp_posevent_callbackr   message_queuelog)r   rL   rN   r   r   r   r   t   s
   
zDebugpyMessageQueue.__init__c                 C   s   d| _ d| _d| _d| _d S )Nr   )
header_posseparator_posmessage_sizemessage_posr3   r   r   r   rK   |   s   
z"DebugpyMessageQueue._reset_tcp_posc                 C   s   | j d ttttjf t|}|d dkr-| j d | j | | 	| d S | j d | j | | j
| d S )NzQUEUE - _put_message:typeeventzQUEUE - received event:zQUEUE - put message:)rN   debugtcastdictstrAnyr   loadsrL   rM   
put_nowait)r   raw_msgmsgr   r   r   _put_message   s   z DebugpyMessageQueue._put_messagec                 C   s  |  j |7  _ | jd 	 | jdkr| j tj| _| jdkr"dS | jd| j | jdkr>| jtj }| j tj	|| _| jdkrEdS | jd| j | j
dkrj| jtj }| jtj | _
t| j || j | _| jd| j
 | jd| j t| j | j
 | jk rdS | | j | j
| j
| j   t| j | j
 | jkr| jd	 d
| _ |   dS | j | j
| j d | _ | jd| j  |   q)zPut a tcp frame in the queue.zQUEUE - received frameTrO   NzQUEUE - found header at pos %iz!QUEUE - found separator at pos %izQUEUE - found message at pos %izQUEUE - message size is %izQUEUE - resetting tcp_bufferrI   zQUEUE - slicing tcp_buffer: %s)rJ   rN   rV   rP   findrF   HEADERrQ   HEADER_LENGTH	SEPARATORrS   SEPARATOR_LENGTHintrR   lenr`   rK   )r   r2   hintsize_posr   r   r   put_tcp_frame   sD   




z!DebugpyMessageQueue.put_tcp_framec                    s   | j  I dH S )zGet a message from the queue.N)rM   getr3   r   r   r   get_message   s   zDebugpyMessageQueue.get_messageN)r   r    r!   r"   rb   rc   rd   re   r   rK   r`   rj   rl   r   r   r   r   rF   l   s    /rF   c                   @   sh   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdS )DebugpyClientzA client for debugpy.c                 C   sL   || _ || _|| _t| j| j | _d| _d| _d| _d| _	t
 | _d| _dS )zInitialize the client.	127.0.0.1rO   NT)rN   debugpy_streamrL   rF   _forward_eventrM   debugpy_hostdebugpy_port
routing_idwait_for_attachr   
init_eventinit_event_seq)r   rN   ro   rL   r   r   r   r      s   
zDebugpyClient.__init__c                 C   s    |   \}}d| d t| S )Ntcp://:)get_host_portrZ   )r   hostportr   r   r   _get_endpoint   s   zDebugpyClient._get_endpointc                 C   s.   |d dkr| j   |d | _| | d S )NrU   initializedseq)ru   setrv   rL   r   r_   r   r   r   rp      s   

zDebugpyClient._forward_eventc                 C   s   | j d u r| jjt| _ tj|tddd}tt	|}t
j| t
j d}||7 }| jd | j| j  | j| | j| j |f d S )NF)defaultensure_ascii	allow_nanasciizDEBUGPYCLIENT:)rs   ro   socket
getsockoptr   r   dumpsr   rZ   rg   rF   rb   rd   encoderN   rV   send_multipart)r   r_   contentcontent_lengthbufr   r   r   _send_request   s"   
zDebugpyClient._send_requestc                    s   | j  I d H S rE   )rM   rl   r3   r   r   r   _wait_for_response   s   z DebugpyClient._wait_for_responsec                    sN   | j  I d H  dt| jd dd}| | |  I d H  |  I d H S )Nrequestr
   configurationDone)rT   r~   command)ru   waitrf   rv   r   r   )r   r   r   r   r   _handle_init_sequence   s   
z#DebugpyClient._handle_init_sequencec                 C   sn   | j dkr1| jj}|d| j  |tjd| _	|
| j	 | j	d}| j	|d d | _ | j| j fS )zGet the host debugpy port.rO   rw   utf-8rx   r
   N)rr   ro   r   bind_to_random_portrq   r   zmqLAST_ENDPOINTdecodeendpointunbindrfind)r   r   indexr   r   r   ry     s   
zDebugpyClient.get_host_portc                 C   s&   | j j|   | j jt| _dS )zConnect to the tcp socket.N)ro   r   connectr|   r   r   rs   r3   r   r   r   connect_tcp_socket  s   z DebugpyClient.connect_tcp_socketc                 C   s0   | j j|   d| _t | _d| _d| _dS )zDisconnect from the tcp socket.NrO   T)	ro   r   
disconnectr|   rs   r   ru   rv   rt   r3   r   r   r   disconnect_tcp_socket  s
   
z#DebugpyClient.disconnect_tcp_socketc                 C   s   | j | dS )zReceive a dap frame.N)rM   rj   )r   r2   r   r   r   receive_dap_frame  s   zDebugpyClient.receive_dap_framec                    s`   |  | | jr|d dkr|  I dH }d| _|S |  I dH }| jd | j| |S )zSend a dap request.r   attachNFzDEBUGPYCLIENT - returning:)r   rt   r   r   rN   rV   )r   r_   repr   r   r   send_dap_request#  s   
zDebugpyClient.send_dap_requestN)r   r    r!   r"   r   r|   rp   r   r   r   ry   r   r   r   r   r   r   r   r   rm      s    rm   c                   @   s   e Zd ZdZg dZg dZ	d4ddZdd Zd	d
 Zdd Z	dd Z
dd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3S )5DebuggerzThe debugger class.)dumpCellsetBreakpointssource
stackTracerD   r   r   )	debugInfoinspectVariablesrichInspectVariablesmodulescopyToGlobalsTc                 C   s   || _ t||| j| _|| _|| _d| _|| _|| _t	 | _
i | _tjD ]
}t| || j|< q$i | _tjD ]
}t| || j|< q5i | _t | _d| _i | _d| _d| _d| _t | _dS )zInitialize the debugger.Frn   r   N)rN   rm   _handle_eventdebugpy_clientshell_socketsession
is_startedrL   just_my_coder   stopped_queuestarted_debug_handlersr   started_debug_msg_typesgetattrstatic_debug_handlersstatic_debug_msg_typesbreakpoint_listr   stopped_threadsdebugpy_initialized_removed_cleanuprq   rr   r   r.   variable_explorer)r   rN   ro   rL   r   r   r   msg_typer   r   r   r   H  s,   

zDebugger.__init__c                 C   s   |d dkr%|d d r| j | d S | j|d d  | | d S |d dkrG|d d r6t | _n
| j|d d  | | d S | | d S )NrU   stoppedbodyallThreadsStoppedthreadId	continuedallThreadsContinued)r   r]   r   addrL   r   remover   r   r   r   r   i  s   
zDebugger._handle_eventc                    s   | j |I d H S rE   )r   r   r   r   r   r   _forward_messagez  s   zDebugger._forward_messagec                    s4    fdd|D }|d d|d d|d d|idS )	Nc                       g | ]}  |d  r|qS nameaccept_variabler<   r9   r3   r   r   r>   ~      z6Debugger._build_variables_response.<locals>.<listcomp>r~   responseTr   rD   )r~   rT   request_seqsuccessr   r   r   )r   r   rD   var_listr   r3   r   _build_variables_response}  s   z"Debugger._build_variables_responsec                 C   s   g d}||vS )N)IPythonHistorySavingThreadzThread-2zThread-3zThread-4r   )r   thread_nameforbid_listr   r   r   _accept_stopped_thread  s   zDebugger._accept_stopped_threadc                    st   | j  I dH }|d d ddd}| |I dH }|d d D ]}| |d r2| j|d	  q!| | dS )
zHandle a stopped event.Nr~   r
   r   threads)r~   rT   r   r   r   r?   )r   rk   r   r   r   r   rL   )r   rU   reqr   threadr   r   r   handle_stopped_event  s   zDebugger.handle_stopped_eventc                 C   s   | j S rE   )r   r3   r   r   r   
tcp_client  s   zDebugger.tcp_clientc           
   	   C   s   | j sQt }t| st|jdd | j \}}d}|d| d | d 7 }|dd}| j| j	d|d	| j	
t | jj| j	d
d\}}|d d dk| _ t jj}t|v rg|t}	||	| j|	< | j  | j S )zStart the debugger.T)parentszimport debugpy;zdebugpy.listen(("z",z)))codesilentexecute_requestNr   )moder   statusok)r   r   r   existsmkdirr   ry   r   sendr   r   r   recvr   input_transformer_managercleanup_transformsr   r   popr   r   )
r   tmp_dirrz   r{   r   r   _identr_   r   r   r   r   r   start  s.   




zDebugger.startc                 C   s@   | j   t jj}t| jD ]}| j|}||| qdS )zStop the debugger.N)	r   r   r   r   r   sortedr   r   insert)r   r   r   funcr   r   r   stop  s   

zDebugger.stopc                    sj   |d d }t |}t|ddd}|| W d   n1 s"w   Y  d|d d	|d
 d|idS )zHandle a dump cell message.	argumentsr   wr   encodingNr   r~   Tr   
sourcePathrT   r   r   r   r   )r   openwrite)r   messager   	file_namefr   r   r   r     s   zDebugger.dumpCellc                    s^   |d d d }|d d | j |< | |I dH }|dr-dd |d	 d D | j |< |S )
z!Handle a set breakpoints message.r   r   pathbreakpointsNr   c                 S   s   g | ]}d |d  iqS )liner   )r<   
breakpointr   r   r   r>     s    
z+Debugger.setBreakpoints.<locals>.<listcomp>r   )r   r   rk   )r   r   r   message_responser   r   r   r     s   

zDebugger.setBreakpointsc                    s   d|d |d d}|d d d }t | r>t|dd	}d
|d< d| i|d< W d   |S 1 s7w   Y  |S d|d< d|d< i |d< |S )zHandle a source message.r   r~   r   )rT   r   r   r   r   r   r   r   Tr   r   r   NFzsource unavailabler   )r   is_filer   read)r   r   replysource_pathr   r   r   r   r     s   
zDebugger.sourcec                    s~   |  |I dH }z+|d d }t|tdd tt|dD  }|d d d|d  |d d< W |S  ty>   Y |S w )zHandle a stack trace message.Nr   stackFramesc                 s   s,    | ]\}}|d  dkr|dkr|V  qdS )r   r4   r
   Nr   )r<   ivr   r   r   	<genexpr>  s    "z&Debugger.stackTrace.<locals>.<genexpr>r
   )r   rg   next	enumeratereversedStopIteration)r   r   r  sf_list
module_idxr   r   r   r     s   

"zDebugger.stackTracec                 C   s>   g d}||v}|ot td| }|o|dd dk}|S )zAccept a variable by name.)r   r"   __package__
__loader____spec____annotations____builtins____builtin____display__r   debugpyexitquitInOut_oh_dh______z^_\dr      _i)boolresearch)r   variable_namer   condr   r   r   r   	  s
   zDebugger.accept_variablec                    sb   i } j s j|d d } ||S  |I dH } fdd|d d D |d d< |S )zHandle a variables message.r   variablesReferenceNc                    r   r   r   r   r3   r   r   r>   1  s
    z&Debugger.variables.<locals>.<listcomp>r   rD   )r   r   rA   r   r   )r   r   r  rD   r   r3   r   rD   &  s   


zDebugger.variablesc                    sR   | j  \}}||d|d d< d|d d< | js!dg|d d< | |I dH S )	zHandle an attach message.)rz   r{   r   r   T	logToFileDebugStdLibdebugOptionsN)r   ry   r   r   )r   r   rz   r{   r   r   r   r   6  s   zDebugger.attachc                    s   |d d|d d|d dS )z$Handle a configuration done message.r~   r   Tr   )r~   rT   r   r   r   r   )r   r   r   r   r   r   C  s   zDebugger.configurationDonec                    sl   g }| j  D ]\}}|||d qd|d d|d | jdt t tj d|t| j	ddgdd	
d
S )zHandle a debug info message.)r   r   r   r~   Tr   Murmur2.pyzPython Exceptions)
	isStarted
hashMethodhashSeedtmpFilePrefixtmpFileSuffixr   stoppedThreadsrichRenderingexceptionPathsr   r   )
r   itemsappendr   r   r   osseplistr   )r   r   r   keyvaluer   r   r   r   M  s(   
zDebugger.debugInfoc                    s4   | j   t | _ | j   | j  }| ||S )z$Handle an inspect variables message.)r   r:   r.   r7   rA   r   )r   r   rD   r   r   r   r   e  s   


zDebugger.inspectVariablesc              	      s>  d|d d|d d}|d d }t |}|s.i i d|d	< |d
ks(|dkr,d|d< |S i  i }| jsTt ||i| }|dddkrS|di  |di }n3d| d}|d d }|d }	| dd|	d ||dddI dH }|d rt|d	 d i i \ }  fdd| D d}
|
|d	< d|d< |S ) z(Handle a rich inspect variables message.r   r~   Fr   )rT   sequence_seqr   r   r   variableName)datametadatar   zspecial variableszfunction variablesTr   r   errorr   rA  rB  z'get_ipython().display_formatter.format()frameIdr   evaluater
   	clipboard)
expressionrE  contextrT   r   r~   r   Nresultc                    s   i | ]\}}| v r||qS r   r   )r<   kr
  	repr_datar   r   
<dictcomp>  r   z1Debugger.richInspectVariables.<locals>.<dictcomp>)	rZ   isidentifierr   r   user_expressionsrk   r   evalr8  )r   r   r  var_name
valid_namerepr_metadatarK  r   frame_idr~   r   r   rM  r   r   p  sN   


zDebugger.richInspectVariablesc              	      s`   |d d }|d d }|d d }d| d}|d }|  dd	|d
 |||ddI d H S )Nr   dstVariableNamesrcVariableName
srcFrameIdzglobals()['z']r~   r   setExpressionr
   )rH  r>  rE  rJ  )r   )r   r   dst_var_namesrc_var_namesrc_frame_idrH  r~   r   r   r   r     s    
zDebugger.copyToGlobalsc           	         s   t tj }|dd}|dt|}g }t||D ]!}|| }tt|dddd}|r>|dr>|	||j
|d qd	|t|d
iS )zHandle a modules message.startModuler   moduleCountr  Noriginr/  )r?   r   r   r   )r   totalModules)r<  sysr   valuesrk   rg   ranger   endswithr9  r   )	r   r   r   r^  r_  modsr	  modulefilenamer   r   r   r     s   zDebugger.modulesc                    s   i }|d dkr,| j r| jd n|  | _ | j r"| jd n
d|d dddd	}| j|d d
}|d
urA||I d
H }n | j ra| j|d d
}|d
urY||I d
H }n| |I d
H }|d dkr{|   i | _	t
 | _d| _ | jd |S )zProcess a request.r   
initializez The debugger has already startedzThe debugger has startedr~      Fr   )r   r   r~   r   rT   Nr   zThe debugger has stopped)r   rN   infor   r   rk   r   r   r   r   r   r   )r   r   r  handlerr   r   r   process_request  s:   
zDebugger.process_requestN)T)r   r    r!   r"   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r   r   rD   r   r   r   r   r   r   r   rm  r   r   r   r   r   1  s:    	
!


4r   )1r"   r:  r&  rb  typingrW   pathlibr   r   IPython.core.getipythonr   IPython.core.inputtransformer2r   tornado.locksr   tornado.queuesr   	zmq.utilsr   jupyter_client.jsonutilr   ImportErrorr	   compilerr   r   r   debugpy.serverr   _pydevd_bundler   &_pydevd_bundle.pydevd_suspended_framesr   r   _is_debugpy_available	Exceptione	__class__r   r   IDENTITYr   r   r$   r)   r.   rF   rm   r   r   r   r   r   <module>   sP    
	
Vo