o
    -wi                  
   @  s.  U d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
 ddlmZ eeZG dd deZG d	d
 d
e
Ze ZdadZded< daded< i Zded< i Zded< d#ddZd#ddZd$ddZd%d!d"Zzeeje eej e W dS  ey Z! ze Ze!e_"W Y dZ![!dS dZ![!ww )&a  Module for intercepting stdout/stderr.

This patches the `write()` method of `stdout` and `stderr` on import.
Once patched, it is not possible to unpatch or repatch, though individual
callbacks can be removed.

We assume that all other writing methods on the object delegate to `write()`,
like `writelines()`. This is not guaranteed to be true, but it is true for
common implementations. In particular, CPython's implementation of IOBase's
`writelines()` delegates to `write()`.

It is important to note that this technique interacts poorly with other
code that performs similar patching if it also allows unpatching as this
discards our modification. This is why we patch on import and do not support
unpatching:

    with contextlib.redirect_stderr(...):
        from ... import console_capture
        # Here, everything works fine.
    # Here, callbacks are never called again.

In particular, it does not work with some combinations of pytest's
`capfd` / `capsys` fixtures and pytest's `--capture` option.
    )annotationsN)IOAnyStrCallableProtocol   )
wb_loggingc                   @  s   e Zd ZdZdS )CannotCaptureConsoleErrorz,The module failed to patch stdout or stderr.N)__name__
__module____qualname____doc__ r   r   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/lib/console_capture.pyr	   &   s    r	   c                   @  s   e Zd ZdZddd	Zd
S )_WriteCallbacka  A callback that receives intercepted bytes or string data.

    This may be called from any thread, but is only called from one thread
    at a time.

    Note on errors: Any error raised during the callback will clear all
    callbacks. This means that if a user presses Ctrl-C at an unlucky time
    during a run, we will stop uploading console output---but it's not
    likely to be a problem unless something catches the KeyboardInterrupt.

    Regular Exceptions are caught and logged instead of bubbling up to the
    user's print() statements; other exceptions like KeyboardInterrupt are
    re-raised.

    Callbacks should handle all exceptions---a callback that raises any
    Exception is considered buggy.
    databytes | strwrittenintreturnNonec                C  s   dS )a?  Intercept data passed to `write()`.

        See the protocol docstring for information about exceptions.

        Args:
            data: The object passed to stderr's or stdout's `write()`.
            written: The number of bytes or characters written.
                This is the return value of `write()`.
        Nr   )selfr   r   r   r   r   __call__=   s    z_WriteCallback.__call__N)r   r   r   r   r   r   )r
   r   r   r   r   r   r   r   r   r   *   s    r   Fz CannotCaptureConsoleError | None_patch_exceptionr   _next_callback_iddict[int, _WriteCallback]_stdout_callbacks_stderr_callbackscallbackr   Callable[[], None]c                 C  :   t  trttt| W  d   S 1 sw   Y  dS )a   Install a callback that runs after every write to sys.stdout.

    Args:
        callback: A callback to invoke after running `sys.stdout.write`.

    Returns:
        A function to uninstall the callback.

    Raises:
        CannotCaptureConsoleError: If patching failed on import.
    N)_module_rlockr   _insert_disposablyr   r   r   r   r   capture_stdoutZ      $r$   c                 C  r    )a   Install a callback that runs after every write to sys.sdterr.

    Args:
        callback: A callback to invoke after running `sys.stderr.write`.

    Returns:
        A function to uninstall the callback.

    Raises:
        CannotCaptureConsoleError: If patching failed on import.
    N)r!   r   r"   r   r#   r   r   r   capture_stderrp   r%   r&   callback_dictc                   s.   t t d7 a dd fdd}| < |S )Nr   Fr   r   c                     sP   t  r	 W d    d S  d  dW d    d S 1 s!w   Y  d S )NT)r!   popr   r'   disposedidr   r   dispose   s   "z#_insert_disposably.<locals>.dispose)r   r   )r   )r'   r   r,   r   r)   r   r"      s   r"   stdout_or_stderr
IO[AnyStr]	callbacksr   c                   s(   t  d fdd}| j|| _d S )Nsr   r   r   c                  s   | }t S tr|W  d    S daz9z  D ]}|| | qW n& tyI } zt  t  t|tr>t	
d n W Y d }~nd }~ww W dandaw W d    |S 1 s\w   Y  |S )NTz(Error in console callback, clearing all!F)r!   _is_writingvaluesBaseExceptionr   clearr   
isinstance	Exception_logger	exception)r0   ncber/   
orig_writer   r   write_with_callbacks   s2   	

  z$_patch.<locals>.write_with_callbacks)r0   r   r   r   )r   log_to_all_runswrite)r-   r/   r>   r   r<   r   _patch   s   (
rA   )r   r   r   r   )r'   r   r   r   r   r   )r-   r.   r/   r   r   r   )#r   
__future__r   loggingsys	threadingtypingr   r   r   r    r   	getLoggerr
   r7   r6   r	   r   RLockr!   r1   r   __annotations__r   r   r   r$   r&   r"   rA   stdoutstderr_patch_exception_cause	__cause__r   r   r   r   <module>   s8    
%



7