
    ACe8:                     6   d Z ddlmZ dZg 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
Z
ddlZddlZddlZddl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 	 dd
lmZmZ  ej6                  d      Zd ZdxZZ	 ej@                  jC                  ed      Zej@                  jC                  ed      ZdZ#dZ$ e%ejL                  dd      xs dZ'e'jQ                         dk(  rdZ'd"dZ) e       d        Z* G d d      Z+ee$e$e'dfd       Z,e'dfdZ-da. ej^                         Z0e'dfdZ1d Z2da3d  Z4d! Z5y# e$ r dZdZY w xY w# e"$ rR 	 ej@                  jC                  ed      Zej@                  jC                  ed      Zn# e"$ r  e       \  ZZY nw xY wY w xY w)#zhCapture C-level FD output on pipes

Use `wurlitzer.pipes` or `wurlitzer.sys_pipes` as context managers.
    )print_functionz3.0.2)pipes	sys_pipessys_pipes_foreverstop_sys_pipes	WurlitzerN)contextmanager)F_GETFLF_SETFLfcntl)	lru_cache)Queue)F_GETPIPE_SZF_SETPIPE_SZi  i  c                     	 ddl } 	 | j                         }|j                  d       |j                  d       |j	                  dj                  g d            }t        j                  |j                               }t        j                  |j                               }||fS # t        $ r t        d      w xY w# t        $ r.}t        j                  dj                  |             Y d}~y	d}~ww xY w)
zeUse CFFI to lookup stdout/stderr pointers

    Should work ~everywhere, but requires compilation
    r   Nz@Failed to lookup stdout symbols in libc. Fallback requires cffi.zconst size_t c_stdout_p();zconst size_t c_stderr_p();
)z#include <stdio.h>z=const size_t c_stdout_p() { return (size_t) (void*) stdout; }z=const size_t c_stderr_p() { return (size_t) (void*) stderr; }zBFailed to lookup stdout with cffi: {}.
Streams may not be flushed.)NN)cffiImportErrorFFIcdefverifyjoinctypesc_void_p
c_stdout_p
c_stderr_p	Exceptionwarningswarnformat)r   _ffi_libr   r   es         )lib/python3.12/site-packages/wurlitzer.py_get_streams_cffir%   *   s    

&xxz		./		./{{II
 __T__%67
__T__%67
 :%%9  
N
 	

(  QXX	

 s#   B% BB= %B:=	C4$C//C4stdoutstderr	__stdoutp	__stderrp      encodingutf8asciic                    d}t        t        d|z              D ]  }	 t        j                  | |      c S  |r|y# t        $ r@}|}|j
                  t
        j                  k(  rt        j                  d       n Y d}~fd}~ww xY w)z Like os.dup2, but retry on EBUSYN
   g?)	rangeintosdup2OSErrorerrnoEBUSYtimesleep)abtimeoutdup_errir#   s         r$   r4   r4   f   s    G3rG|$% 	771a=     	Gww%++%

3  	s   <	B6B  Bc                      t        j                         dk7  ry	 t        dd      5 } t        | j	                               }ddd       dkD  ry|dk  ry|S # 1 sw Y   xY w# t
        $ r Y yw xY w)a  Get max pipe size

    Reads /proc/sys/fs/pipe-max-size on Linux.
    Always returns None elsewhere.

    Returns integer (up to 1MB),
    or None if no value can be determined.

    Adapted from wal-e, (c) 2018, WAL-E Contributors
    used under BSD-3-clause
    LinuxNz/proc/sys/fs/pipe-max-sizeri   i   )platformsystemopenr2   readr   )fpipe_max_sizes     r$   _get_max_pipe_sizerH   w   s}     G#!.4 	*MM	* ;&e#  	* 	*  s(   A" A A" AA" "	A.-A.c                   d    e Zd ZdZdZdde e       fdZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zy)r   zcClass for Capturing Process-level FD output via dup2

    Typically used via `wurlitzer.pipes`
    g?Nc                    || _         |t        k(  r| j                   | _        n|| _        || _        |
t	               }|| _        i | _        i | _        i | _        | j                  | j                  d<   | j                  | j                  d<   y)a  
        Parameters
        ----------
        stdout: stream or None
            The stream for forwarding stdout.
        stderr = stream or None
            The stream for forwarding stderr.
        encoding: str or None
            The encoding to use, if streams should be interpreted as text.
        bufsize: int or None
            Set pipe buffer size using fcntl F_SETPIPE_SZ (linux only)
            default: use /proc/sys/fs/pipe-max-size up to a max of 1MB
            if 0, will do nothing.
        Nr'   r&   )_stdoutSTDOUT_stderrr,   rH   _bufsize	_save_fds	_real_fds	_handlers_handle_stderr_handle_stdout)selfr&   r'   r,   bufsizes        r$   __init__zWurlitzer.__init__   s}    * V<<DL!DL ?(*G#'#6#6x #'#6#6x     c                 $   t        t        d|z        j                         }t        j                  |      }|| j
                  |<   t        j                         \  }}| j                  r	 t        |t        | j                         t        ||       t        j                  |       || j                   |<   t        |t"              }t        |t$        |t        j&                  z         |S # t        $ r t        j                  dt               Y w xY w)Nz__%s__zFailed to set pipe buffer size)getattrsysfilenor3   duprO   piperN   r   r   r5   r   r   RuntimeWarningr4   closerP   r
   r   
