U
    es                     @   s   U d Z ddlZddlZddlZddlZddl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mZ ddlZddlmZ ddlmZmZ ddlmZ ddlmZmZmZmZmZmZmZm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)m*Z* d
dl+m,Z, d
dl-m.Z. d
dl/m0Z1 d
dl/m2Z2 d
dl3m4Z4 zddl5m6Z6 W n e7k
rd   dZ6Y nX z ddl8m9Z: ddl8m;Z< dZ=W n e7k
r   dZ=Y nX dZ>dd Z?e@ ZAdaBe
jCe% eDd< dd ZEe?e_FeEe_GG dd  d e1ZHG d!d" d"eHZ0dS )#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)Debugger_is_debugpy_available)_use_appnope)	OutStream)Kernel)_accepts_parameters)ZMQInteractiveShell)_asyncio_runner)provisionalcompleter)rectify_completionsTFZ_jupyter_types_experimentalc                  O   s
   t | |S )zCreate a new Comm.r   argskwargs r#   1lib/python3.8/site-packages/ipykernel/ipkernel.py_create_comm1   s    r%   _comm_managerc               	   O   s.   t dkr*t t dkr t| |a W 5 Q R X t S )zCreate a new CommManager.N)r&   
_comm_lockr   r    r#   r#   r$   _get_comm_manager;   s
    r(   c                
       sP  e Zd ZdZedddZeeZe	dddj
ddZerDeeddndZe Zed	ed
d ZeedddZededd Ze Ze Z fddZeddejdd  ddddddddddddddddd d!dgj
ddZd"Zej Z!d#ej " d$ d%d"ejd$ d&d'd( d#d)d*Z#d+d, Z$e%d-d. Z&d/d0 Z' fd1d2Z(dd fd4d5	Z) fd6d7Z*d8d9 Z+ded;d<Z,d=d> Z-e%d?d@ Z.e.j/dAd@ Z.e0dBdC Z1 fdDdEZ2dfdddFdGdHZ3dIdJ Z4dKdL Z5dMdN Z6dgdPdQZ7dhdRdSZ8dTdU Z9dVdW Z:dXdY Z;dZd[ Z<d\d] Z=d^d_ Z>e?j@d` e?jAeBe?jf dadbdcZC  ZDS )iIPythonKernelzThe IPython Kernel class.z1IPython.core.interactiveshell.InteractiveShellABCT)
allow_nonezUSet this flag to False to deactivate the use of experimental IPython completion APIs.)help)ZconfigNuser_modulec                 C   s   | j d k	r|d | j _d S Nnew)shellr,   selfZchanger#   r#   r$   _user_module_changedX   s    
z"IPythonKernel._user_module_changed)r!   r*   user_nsc                 C   s$   | j d k	r |d | j _| j   d S r-   )r/   r3   Zinit_user_nsr0   r#   r#   r$   _user_ns_changed`   s    
zIPythonKernel._user_ns_changedc                    sJ  t  jf | tr2t| j| j| j| j| j| j	| _
| jj| | j| j| j| td| _| j| jj_tjd}|rz|| jjd< | j| jj_| d| jj_| j| jj_| j| jj_t | _t| jtst | jj!"| j dddg}|D ]}t#| j|| j$|< qt% r| j&rdd	l'}|(  i | _)| *  t+t,d
rFt,j-"| j. d	S )zInitialize the kernel.)parentprofile_dirr,   r3   kernelZcompiler_classZJPY_SESSION_NAMEZ__session__Zexecute_resultZ	comm_openZcomm_msgZ
comm_closer   N	callbacks)/super__init__r   r   logdebugpy_streamZ_publish_debug_eventZdebug_shell_socketsessionZdebug_just_my_codedebuggershell_classinstancer6   r,   r3   r   r/   displayhookosenvirongetiopub_socketZ
pub_socket_topicZtopicZdisplay_pubcommget_comm_managerZcomm_manager
isinstancer
   AssertionErrorZconfigurablesappendgetattrZshell_handlersr   Z_darwin_app_napappnopeZnope_new_threads_parent_header_initialize_thread_hookshasattrgcr8   _clean_thread_parent_frames)r1   r"   Zjupyter_session_nameZcomm_msg_typesZmsg_typerM   	__class__r#   r$   r:   l   sN    


