Format: application/x-python-code
⬇ Download File (20.4 KB)
   1  o
   2  
   3  �hcs�@s�UdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
   4  ddlmZddl
   5  mZddlZddlmZddlmZmZddlmZddlmZmZmZmZmZmZmZmZdd	l m!Z!d
   6  dl"m#Z#d
   7  dl$m%Z%d
   8  d
   9  l&m'Z'd
  10  dl(m)Z)d
  11  dl*m+Z+d
  12  dl,m-Z.d
  13  dl,m/Z/d
  14  dl0m1Z1zddl2m3Z3Wne4y�dZ3Ynwzddl5m6Z7ddl5m8Z9dZ:Wne4y�dZ:YnwdZ;dd�Z<e�=�Z>da?e
  15  j@e%eAd<dd�ZBe<e_CeBe_DGdd�de.�ZEGd d!�d!eE�Z-dS)"z!The IPython kernel implementation�N)�contextmanager)�partial)�release)�line_at_cursor�token_at_cursor)�extract_header)�Any�Bool�	HasTraits�Instance�List�Type�observe�observe_compat)�	ZMQStream���BaseComm)�CommManager)�XCachingCompiler)�_use_appnope)�	OutStream)�Kernel)�_accepts_parameters)�ZMQInteractiveShell)�_asyncio_runner)�provisionalcompleter)�rectify_completionsTFZ_jupyter_types_experimentalcOst|i|��S)zCreate a new Comm.r��args�kwargs�r!�Hc:\Users\shubh\Videos\agent\nova\Lib\site-packages\ipykernel/ipkernel.py�_create_comm0sr#�
  16  _comm_managercOsXtdur*t�tdurt|i|��aWd�tSWd�tS1s%wYtS)zCreate a new CommManager.N)r$�
  17  _comm_lockrrr!r!r"�_get_comm_manager:s
  18  ��
  19  ��r&c
  20  s\eZdZdZeddd�Zee�Ze	ddd�j
  21  dd�Zeedd�Z
  22  e�Zed�ed	d
  23  ���Zeeddd�Zed
  24  �edd���Ze�Ze�Z�fdd�Zeddejdd�d�ddd�ddd�ddd�ddd�ddd�d d!d�g�j
  25  dd�Zd"ZejZ d#ej�!�d$d%d"ejd$d&�d'd(d#d)d*�Z"d+d,�Z#e$d-d.��Z%d/d0�Z&�fd1d2�Z'de�fd4d5�	Z(�fd6d7�Z)d8d9�Z*dfd;d<�Z+d=d>�Z,e$d?d@��Z-e-j.dAd@��Z-e/dBdC��Z0�fdDdE�Z1			:dgdddF�dGdH�Z2dIdJ�Z3dKdL�Z4dMdN�Z5dhdPdQ�Z6	$	$				:didRdS�Z7dTdU�Z8dVdW�Z9dXdY�Z:dZd[�Z;d\d]�Z<d^d_�Z=d`e>j?dadbe>j@eAe>jffdcdd�ZB�ZCS)j�
  26  IPythonKernelzThe IPython Kernel class.z1IPython.core.interactiveshell.InteractiveShellABCT)�
  27  allow_nonezUSet this flag to False to deactivate the use of experimental IPython completion APIs.)�help)�config�user_modulecCs|jdur
  28  |d|j_dSdS�N�new)�shellr+��selfZchanger!r!r"�_user_module_changedWs
  29  �z"IPythonKernel._user_module_changedN)rr(�user_nscCs(|jdur|d|j_|j��dSdSr,)r.r2Zinit_user_nsr/r!r!r"�_user_ns_changed_s
  30  �zIPythonKernel._user_ns_changedcsZt�jdi|��ddlm}m}|r#||j|j|j|j|j	|j
  31  �|_|jj||j
  32  |j|j|td�|_|j	|jj_	tj�d�}|rG||jjd<|j|jj_|�d�|jj_|j	|jj_	|j|jj_t��|_t|jt�snJ�|jj �!|j�gd�}|D]}t"|j|�|j#|<q|t$�r�|j%r�dd	l&}|�'�i|_(|�)�t*t+d
  33  �r�t+j,�!|j-�d	Sd	S)zInitialize the kernel.r)�Debugger�_is_debugpy_available)�parent�profile_dirr+r2�kernelZcompiler_classZJPY_SESSION_NAMEZ__session__Zexecute_result)Z	comm_openZcomm_msgZ
  34  comm_closerN�	callbacksr!).�super�__init__�debuggerr4r5�log�debugpy_streamZ_publish_debug_eventZdebug_shell_socket�sessionZdebug_just_my_code�shell_class�instancer7r+r2rr.�displayhook�os�environ�get�iopub_socketZ
  35  pub_socket�_topicZtopicZdisplay_pub�comm�get_comm_managerZcomm_manager�
  36  isinstancer
  37  Z
  38  configurables�append�getattrZshell_handlersrZ_darwin_app_nap�appnopeZnope�_new_threads_parent_header�_initialize_thread_hooks�hasattr�gcr9�_clean_thread_parent_frames)r0r r4r5Zjupyter_session_nameZcomm_msg_types�msg_typerM��	__class__r!r"r;ksR�
  39  �
  40  
  41  �zIPythonKernel.__init__zPython Referencezhttps://docs.python.org/%i.%i�)�text�urlzIPython Referencez&https://ipython.org/documentation.htmlzNumPy Referencez+https://docs.scipy.org/doc/numpy/reference/zSciPy Referencez+https://docs.scipy.org/doc/scipy/reference/zMatplotlib Referencez$https://matplotlib.org/contents.htmlzSymPy Referencez'http://docs.sympy.org/latest/index.htmlzpandas Referencez-https://pandas.pydata.org/pandas-docs/stable/�ipython�pythonrz
  42  text/x-python)�name�versionz	ipython%d�z.py)r[r\�mimetypeZcodemirror_modeZpygments_lexerZnbconvert_exporterZfile_extensioncCsDddlm}|r |dj�d�}|j�d|�|jj�|�dSdS)Nr�r5zutf-8zDebugpy received: %s)r<r5�bytes�decoder=�debugZ
  43  tcp_clientZreceive_dap_frame)r0�msgr5�framer!r!r"�dispatch_debugpy�s�zIPythonKernel.dispatch_debugpycC�|jr|jjSdS�N)r.�banner�r0r!r!r"rh��zIPythonKernel.bannerc�s�	|j��IdHq)zPoll the stopped queue.TN)r<Zhandle_stopped_eventrir!r!r"�poll_stopped_queue�s��z IPythonKernel.poll_stopped_queuecsf|jrd|j_|jdur|j�d�n	|jj|jdd�t���|jr1t	�
  44  |��|jj
  45  j�dSdS)zStart the kernel.FNz7debugpy_stream undefined, debugging will not be enabled��copy)r.�exit_nowr>r=�warningZon_recvrer:�start�asyncio�run_coroutine_threadsaferkZcontrol_thread�io_loopZasyncio_looprirTr!r"rp�s
  46  
  47  ��zIPythonKernel.startr.cs6t��|||�|dkr|jr|j�|�dSdSdS)zmOverridden from parent to tell the display hook and output streams
  48          about the parent message.
  49          r.N)r:�
  50  set_parentr.)r0�identr6ZchannelrTr!r"rt�s�zIPythonKernel.set_parentcs"t��|�}|�d|jd��|S)zVInitialize metadata.
  51  
  52          Run at the beginning of each execution request.
  53          T)�dependencies_metZengine)r:�
  54  init_metadata�updateru)r0r6�mdrTr!r"rw�s��zIPythonKernel.init_metadatacCs0|d|d<|ddkr|ddkrd|d<|S)zXFinish populating metadata.
  55  
  56          Run after completing an execution request.
  57          �status�error�enameZUnmetDependencyFrvr!)r0r6�metadata�
  58  reply_contentr!r!r"�finish_metadataszIPythonKernel.finish_metadataFcCs*||_tj|_|jt_tj|_|jt_dS)zZForward raw_input and getpass to the current frontend.
  59  
  60          via input_request
  61          N)Z_allow_stdin�builtins�input�_sys_raw_input�	raw_input�getpass�
  62  _save_getpass)r0�allow_stdinr!r!r"�_forward_inputs
  63  zIPythonKernel._forward_inputcCs|jt_|jt_dS)zRestore raw_input, getpassN)r�r�r�r�r�rir!r!r"�_restore_input(szIPythonKernel._restore_inputcCrfrg)r.�execution_countrir!r!r"r�.rjzIPythonKernel.execution_countcCsdSrgr!)r0�valuer!r!r"r�4sc
  64  #sz�t���dd�}��t||��|�t|�����fdd�}t�tj|�}z
  65  dVWt�tj|�dSt�tj|�w)aPContextManager for capturing SIGINT and cancelling a future
  66  
  67          SIGINT raises in the event loop when running async code,
  68          but we want it to halt a coroutine.
  69  
  70          Ideally, it would raise KeyboardInterrupt,
  71          but this turns it into a CancelledError.
  72          At least it gets a decent traceback to the user.
  73          cSs |��s|��r
  74  dS|��dSrg)�	cancelled�done�cancel)�fZ_ignoredr!r!r"�cancel_unless_doneIsz;IPythonKernel._cancel_on_sigint.<locals>.cancel_unless_donecs�fdd�}�j�|�dS)Ncs"���s���r
  75  dS��d�dS)Nr)r�r��
  76  set_resultr!)�
  77  sigint_futurer!r"�set_sigint_resultVszQIPythonKernel._cancel_on_sigint.<locals>.handle_sigint.<locals>.set_sigint_result)rsZadd_callback)rr��r0r�r!r"�
  78  handle_sigintUsz6IPythonKernel._cancel_on_sigint.<locals>.handle_sigintN)rq�Future�add_done_callbackr�signal�SIGINT)r0�futurer�r�Zsave_sigintr!r�r"�_cancel_on_sigint:s�
  79  "zIPythonKernel._cancel_on_sigintc�s.�t|�}|�|�t��|||�IdHdS)z/Override for cell output - cell reconciliation.N)r�%_associate_new_top_level_threads_withr:�execute_request)r0�streamrur6�
  80  parent_headerrTr!r"r�fs�
  81  zIPythonKernel.execute_request)�	cell_meta�cell_idc�s��|j��dus
  82  J�|�|�i}t�d�r(t�d�r(�j}	�j}
  83  t|	dg�}ndd�}
  84  �fdd�}	t�jdg�}z�d}z��|�}
  85  WntyR|}
  86  t	�
  87  �}YnwtrĈjtur�t
  88  ����r�|
  89  ||
  90  |d	�r�|drv|	||||
  91  ||d
  92  �}n	|	||||
  93  |d�}t
  94  �|�}|�|��2d}z|IdH}W�j�d�|s��j�d
  95  |�n�j�d�|s��j�d
  96  |�wwWd�n1s�wYn|dr҈j||||d�}n�j|||d�}W|��n|��w|jdur�|jn|j}|jr�d|d<n%d|d<|��j�pgtt|�j�t|�d��t|j|jdd�}||d<�j d|d<d|v�r5|j!�"dd�#|d��|ddk�rG��$|�pBi�|d<ni|d<�j%�&�|d<�j%�'�|S)zHandle code execution.N�run_cell_async�should_run_asyncr�cSsdS)NFr!)�cellr!r!r"�<lambda>�sz*IPythonKernel.do_execute.<locals>.<lambda>c�s��j|i|��Srg)�run_cellr�r.r!r"r��s�z*IPythonKernel.do_execute.<locals>.run_cell)�transformed_cell�preprocessing_exc_tuple)�
  97  store_history�silentr�r�r�)r�r�r�r�Zpost_executeZ
  98  post_run_cell)r�r�r�)r�r��okrzr{��	tracebackr|Zevalue�execute�Zengine_uuidZ	engine_id�method�engine_inforr�r�z Exception in execute request:
  99  %s�
 100  �user_expressions�payload)(r.r�rPr�r�rr�Ztransform_cell�	Exception�sys�exc_inforZloop_runnerrq�get_event_loop�
 101  is_running�
 102  ensure_futurer��events�triggerr�Zerror_before_execZ
 103  error_in_exec�successrx�_last_traceback�str�type�__name__�dictru�int_idr�r=�info�joinr�Zpayload_managerZread_payloadZ
 104  clear_payload)r0�coder�r�r�r�r�r�r~r�r�Zaccepts_paramsr�r��coroZcoro_future�res�err�e_infor!r�r"�
 105  do_executels��
 106  ��
 107  
 108  ����	�
 109  ��������
 110  
 111  ��	
 112  �
 113  zIPythonKernel.do_executecCsvtr|jr|�||�S|durt|�}t||�\}}||}|jdus%J�|j�d||�\}}|||t|�idd�S)zHandle code completion.N�r���matchesZ
 114  cursor_endZcursor_startr}rz)�_use_experimental_60_completion�use_experimental_completions�_experimental_do_complete�lenrr.Zcomplete)r0r��
 115  cursor_pos�line�offsetZline_cursor�txtr�r!r!r"�do_complete�s
 116  
 117  �zIPythonKernel.do_completec�s(�ddlm}|r|j�|�IdHSdS)zHandle a debug request.rr_N)r<r5�process_request)r0rcr5r!r!r"�do_debug_requests
 118  �zIPythonKernel.do_debug_requestc
 119  Cs�|durt|�}t��6|jdusJ�|jj�||�}tt||��}g}|D]}|�t|j	|j
 120  |j|j|j
 121  d��q&Wd�n1sDwY|r]|dj	}|dj
 122  }dd�|D�}	n|}|}g}	|	||t|idd�S)zD
 123          Experimental completions from IPython, using Jedi.
 124          N)rp�endrWr��	signaturercSsg|]}|j�qSr!)rW)�.0�cr!r!r"�
 125  <listcomp>2�z;IPythonKernel._experimental_do_complete.<locals>.<listcomp>r�r�)r��_provisionalcompleterr.Z	Completer�completions�list�_rectify_completionsrKr�rpr�rWr�r��_EXPERIMENTAL_KEY_NAME)
 126  r0r�r�Zraw_completionsr��comps�comp�s�er�r!r!r"r�s@����
 127  
 128  �z'IPythonKernel._experimental_do_completer!cCs�t||�}ddi}i|d<i|d<|jdusJ�z0tjdkr(|jj|||d�}n|jj||d�}|d�|�|jjsB|d�d	�d
 129  |d<W|StyVd|d<Y|Sw)
 130  zHandle code inspection.rzr��datar}N)�)�detail_level�
 131  omit_sections)r�z	text/htmlT�foundF)	rr.r�version_infoZobject_inspect_mimerxZenable_html_pager�pop�KeyError)r0r�r�r�r�r[r~Zbundler!r!r"�
 132  do_inspect@s,
 133  
 134  �
 135  �
 136  �zIPythonKernel.do_inspectc
 137  Cs�|jdusJ�|dkr|jjj|||dd�}
 138  n$|dkr(|jjj|||||d�}
 139  n|dkr9|jjj|||||	d�}
 140  ng}
 141  d	t|
 142  �d
 143  �S)zHandle code history.N�tailT)�raw�outputZinclude_latest�range)r�r��search)r�r��n�uniquer�)rz�history)r.Zhistory_managerZget_tailZ	get_ranger�r�)r0Zhist_access_typer�r�r?rp�stopr��patternr��histr!r!r"�
 144  do_history\s"
 145  �
 146  �
 147  ��zIPythonKernel.do_historycCs|jrd|j_td|d�S)zHandle kernel shutdown.Tr�)rz�restart)r.rnr�)r0r�r!r!r"�do_shutdown�szIPythonKernel.do_shutdowncCsZt|jdd�}|dur|jdusJ�|jj}|�|�\}}d|i}|dkr+d||d<|S)zHandle an is_complete request.Zinput_transformer_managerNrz�
 148  incomplete� �indent)rLr.Zinput_splitterZcheck_complete)r0r�Ztransformer_managerrzZ
 149  indent_spaces�rr!r!r"�do_is_complete�szIPythonKernel.do_is_completec
 150  Csz
 151  ddlm}m}Wntyddlm}m}Ynw|j}|dus%J�z�|j}dt|��dd�d}	|||dd	�\}
 152  }}t	|
 153  d
 154  d�}
 155  |	d}
 156  |	d}|	d
 157  }|	d}|
 158  |
 159  |||||di}|�
 160  |�|�d|
 161  �d|�d|�d�}zt||j|j�|�
 162  |�}W|D]}|�|�q�n|D]}|�|�q�w|jdus�J�|||jj|jjd�}WnUty�}zI|��|jp�gtt|�j�t|�d�}t|j|jdd�}||d<|j|jd||�d�d�|j�dd�|d��g}d|d<WYd}~||fSd}~wwddi}||fS)zHandle an apply request.r)�serialize_object�unpack_apply_messagerN�_�-r�Frlr�r�rr �resultz = z(*z,**�))�buffer_threshold�item_thresholdr��applyr�r�r{�ruzException in apply request:
 163  %sr�r�rzr�) Zipyparallel.serializerr�ImportError�	serializer.r2r��replacerLrx�execZuser_global_nsrEr�r?rr�
 164  BaseException�
 165  showtracebackr�r�r�r�rur�Z
 166  send_responserFrGr=r�r�)r0�contentZbufsZmsg_idZreply_metadatarrr.Zworking�prefixr�rr �fname�argnameZ	kwargnameZ
 167  resultname�nsr�r	�keyZ
 168  result_bufr�r~r�r!r!r"�do_apply�sn�
 169  
 170  ��
 171  �����zIPythonKernel.do_applycCs|jr	|j�d�tdd�S)zClear the kernel.Fr�)rz)r.�resetr�rir!r!r"�do_clear�s
 172  zIPythonKernel.do_clearcCs
 173  ||_dS)zBStore the parent header to associate it with new top-level threadsN)rN)r0r�r!r!r"r��s
 174  z3IPythonKernel._associate_new_top_level_threads_withcsl|j�|j�t���|�tjj�tjj�dtjf�����fdd�}dtjf�fdd�}|tj_|tj_dS)z=Store thread hierarchy and thread-parent_header associations.r0cshz|j}Wn
 175  tyYdSw��fD]}t|t�r-|�kr'�j|j|j<q||j|j<q�|�dS)aYWrap the `threading.Thread.start` to intercept thread identity.
 176  
 177              This is needed because there is no "start" hook yet, but there
 178              might be one in the future: https://bugs.python.org/issue14073
 179  
 180              This is a no-op if the `self._stdout` and `self._stderr` are not
 181              sub-classes of `OutStream`.
 182              N)�_ipykernel_parent_thread_ident�AttributeErrorrJrrN�_thread_to_parent_headerru�_thread_to_parent)r0r6r�)�_threading_Thread_runr8�kernel_thread_ident�stderr�stdoutr!r"�run_closure�s
 183  
 184  �
 185  ���z;IPythonKernel._initialize_thread_hooks.<locals>.run_closurecs$�|g|�Ri|��t��|_dSrg)�	threading�	get_identr)r0rr )�_threading_Thread__init__r!r"�init_closuresz<IPythonKernel._initialize_thread_hooks.<locals>.init_closureN)�_stdout�_stderrr'r(�Thread�runr;)r0r&r*r!)r)r"r8r#r$r%r"rO�sz&IPythonKernel._initialize_thread_hooks�phase)rpr�r�c
 186  Cs�|dkrdSdd�t��D�}|j|jfD]E}t|t�rZ|j}t|���D]}||vr:z||=Wq%t	y9Yq%wq%|j
 187  }t|���D]}||vrYz||=WqDt	yXYqDwqDqdS)a�Clean parent frames of threads which are no longer running.
 188          This is meant to be invoked by garbage collector callback hook.
 189  
 190          The implementation enumerates the threads because there is no "exit" hook yet,
 191          but there might be one in the future: https://bugs.python.org/issue14073
 192  
 193          This is a no-op if the `self._stdout` and `self._stderr` are not
 194          sub-classes of `OutStream`.
 195          rpNcSsh|]}|j�qSr!r)r��threadr!r!r"�	<setcomp>r�z<IPythonKernel._clean_thread_parent_frames.<locals>.<setcomp>)r'�	enumerater+r,rJrr r��keysr�r!)r0r/r�Zactive_threadsr�Zthread_to_parent_header�identityZthread_to_parentr!r!r"rRs2
 196  
 197  
 198  ��
 199  ����z)IPythonKernel._clean_thread_parent_framesr�)F)TNF)rr!)rrNNNF)Dr��
 200  __module__�__qualname__�__doc__rr.r
 201  rr@r	�tagr�rr>rr+rrr1r�r2r3r�Z_sys_eval_inputr;rr�r�Z
 202  help_links�implementationrr\�implementation_version�splitZ
 203  language_infore�propertyrhrkrprtrwrr�r�r��setterrr�r�r�r�r�r�r�r�r�rrrr�rO�t�Literal�Dictr�rR�
 204  __classcell__r!r!rTr"r'Hs���;����������"�
 205  	
 206  
 207  
 208  
 209  
 210  
 211  
 212  +
 213  ��
 214  (!
 215  �$
 216  B(��r'cs eZdZdZ�fdd�Z�ZS)rz3DEPRECATED.  An alias for the IPython kernel class.cs.ddl}|jdtdd�t�j|i|��dS)zDEPRECATED.rNz@Kernel is a deprecated alias of ipykernel.ipkernel.IPythonKernelrV)�
 217  stacklevel)�warnings�warn�DeprecationWarningr:r;)r0rr rCrTr!r"r;/s�zKernel.__init__)r�r5r6r7r;rAr!r!rTr"r,sr)Fr7rqr�rQr�rCr�r�r'�typingr>�
 218  contextlibr�	functoolsrrHZIPython.corerZIPython.utils.tokenutilrrZjupyter_client.sessionrZ	traitletsrr	r
 219  rrr
 220  rrZzmq.eventloop.zmqstreamrZ	comm.commrZcomm.managerr�compilerrZ
 221  eventloopsrZiostreamrZ
 222  kernelbaserZ
 223  KernelBaserZzmqshellrZIPython.core.interactiveshellrrZIPython.core.completerrr�rr�r�r�r#�Lockr%r$�Optional�__annotations__r&Zcreate_commrIr'r!r!r!r"�<module>sh(��
 224  i