O_NONBLOCK)rT   namereal_fdsave_fdpipe_outpipe_inflagss          r$   _setup_pipezWurlitzer._setup_pipe   s    #x$/668&&/&tGGI'==Pg|T]]; 	Wg
&t h(h!67  P>OPs   *C) )#DDc                 V    | j                   r|j                  | j                   d      }|S )zTDecode data, if any

        Called before passing to stdout/stderr streams
        replace)r,   decoderT   datas     r$   _decodezWurlitzer._decode   s$    
 ==;;t}}i8DrW   c                 r    | j                   r+| j                   j                  | j                  |             y y N)rK   writerm   rk   s     r$   rS   zWurlitzer._handle_stdout   )    <<LLt||D12 rW   c                 r    | j                   r+| j                   j                  | j                  |             y y ro   )rM   rp   rm   rk   s     r$   rR   zWurlitzer._handle_stderr   rq   rW   c                 >    | j                   | j                  f| _        y)zSetup handle for output, if anyN)rK   rM   handlerT   s    r$   _setup_handlezWurlitzer._setup_handle   s    ||T\\2rW   c                      y)zCFinish handle, if anything should be done when it's all wrapped up.N ru   s    r$   _finish_handlezWurlitzer._finish_handle   s    rW   c                 j   | j                   r.t        j                  rt        j                  j                          | j                  r.t        j
                  rt        j
                  j                          t        t        j                  t               t        t        j                  t               yy)z&flush sys.stdout/err and low-level FDsN)
rK   rZ   r&   flushrM   r'   r   libcfflushr   ru   s    r$   _flushzWurlitzer._flush   se    <<CJJJJ<<CJJJJ!KK
#!KK
# "rW   c                      j                           j                          t        j                         \   _         _         j                  g j                  di j                  r' j                  d      }j                  |       d|<    j                  r' j                  d      }j                  |       d|<   t                fd}t        j                  |      d_        j                           fd}t        j                  |       _        d j                  _         j                  j                           j                   S )Ncontrolr&   r'   c                  T    	 j                         } | dk(  ry j                          ')Nstop)getr~   )msgflush_queuerT   s    r$   
flush_mainz'Wurlitzer.__enter__.<locals>.flush_main  s*    !oo'&=	 rW   )targetTc                     d} d}t        j                         }D ]"  }|j                  |t         j                         $ r(|j	                  |      }|rd}n"| rnj                  d       j                  };|D ]  \  }}|j                  }|j                  k(  rXd} j                  j                         |j                  j                         t        j                  j                         y|   }t        j                  |d      }	|	s8j                  |       |j                  |       t        j                  |       t        d|z        }
 |
|	        snr(j                  d       j                          D cg c]  }t        j                  |       c} |j                          yc c}w )	z*Forward bytes on a pipe to stream messagesFr   r{   Ti   z
_handle_%sr   N)	selectorsDefaultSelectorregister
EVENT_READselectputflush_intervalfd
_control_rremove
unregisterr3   r_   rE   rY   r   )drainingr   pollerpipe_eventsselector_keyrf   r   ra   rl   handlerr]   r   flush_threadnamesr   rT   s               r$   	forwarderz&Wurlitzer.__enter__.<locals>.forwarder$  s   HN..0F =y';';<= ~6 &'N  
 $0)-)<)< +1 &'L%%BT__,#'T__5))$//:1  9D772t,DR())"-")$t0C"D#&$ O R OOF#(-.RXXd^.LLN /s   G)r~   rv   r3   r]   r   
