
    g*                    l   U d dl mZ d dl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mZ d dlmZ d dlmZ d dlmZ d d	lmZ erd d
lmZ  ee      Zded<   dZded<    G d d      Z ed      	 d	 	 	 	 	 	 	 dd       Z	 d	 	 	 	 	 	 	 ddZ ddZ!ddZ"ddZ#	 	 	 	 d dZ$ ed      Z%	 	 	 	 	 	 d!dZ&y)"    )annotationsN)TYPE_CHECKINGCallableFinalTypeVarcast)config)MarkdownFormattedExceptionStreamlitAPIWarning)
get_logger)	Exception)gather_metrics)get_script_run_ctx)DeltaGeneratorr   _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_TEXTc                  <    e Zd Z ed      dd       Zedd       Zy)ExceptionMixin	exceptionc                .    t        | j                  |      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)

        )
_exceptiondg)selfr   s     Q/var/www/openai/venv/lib/python3.12/site-packages/streamlit/elements/exception.pyr   zExceptionMixin.exception*   s    " $''9--    c                    t        d|       S )zGet our DeltaGenerator.r   )r   )r   s    r   r   zExceptionMixin.dg=   s     $d++r   N)r   BaseExceptionreturnr   )r   r   )__name__
__module____qualname__r   r   propertyr    r   r   r   r   )   s-    K . !.$ , ,r   r   r   c                T    t               }t        |||       | j                  d|      S )Nr   )ExceptionProtomarshall_enqueue)r   r   is_uncaught_app_exceptionexception_protos       r   r   r   D   s*     %&O_i)BC;;{O44r   c                   t        |t              }t        |dd      |j                  | _        nt	        |      j
                  | _        t        |      }| j                  j                  |       t        |t              | _
        	 t        |t              rt        |      | _        n%t        |      j                         | _        || _        |r't+        j,                  d      }|t*        j.                  j0                  k(  xs t*        j.                  j3                  |      }|xs> |t*        j.                  j4                  k(  xs t*        j.                  j7                  |      }|xs |t*        j.                  j8                  k(  }	|st:        | _        |	s| j=                  d       n;t        t	        |            }
|
j?                  dd      j?                  d	d      | _        |s| j=                  d
       yyy# t"        $ rU}d| _        t$        j'                  dt	        |      j
                  |dj)                  t        |                   Y d}~d}~ww xY w)ac  Marshalls an Exception.proto message.

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

    exception : BaseException
        The exception whose data we're extracting.

    is_uncaught_app_exception: bool
        The exception originates from an uncaught error during script execution.
    alternate_nameN a*  

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:
  %s

Problem:
  %s

Traceback:
%s

        
zclient.showErrorDetailstypez<class 'z'>stack_trace) 
isinstancer
   getattrr+   r.   r   _get_stack_trace_str_listr/   extendWarning
is_warningSyntaxError_format_syntax_error_messagemessagestrstripmessage_is_markdownr   r   warningjoinr	   
get_optionShowErrorDetailsConfigOptionsFULLis_true_variation
STACKTRACEis_false_variationTYPEr   
ClearFieldreplace)r)   r   r(   is_markdown_exceptionr/   str_exceptionshow_error_detailsshow_message
show_trace	show_typetype_strs              r   r&   r&   O   s   $ 'y2LM y*D1=(77#I77+I6K&&{3!+Iw!?O"
i- 'C9&MO#&))n&:&:&<O#2GO/8 !#../HI &"F"F"K"KK 33EE" 	  !V%I%I%T%TT33FF" 	  O!V%I%I%N%NN 	
 &FO#&&v.4	?+H#+#3#3J#C#K#KDRT#UO &&}5 = !5  
 #%  O$$II/>?'	
 	
	
s   AG7 7	I A
IIc                0   | j                   r| j                  dt        | j                  dz
  d      z  }nd}d| j                  | j                  | j                   j                         |t        |       j                  | j                  dz  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

        r   r,   zXFile "%(filename)s", line %(lineno)s
  %(text)s
  %(caret_indent)s^