zIPythonKernel.__init__zPython Referencezhttps://docs.python.org/%i.%i   )textZurlzIPython Referencez&https://ipython.org/documentation.htmlzNumPy Referencez+https://docs.scipy.org/doc/numpy/reference/zSciPy Referencez+https://docs.scipy.org/doc/scipy/reference/zMatplotlib Referencez$https://matplotlib.org/contents.htmlzSymPy Referencez'http://docs.sympy.org/latest/index.htmlzpandas Referencez-https://pandas.pydata.org/pandas-docs/stable/ZipythonZpythonr   ztext/x-python)nameversionz	ipython%d   z.py)rW   rX   ZmimetypeZcodemirror_modeZpygments_lexerZnbconvert_exporterZfile_extensionc                 C   s4   t r0|d jd}| jd| | jj| d S )Nr   zutf-8zDebugpy received: %s)r   bytesdecoder;   debugr>   Z
tcp_clientZreceive_dap_frame)r1   msgframer#   r#   r$   dispatch_debugpy   s    zIPythonKernel.dispatch_debugpyc                 C   s   | j r| j jS d S N)r/   bannerr1   r#   r#   r$   ra      s    zIPythonKernel.bannerc                    s   | j  I dH  q dS )zPoll the stopped queue.N)r>   Zhandle_stopped_eventrb   r#   r#   r$   poll_stopped_queue   s    z IPythonKernel.poll_stopped_queuec                    sb   | j rd| j _| jdkr&| jd n| jj| jdd t   | jr^t	
|  | jjj dS )zStart the kernel.FNz7debugpy_stream undefined, debugging will not be enabledcopy)r/   exit_nowr<   r;   ZwarningZon_recvr_   r9   startasyncioZrun_coroutine_threadsaferc   Zcontrol_threadio_loopZasyncio_looprb   rS   r#   r$   rg      s    

 zIPythonKernel.startr/   c                    s.   t  ||| |dkr*| jr*| j| dS )zmOverridden from parent to tell the display hook and output streams
        about the parent message.
        r/   N)r9   
set_parentr/   )r1   identr5   ZchannelrS   r#   r$   rj      s    zIPythonKernel.set_parentc                    s"   t  |}|d| jd |S )zVInitialize metadata.

        Run at the beginning of each execution request.
        T)dependencies_metZengine)r9   init_metadataupdaterk   )r1   r5   ZmdrS   r#   r$   rm      s    zIPythonKernel.init_metadatac                 C   s0   |d |d< |d dkr,|d dkr,d|d< |S )zXFinish populating metadata.

        Run after completing an execution request.
        statuserrorenameZUnmetDependencyFrl   r#   )r1   r5   metadatareply_contentr#   r#   r$   finish_metadata  s    zIPythonKernel.finish_metadataFc                 C   s*   || _ tj| _| jt_tj| _| jt_dS )zZForward raw_input and getpass to the current frontend.

        via input_request
        N)Z_allow_stdinbuiltinsinput_sys_raw_inputZ	raw_inputgetpass_save_getpass)r1   allow_stdinr#   r#   r$   _forward_input  s
    zIPythonKernel._forward_inputc                 C   s   | j t_| jt_dS )zRestore raw_input, getpassN)rw   ru   rv   ry   rx   rb   r#   r#   r$   _restore_input%  s    zIPythonKernel._restore_inputc                 C   s   | j r| j jS d S r`   )r/   execution_countrb   r#   r#   r$   r}   +  s    zIPythonKernel.execution_countc                 C   s   d S r`   r#   )r1   valuer#   r#   r$   r}   1  s    c              
   #   sl   t  dd }t|| |t|  fdd}ttj|}z
