Format: application/x-python-code
⬇ Download File (20.1 KB)
   1  o
   2  
   3  �h�i�
   4  @s�dZddlZddlZddlZddlZddlmZddlZddl	m
   5  Z
   6  ddlmZddl
   7  mZddlmZddlmZzdd	lmZWneyQdd
   8  lmZYnwddlmZmZmZzdd
   9  lmZddlmZddlm Z m!Z!dZ"Wn'eydZ"Yne#y�Z$ze$j%j&dkr�dZ"ne$�WYdZ$[$ndZ$[$wwe'edd�p�ej(Z)Gdd�d�Z*Gdd�d�Z+Gdd�d�Z,Gdd�d�Z-Gdd�d�Z.Gdd�d�Z/Gd d!�d!�Z0dS)"z/Debugger implementation for the IPython kernel.�N)�Path)�get_ipython)�leading_empty_lines)�Event)�Queue)�jsonapi)�json_default)�date_default�)�
  10  get_file_name�get_tmp_directory�get_tmp_hash_seed)�api)�pydevd_frame_utils)�SuspendedFramesManager�_FramesTrackerTFZDebuggerInitializationError�
  11  ROUTING_IDc@�eZdZdZdd�ZdS)�	_FakeCodezFake code class.cCs||_||_dS��Init.N)�co_filename�co_name)�selfrr�r�Hc:\Users\shubh\Videos\agent\nova\Lib\site-packages\ipykernel/debugger.py�__init__3s
  12  z_FakeCode.__init__N��__name__�
  13  __module__�__qualname__�__doc__rrrrrr0�rc@r)�
  14  _FakeFramezFake frame class.cCs||_||_||_d|_dSr)�f_code�	f_globals�f_locals�f_back)rr$r%r&rrrr<s
  15  z_FakeFrame.__init__Nrrrrrr#9r"r#c@r)�
  16  _DummyPyDBzFake PyDb class.cCsddlm}|��|_dS)rr)�	PyDevdAPIN)Z_pydevd_bundle.pydevd_apir)ZVariablePresentationZvariable_presentation)rr)rrrrGsz_DummyPyDB.__init__Nrrrrrr(Dr"r(c@s2eZdZdZdd�Zdd�Zdd�Zdd	d
  17  �ZdS)�VariableExplorerzA variable explorer.cCs*t�|_t�|_t|j|j�|_d|_dS)zInitialize the explorer.N)r�suspended_frame_managerr(Zpy_dbr�tracker�frame�rrrrrQs
  18  zVariableExplorer.__init__cCs:t�j}ttdtd��||�|_|j�dt�	|j��dS)zStart tracking.�<module>zsys._getframe()Zthread1N)
  19  rZuser_nsr#rrr-r,�trackrZcreate_frames_list_from_frame)r�varrrrr0XszVariableExplorer.trackcCs|j��dS)zStop tracking.N)r,�untrack_allr.rrrr2^szVariableExplorer.untrack_allNcCs0|}|s	t|j�}|j�|�}dd�|��D�S)z1Get the child variables for a variable reference.cSsg|]}|���qSr)Zget_var_data)�.0�xrrr�
  20  <listcomp>hsz;VariableExplorer.get_children_variables.<locals>.<listcomp>)�idr-r+Zget_variable�get_children_variables)rZvariable_refZvar_ref�	variablesrrrr7bs
  21  
  22  z'VariableExplorer.get_children_variables�N)rrr r!rr0r2r7rrrrr*Nsr*c@sHeZdZdZdZdZdZdZdd�Zdd	�Z	d
  23  d�Z
  24  dd
  25  �Zdd�ZdS)�DebugpyMessageQueuezA debugpy message queue.zContent-Length: �z
  26  
  27  �cCs&d|_|��||_t�|_||_dS)zInit the queue.�N)�
  28  tcp_buffer�_reset_tcp_pos�event_callbackr�
  29  message_queue�log)rr@rBrrrrss
  30  
  31  zDebugpyMessageQueue.__init__cCsd|_d|_d|_d|_dS)N�����r)�
  32  header_pos�
  33  separator_pos�message_size�message_posr.rrrr?{s
  34  z"DebugpyMessageQueue._reset_tcp_poscCs�|j�d�t�tjttjft�|��}|ddkr.|j�d�|j�|�|�	|�dS|j�d�|j�|�|j
  35  �|�dS)NzQUEUE - _put_message:�type�eventzQUEUE - received event:zQUEUE - put message:)rB�debug�t�cast�Dict�str�Anyr�loadsr@rA�
  36  put_nowait)rZraw_msg�msgrrr�_put_message�sz DebugpyMessageQueue._put_messagecCs�|j|7_|j�d�	|jdkr|j�tj�|_|jdkr"dS|j�d|j�|jdkr>|jtj}|j�tj	|�|_|jdkrEdS|j�d|j�|j
  37  dkrj|jtj}|jtj|_
  38  t|j||j��|_
  39  |j�d|j
  40  �|j�d|j
  41  �t|j�|j
  42  |j
  43  kr�dS|�|j|j
  44  |j
  45  |j
  46  ��t|j�|j
  47  |j
  48  kr�|j�d	�d
  49  |_|��dS|j|j
  50  |j
  51  d�|_|j�d|j�|��q)zPut a tcp frame in the queue.zQUEUE - received frameTrCNzQUEUE - found header at pos %iz!QUEUE - found separator at pos %izQUEUE - found message at pos %izQUEUE - message size is %izQUEUE - resetting tcp_bufferr=zQUEUE - slicing tcp_buffer: %s)r>rBrJrD�findr:�HEADERrE�
  52  HEADER_LENGTH�	SEPARATORrG�SEPARATOR_LENGTH�intrF�lenrSr?)rr-�hintZsize_posrrr�
  53  put_tcp_frame�sD
  54  
  55  
  56  
  57  
  58  ��z!DebugpyMessageQueue.put_tcp_framec�s�|j��IdHS)zGet a message from the queue.N)rA�getr.rrr�get_message�s�zDebugpyMessageQueue.get_messageN)
  59  rrr r!rUrVrWrXrr?rSr\r^rrrrr:ks/r:c@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
  60  d�Zdd
  61  �Z	dd�Z
  62  dd�Zdd�Zdd�Z
  63  dd�ZdS)�
  64  DebugpyClientzA client for debugpy.cCsL||_||_||_t|j|j�|_d|_d|_d|_d|_	t
  65  �|_d|_dS)zInitialize the client.�	127.0.0.1rCNT)
  66  rB�debugpy_streamr@r:�_forward_eventrA�debugpy_host�debugpy_port�
  67  routing_id�wait_for_attachr�
  68  init_event�init_event_seq)rrBrar@rrrr�s
  69  zDebugpyClient.__init__cCs |��\}}d|dt|�S)N�tcp://�:)�
  70  get_host_portrN)r�host�portrrr�
  71  _get_endpoint�szDebugpyClient._get_endpointcCs.|ddkr|j��|d|_|�|�dS)NrIZinitialized�seq)rg�setrhr@�rrRrrrrb�s
  72  
  73  zDebugpyClient._forward_eventcCs�|jdur
  74  |jj�t�|_tj|tddd�}tt	|��}t
  75  j|t
  76  j�
  77  d�}||7}|j�d�|j�|j�|j�|�|j�|j|f�dS)NF)�default�ensure_ascii�	allow_nan�asciizDEBUGPYCLIENT:)rera�socket�
  78  getsockoptrr�dumpsrrNrZr:rUrW�encoderBrJZsend_multipart)rrR�content�content_length�bufrrr�
  79  _send_request�s"
  80  ��zDebugpyClient._send_requestc�s�|j��IdHSr9)rAr^r.rrr�_wait_for_response�s�z DebugpyClient._wait_for_responsec�sN�|j��IdHdt|j�ddd�}|�|�|��IdH|��IdHS)N�requestr
  81  �configurationDone)rHro�command)rg�waitrYrhr}r~)rr�rrr�_handle_init_sequence�s��
  82  z#DebugpyClient._handle_init_sequencecCsn|jdkr1|jj}|�d|j�|�tj��d�|_	|�
  83  |j	�|j	�d�}|j	|dd�|_|j|jfS)zGet the host debugpy port.rCri�utf-8rjr
  84  N)rdrarvZbind_to_random_portrcrw�zmqZ
  85  LAST_ENDPOINT�decode�endpointZunbind�rfind)rrv�indexrrrrks
  86  zDebugpyClient.get_host_portcCs&|jj�|���|jj�t�|_dS)zConnect to the tcp socket.N)rarv�connectrnrwrrer.rrr�connect_tcp_socketsz DebugpyClient.connect_tcp_socketcCs0|jj�|���d|_t�|_d|_d|_dS)zDisconnect from the tcp socket.NrCT)	rarv�
  87  disconnectrnrerrgrhrfr.rrr�disconnect_tcp_sockets
  88  
  89  z#DebugpyClient.disconnect_tcp_socketcCs|j�|�dS)zReceive a dap frame.N)rAr\)rr-rrr�receive_dap_frameszDebugpyClient.receive_dap_framec�s`�|�|�|jr|ddkr|��IdH}d|_|S|��IdH}|j�d�|j�|�|S)zSend a dap request.r��attachNFzDEBUGPYCLIENT - returning:)r}rfr�r~rBrJ)rrR�reprrr�send_dap_request"s�
  90  zDebugpyClient.send_dap_requestN)rrr r!rrnrbr}r~r�rkr�r�r�r�rrrrr_�s
  91  r_c@s�eZdZdZgd�Zgd�Z	d4dd�Zdd�Zd	d
  92  �Zdd�Z	d
  93  d�Z
  94  dd�Zedd��Z
  95  dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3S)5�DebuggerzThe debugger class.)�dumpCell�setBreakpoints�source�
  96  stackTracer8r�r�)�	debugInfo�inspectVariables�richInspectVariables�modules�
  97  copyToGlobalsTcCs�||_t|||j�|_||_||_d|_||_||_t	�|_
  98  i|_tj
  99  D]
 100  }t||�|j|<q$i|_tjD]
 101  }t||�|j|<q5i|_t�|_d|_i|_d|_d|_d|_t�|_dS)zInitialize the debugger.Fr`rN)rBr_�
 102  _handle_event�debugpy_client�shell_socket�session�
 103  is_startedr@�just_my_coder�
 104  stopped_queue�started_debug_handlersr��started_debug_msg_types�getattr�static_debug_handlers�static_debug_msg_types�breakpoint_listrp�stopped_threads�debugpy_initialized�_removed_cleanuprcrdr�r*�variable_explorer)rrBrar@r�r�r��msg_typerrrrGs,
 105  
 106  zDebugger.__init__cCs�|ddkr%|ddr|j�|�dS|j�|dd�|�|�dS|ddkrG|ddr6t�|_n
 107  |j�|dd�|�|�dS|�|�dS)NrI�stopped�bodyZallThreadsStoppedZthreadId�	continuedZallThreadsContinued)r�rQr��addr@rp�removerqrrrr�hs
 108  zDebugger._handle_eventc�s�|j�|�IdHSr9)r�r�rqrrr�_forward_messageys�zDebugger._forward_messagecs4�fdd�|D�}|dd|dd|dd|id�S)	Nc�g|]}��|d�r|�qS��name��accept_variable�r3r1r.rrr5}�z6Debugger._build_variables_response.<locals>.<listcomp>ro�responseTr�r8)rorH�request_seq�successr�r�r)rrr8Zvar_listrr.r�_build_variables_response|s�z"Debugger._build_variables_responsecCsgd�}||vS)N)ZIPythonHistorySavingThreadzThread-2zThread-3zThread-4r)rZthread_name�forbid_listrrr�_accept_stopped_thread�szDebugger._accept_stopped_threadc�st�|j��IdH}|ddddd�}|�|�IdH}|ddD]}|�|d�r2|j�|d	�q!|�|�dS)
 109  zHandle a stopped event.Nror
 110  r�threads)rorHr�r�r�r6)r�r]r�r�r�r�r@)rrI�reqr��threadrrr�handle_stopped_event�s��zDebugger.handle_stopped_eventcCs|jSr9)r�r.rrr�
 111  tcp_client�szDebugger.tcp_clientc
 112  	Cs�|jsQt�}t|���st|�jdd�|j��\}}d}|d|d|d7}|dd�}|j�|j	d|d	|j	�
 113  t��|jj|j	d
 114  d�\}}|dd
 115  dk|_t
 116  �jj}t|vrg|�t�}	|�|	�|j|	<|j��|jS)zStart the debugger.T)�parentszimport debugpy;zdebugpy.listen(("z",z)))�codeZsilentZexecute_requestNr)�moderz�status�ok)r�rr�exists�mkdirr�rkr��sendr�rwr�recvr�input_transformer_manager�cleanup_transformsrr��popr�r�)
 117  rZtmp_dirrlrmr�rz�identrRr�r�rrr�start�s.
 118  
 119  �
 120  
 121  
 122  zDebugger.startcCs@|j��t�jj}t|j�D]}|j�|�}|�||�qdS)zStop the debugger.N)	r�r�rr�r��sortedr�r��insert)rr�r��funcrrr�stop�s
 123  
 124  �z
 125  Debugger.stopc�sj�|dd}t|�}t|ddd��
 126  }|�|�Wd�n1s"wYd|dd	|d
 127  d|id�S)
 128  zHandle a dump cell message.�	argumentsr��wr���encodingNr�roTr�Z
 129  sourcePath�rHr�r�r�r�)r�open�write)r�messager��	file_name�frrrr��s���zDebugger.dumpCellc�s^�|ddd}|dd|j|<|�|�IdH}|�d�r-dd�|d	dD�|j|<|S)
 130  z!Handle a set breakpoints message.r�r��path�breakpointsNr�cSsg|]}d|di�qS)�liner)r3�
 131  breakpointrrrr5�s
 132  ��z+Debugger.setBreakpoints.<locals>.<listcomp>r�)r�r�r])rr�r�Zmessage_responserrrr��s�
 133  
 134  �zDebugger.setBreakpointsc�s��d|d|dd�}|ddd}t|���r>t|dd	��}d
 135  |d<d|��i|d
 136  <Wd�|S1s7wY|Sd|d<d|d<i|d
 137  <|S)zHandle a source message.r�ror�)rHr�r�r�r�r�r�r�Tr�rzr�NFzsource unavailabler�)r�is_filer��read)rr��reply�source_pathr�rrrr��s�
 138  ���zDebugger.sourcec�s~�|�|�IdH}z+|dd}t|�tdd�tt|�d�D��}|ddd|d�|dd<W|Sty>Y|Sw)zHandle a stack trace message.Nr�ZstackFramescss,�|]\}}|ddkr|dkr|VqdS)r�r/r
 139  Nr)r3�i�vrrr�	<genexpr>s�"�z&Debugger.stackTrace.<locals>.<genexpr>r
 140  )r�rZ�next�	enumerate�reversed�
 141  StopIteration)rr�r�Zsf_listZ
 142  module_idxrrrr��s�
 143  
 144  �"��zDebugger.stackTracecCs>gd�}||v}|ott�d|��}|o|dd�dk}|S)zAccept a variable by name.)rr!�__package__�
 145  __loader__�__spec__�__annotations__�__builtins__�__builtin__Z__display__rZdebugpy�exit�quit�InZOutZ_ohZ_dh�_�__Z___z^_\dr�Z_i)�bool�re�search)rZ
 146  variable_namer�Zcondrrrr�s
 147  zDebugger.accept_variablec�sb�i}�js�j�|dd�}��||�S��|�IdH}�fdd�|ddD�|dd<|S)zHandle a variables message.r�ZvariablesReferenceNcr�r�r�r�r.rrr50s
 148  ��z&Debugger.variables.<locals>.<listcomp>r�r8)r�r�r7r�r�)rr�r�r8rr.rr8%s�
 149  �
 150  
 151  �zDebugger.variablesc�sR�|j��\}}||d�|dd<d|dd<|js!dg|dd<|�|�IdHS)	zHandle an attach message.)rlrmr�r�TZ	logToFileZDebugStdLibZdebugOptionsN)r�rkr�r�)rr�rlrmrrrr�5s�zDebugger.attachc�s�|dd|dd|dd�S)z$Handle a configuration done message.ror�Tr�)rorHr�r�r�r)rr�rrrr�Bs��zDebugger.configurationDonec�sl�g}|j��D]\}}|�||d��qd|dd|d|jdt�t�tjd|t|j	�ddgdd	�
 152  d
 153  �S)zHandle a debug info message.)r�r�r�roTr�ZMurmur2�.pyzPython Exceptions)
 154  Z	isStartedZ
 155  hashMethodZhashSeedZ
 156  tmpFilePrefixZ
 157  tmpFileSuffixr�ZstoppedThreadsZ
 158  richRenderingZexceptionPathsr�r�)
 159  r��items�appendr�r
 160  r�os�sep�listr�)rr�r��key�valuerrrr�Ls(�
 161  ��zDebugger.debugInfoc�s4�|j��t�|_|j��|j��}|�||�S)z$Handle an inspect variables message.)r�r2r*r0r7r�)rr�r8rrrr�ds�
 162  
 163  
 164  zDebugger.inspectVariablesc	�s>�d|dd|dd�}|dd}t�|�}|s.iid�|d	<|d
 165  ks(|dkr,d|d
 166  <|Si�i}|jsTt��||i�|}|�dd�dkrS|�di��|�di�}n3d|�d�}|dd}|d}	|�dd|	d||dd�d��IdH}|d
 167  r�t|d	dii�\�}��fdd�|��D�d�}
 168  |
 169  |d	<d|d
 170  <|S) z(Handle a rich inspect variables message.r�roFr�)rHZsequence_seqr�r�r�ZvariableName)�data�metadatar�zspecial variableszfunction variablesTr�r��errorr�rr	z'get_ipython().display_formatter.format(�)�frameIdr�evaluater
 171  �	clipboard)�
 172  expressionr�context�rHr�ror�N�resultcsi|]\}}|�vr||�qSrr)r3�kr��Z	repr_datarr�
 173  <dictcomp>�r�z1Debugger.richInspectVariables.<locals>.<dictcomp>)	rN�isidentifierr�rZuser_expressionsr]r��evalr)rr�r��var_nameZ
 174  valid_nameZ
 175  repr_metadatarr�Zframe_idror�rrrr�osN��
 176  �
 177  �
 178  ��zDebugger.richInspectVariablesc	�s`�|dd}|dd}|dd}d|�d�}|d}|�dd	|d
 179  |||d�d��IdHS)
 180  Nr�ZdstVariableNameZsrcVariableNameZ
 181  srcFrameIdzglobals()['z']rorZ
 182  setExpressionr
 183  )rrrr)r�)rr�Zdst_var_nameZsrc_var_nameZsrc_frame_idrrorrrr��s ���
 184  �zDebugger.copyToGlobalsc	�s��ttj���}|�dd�}|�dt|��}g}t||�D]!}||}tt|dd�dd�}|r>|�d�r>|�	||j
 185  |d��qd	|t|�d
 186  �iS)zHandle a modules message.�startModuler�moduleCountr�N�originr)r6r�r�r�)r�ZtotalModules)r�sysr��valuesr]rZ�ranger��endswithrr)	rr�r�rr�modsr��module�filenamerrrr��s��zDebugger.modulesc�s��i}|ddkr,|jr|j�d�n|��|_|jr"|j�d�n
 187  d|ddddd	�}|j�|dd
 188  �}|d
 189  urA||�Id
 190  H}n |jra|j�|dd
 191  �}|d
 192  urY||�Id
 193  H}n|�|�Id
 194  H}|ddkr{|��i|_	t
 195  �|_d|_|j�d�|S)
 196  zProcess a request.r�Z
 197  initializez The debugger has already startedzThe debugger has startedro�Fr�)r�r�ror�rHNr�zThe debugger has stopped)r�rB�infor�r�r]r�r�r�r�rpr�)rr�r��handlerrrr�process_request�s:�
 198  �zDebugger.process_requestN)T)rrr r!r�r�rr�r�r�r�r��propertyr�r�r�r�r�r�r�r�r8r�r�r�r�r�r�r�r&rrrrr�0s:	
 199  �!
 200  
 201  
 202  
 203  4r�)1r!rr�r�typingrK�pathlibrr�ZIPython.core.getipythonrZIPython.core.inputtransformer2rZ
 204  tornado.locksrZtornado.queuesrZ	zmq.utilsrZjupyter_client.jsonutilr�ImportErrorr	�compilerrrr
 205  Zdebugpy.serverrZ_pydevd_bundlerZ&_pydevd_bundle.pydevd_suspended_framesrrZ_is_debugpy_available�	Exception�e�	__class__rr�ZIDENTITYrrr#r(r*r:r_r�rrrr�<module>sP����
 206  	
 207  Vo