
    e                        d Z ddlZddlZddlZddlZddlmZ g ag Zda	d Z
d Zd Zd Zdd	Z G d
 de      Z eej$                  d      rej$                  j'                  d      s  e       Zej*                  Zej.                  Zyy)a  This module installs a wrapper around sys.excepthook and threading.excepthook that allows multiple
new exception handlers to be registered. 

Optionally, the wrapper also stops exceptions from causing long-term storage 
of local stack frames. This has two major effects:
  - Unhandled exceptions will no longer cause memory leaks
    (If an exception occurs while a lot of data is present on the stack, 
    such as when loading large files, the data would ordinarily be kept
    until the next exception occurs. We would rather release this memory 
    as soon as possible.)
  - Some debuggers may have a hard time handling uncaught exceptions
    N)SimpleNamespaceFc                 .    t         j                  |        y)a  Register a callable to be invoked when there is an unhandled exception.
    The callback will be passed an object with attributes: [exc_type, exc_value, exc_traceback, thread]
    (see threading.excepthook).
    Multiple callbacks will be invoked in the order they were registered.
    N)	callbacksappendfns    =/usr/lib/python3/dist-packages/pyqtgraph/exceptionHandling.pyregisterCallbackr
      s     R    c                 .    t         j                  |        y)z1Unregister a previously registered callback.
    N)r   remover   s    r	   unregisterCallbackr   !   s     Rr   c                 .    t         j                  |        y)a!  Deprecated; see registerCallback

    Register a callable to be invoked when there is an unhandled exception.
    The callback will be passed the output of sys.exc_info(): (exception type, exception, traceback)
    Multiple callbacks will be invoked in the order they were registered.
    N)old_callbacksr   r   s    r	   registerr   '   s     r   c                 .    t         j                  |        y)zYDeprecated; see unregisterCallback

    Unregister a previously registered callback.
    N)r   r   r   s    r	   
unregisterr   1   s    
 r   c                     | a y)a  
    Enable or disable traceback clearing.
    By default, clearing is disabled and Python will indefinitely store unhandled exception stack traces.
    This function is provided since Python's default behavior can cause unexpected retention of 
    large memory-consuming objects.
    N)clear_tracebacks)clears    r	   setTracebackClearingr   9   s
     r   c                   2    e Zd Zd Zd Zd Zd Zd ZddZy)	ExceptionHandlerc                     t         j                  | _        t        j                  | _        | j
                  t         _        | j                  t        _        y N)sys
excepthookorig_sys_excepthook	threadingorig_threading_excepthooksys_excepthookthreading_excepthookselfs    r	   __init__zExceptionHandler.__init__E   s7    #&>> )2)=)=&,,#88	r   c                 X    | j                   t        _        | j                  t        _        y)zNRestore original exception hooks, deactivating this exception handler
        N)r   r   r   r    r   r#   s    r	   r   zExceptionHandler.removeK   s     11#==	r   c                 X    t        |d   |d   |d   d       }| j                  |d      S )Nr         )exc_type	exc_valueexc_tracebackthreadFuse_thread_hook)r   _excepthookr$   argss     r	   r!   zExceptionHandler.sys_excepthookQ   s8    Q47RVWXRYbfge<<r   c                 (    | j                  |d      S )NTr.   )r0   r1   s     r	   r"   z%ExceptionHandler.threading_excepthookV   s    d;;r   c           
         t        j                         }	 t        j                  |dz          dt        t	        j
                  dt	        j                  t	        j                                           z  }	 t        |       |r| j                  |      }n1| j                  |j                  |j                  |j                         }t"        D ]  }	  ||        t*        D ]+  }	  ||j                  |j                  |j                          - t,        du rd t         _        |t        j                  |       S # t        $ r; t         j                  j                  d       t         j                  t         _        Y w xY w# t        $ rU t        d       t        dt        |      z         t        d       t%        j&                  t        j(                           Y w xY w# t        $ rU t        d       t        dt        |      z         t        d       t%        j&                  t        j(                           Y ]w xY w# t        j                  |       w xY w)Nd   z===== %s =====z%Y.%m.%d %H:%m:%Sz3Warning: stdout is broken! Falling back to stderr.
zA   --------------------------------------------------------------z1      Error occurred during exception callback %sT)r   getrecursionlimitsetrecursionlimitstrtimestrftime	localtimeprint	Exceptionstderrwritestdoutr    r   r*   r+   r,   r   	tracebackprint_exceptionexc_infor   r   last_traceback)r$   r2   r/   recursionLimitheaderretcbs          r	   r0   zExceptionHandler._excepthookZ   s    ..0/	2!!."45 &DMM:Mt~~^b^g^g^iOj,k(llF(f
 44T:..t}}dnndN`N`a ??tH? $ ??t}}dnnd6H6HI?  4'%)" !!.1O  (

  !WX ZZ
( ! ?]^MPSTVPWWX]^--s||~>	? ! ?]^MPSTVPWWX]^--s||~>	?" !!.1s|   AI 6D8 AI E?I $(G I 8AE<9I ;E<<I ?AGI GI  AH>:I =H>>I INc                     |dgS |dk(  S )Nr    )r$   	interfaces     r	   
implementszExceptionHandler.implements   s    &'' 222r   r   )	__name__
__module____qualname__r%   r   r!   r"   r0   rL   rJ   r   r	   r   r   D   s!    9>=
<32j3r   r   rL   )T)__doc__r   r   r9   rA   typesr   r   r   r   r
   r   r   r   r   objectr   hasattrr   rL   handlerr   original_excepthookr    original_threading_excepthookrJ   r   r	   <module>rW      s        !	 P3v P3j 	-#..2K2KL^2_ G!55$+$E$E! 3`r   