
    Mh+                         d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
mZ ddlmZ 	 	 	 	 	 	 dde
e         dee         d	ee         d
ee         dee	eef                  dedee         dedefdZdgZdS )zUtilities for launching kernels    N)PIPEPopen)AnyDictListOptional)
get_loggerFcmdstdinstdoutstderrenvindependentcwdkwreturnc                    d}|t           n|}	t          j                            d          }
|
r't	          t
          j        d          }||n|}||n|}n||}}||nt
          j                                        }|                                }|	||||d}|	                    |           t          j
        dk    r|r||d<   dd	lm}  |            }t          |          |d
<   |d
         |d<   	 ddlm}m}m}m} n#  ddlm}m}m}m} Y nxY w|r||d<   n9 |            } ||||dd|          }t          t)          |                    |d<   |
r|                    dd          dz  |d<   d|d<   n*d|d<   |s#t          t          j                              |d<   	 d | D             } t/          | fi |}n# t0          $ r}	 d}d |                                D             }|                    | |                    dt
          j                  |          }t;                                          |           nL# t0          $ r?}t?          j         d|  d| d           t?          j         d| d           Y d}~nd}~ww xY w|d}~ww xY wt          j
        dk    r||_!        |r$|"|j"        J |j"        #                                 |S )a  Launches a localhost kernel, binding to the specified ports.

    Parameters
    ----------
    cmd : Popen list,
        A string of Python code that imports and executes a kernel entry point.

    stdin, stdout, stderr : optional (default None)
        Standards streams, as defined in subprocess.Popen.

    env: dict, optional
        Environment variables passed to the kernel

    independent : bool, optional (default False)
        If set, the kernel process is guaranteed to survive if this process
        dies. If not set, an effort is made to ensure that the kernel is killed
        when this process dies. Note that in this case it is still good practice
        to kill kernels manually before exiting.

    cwd : path, optional
        The working dir of the kernel process (default: cwd of this process).

    **kw: optional
        Additional arguments for Popen

    Returns
    -------

    Popen instance for the kernel subprocess
    TNzpythonw.exew)r   r   r   r   r   win32r      )create_interrupt_eventJPY_INTERRUPT_EVENTIPY_INTERRUPT_EVENTr   )CREATE_NEW_PROCESS_GROUPDUPLICATE_SAME_ACCESSDuplicateHandleGetCurrentProcesscreationflagsJPY_PARENT_PIDi   F	close_fdsstart_new_sessionc                 L    g | ]!}t           j                            |          "S  )ospath
expanduser).0ss     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_client/launcher.py
<listcomp>z!launch_kernel.<locals>.<listcomp>   s(    222rw!!!$$222    z>Failed to run command:
{}
    PATH={!r}
    with kwargs:
{!r}
c                 &    i | ]\  }}|d k    ||S )r   r#   )r'   keyvalues      r)   
<dictcomp>z!launch_kernel.<locals>.<dictcomp>   s$    WWW*#u#QV,,3,,,r+   PATHzFailed to run command: 'z' due to exception:    )
stacklevelz@The following exception occurred handling the previous failure: )$r   sys
executableendswithopenr$   devnullenvironcopyupdateplatformwin_interruptr   str_winapir   r   r   r   _subprocessint
setdefaultgetpidr   	Exceptionitemsformatgetdefpathr	   errorwarningswarnwin32_interrupt_eventr   close)r
   r   r   r   r   r   r   r   redirect_in_stdinredirect_out	blackhole_stdout_stderrkwargs	main_argsr   interrupt_eventr   r   r   r   pidhandleprocexmsgwithout_envex2s                                r)   launch_kernelr]      s   b K]TTF >**=99L *S))	%~))6%~))6!6/##
(9(9CWWYYF I MM) |w 	 F5M999999 1022%(%9%9!"%()>%?!"	            	               	5&>F?####%%C$_% F %(F$4$4C !  	!!/155
B ?# ${ '+"# 	5$'	$4$4C !22c222S##F##   	WC XWWWWK**S#''&"*"="={KKCLLs#### 	 	 	MRSRRbRR_`aaaaMXSVXX        	   |w%4"  u}z%%%
KsI   2C? ?D"F< <
JA6H>=J>
J5J=JJJJr]   )NNNNFN)__doc__r$   r3   rI   
subprocessr   r   typingr   r   r   r   traitlets.logr	   r=   r@   boolr]   __all__r#   r+   r)   <module>rd      s9   % % 
			 



  " " " " " " " " , , , , , , , , , , , , $ $ $ $ $ $
    $(h h	chC=h SMh SM	h
 
$sCx.	!h h 
#h h h h h hX r+   