%(errname)s: %(msg)s)filenamelinenotextcaret_indenterrnamemsg)
rS   offsetmaxrQ   rR   rstripr.   r   rV   r9   )r   rT   s     r   r7   r7      s     ~~'Y%5%5%91!==LL#
 &..#**!--/ ,	?33 }}		
" y>r   c                b   d}t        | t              r| j                  }n+t        | d      rt	        j
                  | j                        }|dg}|S t        |      \  }}|rt	        j                  |      }nt	        j                  |      }|D cg c]  }|j                          }}|S c c}w )a  Get the stack trace for the given exception.

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

    Returns
    -------
    tuple of two string lists
        The exception traceback as two lists of strings. The first represents the part
        of the stack trace the users don't typically want to see, containing internal
        Streamlit code. The second is whatever comes after the Streamlit stack trace,
        which is usually what the user wants.

    N__traceback__zdCannot extract the stack trace for this exception. Try calling exception() within the `catch` block.)
r0   r   tacked_on_stackhasattr	traceback
extract_tbr[    _split_internal_streamlit_framesformat_listr:   )r   extracted_tracebacktrace_str_listinternal_framesexternal_framesitems         r   r2   r2      s    " :>)01'77	O	,'2293J3JK "@
   ,L,
( &22?CN&22?CN3AB>4$**,>B Cs   B,c                    	 t         j                  j                  t         j                  j                  |       |g      }||k(  S # t        $ r Y yw xY w)z/True if the given file is part of package_path.F)ospathcommonprefixrealpath
ValueError)filepackage_pathcommon_prefixs      r   _is_in_packagerp     sP    ,,bgg.>.>t.Dl-ST
 L((	  s   >A 	AAc                    t               }|sg t        |       fS t        j                  j	                  t        j                  j                  t        |j                              d      t        | fd      S )a  Split the traceback into a Streamlit-internal part and an external part.

    The internal part is everything up to (but excluding) the first frame belonging to
    the user's code. The external part is everything else.

    So if the stack looks like this:

        1. Streamlit frame
        2. Pandas frame
        3. Altair frame
        4. Streamlit frame
        5. User frame
        6. User frame
        7. Streamlit frame
        8. Matplotlib frame

    ...then this should return 1-4 as the internal traceback and 5-8 as the external.

    (Note that something like the example above is extremely unlikely to happen since
    it's not like Altair is calling Streamlit code, but you get the idea.)
    r,   c                0    t        | j                        S )N)rp   rQ   )tbrn   s    r   <lambda>z2_split_internal_streamlit_frames.<locals>.<lambda>2  s    ~bkk<Hr   )split_point)	r   listrh   ri   r=   rk   r9   main_script_parent_split_list)extracted_tbctxrn   s     @r   r`   r`     sa    2 
C4%%%77<< 0 0S5K5K1L MrRLH r   Tc                    g }g }d}| D ]3  }|s
 ||      rd}|r|j                  |       #|j                  |       5 ||fS )NFT)append)	orig_listru   beforeaftersaw_split_pointrf   s         r   rx   rx   9  sT     FEO4 "&LLMM$  5=r   )F)r   r   r   r   r(   boolr   r   )r)   r%   r   r   r(   r   r   None)r   r6   r   r9   )r   r   r   z	list[str])rm   r9   rn   r9   r   r   )ry   ztraceback.StackSummaryr   zAtuple[list[traceback.FrameSummary], list[traceback.FrameSummary]])r~   zlist[T]ru   zCallable[[T], bool]r   ztuple[list[T], list[T]])'
__future__r   rh   r^   typingr   r   r   r   r   	streamlitr	   streamlit.errorsr
   r   streamlit.loggerr   streamlit.proto.Exception_pb2r   r%   streamlit.runtime.metrics_utilr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   streamlit.delta_generatorr   r   r   __annotations__r   r   r   r&   r7   r2   rp   r`   r{   rx   r#   r   r   <module>r      s!   # 	  @ @  ( E 9 V8H% % +V  %  V, ,6  ',555  $5 	5 5 ',c6#c6c6  $c6 
	c6L!H)X)#(#F#L CL%8r   