
    f                        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 ddlmZm	Z	 ddl
mZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ  G d de      Zd Zd Zd Z	 ej:                  j<                  j>                  Ze ee      ge_          ee      e_!        ej:                  jD                  jF                  Z#e	e#_$        e	ge#_         ddZd Z&y# e%$ r eZY d Z&yw xY w)zWindows-specific implementation of process utilities.

This file is only meant to be imported by process.py, not by end-users.
    N)c_intPOINTER)LPCWSTRHLOCAL)STDOUTTimeoutExpired)Thread   )read_no_interruptprocess_handler	arg_split)	py3compat)DEFAULT_ENCODINGc                       e Zd ZdZd Zd Zy)AvoidUNCPatha  A context manager to protect command execution from UNC paths.

    In the Win32 API, commands can't be invoked with the cwd being a UNC path.
    This context manager temporarily changes directory to the 'C:' drive on
    entering, and restores the original working directory on exit.

    The context manager returns the starting working directory *if* it made a
    change and None otherwise, so that users can apply the necessary adjustment
    to their system calls in the event of a change.

    Examples
    --------
    ::
        cmd = 'dir'
        with AvoidUNCPath() as path:
            if path is not None:
                cmd = '"pushd %s &&"%s' % (path, cmd)
            os.system(cmd)
    c                     t        j                         | _        | j                  j                  d      | _        | j                  r!t        j
                  d       | j                  S y )Nz\\zC:)osgetcwdpath
startswithis_unc_pathchdir)selfs    W/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/utils/_process_win32.py	__enter__zAvoidUNCPath.__enter__9   sI    IIK	99//6HHTN99     c                 \    | j                   r t        j                  | j                         y y )N)r   r   r   r   )r   exc_type	exc_value	tracebacks       r   __exit__zAvoidUNCPath.__exit__E   s     HHTYY r   N)__name__
__module____qualname____doc__r   r!    r   r   r   r   %   s    &
 r   r   c                      t          fd} fd}t        |      j                          t        |      j                          	  j                         }|t	        j
                  d       n|S +)zCallback for _system.c                      t        j                        j                         D ]/  } | j                  d      } t	        | t
        j                         1 y Nreplace)file)r   stdout
splitlinesdecodeprintsyslineencps    r   stdout_readz!_system_body.<locals>.stdout_readN   B    %ahh/::< 	)D;;sI.D$SZZ(	)r   c                      t        j                        j                         D ]/  } | j                  d      } t	        | t
        j                         1 y r)   )r   stderrr-   r.   r/   r0   r1   s    r   stderr_readz!_system_body.<locals>.stderr_readS   r6   r   )targetg{Gz?)r   r	   startpolltimesleep)r4   r5   r9   resultr3   s   `   @r   _system_bodyr@   J   sa    
C)
)
 +$$&
+$$&
 >JJtM r   c                 v    t               5 }|d|d| } t        | t              cddd       S # 1 sw Y   yxY w)a7  Win32 version of os.system() that works with network shares.

    Note that this implementation returns None, as meant for use in IPython.

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

    Returns
    -------
    int : child process' exit code.
    N"pushd  &&")r   r   r@   )cmdr   s     r   systemrE   f   s:    & 
 24'+S1CsL12 2 2s   /8c                     t               5 }|d|d| } t        | d t              }ddd       d}t        j                  |      S # 1 sw Y   "xY w)zReturn standard output 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
    NrB   rC   c                 (    | j                         d   S )Nr   )communicate)r4   s    r   <lambda>zgetoutput.<locals>.<lambda>   s    Q]]_Q-? r   r   )r   r   r   r   r.   )rD   r   outs      r   	getoutputrK   ~   s`     
 I4'+S1Cc#?HI
 {C  I Is   A		Ac                    | j                         dk(  rg S |st        | ||      S t               }t        t	        j
                  | j                               t        j                  |            }t        |j                  z  }|j                  t        j                  |j                              D cg c]  }| }}t        |      }|S c c}w )a4  Split a command line's arguments in a shell-like manner.

        This is a special version for windows that use a ctypes call to CommandLineToArgvW
        to do the argv splitting. The posix parameter is ignored.

        If strict=False, process_common.arg_split(...strict=False) is used instead.
         )posixstrict)strippy_arg_splitr   CommandLineToArgvWr   cast_unicodelstripctypesbyrefr   valuefrom_address	addressofcontents	LocalFree)	commandlinerN   rO   argvnresult_pointerresult_array_typeargr?   retvals	            r   r   r      s     "$I5HH+I,B,B;CUCUCW,XZ`ZfZfglZmn#ekk1!2!?!?@P@PQ_QhQh@i!jk##kk>* ls   3	Cc                 j    t        t        j                  j                  j	                  dd|             S )Nr
   r   )boolrU   windllkernel32OpenProcess)pids    r   	check_pidrh      s(     &&221Qs;<<r   )FT)'r%   r   r0   rU   r=   r   r   ctypes.wintypesr   r   
subprocessr   r   	threadingr	   _process_commonr   r   r   rQ   rM   r   encodingr   objectr   r@   rE   rK   rd   shell32rR   	arg_typesrestypere   r[   res_typeAttributeErrorrh   r&   r   r   <module>rt      s   " 
 
   ! + -  [ Z  &" 6 " J820!0..AA$+WU^#< !(!1&&00II!(I.=  I=s   A/C CC