3
d                 @   sR   d dl Z d dlZe j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 k	r|n| j | _|| _d S )N)_log_exceptionhandlerreraise_exceptions)selfr   r    r   G/tmp/pip-build-7vycvbft/traits/traits/observation/exception_handling.py__init__!   s    z!ObserverExceptionHandler.__init__c             C   s   t j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 )N)handlers)r   r   r   r   r	   =   s    z&ObserverExceptionHandlerStack.__init__NFc             C   s   | j 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   r   r   push_exception_handler@   s    z4ObserverExceptionHandlerStack.push_exception_handlerc             C   s
   | j j S )z Pop the current exception handler from the stack.

        Raises
        ------
        IndexError
            If there are no handlers to pop.
        )r   pop)r   r   r   r   pop_exception_handlerS   s    z3ObserverExceptionHandlerStack.pop_exception_handlerc             C   sV   t j \}}}y| jd }W n  tk
r<   tddd}Y nX |j| |jrR|dS )z Handle a notification exception using the handler last pushed.

        Parameters
        ----------
        event : object
            An event object emitted by the notification.
           NF)r   r   )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   
?