
    Mhw              
          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j#        Z#e#j$        j%        Z%e
 ee          ge%_&         ee
          e%_'        e#j(        j)        Z)e	e)_*        e	ge)_&        	 dde de+de+dee          fdZn# e,$ r eZY nw xY wdede+f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)POINTERc_int)HLOCALLPCWSTR)STDOUT)Thread)TracebackType)ListOptional   )	py3compat)	arg_split)process_handlerread_no_interrupt)DEFAULT_ENCODINGc                   f    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
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)
    returnc                     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    \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/utils/_process_win32.py	__enter__zAvoidUNCPath.__enter__.   sN    IKK	9//66 	HTNNN9 4    exc_type	exc_value	tracebackNc                 J    | j         rt          j        | j                   d S d S N)r   r   r   r   )r   r    r!   r"   s       r   __exit__zAvoidUNCPath.__exit__:   s1      	 HTY	  	 r   )__name__
__module____qualname____doc__r   strr   typeBaseExceptionr	   r%    r   r   r   r      s         (
8C= 
 
 
 
 4./  M*  !	 
 
           r   r   pr   c                 p    t           d fd}d fd}t          |          }t          |          }|                                 |                                 	                                  }|t	          j        d           nn-|                                 |                                 |S )	zCallback for _system.r   Nc                  <   	 j         J t          j                                                   D ]3} |                     d          }t	          |t
          j                    4d S # t          $ r)}t	          d| t
          j                   Y d }~d S 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8'''.qx88CCEE - -	 ''Y77d,,,,,- -  	A 	A 	A.1..SZ@@@@@@@@@@	A   A#A( (
B2BBc                  <   	 j         J t          j                                                   D ]3} |                     d          }t	          |t
          j                    4d S # t          $ r)}t	          d| t
          j                    Y d }~d S d }~ww xY w)Nr1   r2   zError reading stderr: )r:   r   r5   r6   r7   r8   r9   r;   s      r   stderr_readz!_system_body.<locals>.stderr_readT   rA   rB   )targetTg{Gz?)r   N)r   r   startpolltimesleepjoin)r.   r@   rD   stdout_threadstderr_threadresultr?   s   `     @r   _system_bodyrN   D   s    
C
A A A A A A AA A A A A A A +...M+...M
>Jt Mr   cmdc                     t                      5 }|d|d| } t          | t                    }t          |t          t          d          z            sJ |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   rN   
isinstanceintr+   )rO   r   ress      r   systemrV   t   s    & 
 4'+ttSS1Cc<00#sT$ZZ/00000                4s   AA$$A(+A(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
    NrQ   rR   c                 6    |                                  d         S )Nr   )communicate)r.   s    r   <lambda>zgetoutput.<locals>.<lambda>   s    Q]]__Q-? r   r   )r   r   r   r   r6   )rO   r   outs      r   	getoutputr\      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FTcommandlineposixstrictc                    |                                  dk    rg S |st          | ||          S t                      }t          |                                 t          j        |                    }	 t          |j        z  }d |	                    t          j
        |j                            D             }t          |          }n# t          |          }w xY w|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.
         )r^   r_   c                     g | ]}||S r$   r-   ).0args     r   
<listcomp>zarg_split.<locals>.<listcomp>   s)        ?	  #??r   )strippy_arg_splitr   CommandLineToArgvWlstripctypesbyrefr   valuefrom_address	addressofcontents	LocalFree)r]   r^   r_   argvnresult_pointerresult_array_typerM   _s           r   r   r      s     "$$I 	I5HHHH+K,>,>,@,@&,uBUBUVV	* '%+ 5 ,99$^%<==   F .))AA	.))A))))s   2AC Cpidc                 ^    t          t          j                            dd|                     S )Nr   r   )boolwindllkernel32OpenProcess)ru   s    r   	check_pidr{      s&     ++Aq#66777r   )FT).r)   rj   r   
subprocessr8   rH   r   r   ctypes.wintypesr   r   r   	threadingr   typesr	   typingr
   r   ra   r   _process_commonr   rg   r   r   encodingr   r   PopenrT   rN   r*   rV   r\   rx   shell32rh   	arg_typesrestypery   rp   res_typerw   AttributeErrorr{   r-   r   r   <module>r      s   
  				     



  ! ! ! ! ! ! ! ! + + + + + + + +                   ! ! ! ! ! ! ! !       6 6 6 6 6 6 ? ? ? ? ? ? ? ? & & & & & &(  (  (  (  (  (  (  ( V-J$ - - - - -`     8!3 !3 ! ! ! !2)]F:$+WWU^^#< !(!1!1)II!(I ?C !%7;	c    >    III83 84 8 8 8 8 8 8s   A$C1 1C;:C;