
    kKjZ              
       0   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:                  e   defdZ de!dee   fdZ"de!de!fdZ#	 ejH                  Z$e$jJ                  jL                  Z&e
 ee      ge&_'         ee
      e&_(        e$jR                  jT                  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                   R    e Zd ZdZdee   fdZdeee      dee   de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    m/Users/ahmed/devFolder/Ultron/claude-voice/.venv/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      sV    (
8C= 
 4./  M*  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                  >   	 j                   J t        j                         xs dj                         D ]/  } | j                  d      }t	        |t
        j                          1 y # t        $ r(}t	        d| t
        j                         Y d }~y d }~ww xY w)Nr   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'''/9@SLLN	 ''Y7d, O  	A*1#.SZZ@@	A   A'A+ +	B4BBc                  >   	 j                   J t        j                         xs dj                         D ]/  } | j                  d      }t	        |t
        j                          1 y # t        $ r(}t	        d| t
        j                          Y d }~y d }~ww xY w)Nr   r0   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                 z    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   rM   )rN   r   ress      r   systemrS   t   s4    & 
4'+S1Cc<0	 
s   1: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   	getoutputrY      sT     
4'+S1Cc#?H 

 {C   
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)	rZ   r[   r\   argvnresult_pointerresult_array_typeargrL   _s	            r   r   r      s     "$I5HH+K,>,>,@&,,uBUV	* '%++ 5 -99$$^%<%<=C ?	    .)A .)As   AC  !B;,C  ;C   Cpidc                 V    t        t        j                  j                  dd|             S )Nr   r   )boolwindllkernel32OpenProcess)ro   s    r   	check_pidru      s"     ++Aq#677r   )FT)/r(   rc   r   
subprocessr7   rG   r   r   ctypes.wintypesr   r   r   	threadingr   typesr	   typingr
   r   r^   r   _process_commonr   r`   r   r   encodingr   r   PopenbytesintrM   r)   rS   rY   rr   shell32ra   	arg_typesrestypers   ri   res_typerq   AttributeErrorru   r,   r   r   <module>r      sE  
  	  
  ! +    !  6 ? &(  ( V-J$$U+ - -`  4!3 !3 !2)]]F::$+WU^#< !(!1))II!(I ?C!%7;	cH83 84 8  Is   A4D DD