U
    ARd#                     @   sF  U d dl Z d dlZd dlmZmZmZmZ d dlmZ d dl	Z	d dl
mZmZmZmZmZ d dlmZ d dlmZ d dlmZ erd dlmZ eeZeed	< d
Zeed< e je je j e	j!dZ"eed< G dd dZ#ee$ddddZ%e&e'dddZ(d e$e)ee' dddZ*e'e)dddZ+ej,eej- dddZ.dS )!    N)TYPE_CHECKINGListOptionalcast)Final)MarkdownFormattedExceptionStreamlitAPIExceptionStreamlitAPIWarningStreamlitDeprecationWarningUncaughtAppException)
get_logger)	Exception)gather_metrics)DeltaGeneratorLOGGERzThis app has encountered an error. The original error message is redacted to prevent data leaks.  Full error details have been recorded in the logs (if you're on Streamlit Cloud, click on 'Manage app' in the lower right of your app). _GENERIC_UNCAUGHT_EXCEPTION_TEXT _STREAMLIT_DIRc                   @   s6   e Zd ZededdddZeddddZd	S )
ExceptionMixin	exceptionr   r   returnc                 C   s   t  }t|| | jd|S )a@  Display an exception.

        Parameters
        ----------
        exception : Exception
            The exception to display.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> e = RuntimeError('This is an exception of type RuntimeError')
        >>> st.exception(e)

        r   )ExceptionProtomarshalldgZ_enqueue)selfr   exception_proto r   @/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/exception.pyr   2   s    
zExceptionMixin.exception)r   c                 C   s
   t d| S )zGet our DeltaGenerator.r   )r   )r   r   r   r   r   G   s    zExceptionMixin.dgN)__name__
__module____qualname__r   BaseExceptionr   propertyr   r   r   r   r   r   1   s   r   )r   r   r   c           
      C   s:  t |t}t |t}t |t}t |t}|r0g n
t||d}t|dddk	rZt|d| _nt|j| _| j	
| t |t| _z.t |trt|| _nt| | _|| _W nN tk
r } z0d| _tdt|j|dt|d  W 5 d}~X Y nX |r6tt|}t| _tt|j}	|	ddd	d| _dS )
zMarshalls an Exception.proto message.

    Parameters
    ----------
    exception_proto : Exception.proto
        The Exception protobuf to fill out

    exception : BaseException
        The exception whose data we're extracting
    )strip_streamlit_stack_entriesZalternate_nameNr   aR  

Streamlit was unable to parse the data from an exception in the user's script.
This is usually due to a bug in the Exception object itself. Here is some info
about that Exception object, so you can report a bug to the original author:

Exception type:
  %(etype)s

Problem:
  %(str_exception)s

Traceback:
%(str_exception_tb)s

        
)etypestr_exceptionZstr_exception_tbz<class 'z'>)
isinstancer   r
   r   r   _get_stack_trace_str_listgetattrtyper   stack_traceextendWarningZ
is_warningSyntaxError_format_syntax_error_messagemessagestrstripZmessage_is_markdownr   r   warningjoinr   r   excreplace)
r   r   Zis_api_exceptionZis_deprecation_exceptionZis_markdown_exceptionZis_uncaught_app_exceptionr,   r'   ZuaeZtype_strr   r   r   r   M   sH    



 



r   r   c                 C   sZ   | j rR| jdk	r&dt| jd d }nd}d| j| j| j  |t| j| jd S t	| S )zReturns a nicely formatted SyntaxError message that emulates
    what the Python interpreter outputs, e.g.:

    > File "raven.py", line 3
    >   st.write('Hello world!!'))
    >                            ^
    > SyntaxError: invalid syntax

    N    r   r   zXFile "%(filename)s", line %(lineno)s
  %(text)s
  %(caret_indent)s^
%(errname)s: %(msg)s)filenamelinenotextcaret_indentZerrnamemsg)
r<   offsetmaxr:   r;   rstripr+   r   r>   r2   )r   r=   r   r   r   r0      s    

r0   F)r   r$   r   c                 C   s   d}t | tr| j}nt| dr,t| j}t | trDt| jj}|dkrTdg}n"|rlt	|}t
|}n
t
|}dd |D }|S )aI  Get the stack trace for the given exception.

    Parameters
    ----------
    exception : BaseException
        The exception to extract the traceback from

    strip_streamlit_stack_entries : bool
        If True, all traceback entries that are in the Streamlit package
        will be removed from the list. We do this for exceptions that result
        from incorrect usage of Streamlit APIs, so that the user doesn't see
        a bunch of noise about ScriptRunner, DeltaGenerator, etc.

    Returns
    -------
    list
        The exception traceback as a list of strings

    N__traceback__zdCannot extract the stack trace for this exception. Try calling exception() within the `catch` block.c                 S   s   g | ]}|  qS r   )r3   ).0itemr   r   r   
<listcomp>   s     z-_get_stack_trace_str_list.<locals>.<listcomp>)r(   r	   Ztacked_on_stackhasattr	traceback
extract_tbrB   r   r6   _get_nonstreamlit_tracebackformat_list)r   r$   Zextracted_tracebackZstack_trace_str_listZextracted_framesr   r   r   r)      s     



r)   )filer   c                 C   s<   zt jt j| tg}W n tk
r2   Y dS X |tkS )z8True if the given file is part of the streamlit package.F)ospathcommonprefixrealpathr   
ValueError)rK   common_prefixr   r   r   _is_in_streamlit_package   s
    rR   )extracted_tbr   c                 C   s   dd | D S )Nc                 S   s   g | ]}t |js|qS r   )rR   r:   )rC   entryr   r   r   rE     s    
 z/_get_nonstreamlit_traceback.<locals>.<listcomp>r   )rS   r   r   r   rI      s    rI   )F)/rL   rG   typingr   r   r   r   Ztyping_extensionsr   Z	streamlitZstreamlit.errorsr   r   r	   r
   r   Zstreamlit.loggerr   Zstreamlit.proto.Exception_pb2r   r   Zstreamlit.runtime.metrics_utilr   Zstreamlit.delta_generatorr   r   r   __annotations__r   rM   r5   rO   dirname__file__r   r   r"   r   r/   r2   r0   boolr)   rR   StackSummaryFrameSummaryrI   r   r   r   r   <module>   s<     Q%  1