dV  W 5 ttj| X dS )aP  ContextManager for capturing SIGINT and cancelling a future

        SIGINT raises in the event loop when running async code,
        but we want it to halt a coroutine.

        Ideally, it would raise KeyboardInterrupt,
        but this turns it into a CancelledError.
        At least it gets a decent traceback to the user.
        c                 S   s    |   s|  rd S |   d S r`   )	cancelleddonecancel)fZ_ignoredr#   r#   r$   cancel_unless_doneF  s    z;IPythonKernel._cancel_on_sigint.<locals>.cancel_unless_donec                     s   fdd} j | d S )Nc                      s"      s  rd S  d d S )Nr   )r   r   Z
set_resultr#   )sigint_futurer#   r$   set_sigint_resultS  s    zQIPythonKernel._cancel_on_sigint.<locals>.handle_sigint.<locals>.set_sigint_result)ri   Zadd_callback)r!   r   r1   r   r#   r$   handle_sigintR  s    z6IPythonKernel._cancel_on_sigint.<locals>.handle_sigintN)rh   ZFutureZadd_done_callbackr   signalSIGINT)r1   Zfuturer   r   Zsave_sigintr#   r   r$   _cancel_on_sigint7  s    

zIPythonKernel._cancel_on_sigintc                    s,   t |}| | t |||I dH  dS )z/Override for cell output - cell reconciliation.N)r   %_associate_new_top_level_threads_withr9   execute_request)r1   streamrk   r5   parent_headerrS   r#   r$   r   c  s    
zIPythonKernel.execute_request)	cell_metacell_idc                   s  | j   dk	st| | i }t drNt drN j}	 j}
t|	dg}n"dd }
 fdd}	t jdg}zd}z 	|}W n  t
k
r   |}t }Y nX tr` jtkr`t  r`|
|||d	r`|d r|	||||||d
}n|	|||||d}t|}| |< d}z|I dH }W 5  jd |sR jd| X W 5 Q R X n.|d r~ j||||d}n j|||d}W 5 |   X |jdk	r|jn|j}|jrd|d< nJd|d< | jpg tt|jt|d t| j| j dd}||d<  j!d |d< d|kr@| j"#dd$|d  |d dkrd %|pZi |d< ni |d<  j&' |d<  j&(  |S )zHandle code execution.Nrun_cell_asyncshould_run_asyncr   c                 S   s   dS )NFr#   )Zcellr#   r#   r$   <lambda>      z*IPythonKernel.do_execute.<locals>.<lambda>c                     s    j | |S r`   )run_cellr    r/   r#   r$   r     s    z*IPythonKernel.do_execute.<locals>.run_cell)transformed_cellpreprocessing_exc_tuple)store_historysilentr   r   r   )r   r   r   r   Zpost_executeZpost_run_cell)r   r   r   )r   r   okro   rp   	tracebackrq   ZevalueZexecuteZengine_uuidZ	engine_idmethodengine_infor   r}   r   z Exception in execute request:
%s
user_expressionsZpayload))r/   rJ   r{   rP   r   r   r   r   r|   Ztransform_cell	Exceptionsysexc_infor   Zloop_runnerrh   Zget_event_loopZ
is_runningZensure_futurer   ZeventsZtriggerZerror_before_execZerror_in_execsuccessrn   _last_tracebackstrtype__name__dictrk   int_idr}   r;   infojoinr   Zpayload_managerZread_payloadZclear_payload)r1   coder   r   r   rz   r   r   rs   r   r   Zaccepts_paramsr   r   coroZcoro_futurereserre_infor#   r   r$   
do_executei  s    


	




	

zIPythonKernel.do_executec                 C   sv   t r| jr| ||S |dkr&t|}t||\}}|| }| jdk	sJt| jd||\}}|||t| i ddS )zHandle code completion.N r   matchesZ
cursor_endZcursor_startrr   ro   )_use_experimental_60_completionuse_experimental_completions_experimental_do_completelenr   r/   rJ   Zcomplete)r1   r   
cursor_poslineoffsetZline_cursorZtxtr   r#   r#   r$   do_complete  s    

zIPythonKernel.do_completec                    s   t r| j|I dH S dS )zHandle a debug request.N)r   r>   Zprocess_request)r1   r]   r#   r#   r$   do_debug_request  s    zIPythonKernel.do_debug_requestc           
      C   s   |dkrt |}t b | jdk	s&t| jj||}tt||}g }|D ]&}|t	|j
