a
    ¦ˆcÛ  ã                   @   sR   d dl Z d dlZe  d¡ZG dd„ dƒZG dd„ dƒZeƒ ZejZejZej	Z	dS )é    NZtraitsc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚObserverExceptionHandlera,   State for an exception handler.

    Parameters
    ----------
    handler : callable(event) or None
        A callable to handle an event, in the context of
        an exception. If None, the exceptions will be logged.
    reraise_exceptions : boolean
        Whether to reraise the exception.
    c                 C   s   |d ur|n| j | _|| _d S ©N)Ú_log_exceptionÚhandlerÚreraise_exceptions©Úselfr   r   © r	   úh/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/observation/exception_handling.pyÚ__init__!   s    z!ObserverExceptionHandler.__init__c                 C   s   t  d|¡ dS )z¸ A handler that logs the exception with the given event.

        Parameters
        ----------
        event : object
            An event object emitted by the notification.
        zFException occurred in traits notification handler for event object: %rN)Ú_loggerÚ	exception)r   Úeventr	   r	   r
   r   %   s    ýz'ObserverExceptionHandler._log_exceptionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r	   r	   r	   r
   r      s   r   c                   @   s2   e Zd ZdZdd„ Zddd„Zdd	„ Zd
d„ ZdS )ÚObserverExceptionHandlerStackz¡ A stack of exception handlers.

    Parameters
    ----------
    handlers : list of ObserverExceptionHandler
        The last item is the current handler.
    c                 C   s
   g | _ d S r   )Úhandlers©r   r	   r	   r
   r   =   s    z&ObserverExceptionHandlerStack.__init__NFc                 C   s   | j  t||d�¡ dS )aŠ   Push a new exception handler into the stack. Making it the
        current exception handler.

        Parameters
        ----------
        handler : callable(event) or None
            A callable to handle an event, in the context of
            an exception. If None, the exceptions will be logged.
        reraise_exceptions : boolean
            Whether to reraise the exception.
        ©r   r   N)r   Úappendr   r   r	   r	   r
   Úpush_exception_handler@   s
    ÿÿz4ObserverExceptionHandlerStack.push_exception_handlerc                 C   s
   | j  ¡ S )zš Pop the current exception handler from the stack.

        Raises
        ------
        IndexError
            If there are no handlers to pop.
        )r   Úpopr   r	   r	   r
   Úpop_exception_handlerS   s    z3ObserverExceptionHandlerStack.pop_exception_handlerc                 C   sT   t  ¡ \}}}z| jd }W n ty:   tddd�}Y n0 | |¡ |jrP|‚dS )z¿ Handle a notification exception using the handler last pushed.

        Parameters
        ----------
        event : object
            An event object emitted by the notification.
        éÿÿÿÿNFr   )ÚsysÚexc_infor   Ú
IndexErrorr   r   r   )r   r   Ú_ZexcpZhandler_stater	   r	   r
   Úhandle_exception]   s    þ
z.ObserverExceptionHandlerStack.handle_exception)NF)r   r   r   r   r   r   r   r    r	   r	   r	   r
   r   4   s    ÿ

r   )
Úloggingr   Ú	getLoggerr   r   r   Z_exception_handler_stackr   r   r    r	   r	   r	   r
   Ú<module>   s   
?