a
    lc[                     @   sl   d Z ddlZddlZddlZddlZddlmZ dd ZejfddZ	dd	 Z
d
d Zdd ZdddZdS )zCommon utilities for the various process_* implementations.

This file is only meant to be imported by the platform-specific implementations
of subprocess utilities, and it contains tools that are common to all of them.
    N)	py3compatc              
   C   sJ   ddl }z
|  W S  tyD } z|j |jkr0 W Y d}~n
d}~0 0 dS )zRead from a pipe ignoring EINTR errors.

    This is necessary because when reading from pipes with GUI event loops
    running in the background, often interrupts are raised that stop the
    command from completing.r   N)errnoreadIOErrorEINTR)pr   err r	   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/utils/_process_common.pyread_no_interrupt   s    
r   c                 C   sz  t j  t j  t jdk}t| t}d}|rNtjdkrNdtj	v rNtj	d }t
j| ||t
jt
j||d}zz||}W n2 ty   td t j  t j  d}Y n0 W |jdu rz|  |  W n ty   Y n0 |jdu rvz|  W n ty   Y n0 nf|jdu rFz|  |  W n tyD   Y n0 |jdu rtz|  W n tyr   Y n0 0 |S )a  Open a command in a shell subprocess and execute a callback.

    This function provides common scaffolding for creating subprocess.Popen()
    calls.  It creates a Popen object and then calls the callback with it.

    Parameters
    ----------
    cmd : str or list
        A command to be executed by the system, using :class:`subprocess.Popen`.
        If a string is passed, it will be run in the system shell. If a list is
        passed, it will be used directly as arguments.
    callback : callable
        A one-argument function that will be called with the Popen object.
    stderr : file descriptor number, optional
        By default this is set to ``subprocess.PIPE``, but you can also pass the
        value ``subprocess.STDOUT`` to force the subprocess' stderr to go into
        the same file descriptor as its stdout.  This is useful to read stdout
        and stderr combined in the order they are generated.

    Returns
    -------
    The return value of the provided callback is returned.
    win32NposixSHELL)shell
executablestdinstdoutstderr	close_fdsz^C)sysr   flushr   platform
isinstancestrosnameenviron
subprocessPopenPIPEKeyboardInterruptprint
returncode	terminatepollOSErrorkill)cmdcallbackr   r   r   r   r   outr	   r	   r
   process_handler+   sX    







r*   c                 C   s(   t | dd tj}|du rdS t|S )a  Run a command and return its stdout/stderr as a string.

    Parameters
    ----------
    cmd : str or list
        A command to be executed in the system shell.

    Returns
    -------
    output : str
        A string containing the combination of stdout and stderr from the
    subprocess, in whatever order the subprocess originally wrote to its
    file descriptors (so the order of the information in this string is the
    correct order as would be seen if running the command in a terminal).
    c                 S   s   |   d S )Nr   communicater   r	   r	   r
   <lambda>       zgetoutput.<locals>.<lambda>N )r*   r   STDOUTr   decode)r'   r)   r	   r	   r
   	getoutputp   s    r3   c                 C   s   t | dd S )a   Return (standard output, standard error) of executing cmd in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str or list
        A command to be executed in the system shell.

    Returns
    -------
    stdout : str
    stderr : str
    N   )get_output_error_code)r'   r	   r	   r
   getoutputerror   s    r6   c                 C   sF   t | dd \}}|du r&dd|jfS |\}}t|t||jfS )aE  Return (standard output, standard error, return code) of executing cmd
    in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str or list
        A command to be executed in the system shell.

    Returns
    -------
    stdout : str
    stderr : str
    returncode: int
    c                 S   s   |   | fS )Nr+   r-   r	   r	   r
   r.      r/   z'get_output_error_code.<locals>.<lambda>Nr0   )r*   r"   r   r2   )r'   Zout_errr   r)   r   r	   r	   r
   r5      s
    r5   FTc                 C   st   t j | |d}d|_d|_g }z|t| W q tyF   Y qpY q tyl   |rX ||j Y qpY q0 q|S )a  Split a command line's arguments in a shell-like manner.

    This is a modified version of the standard library's shlex.split()
    function, but with a default of posix=False for splitting, so that quotes
    in inputs are respected.

    if strict=False, then any errors shlex.split would raise will result in the
    unparsed remainder being the last element of the list, rather than raising.
    This is because we sometimes use arg_split to parse things other than
    command-line args.
    )r   Tr0   )shlexwhitespace_split
commentersappendnextStopIteration
ValueErrortoken)sr   strictlextokensr	   r	   r
   	arg_split   s    rC   )FT)__doc__r   r7   r   r   IPython.utilsr   r   r   r*   r3   r6   r5   rC   r	   r	   r	   r
   <module>   s   E