|j|j|j|jd qLW 5 Q R X |r|d j
}|d j}dd |D }	n|}|}g }	|	||t|iddS )zD
        Experimental completions from IPython, using Jedi.
        N)rg   endrV   r   	signaturer   c                 S   s   g | ]
}|j qS r#   )rV   ).0cr#   r#   r$   
<listcomp>-  s     z;IPythonKernel._experimental_do_complete.<locals>.<listcomp>r   r   )r   _provisionalcompleterr/   rJ   Z	Completercompletionslist_rectify_completionsrK   r   rg   r   rV   r   r   _EXPERIMENTAL_KEY_NAME)
r1   r   r   Zraw_completionsr   compscompser   r#   r#   r$   r     s<    


z'IPythonKernel._experimental_do_completer#   c                 C   s   t ||}ddi}i |d< i |d< | jdk	s0tz^tjdkrP| jj|||d}n| jj||d}|d | | jjs|d d	 d
|d< W n t	k
r   d|d< Y nX |S )zHandle code inspection.ro   r   datarr   N)   )detail_levelomit_sections)r   z	text/htmlTfoundF)
r   r/   rJ   r   version_infoZobject_inspect_mimern   Zenable_html_pagerpopKeyError)r1   r   r   r   r   rW   rs   Zbundler#   r#   r$   
do_inspect;  s(    

zIPythonKernel.do_inspectc
                 C   s   | j dk	st|dkr.| j jj|||dd}
nH|dkrP| j jj|||||d}
n&|dkrr| j jj|||||	d}
ng }
d	t|
d
S )zHandle code history.NtailT)rawoutputZinclude_latestrange)r   r   search)r   r   nuniquer   )ro   history)r/   rJ   Zhistory_managerZget_tailZ	get_ranger   r   )r1   Zhist_access_typer   r   r=   rg   stopr   patternr   Zhistr#   r#   r$   
do_historyW  s8               zIPythonKernel.do_historyc                 C   s   | j rd| j _td|dS )zHandle kernel shutdown.Tr   )ro   restart)r/   rf   r   )r1   r   r#   r#   r$   do_shutdown{  s    zIPythonKernel.do_shutdownc                 C   sZ   t | jdd}|dkr,| jdk	s$t| jj}||\}}d|i}|dkrVd| |d< |S )zHandle an is_complete request.Zinput_transformer_managerNro   Z
incomplete indent)rL   r/   rJ   Zinput_splitterZcheck_complete)r1   r   Ztransformer_managerro   Zindent_spacesrr#   r#   r$   do_is_complete  s    zIPythonKernel.do_is_completec              
   C   s  zddl m}m} W n$ tk
r8   ddlm}m} Y nX | j}|dk	sLtz|j}dt|	dd d }	|||dd	\}
}}t
|
d
d}|	d }|	d }|	d }|	d }||
|||||di}|| | d| d| d| d}zt||j|j ||}W 5 |D ]}|| q
X | jdk	s.t||| jj| jjd}W n tk
r } z|  |jplg tt|jt|d}t| j| jdd}||d< | j| jd|| dd | jdd |d  g }d|d< W 5 d}~X Y n
X ddi}||fS )zHandle an apply request.r   )serialize_objectunpack_apply_messager   N_-r   Frd   r   r   r!   r"   resultz = z(*z,**))buffer_thresholditem_thresholdr   Zapplyr   r   rp   rk   zException in apply request:
%sr   r   ro   r   )!Zipyparallel.serializer   r   ImportErrorZ	serializer/   rJ   r3   r   replacerL   rn   r   execZuser_global_nsrD   r=   r   r   BaseExceptionZshowtracebackr   r   r   r   rk   r   Zsend_responserE   rF   r;   r   r   )r1   ZcontentZbufsZmsg_idZreply_metadatar   r   r/   Zworkingprefixr   r!   r"   fnameargnameZ	kwargnameZ
resultnamensr   keyr   Z
result_bufr   rs   r   r#   r#   r$   do_apply  s`    


zIPythonKernel.do_applyc                 C   s   | j r| j d tddS )zClear the kernel.Fr   )ro   )r/   resetr   rb   r#   r#   r$   do_clear  s    zIPythonKernel.do_clearc                 C   s
   || _ dS )zBStore the parent header to associate it with new top-level threadsN)rN   )r1   r   r#   r#   r$   r     s    z3IPythonKernel._associate_new_top_level_threads_withc                    sl   | j | jt | tjjtjj tjdfdd}tjd fdd}|tj_|tj_dS )z=Store thread hierarchy and thread-parent_header associations.rb   c                    sj   z
