
    &e                        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        j"        j#        Z#e	e#_$        e	ge#_         ddZn# e%$ r eZY nw xY wd Z&dS )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dS )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                            d          | _        | j        rt          j        d           | j        S d S )Nz\\zC:)osgetcwdpath
startswithis_unc_pathchdir)selfs    <lib/python3.11/site-packages/IPython/utils/_process_win32.py	__enter__zAvoidUNCPath.__enter__9   sN    IKK	9//66 	HTNNN9 4    c                 J    | j         rt          j        | j                   d S d S )N)r   r   r   r   )r   exc_type	exc_value	tracebacks       r   __exit__zAvoidUNCPath.__exit__E   s/     	 HTY	  	 r   N)__name__
__module____qualname____doc__r   r!    r   r   r   r   %   s<         &
 
 
         r   r   c                     t            fd} fd}t          |                                           t          |                                           	                                  }|t	          j        d           n|S .)zCallback for _system.c                      t          j                                                  D ]3} |                     d          } t	          | t
          j                   4d S Nreplace)file)r   stdout
splitlinesdecodeprintsyslineencps    r   stdout_readz!_system_body.<locals>.stdout_readN   ]    %ah//::<< 	) 	)D;;sI..D$SZ(((((	) 	)r   c                      t          j                                                  D ]3} |                     d          } t	          | t
          j                   4d S r)   )r   stderrr-   r.   r/   r0   r1   s    r   stderr_readz!_system_body.<locals>.stderr_readS   r6   r   )targetTNg{Gz?)r   r	   startpolltimesleep)r4   r5   r9   resultr3   s   `   @r   _system_bodyr@   J   s    
C) ) ) ) ) )
) ) ) ) ) )
 +$$&&&
+$$&&&
>JtMr   c                     t                      5 }|d|d| } t          | t                    cddd           S # 1 swxY w Y   dS )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'+ttSS1CsL112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s   ;??c                     t                      5 }|d|d| } t          | d t                    }ddd           n# 1 swxY w Y   |d}t          j        |          S )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                 6    |                                  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'+ttSS1Cc#?#?HHI I I I I I I I I I I I I I I
 {C   s   "=AAFTc                    |                                  dk    rg S |st          | ||          S t                      }t          t	          j        |                                           t          j        |                    }t          |j
        z  }d |                    t          j        |j                            D             }t          |          }|S )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strictc                     g | ]}|S r&   r&   ).0args     r   
<listcomp>zarg_split.<locals>.<listcomp>   s    kkk##kkkr   )strippy_arg_splitr   CommandLineToArgvWr   cast_unicodelstripctypesbyrefr   valuefrom_address	addressofcontents	LocalFree)commandlinerN   rO   argvnresult_pointerresult_array_typer?   retvals           r   r   r      s     "$$I 	I5HHHH+I,B;CUCUCWCW,X,XZ`ZfglZmZmnn#ek1kk!2!?!?@PQ_Qh@i@i!j!jkkk>**r   c                 h    t          t          j        j                            dd|                     S )Nr
   r   )boolrY   windllkernel32OpenProcess)pids    r   	check_pidrk      s)     &221Qs;;<<<r   )FT)'r%   r   r0   rY   r=   r   r   ctypes.wintypesr   r   
subprocessr   r   	threadingr	   _process_commonr   r   r   rU   rM   r   encodingr   objectr   r@   rE   rK   rg   shell32rV   	arg_typesrestyperh   r_   res_typeAttributeErrorrk   r&   r   r   <module>rw      s   " 
			 



   ! ! ! ! ! ! ! ! + + + + + + + + - - - - - - - -       [ Z Z Z Z Z Z Z Z Z       & & & & & &"  "  "  "  " 6 "  "  " J  82 2 20! ! !0.A$+WWU^^#< !(!1!1&0II!(I    (    III= = = = =s   AB7 7C C