Format: application/x-python-code
⬇ Download File (20.4 KB)
   1  o
   2  
   3  �h�q�@s~dZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
   4  Zddlm
   5  Z
   6  ddlmZmZddlmZddlmZddlZddlmZmZmZmZdd	lmZdd
   7  lmZmZmZddl m!Z!ddl"m#Z#m$Z$m%Z%dd
   8  l&m'Z'ddl(m)Z)ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9ddl:m;Z;m<Z<ddl=m>Z>ddl?m@Z@ddlAmBZBddlCmDZDddlEmFZFmGZGddlHmIZIeJe�ZKeK�Ldddddd d!d"d#��eJe�ZMeM�Ld$d%d&iid'fd$d(d&iid)fd$d*d+iid,fd-d.d/iid0fd1��eK�Le�eM�Le�eK�Le$�eM�Le%�d2ZNGd3d$�d$eee!�ZOeOjPZQd4d5�ZReSd6k�r=eR�dSdS)7z%An Application for launching a kernel�)�annotationsN)�partial)�FileIO�
   9  TextIOWrapper)�
  10  StreamHandler)�Path)�BaseIPythonApplication�base_aliases�
  11  base_flags�catch_config_error)�
  12  ProfileDir)�InteractiveShellApp�
  13  shell_aliases�shell_flags)�ConnectionFileMixin)�Session�session_aliases�
  14  session_flags��jupyter_runtime_dir)�ioloop)	�Any�Bool�Dict�DottedObjectName�Instance�Integer�Type�Unicode�default)�filefind)�import_item)�	ZMQStream�)�get_connection_info�write_connection_file)�
  15  ControlThread)�	Heartbeat)�IOPubThread)�
  16  IPythonKernel)�ParentPollerUnix�ParentPollerWindows)�ZMQInteractiveShellzIPKernelApp.ipzIPKernelApp.hb_portzIPKernelApp.shell_portzIPKernelApp.iopub_portzIPKernelApp.stdin_portzIPKernelApp.control_portzIPKernelApp.connection_filezIPKernelApp.transport)�ip�hb�shell�iopub�stdin�control�f�	transport�IPKernelApp�	no_stdoutT�"redirect stdout to the null device�	no_stderr�"redirect stderr to the null device�pylab�autoz^Pre-load matplotlib and numpy for interactive use with
  17          the default matplotlib backend.ZInteractiveShell�	trio_loopFzEnable Trio as main event loop.)z	no-stdoutz	no-stderrr:z	trio-loopa#NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.
  18  
  19  To exit, you will have to explicitly quit this process, by either sending
  20  "quit" from a client, or using Ctrl-\ in UNIX-like environments.
  21  
  22  To read more about this, see https://github.com/ipython/ipython/issues/2049
  23  
  24  cs�eZdZUdZdZee�Zee�Z	e
  25  eee
  26  gZedddd�jdd�Ze�Ze�Zeedd	�Ze�Zd
  27  ed<e�Ze�Ze�Ze�Ze�Ze�Ze�Z e�Z!e�Z"dd
  28  iZ#e$�Z%e&d�dd��Z'e(dd��Z)e*ddd�jdd�Z+e*ddd�jdd�Z,e*ddd�jdd�Z-e*ddd�jdd�Z.e/dddd�jdd�Z0e/ddd�jdd�Z1e*ddd�jdd�Z2e3e4e5j6�7d�p�d �d!d�jdd�Z8e3e4e5j6�7d"�p�d �d#d�jdd�Z9d$d%�Z:d&d'�Z;d(d)�Z<d*d+�Z=d,d-�Z>d.d/�Z?d0d1�Z@d2d3�ZAd4d5�ZBd6d7�ZCd8d9�ZDd:d;�ZEd<d=�ZFd>d?�ZGd@dA�ZHdBdC�ZIdDdE�ZJdFdG�ZKdHdI�ZLdJdK�ZMdLdM�ZNdNdO�ZOdPdQ�ZPdRdS�ZQdTdU�ZReSd[�fdWdX�	�ZTdYdZ�ZU�ZVS)\r5z The IPYKernel application class.zipython-kernelz ipykernel.ipkernel.IPythonKernelzipykernel.kernelbase.Kernelz�The Kernel subclass to be used.
  29  
  30      This should allow easy reuse of the IPKernelApp entry point
  31      to configure and launch kernels other than IPython's own.
  32      )�klass�helpT)�config)�
  33  allow_nonezzmq.Context[t.Any] | None�context�install)z0ipykernel.kernelspec.InstallIPythonKernelSpecAppzInstall the IPython kernel�connection_dircCst�S�Nr��self�rG�Ic:\Users\shubh\Videos\agent\nova\Lib\site-packages\ipykernel/kernelapp.py�_default_connection_dir�sz#IPKernelApp._default_connection_dircCs6t|j�j|jkr|jrttt|j��|j�S|jSrD)r�connection_file�namerC�strrErGrGrH�abs_connection_file�szIPKernelApp.abs_connection_fileFr7)r>r9zSet main event loop.z(Only send stdout/stderr to output streamzipykernel.iostream.OutStreamz*The importstring for the OutStream factory)r>r@z$ipykernel.displayhook.ZMQDisplayHookz,The importstring for the DisplayHook factoryz[Attempt to capture and forward low-level output, e.g. produced by Extension libraries.
  34      ZJPY_PARENT_PIDrz�kill this process if its parent dies.  On Windows, the argument
  35          specifies the HANDLE of the parent process, otherwise it is simply boolean.
  36          ZJPY_INTERRUPT_EVENTzYONLY USED ON WINDOWS
  37          Interrupt this process when the parent is signaled.
  38          cCs|jt_dS)zInitialize the crash handler.N)�
  39  excepthook�sysrErGrGrH�init_crash_handler�szIPKernelApp.init_crash_handlercCstj|||tjd�dS)zHandle an exception.��fileN)�	traceback�print_exceptionrO�
  40  __stderr__)rF�etype�evalue�tbrGrGrHrN�szIPKernelApp.excepthookcCsRtjdkr|js|jrt|j|j�|_dSdS|jr%|jdkr't�|_dSdSdS)zInitialize the poller.�win32r#N)rO�platform�	interrupt�
  41  parent_handler+�pollerr*rErGrGrH�init_poller�s
  42  ��zIPKernelApp.init_pollercCs�|j�d|j��}|jdkr%|dkr|�|�}|S|�d|j|f�|S|jdkr]|dkrOd}d|j|f}t|���rN|d}d|j|f}t|���s=nd|j|f}|�d|�|S)	Nz://Ztcprztcp://%s:%iZipcr#z%s-%izipc://%s)r4r-Zbind_to_random_port�bindr�exists)rF�s�portZiface�pathrGrGrH�_try_bind_socket�s&
  43  
  44  
  45  �
  46  ���zIPKernelApp._try_bind_socketcCs�ztj}Wntyd}Ynw|rdnd}t|�D]1}z	|�||�WStjyL}z|jtjkr;|j|kr;�||dkrB�WYd}~qd}~wwdS)Nr#�d)�errnoZ
  47  WSAEADDRINUSE�AttributeError�rangerd�zmqZZMQErrorZ
  48  EADDRINUSE)rFrarbZ
  49  win_in_useZmax_attempts�attemptZzerGrGrH�_bind_socket�s$
  50  ����zIPKernelApp._bind_socketc
  51  Cs�|j}t|j|jj|j|j|j|j|j	|j
  52  d�}t|���r@t
  53  |dd�}t|t�s+J�t|fi|��}||kr@|j�d|�dS|j�d|�t|fi|��dS)z"write connection info to JSON file)r-�keyr4�
  54  shell_port�
  55  stdin_port�hb_port�
  56  iopub_port�control_portT)�unpackz:Connection file %s with current information already existsNzWriting connection file: %s)rM�dictr-�sessionrlr4rmrnrorprqrr`r$�
  57  isinstance�log�debugr%)rF�cfZconnection_infoZexisting_connection_inforGrGrHr%s(�
  58  z!IPKernelApp.write_connection_filecCsD|j}|j�d|�zt|���Wn	tyYnw|��dS)zClean up our connection file.zCleaning up connection file: %sN)rMrvrwr�unlink�OSErrorZcleanup_ipc_files)rFrxrGrGrH�cleanup_connection_file"s�z#IPKernelApp.cleanup_connection_filecCs�|js
  59  dt��|_zt|jd|jg�|_Wn$ty:|j�d|j�t|j	�j
  60  jdddd�t�
  61  |j�YdSwz|��WdSty[|jjd|jdd	�|�d
  62  �YdSw)zInitialize our connection file.zkernel-%s.json�.zConnection file not found: %si�T)�mode�exist_ok�parentsNz"Failed to load connection file: %r��exc_infor#)rJ�os�getpidr rCrzrvrwrrM�parent�mkdir�atexit�registerr{Zload_connection_file�	Exception�error�exitrErGrGrH�init_connection_file-s$���z IPKernelApp.init_connection_filecCs�|j�dt���|jdusJd��t��|_}t�|j	�|�
  63  tj�|_d|j_
  64  |�|j|j�|_|j�d|j�|�
  65  tj�|_d|j_
  66  |�|j|j�|_|j�d|j�ttd�rfd|j_|j_|�|�|�|�dS)	z4Create a context, a session, and the kernel sockets.zStarting the kernel at pid: %iNz$init_sockets cannot be called twice!��z shell ROUTER Channel on port: %iz stdin ROUTER Channel on port: %i�ROUTER_HANDOVERr#)rv�infor�r�rAri�Contextr�r��close�socket�ROUTER�shell_socket�lingerrkrmrw�stdin_socketrn�hasattr�router_handover�init_control�
  67  init_iopub�rFrArGrGrH�init_socketsBs 
  68  
  69  zIPKernelApp.init_socketscCs�|�tj�|_d|j_|�|j|j�|_|j�d|j�|�tj	�|_
  70  d|j
  71  _|�tj�|_d|j_|j
  72  �tj�rE|j�|j
  73  �tj��ttd�rNd|j_tdd�|_dS)zInitialize the control channel.r�z"control ROUTER Channel on port: %ir�r#T)�daemonN)r�rir��control_socketr�rkrqrvrwZSTREAM�debugpy_socketZDEALER�debug_shell_socketr��
  74  getsockoptZ
  75  LAST_ENDPOINT�connectr�r�r&�control_threadr�rGrGrHr�\s
  76  zIPKernelApp.init_controlcCsj|�tj�|_d|j_|�|j|j�|_|j�d|j�|�	�t
  77  |jdd�|_|j��|jj
  78  |_dS)zInitialize the iopub channel.r�ziopub PUB Channel on port: %iT)�pipeN)r�riZPUB�iopub_socketr�rkrprvrw�configure_tornado_loggerr(�iopub_thread�startZbackground_socketr�rGrGrHr�ss
  79  zIPKernelApp.init_iopubcCsJt��}t||j|j|jf�|_|jj|_|j�	d|j�|j�
  80  �dS)zstart the heart beatingz!Heartbeat REP Channel on port: %iN)rir�r'r4r-ro�	heartbeatrbrvrwr�)rFZhb_ctxrGrGrH�init_heartbeats
  81  
  82  zIPKernelApp.init_heartbeatcCs|��|j�d�|jr|j�d�|jj��|jr,|j�d�|j��|j�	�|j
  83  rD|j
  84  ��rD|j�d�|j
  85  ��|j
  86  ��|j
  87  rP|j
  88  jsP|j
  89  �	�|jr\|jjs\|j�	�dD]}|j�d|�t||dd�}|rx|jsx|�	�q^|j�d	�|jr�|j��|j�d
  90  �dS)z'Close zmq sockets in an orderly fashionzCleaning up socketszClosing heartbeat channelzClosing iopub channelzClosing control thread)r/r2r1zClosing %s channel�_socketNzTerminating zmq contextzTerminated zmq context)�reset_iorvr�r�rwrA�termr��stopr�r��is_alive�joinr��closedr��getattr)rFZchannelr�rGrGrHr��s6
  91  
  92  
  93  
  94  
  95  
  96  
  97  �
  98  zIPKernelApp.closecCs�t|j�j}||jkstt|j�j�|jkr|}n|j}dd|g}|D]}|j�|�q$|jsKt	|j
  99  �tjkrKt
 100  ttjd�|D]	}t
 101  |tjd�qAt|j|j|j|j|jd�|_dS)z(display connection info, and store portsz.To connect another client to this kernel, use:z    --existing %srQ)r/r0r1r.r2N)rrJrKrLr�rCrvr�r\�int�	log_level�logging�CRITICAL�print�_ctrl_c_messagerO�
 102  __stdout__rsrmrprnrorq�_ports)rF�basename�tail�lines�linerGrGrH�log_connection_info�s*
 103  ��zIPKernelApp.log_connection_infocCsH|js|jr ttjd�}|jr|t_t_|jr"|t_t_	dSdSdS)z/redirects stdout/stderr to devnull if necessary�wN)
 104  r6r8�openr��devnullrO�stdoutr��stderrrU)rFZ	blackholerGrGrH�init_blackhole�s�zIPKernelApp.init_blackholecCs&|jrwtt|j��}tjdurtj��|jrdntj}|jr!dntj}|j	s-t
 105  |dd�}||j|jd|d�t_tj
 106  durBtj
 107  ��||j|jd|d�t_
 108  ttj
 109  d�rw|jjD]}t|t�rv|jj��dkrv|j�d	�tttj
 110  jd
 111  ��|_qW|jr�tt|j��}||j|j�|_|jt_|��dS)z.Redirect input streams and set a display hook.NF)Zwatchfdr�)Zechor��_original_stdstream_copy�z9Seeing logger to stderr, rerouting to raw filedescriptor.r�)�outstream_classr!rLrOr��flush�quietr�rU�capture_fd_outputrrtr�r�r�rv�handlersrur�stream�buffer�filenorwrrr��displayhook_classr��displayhook�patch_io)rFZoutstream_factoryZe_stdoutZe_stderr�handlerZdisplayhook_factoryrGrGrH�init_io�s8
 112  
 113  
 114  
 115  ���zIPKernelApp.init_iocCstjt_tjt_tjt_dS)zBrestore original io
 116  
 117          restores state after init_io
 118          N)rOr�r�rUr��__displayhook__r�rErGrGrHr��szIPKernelApp.reset_iocsvzddl}Wn
 119  tyYdSw|j�tjdf�fdd�	}||_t|d�r9|j�tjddf�fdd	�	}||_dSdS)
 120  zAPatch important libraries that can't handle sys.stdout forwardingrNTcs�d||d�|��S)N)rR�all_threadsrGrG)rRr��kwargs)�faulthandler_enablerGrH�enablesz$IPKernelApp.patch_io.<locals>.enabler�Fcs�|f|||d�|��S)N)rRr��chainrG)ZsignumrRr�r�r�)�faulthandler_registerrGrHr�s���z&IPKernelApp.patch_io.<locals>.register)�faulthandler�ImportErrorr�rOrUr�r�)rFr�r�r�rG)r�r�rHr�s�
 121  
 122  �zIPKernelApp.patch_iocCst�tjtj�dS)zInitialize the signal handler.N)�signal�SIGINT�SIG_IGNrErGrGrH�init_signalszIPKernelApp.init_signalcs�t|j�}t|j|jj�}t|j|jj�}|j��|jj}|||j	|||j
 123  ||j|j|j|j
 124  |j|j|jd�
 125  ���dd�|j��D���|_�fdd�|j_dS)zCreate the Kernel object itself)
 126  r�rt�control_stream�debugpy_streamr��shell_streamr�r�r�r�rv�profile_dir�user_nscSsi|]	\}}|d|�qS)Z_portrG)�.0rKrbrGrGrH�
 127  <dictcomp>:sz+IPKernelApp.init_kernel.<locals>.<dictcomp>cs�jSrD)Zexecution_countrG��kernelrGrH�<lambda>>sz)IPKernelApp.init_kernel.<locals>.<lambda>N)r"r�r�r��io_loopr�r��kernel_class�instancertr�r�r�r�rvr�r�Zrecord_portsr��itemsr�r�Zget_execution_count)rFr�r�r�Zkernel_factoryrGr�rH�init_kernel#s.
 128  
 129  �zIPKernelApp.init_kernelcs`tj�d�sdtjd<|j��dusJ��j}z�fdd�}|�_t�|�W|�_dS|�_w)z=Enable GUI event loop integration, taking pylab into account.Z
 130  MPLBACKENDz)module://matplotlib_inline.backend_inlineNcs4tdtjd��dus
 131  J�t�j�|�tjd�dS)Nz-GUI event loop or pylab initialization failedrQ)r�rOr�Z
 132  InteractiveTBZstb2text)rVrWZstb�r/rGrH�print_tbUsz,IPKernelApp.init_gui_pylab.<locals>.print_tb)r��environ�getr/�_showtracebackr
 133  �init_gui_pylab)rFr�r�rGr�rHr�@s
 134  zIPKernelApp.init_gui_pylabcCs,t|jdd�|_|jr|jj�|�dSdS)zInitialize the shell channel.r/N)r�r�r/Z
 135  configurables�appendrErGrGrH�
 136  init_shell_s�zIPKernelApp.init_shellcCs6t�d�}t��}t�tj�}|�|�|�|�dS)a5Configure the tornado logging.Logger.
 137  
 138          Must set up the tornado logger or else tornado will call
 139          basicConfig for the root logger which makes the root logger
 140          go to the real sys.stderr instead of the capture streams.
 141          This function mimics the setup of logging.basicConfig.
 142          �tornadoN)r��	getLoggerr�	Formatter�BASIC_FORMAT�setFormatter�
 143  addHandler)rF�loggerr��	formatterrGrGrHr�es
 144  
 145  
 146  z$IPKernelApp.configure_tornado_loggercCsttj�d�r4tjdkr6ddl}z
 147  ddlm}m}Wn
 148  ty#YdSwt|�	��|ur8|�
 149  |��dSdSdSdS)a�set default asyncio policy to be compatible with tornado
 150  
 151          Tornado 6 (at least) is not compatible with the default
 152          asyncio implementation on Windows
 153  
 154          Pick the older SelectorEventLoopPolicy on Windows
 155          if the known-incompatible default policy is in use.
 156  
 157          Support for Proactor via a background thread is available in tornado 6.1,
 158          but it is still preferable to run the Selector in the main thread
 159          instead of the background.
 160  
 161          do this as early as possible to make it a low priority and overridable
 162  
 163          ref: https://github.com/tornadoweb/tornado/issues/2608
 164  
 165          FIXME: if/when tornado supports the defaults in asyncio without threads,
 166                 remove and bump tornado requirement for py38.
 167                 Most likely, this will mean a new Python version
 168                 where asyncio.ProactorEventLoop supports add_reader and friends.
 169  
 170          �win)��rN)�WindowsProactorEventLoopPolicy�WindowsSelectorEventLoopPolicy)rOrZ�
 171  startswith�version_info�asynciorrr��type�get_event_loop_policy�set_event_loop_policy)rFrrrrGrGrH�_init_asyncio_patchss��	zIPKernelApp._init_asyncio_patchcCs>ddl}ddlm}t|d�r|j|_|j|_|j|_dSdS)z�Replace pdb with IPython's version that is interruptible.
 172  
 173          With the non-interruptible version, stopping pdb() locks up the kernel in a
 174          non-recoverable state.
 175          rN)�debugger�InterruptiblePdb)�pdbZIPython.corerr�r
 176  ZPdb�	set_trace)rFrrrGrGrH�init_pdb�s
 177  �zIPKernelApp.init_pdbNcs�|��t��|�|jdurdS|��|��|��|��|��|�	�|�
 178  �|��|��z|�
 179  �WntyTt|j�tjkrR|jjddd�Ynw|��|��|��|jrp|��|��|��tj��tj��dS)zInitialize the application.NzUnable to initialize signal:Tr�) r�super�
 180  initialize�subapprr�r�r^r�r�r%r�r�r�r�r�r�r�r�rvr�r�Z	init_pathr�r/r�Zinit_extensionsZ	init_coderOr�r�r�)rF�argv��	__class__rGrHr�s:
 181  ��
 182  zIPKernelApp.initializecCs�|jdur
 183  |j��S|jdur|j��|j��tj��|_|jrEddl	m
 184  }|�}|�|j|j�z|��WdSt
 185  yDYdSwz|j��WdSt
 186  yWYdSw)zStart the application.Nr)�
 187  TrioRunner)rr�r]r�rZIOLoop�currentr�r<Zipykernel.trio_runnerrr�run�KeyboardInterrupt)rFr�trrGrGrHr��s(
 188  
 189  
 190  
 191  
 192  ��zIPKernelApp.startrD)W�__name__�
 193  __module__�__qualname__�__doc__rKr�kernel_aliases�aliases�kernel_flags�flagsr)r,rr�classesr�tagr�rr�r]rr'r�rA�__annotations__r�r�r�r�r�r�r�r�r��subcommandsrrCrrI�propertyrMrr6r8r<r�rr�r�r�rr�r�r�r�r\r[rPrNr^rdrkr%r{r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr��
 194  __classcell__rGrGrrHr5ts�
 195  ��	�
 196  
 197  ����������
 198   $	#	%'cCst��}|��|��dS)z!Run an IPKernel as an applicationN)r5r�rr�)ZapprGrGrH�main�sr*�__main__)Tr�
 199  __future__rr�rfr�r�r�rOrS�typing�t�	functoolsr�iorrr�pathlibrriZIPython.core.applicationrr	r
 200  rZIPython.core.profiledirrZIPython.core.shellappr
 201  rrZjupyter_client.connectrZjupyter_client.sessionrrrZjupyter_core.pathsrr�rZtraitlets.traitletsrrrrrrrrrZtraitlets.utilsr Ztraitlets.utils.importstringr!Zzmq.eventloop.zmqstreamr"r�r$r%r2r&r�r'Ziostreamr(Zipkernelr)Zparentpollerr*r+Zzmqshellr,rsr �updater"r�r5Zlaunch_instanceZlaunch_new_instancer*rrGrGrGrH�<module>s�,��
 202  
 203  �
 204  ���
 205  
 206  
 207  
 208  x
 209  
 210  