B
    \j                 @   s`   d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ d
dd	Zd	gZdS )zUtilities for launching kernels    N)PopenPIPE)getdefaultencoding)cast_bytes_py2PY3)
get_loggerFc          
      sx  d}|dkrt n|}	tjd}
|
rRttjd}|dkr<|n|}|dkrL|n|}n
|| }}|dk	rh|ntj }t	dd | }t
|	||||d}|| tjdkr fd	d
| D } |rt|t pd}||d< ddlm} | }t||d< |d |d< yddlm}m}m}m} W n$   ddlm}m}m}m} Y nX |rX||d< n(| }||||dd|}tt||d< |
r|dddB |d< d|d< n2trd|d< ndd |d< |stt |d< yt| f|}W n` tk
rF } z@d}dd | D }|| | dtj!|}t" #|  W dd}~X Y nX tjdkrZ||_$|rt|dkrt|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wF)Zprefer_stream)stdinstdoutstderrcwdenvZwin32c                s   g | ]}t | qS  )r   ).0c)encodingr   6lib/python3.7/site-packages/jupyter_client/launcher.py
<listcomp>U   s    z!launch_kernel.<locals>.<listcomp>asciir      )create_interrupt_eventZJPY_INTERRUPT_EVENTZIPY_INTERRUPT_EVENTr   )DuplicateHandleGetCurrentProcessDUPLICATE_SAME_ACCESSCREATE_NEW_PROCESS_GROUPZcreationflagsZJPY_PARENT_PIDi   Z	close_fdsZstart_new_sessionc               S   s   t  S )N)ossetsidr   r   r   r   <lambda>   s    zlaunch_kernel.<locals>.<lambda>Z
preexec_fnz>Failed to run command:
{}
    PATH={!r}
    with kwargs:
{!r}
c             S   s   i | ]\}}|d kr||qS )r   r   )r   keyvaluer   r   r   
<dictcomp>   s    z!launch_kernel.<locals>.<dictcomp>PATH)'r   sys
executableendswithopenr   devnullenvironcopyr   dictupdateplatformr   getfilesystemencodingZwin_interruptr   strZ_winapir   r   r   r   Z_subprocessint
setdefaultr   getpidr   	Exceptionitemsformatgetdefpathr   errorZwin32_interrupt_eventr	   close)cmdr	   r
   r   r   Zindependentr   kwZredirect_inZ_stdinZredirect_outZ	blackholeZ_stdoutZ_stderrkwargsZ	main_argsr   Zinterrupt_eventr   r   r   r   pidZhandleprocexcmsgZwithout_envr   )r   r   launch_kernel   sx    )








r?   )NNNNFN)__doc__r   r"   
subprocessr   r   Zipython_genutils.encodingr   Zipython_genutils.py3compatr   r   Ztraitlets.logr   r?   __all__r   r   r   r   <module>   s    
 