
    ^j$              
          d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZ  ed      ZddlmZ dee   dedz  fd	Zej"                  fd
e	eee   f   deej&                  e   gef   dededz  fdZd
eee   z  defdZd
eee   z  deeef   fdZd
eee   z  deeeedz  f   fdZddedededee   fdZ	 ddededeeeef      fdZy)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)IOListTypeVarUnion)Callable_T)	py3compatstreamreturnc                     ddl }	 | j                         S # t        $ r$}|j                   |j                  k7  r Y d}~yd}~ww xY w)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)r
   r   errs      V/opt/ringagent/.cad-venv/lib/python3.12/site-packages/IPython/utils/_process_common.pyread_no_interruptr       sC     {{} 99# $s    	A>Acmdcallbackstderrc           	      R   t         j                  j                          t         j                  j                          t         j                  dk(  r|t
        j                  k7  rd}nd}t        | t              }d}|r8t        j                  dk(  r%dt        j                  v rt        j                  d   }t        j                  | ||t
        j                  t
        j                  ||      }	  ||      }|j                  !	 |j!                          |j#                          |j                  	 |j'                          |S |S # t        $ rL t        d       t         j                  j                          t         j                  j                          d}Y w xY w# t$        $ r Y w xY w# t$        $ r Y |S w xY w# |j                  1	 |j!                          |j#                          n# t$        $ r Y nw xY w|j                  !	 |j'                          w # t$        $ r Y w w xY ww xY w)	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.
    win32FTNposixSHELL)shell
executablestdinstdoutr   	close_fdsz^C)sysr   flushr   platform
subprocessPIPE
isinstancestrosnameenvironPopenKeyboardInterruptprint
returncode	terminatepollOSErrorkill)r   r   r   r   r   r   pouts           r   process_handlerr4   0   s   8 JJJJ
||w6Z__#<		sC EJG#2::(=ZZ(
E$.) * &#,	.Aqk << << J3J/  d



	    J <<  <<   s   -E  F /F* AFF: FF: 	F'&F'*	F76F7:H& G)(H&)	G52H&4G55H&HH&	H"H&!H""H&c                 j    t        | d t        j                        }|yt        j                  |      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                 (    | j                         d   S )Nr   communicater2   s    r   <lambda>zgetoutput.<locals>.<lambda>   s    );      )r4   r#   STDOUTr	   decode)r   r3   s     r   	getoutputr?   |   s2      #;Z=N=N
OC
{C  r;   c                     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   s    r   getoutputerrorrC      s     !%bq))r;   c                     t        | d       }|y|\  \  }}}t        j                  |      t        j                  |      |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                 &    | j                         | fS )Nr7   r9   s    r   r:   z'get_output_error_code.<locals>.<lambda>   s    Q]]_a,@ r;   )r<   r<   N)r4   r	   r>   r-   )r   resultr3   r   r2   s        r   rB   rB      sM    $ S"@AF~MJS#C )"2"23"7EEr;   commandliner   strictc                     t        j                   | |      }d|_        d|_        g }	 	 |j                  t	        |             # t
        $ r Y |S t        $ r" |r |j                  |j                         Y |S w xY w)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   Tr<   shlexwhitespace_split
commentersappendnextStopIteration
ValueErrortoken)rG   r   rH   lextokenss        r   	arg_splitrV      s     ++k
/CC CNF
		MM$s)$   	 M  	MM#))$M	s   A 	A;&A;:A;c                 $   dt         dt        dt        t            ffd} || d      } || d      }t        |      t        |      k7  r|D cg c]  }|df c}S t	        ||      D cg c]  \  }}|d|v xs d	|v f c}}S c c}w c c}}w )
a  Split a command line and report which tokens were originally quoted.

    Returns a list of ``(token, was_quoted)`` pairs. ``token`` is the unquoted
    form, as ``shlex.split(posix=True)`` returns, and ``was_quoted`` is True
    if that token had any single- or double-quote characters in ``commandline``.

    Useful for callers like ``%run`` that want to honor shell quoting when
    deciding wether to apply further expansion (glob, tilde) to a token.

    Detection is shlex-based on both passes so the quote semantics are the
    same on Posix and Windows. If ``strict`` is False, malformed input (e.g.
    an unbalanced quote) returns whatever was parsed so far instead of raising.
    sr   r   c                     t        j                   | |      }d|_        d|_        g }	 	 |j                  t	        |             # t
        $ r Y |S t        $ r" r |j                  |j                         Y |S w xY w)NrJ   Tr<   rK   )rX   r   rT   r3   rH   s       r   	_tokenizez(arg_split_with_quotes.<locals>._tokenize   s    kk!5)#

49%  !  
  

399%
s   A 	A<&A<;A<FrJ   T'")r&   boollistlenzip)rG   rH   rZ   
raw_tokensclean_tokenstcleanraws    `      r   arg_split_with_quotesrf      s     S  $s) " ;e4J[5L
:#l++ %11qE
11 lJ7E3 
,-  2s   B-B)FT)T)__doc__r'   rL   r#   r    typingr   r   r   r   collections.abcr   r   IPython.utilsr	   bytesr   r$   r&   r*   intr4   r^   r?   tuplerC   rB   r]   rV   rf    r;   r   <module>ro      sl    
   
 + + $T] #bi EDL & //I	sDI~	I
((/0"45I I $Y	IX!3c? !s !,*d3i *E#s(O *$FsT#Y F5c3:9M3N F0#3 #t #T #TRUY #N &*,,",	%T	
,r;   