a
    §ˆca	  ã                   @   sN   d Z ddlmZ ddlZddlZddlZe d¡Zdd„ Zeefdd„ƒZ	dS )	z‹ Utility functions for handling exceptions from the GUI event loop.

In the test context, uncaught exceptions should cause tests to error.
é    )ÚcontextmanagerNZtraitsuic              
   C   s&   t | ƒt |ƒt d t | ||¡¡ƒfS )zŽSerialize exception and traceback for reporting.
    This is such that the stack frame is not prevented from being garbage
    collected.
    Ú )ÚstrÚjoinÚ	tracebackÚformat_exception)Úexc_typeÚvalueÚtb© r   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/testing/_exception_handling.pyÚ_serialize_exception   s    ýr   c                 #   sŠ   g ‰‡ ‡fdd„}|t _z:dV  W t jt _ˆr†d}|d dd„ ˆD ƒ¡7 }t|ƒ‚n2t jt _ˆr„d}|d dd„ ˆD ƒ¡7 }t|ƒ‚0 dS )aL  Context manager to capture all exceptions occurred in the context and
    then reraise a RuntimeError if there are any exceptions captured.

    Depending on the GUI toolkit backend, unexpected exceptions occurred in the
    GUI event loop may (1) cause fatal early exit of the test suite or (2) be
    printed to the console without causing the test to error. This context
    manager is intended for testing purpose such that unexpected exceptions
    will result in a test error.

    Parameters
    ----------
    logger : logging.Logger
        Logger to use for logging errors.
    c                    s*   t | ||ƒ}ˆ |¡ ˆ  d|d ¡ d S )Nz0Unexpected error captured in sys excepthook. 
%séÿÿÿÿ)r   ÚappendÚerror)Útyper	   r
   Z
serialized©ÚloggerZserialized_exceptionsr   r   Ú
excepthook6   s
    
ÿz&reraise_exceptions.<locals>.excepthookNzUncaught exceptions found.
Ú
c                 s   s   | ]}d j |Ž V  qdS )z*=== Exception (type: {}, value: {}) ===
{}N)Úformat)Ú.0Úrecordr   r   r   Ú	<genexpr>D   s   þÿz%reraise_exceptions.<locals>.<genexpr>)Úsysr   Ú__excepthook__r   ÚRuntimeError)r   r   Úmsgr   r   r   Úreraise_exceptions$   s&    ý

øý
r   )
Ú__doc__Ú
contextlibr   Úloggingr   r   Ú	getLoggerZ_TRAITSUI_LOGGERr   r   r   r   r   r   Ú<module>   s   
