B
    lU\                 @   s~   d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ G dd dZG d	d
 d
eZdS )a  
Some utility classes for exception handling of exceptions raised
within listeners:

- TracebackInfo: convenient way of getting stack trace of latest
  exception raised. The handler can create the instance to retrieve
  the stack trace and then log it, present it to user, etc.
- ExcPublisher: example handler that publishes a message containing
  traceback info

:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE_BSD_Simple.txt for details.

    N)
TupleListSequenceMappingDictCallableAnyOptionalUnionTextIO   )IListenerExcHandler)TopicManagerc               @   s@   e Zd ZdZdd Zee dddZedddZd	d
 Z	dS )TracebackInfoaR  
    Represent the traceback information for when an exception is
    raised -- but not caught -- in a listener. The complete
    traceback cannot be stored since this leads to circular
    references (see docs for sys.exc_info()) which keeps
    listeners alive even after the application is no longer
    referring to them.

    Instances of this object are given to listeners of the
    'uncaughtExcInListener' topic as the excTraceback kwarg.
    The instance calls sys.exc_info() to get the traceback
    info but keeps only the following info:

     * self.ExcClass: the class of exception that was raised and not caught
     * self.excArg: the argument given to exception when raised
     * self.traceback: list of quadruples as returned by traceback.extract_tb()

    Normally you just need to call one of the two getFormatted() methods.
    c             C   sf   t  }|d | _|d | _t|d }d}dd l}|d||d  d sRt||d  | _~d S )Nr      r   zpubsub.core.listener\.py)	sysexc_infoExcClassexcArg	traceback
extract_tbresearchAssertionError)selfZtmpInfoZtb_listZIGNORE_FRAMESr    r   7lib/python3.7/site-packages/pubsub/utils/exchandling.py__init__,   s    

zTracebackInfo.__init__)returnc             C   s&   t | j }|t | j| j |S )z
        Get a list of strings as returned by the traceback module's
        format_list() and format_exception_only() functions.
        )r   format_listextendformat_exception_onlyr   r   )r   Ztmpr   r   r   getFormattedList:   s    zTracebackInfo.getFormattedListc             C   s   d |  S )z
        Get a string similar to the stack trace that gets printed
        to stdout by Python interpreter when an exception is not caught.
         )joinr"   )r   r   r   r   getFormattedStringC   s    z TracebackInfo.getFormattedStringc             C   s   |   S )N)r%   )r   r   r   r   __str__J   s    zTracebackInfo.__str__N)
__name__
__module____qualname____doc__r   r   strr"   r%   r&   r   r   r   r   r      s
   	r   c               @   s@   e Zd ZdZdZdedddZedddZed	d
dZ	dS )ExcPublisherz
    Example exception handler that simply publishes the exception traceback.
    The messages will have topic name given by topicUncaughtExc.
    ZuncaughtExcInListenerN)topicMgrc             C   s   |dk	r|  | dS )z
        If topic manager is specified, will automatically call init().
        Otherwise, caller must call init() after pubsub imported. See
        pub.setListenerExcHandler().
        N)init)r   r-   r   r   r   r   W   s    zExcPublisher.__init__c             C   s2   | | j}|d |tddd || _dS )zw
        Must be called only after pubsub has been imported since this
        handler creates a pubsub topic.
        z-generated when a listener raises an exceptionz!string representation of listenerz3instance of TracebackInfo containing exception info)listenerStrexcTracebackN)ZgetOrCreateTopictopicUncaughtExcZsetDescriptionZsetMsgArgSpecdict_ExcPublisher__topicObj)r   r-   objr   r   r   r.   `   s    

zExcPublisher.init)
listenerIDc             C   s   t  }| jj||d dS )z
        Handle the exception raised by given listener. Send the
        Traceback to all subscribers of topic self.topicUncaughtExc.
        )r/   r0   N)r   r3   Zpublish)r   r5   ZtopicObjZtbInfor   r   r   __call__l   s    zExcPublisher.__call__)N)
r'   r(   r)   r*   r1   r   r   r.   r+   r6   r   r   r   r   r,   N   s
   	r,   )r*   r   r   typingr   r   r   r   r   r   r   r	   r
   r   Zcore.listenerr   Zcore.topicmgrr   r   r,   r   r   r   r   <module>   s   07