
    :c8:                     L   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 n# e$ r dZdZY nw xY w ej        d          Zd ZdxZZ	 ej         !                    ed          Zej         !                    ed          Zn[# e"$ rS 	 ej         !                    ed          Zej         !                    ed          Zn# e"$ r  e            \  ZZY nw xY wY nw xY wdZ#dZ$ e%ej&        dd          pdZ'e'(                                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! Z5dS )#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                  8   	 ddl } n# t          $ r t          d          w xY w	 |                                 }|                    d           |                    d           |                    d                    g d                    }t          j        |                                          }t          j        |	                                          }||fS # t          $ r2}t          j        d                    |                     Y d}~d	S 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.11/site-packages/wurlitzer.py_get_streams_cffir%   *   sH   

 
 
 
N
 
 	


&xxzz		.///		.///{{II   
 
 _T__%6%677
_T__%6%677
 :%%    QXX 	
 	
 	

 |||||s     !B4C 
D''DDstdoutstderr	__stdoutp	__stderrp      encodingutf8asciic                    d}t          t          d|z                      D ]]}	 t          j        | |          c S # t          $ r7}|}|j        t
          j        k    rt          j        d           n Y d}~Vd}~ww xY w|r|dS )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|$$%%  	71a==    	 	 	Gw%+% 
3  	   s   <
A=-A88A=c                     t          j                    dk    rdS 	 t          dd          5 } t          |                                           }ddd           n# 1 swxY w Y   |dk    rdS |dk    rdS |S # t
          $ r Y dS 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4 	*MMM	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* ;& 	!;e# 	!4      s4   A6 "AA6 AA6  A!A6 6
BBc                   l    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dS )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<   dS )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 	"<DLL!DL  	+(**G#'#6x #'#6x       c                 *   t          t          d|z                                            }t          j        |          }|| j        |<   t          j                    \  }}| j        rG	 t          |t          | j                   n*# t          $ r t          j        dt                     Y nw xY wt          ||           t          j        |           || j        |<   t          |t"                    }t          |t$          |t          j        z             |S )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8&//&tGII'= 	PPg|T];;;; P P P>OOOOOP 	Wg
&t h((h!6777s   'B $B*)B*c                 J    | j         r|                    | j         d          }|S )zTDecode data, if any

        Called before passing to stdout/stderr streams
        replace)r,   decoderT   datas     r$   _decodezWurlitzer._decode   s(    
 = 	9;;t}i88DrW   c                 r    | j         r/| j                             |                     |                     d S d S N)rK   writerm   rk   s     r$   rS   zWurlitzer._handle_stdout   >    < 	3Lt||D1122222	3 	3rW   c                 r    | j         r/| j                             |                     |                     d S d S ro   )rM   rp   rm   rk   s     r$   rR   zWurlitzer._handle_stderr   rq   rW   c                 ,    | j         | j        f| _        dS )zSetup handle for output, if anyN)rK   rM   handlerT   s    r$   _setup_handlezWurlitzer._setup_handle   s    |T\2rW   c                     dS )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                 f   | j         r*t          j        rt          j                                         | j        r*t          j        rt          j                                         t          t                              t                     t          !t                              t                     dS dS )z&flush sys.stdout/err and low-level FDsN)
rK   rZ   r&   flushrM   r'   r   libcfflushr   ru   s    r$   _flushzWurlitzer._flush   s    < 	CJ 	J< 	CJ 	J 	$KK
### 	$KK
#####	$ 	$rW   c                                                                                          t          j                    \   _         _         j        g j        di j        r/                     d          }                    |           d|<    j	        r/                     d          }                    |           d|<   t                       fd}t          j        |          d_                                          fd}t          j        |           _        d j        _         j                                          j        S )Ncontrolr&   r'   c                  h    	                                  } | dk    rd S                                  1)NTstop)getr~   )msgflush_queuerT   s    r$   
flush_mainz'Wurlitzer.__enter__.<locals>.flush_main  s:    !oo''&= F	rW   )targetTc                  f   d} d}t          j                    }D ]"}|                    |t           j                   #r'|                    |          }|rd}n!| rn	                    d           j        }>|D ]\  }}|j        }|j        k    rPd} 	                    j                   |
                    j                   t          j        j                   g|         }t          j        |d          }	|	s?	                    |           |
                    |           t          j        |           t          d|z            }
 |
|	           sn'                    d                                            d D              |                                 d	S )
z*Forward bytes on a pipe to stream messagesFr   r{   Ti   z
_handle_%sr   c                 6    g | ]}t          j        |          S rx   )r3   r_   ).0r]   s     r$   
<listcomp>z:Wurlitzer.__enter__.<locals>.forwarder.<locals>.<listcomp>Y  s     ...RXd^^...rW   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   flush_threadnamesr   rT   s              r$   	forwarderz&Wurlitzer.__enter__.<locals>.forwarder$  s   HN.00F = =y';<<<< '~66 ! &'NN   	!
 $000)-)< +1 & &'L%%BT_, !#'T_555))$/:::111  9D72t,,D &R((())"---")$t0C"D"D O  'R OOF###......LLNNNNNrW   )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   +-799( !),< 	###H--DLL"E$K< 	###H--DLL"E$K gg	 	 	 	 	 	 !'z:::"6	 6	 6	 6	 6	 6	 6	 6	 6	p  &i888!{rW   c                    |                                   t          j        | j        d           | j                                         t          j        | j                   | j                                        D ]6\  }}| j	        |         }t          ||           t          j        |           7|                                  d S )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3 	 	MD'nT*G'"""HWrW   )__name__
__module____qualname____doc__r   _default_encodingrH   rV   rg   rm   rS   rR   rv   ry   r~   r   r   rx   rW   r$   r   r      s         
 N """$$"7 "7 "7 "7H  ,  3 3 33 3 3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V  ddd           n# 1 swxY w Y   |r|                    d           |r|	                    d           dS dS # |r|                    d           |r|	                    d           w w xY 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+ ~ %$fhh&8$$8 %	4 %$fhh&8$$8(XwWWWA
 	% 	%H$$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%  	MM! 	MM!	 	  	MM! 	MM!	s0   C BC BC BC 1Dc                 P    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     SZ(GLLLLrW   c                     t           5  t          )t          | |          at                                           ddd           dS # 1 swxY w Y   dS )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   1AA
A
c                      t           5  t          t                              ddd           daddd           dS # 1 swxY w Y   dS )z7Stop permanent redirection started by sys_pipes_foreverN)r   r   r   rx   rW   r$   r   r     s     
 % % 	%&&tT4888 $% % % % % % % % % % % % % % % % % %s   &;??Fc                 :   t          | dd          st          j        d           dS dD ].}t          t          |          t          j        d            dS /| j                            dt                     | j                            dt                     dadS )	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TT,  3 	MVWWWFF	 I}&7888I~~666rW   c                     t           sdS | j                            dt                     | j                            dt                     t	                       da dS )zFUnload me as an IPython extension

    Use: %unload_ext wurlitzer
    Nr   r   F)r   r   r   r   r   )r   s    r$   unload_ipython_extensionr     s\      I(9:::I88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00000000   LLLL
 v{4#& #& #&L  
Z	5''h77J''h77JJ 5 5 55_++D+>>
_++D+>>

 5 5 5!2!2!4!4
JJJ55 
GCIz488BF '    " ! ! !DW W W W W W W Wt d-> 3 3 3 3l )$ M M M M  y~  1$ 	* 	* 	* 	*% % %    0    sH   A 	A)(A)6B; ;D6C87D8D
DDDD