| j }W n tk
r    Y dS X fD ]2}t|tr*|krPj|j| j< q*||j| j< q* |  dS )aY  Wrap the `threading.Thread.start` to intercept thread identity.

            This is needed because there is no "start" hook yet, but there
            might be one in the future: https://bugs.python.org/issue14073

            This is a no-op if the `self._stdout` and `self._stderr` are not
            sub-classes of `OutStream`.
            N)_ipykernel_parent_thread_identAttributeErrorrI   r   rN   _thread_to_parent_headerrk   _thread_to_parent)r1   r5   r   )_threading_Thread_runr7   kernel_thread_identstderrstdoutr#   r$   run_closure  s    


z;IPythonKernel._initialize_thread_hooks.<locals>.run_closurec                    s    | f|| t  | _d S r`   )	threading	get_identr   )r1   r!   r"   )_threading_Thread__init__r#   r$   init_closure  s    z<IPythonKernel._initialize_thread_hooks.<locals>.init_closureN)_stdout_stderrr  r  Threadrunr:   )r1   r  r
  r#   )r	  r  r7   r  r  r  r$   rO     s    z&IPythonKernel._initialize_thread_hooks)rg   r   )phaser   c              
   C   s   |dkrdS dd t  D }| j| jfD ]}t|tr*|j}t| D ],}||krJz
||= W qJ t	k
rt   Y qJX qJ|j
}t| D ],}||krz
||= W q t	k
r   Y qX qq*dS )a  Clean parent frames of threads which are no longer running.
        This is meant to be invoked by garbage collector callback hook.

        The implementation enumerates the threads because there is no "exit" hook yet,
        but there might be one in the future: https://bugs.python.org/issue14073

        This is a no-op if the `self._stdout` and `self._stderr` are not
        sub-classes of `OutStream`.
        rg   Nc                 S   s   h | ]
}|j qS r#   r   )r   threadr#   r#   r$   	<setcomp>  s     z<IPythonKernel._clean_thread_parent_frames.<locals>.<setcomp>)r  	enumerater  r  rI   r   r   r   keysr   r  )r1   r  r   Zactive_threadsr   Zthread_to_parent_headeridentityZthread_to_parentr#   r#   r$   rR     s&    


z)IPythonKernel._clean_thread_parent_frames)r/   )F)TNF)r   r#   )r   r   NNNF)Er   
__module____qualname____doc__r   r/   r   r   r?   r	   tagr   r   r   r<   r   r,   r   r   r2   r   r3   r4   rw   Z_sys_eval_inputr:   r   r   r   Z
help_linksimplementationr   rX   Zimplementation_versionsplitZlanguage_infor_   propertyra   rc   rg   rj   rm   rt   r{   r|   r}   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   tZLiteralZDictr   rR   __classcell__r#   r#   rS   r$   r)   I   s   9"





+
    (
!      
$B) r)   c                       s    e Zd ZdZ fddZ  ZS )r   z3DEPRECATED.  An alias for the IPython kernel class.c                    s*   ddl }|jdtdd t j|| dS )zDEPRECATED.r   Nz@Kernel is a deprecated alias of ipykernel.ipkernel.IPythonKernelrU   )
stacklevel)warningswarnDeprecationWarningr9   r:   )r1   r!   r"   r   rS   r#   r$   r:   *  s    zKernel.__init__)r   r  r  r  r:   r  r#   r#   rS   r$   r   '  s   r   )Ir  rh   ru   rQ   rx   rB   r   r   r  typingr  
contextlibr   	functoolsr   rG   ZIPython.corer   ZIPython.utils.tokenutilr   r   Zjupyter_client.sessionr   Z	traitletsr   r	   r
   r   r   r   r   r   Zzmq.eventloop.zmqstreamr   Z	comm.commr   Zcomm.managerr   Zcompilerr   r>   r   r   Z
eventloopsr   Ziostreamr   Z
kernelbaser   Z
KernelBaser   Zzmqshellr   ZIPython.core.interactiveshellr   r   ZIPython.core.completerr   r   r   r   r   r   r%   Lockr'   r&   ZOptional__annotations__r(   Zcreate_commrH   r)   r#   r#   r#   r$   <module>   sd   (


     c