_control_wrK   rg   appendrM   r   	threadingThreaddaemonstartthreadrt   )rT   r]   r   r   r   r   r   r   s   `   @@@@r$   	__enter__zWurlitzer.__enter__  s   +-779( !),<<##H-DLL"E$K<<##H-DLL"E$K g	 !''z:"6	 6	p  &&i8!{{rW   c                    | j                          t        j                  | j                  d       | j                  j                          t        j                  | j                         | j                  j                         D ]5  \  }}| j                  |   }t        ||       t        j                  |       7 | j                          y )N   )r~   r3   rp   r   r   r   r_   rP   itemsrO   r4   ry   )rT   exc_type	exc_value	tracebackra   rb   rc   s          r$   __exit__zWurlitzer.__exit__b  s     	%(
! "^^113 	MD'nnT*G'"HHW	
 	rW   )__name__
__module____qualname____doc__r   _default_encodingrH   rV   rg   rm   rS   rR   rv   ry   r~   r   r   rx   rW   r$   r   r      sT    
 N ""$"7H,333$^@rW   r   c              #     K   dx}}|rt         j                  }nt         j                  }| t        k(  r |       x}}d}n| x}}|t        k(  rd}	|}
n|t        k(  r |       x}	}
d}n|x}	}
t        ||
||      }	 |5  ||	f ddd       |r|j                  d       |r|	j                  d       yy# 1 sw Y   1xY w# |r|j                  d       |r|	j                  d       w w xY ww)ah  Capture C-level stdout/stderr in a context manager.

    The return value for the context manager is (stdout, stderr).

    .. versionchanged:: 3.0

        when using `PIPE` (default), the type of captured output
        is `io.StringIO/BytesIO` instead of an OS pipe.
        This eliminates max buffer size issues (and hang when output exceeds 65536 bytes),
        but also means the buffer cannot be read with `.read()` methods
        until after the context exits.

    Examples
    --------

    >>> with capture() as (stdout, stderr):
    ...     printf("C-level stdout")
    ... output = stdout.read()
    FTN)r&   r'   r,   rU   r   )ioStringIOBytesIOPIPErL   r   seek)r&   r'   r,   rU   stdout_pipestderr_pipePipeIOstdout_rstdout_wstderr_rstderr_wws               r$   r   r   t  s     * !&%K+~$h&8$$8	4$h&8$$8(XwWA
 	%H$$	% MM!MM! 	% 	% MM!MM! s6   A7C+:B? <B3B? (C+3B<8B? ?)C((C+c                 X    t        t        j                  t        j                  | |      S )zRedirect C-level stdout/stderr to sys.stdout/stderr

    This is useful of sys.sdout/stderr are already being forwarded somewhere.

    DO NOT USE THIS if sys.stdout and sys.stderr are not already being forwarded.
    r,   rU   )r   rZ   r&   r'   r   s     r$   r   r     s     SZZ(GLLrW   c                     t         5  t         t        | |      at        j                          ddd       y# 1 sw Y   yxY w)zvRedirect all C output to sys.stdout/err

    This is not a context manager; it turns on C-forwarding permanently.
    N)_mighty_lock_mighty_wurlitzerr   r   r   s     r$   r   r     s;     
 *$ )(G <'')* * *s	   '7A c                  x    t         5  t        t        j                  ddd       daddd       y# 1 sw Y   yxY w)z7Stop permanent redirection started by sys_pipes_foreverN)r   r   r   rx   rW   r$   r   r     s:     
 %(&&tT48 $% % %s    09Fc                 *   t        | dd      st        j                  d       ydD ])  }t        t        |      t        j                  d        y | j                  j                  dt               | j                  j                  dt               day)	zRegister me as an IPython extension

    Captures all C output during execution and forwards to sys.

    Does nothing on terminal IPython.

    Use: %load_ext wurlitzer
    kernelNz;wurlitzer extension doesn't do anything in terminal IPython)
__stdout__
__stderr__z:sys.{} is None. Wurlitzer can't capture output without it.pre_executepost_executeT)	rY   r   r   rZ   r   r   r   r   _extension_enabled)ipra   s     r$   load_ipython_extensionr     s}     2x&ST, 3%MMVW
 II}&78II~~6rW   c                     t         sy| j                  j                  dt               | j                  j                  dt               t	                da y)zFUnload me as an IPython extension

    Use: %unload_ext wurlitzer
    Nr   r   F)r   r   r   r   r   )r   s    r$   unload_ipython_extensionr     sA     II(9:II8 rW   )r+   )6r   
__future__r   __version____all__r   r6   r   r3   rB   r   rZ   r   r8   r   
contextlibr	   r   r
   r   	functoolsr   queuer   r   r   r   CDLLr|   r%   r   r   r   in_dll
ValueErrorrL   r   rY   stdinr   lowerr4   rH   r   r   r   r   Lockr   r   r   r   r   r   rx   rW   r$   <module>r      s   &   	 	   
    % ) )  0 v{{4#&L  
Z	5''h7J''h7J 
CIIz48BF '" ! !DW Wt d-> 3 3l )$ M  y~~  1$ 	*%  0O  LLd  55__++D+>
__++D+>
 5!2!4
J55sH   D2 68E 2	D>=D>F8F ?F FFFFF