
    Xhw              
       *   d Z ddl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 ddlmZ ddlmZ ddlmZmZ d	d
lmZ d	dlmZ d	dlmZmZ d	dlmZ  G d d      Zdej:                  defdZde dee   fdZ!de de fdZ"	 ejF                  Z#e#jH                  jJ                  Z%e
 ee      ge%_&         ee
      e%_'        e#jP                  jR                  Z)e	e)_*        e	ge)_&        	 dde de+de+dee    fdZdede+fdZ-y# e,$ r e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)POINTERc_int)HLOCALLPCWSTR)STDOUT)Thread)TracebackType)ListOptional   )	py3compat)	arg_split)process_handlerread_no_interrupt)DEFAULT_ENCODINGc                   L    e Zd ZdZdee   fdZdeee      dee   de	ddf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)
    returnc                     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    P/var/www/html/myenv/lib/python3.12/site-packages/IPython/utils/_process_win32.py	__enter__zAvoidUNCPath.__enter__.   sI    IIK	99//6HHTN99     exc_type	exc_value	tracebackNc                 \    | j                   r t        j                  | j                         y y )N)r   r   r   r   )r   r    r!   r"   s       r   __exit__zAvoidUNCPath.__exit__:   s"     HHTYY r   )__name__
__module____qualname____doc__r   strr   typeBaseExceptionr	   r$    r   r   r   r      sQ    (
8C= 
 4./  M*  !	 
 
 r   r   pr   c                 8    t         d fd}d fd}t        |      }t        |      }|j                          |j                          	  j                         }|t	        j
                  d       nn*|j                          |j                          |S )zCallback for _system.c                  6   	 j                   J t        j                         j                         D ]/  } | j                  d      }t	        |t
        j                          1 y # t        $ r(}t	        d| t
        j                         Y d }~y d }~ww xY w)NreplacefilezError reading stdout: )stdoutr   
splitlinesdecodeprintsys	Exceptionstderr	byte_linelineeencr-   s      r   stdout_readz!_system_body.<locals>.stdout_readK       	A88'''.qxx8CCE -	 ''Y7d,-  	A*1#.SZZ@@	A   A#A' '	B0BBc                  6   	 j                   J t        j                         j                         D ]/  } | j                  d      }t	        |t
        j                          1 y # t        $ r(}t	        d| t
        j                          Y d }~y d }~ww xY w)Nr0   r1   zError reading stderr: )r9   r   r4   r5   r6   r7   r8   r:   s      r   stderr_readz!_system_body.<locals>.stderr_readT   r@   rA   )targetg{Gz?)r   N)r   r   startpolltimesleepjoin)r-   r?   rC   stdout_threadstderr_threadresultr>   s   `     @r   _system_bodyrM   D   s    
C
AA +.M+.M
 >JJt  Mr   cmdc                     t               5 }|d|d| } t        | t              }t        |t        t        d      z        sJ |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   rM   
isinstanceintr*   )rN   r   ress      r   systemrU   t   s]    & 
 4'+S1Cc<0#sT$Z/000   s   :AA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
    NrP   rQ   c                 (    | j                         d   S )Nr   )communicate)r-   s    r   <lambda>zgetoutput.<locals>.<lambda>   s    Q]]_Q-? r   r   )r   r   r   r   r5   )rN   r   outs      r   	getoutputr[      s`     
 I4'+S1Cc#?HI
 {C  I Is   A		Acommandlineposixstrictc                    | j                         dk(  rg S |st        | ||      S t               }t        | j	                         t        j                  |            }	 t        |j                  z  }|j                  t        j                  |j                              D cg c]  }|| }}t        |      }|S c c}w # t        |      }w xY 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.
         )r]   r^   )strippy_arg_splitr   CommandLineToArgvWlstripctypesbyrefr   valuefrom_address	addressofcontents	LocalFree)	r\   r]   r^   argvnresult_pointerresult_array_typeargrL   _s	            r   r   r      s     "$I5HH+K,>,>,@&,,uBUV	* '%++ 5 -99$$^%<%<= ?	 F  .)A .)As   AC  !B;,C  ;C   Cpidc                 V    t        t        j                  j                  dd|             S )Nr   r   )boolwindllkernel32OpenProcess)rq   s    r   	check_pidrw      s"     ++Aq#677r   )FT).r(   re   r   
subprocessr7   rG   r   r   ctypes.wintypesr   r   r   	threadingr   typesr	   typingr
   r   r`   r   _process_commonr   rb   r   r   encodingr   r   PopenrS   rM   r)   rU   r[   rt   shell32rc   	arg_typesrestyperu   rk   res_typers   AttributeErrorrw   r,   r   r   <module>r      s@  
  	  
  ! +    !  6 ? &(  ( V-J$$ - -`  8!3 !3 !2)]]F::$+WU^#< !(!1))II!(I ?C!%7;	cF83 84 8	  Is   A4D DD