
    g                        d dl mZ d dlZd dl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mZ erd d	lmZ d d
lmZ  G d d      Z	 	 	 	 	 	 ddZy)    )annotationsN)TYPE_CHECKINGAnyCallable)util)context_dg_stackget_default_dg_stack_value)handle_uncaught_app_exception)FragmentHandledException)RerunExceptionStopException)	RerunData)ScriptRunContextc                  ,    e Zd ZdZddZddZd Zd Zy)	modified_sys_pathzA context for prepending a directory to sys.path for a second.

    Code inspired by IPython:
    Source: https://github.com/ipython/ipython/blob/master/IPython/utils/syspathcontext.py#L42
    c                     || _         d| _        y NF)_main_script_path_added_path)selfmain_script_paths     ]/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/exec_code.py__init__zmodified_sys_path.__init__,   s    !1     c                ,    t        j                  |       S )N)r   repr_r   s    r   __repr__zmodified_sys_path.__repr__0   s    zz$r   c                    | j                   t        j                  vr2t        j                  j                  d| j                          d| _        y y )Nr   T)r   syspathinsertr   r   s    r   	__enter__zmodified_sys_path.__enter__3   s9    !!1HHOOAt556#D 2r   c                    | j                   r+	 t        j                  j                  | j                         yy# t
        $ r Y yw xY wr   )r   r    r!   remover   
ValueError)r   typevalue	tracebacks       r   __exit__zmodified_sys_path.__exit__8   sD     6 67    s   )9 	AAN)r   str)returnr+   )__name__
__module____qualname____doc__r   r   r#   r*    r   r   r   r   %   s    ! $
	r   r   c                b   d}d}d}d}d}	  |        }|||||fS # t         $ rO}|j                  }|j                  j                          t	        j
                  t                      d}Y d}~Zd}~wt        $ r d}Y jt        $ r d}d}Y xt        $ r}d}d}t        |       |}Y d}~d}~ww xY w)ad  Execute the passed function wrapped in a try/except block.

    This function is called by the script runner to execute the user's script or
    fragment reruns, but also for the execution of fragment code in context of a normal
    app run. This wrapper ensures that handle_uncaught_exception messages show up in the
    correct context.

    Parameters
    ----------
    func : callable
        The function to execute wrapped in the try/except block.
    ctx : ScriptRunContext
        The context in which the script is being run.

    Returns
    -------
    tuple
        A tuple containing:
        - The result of the passed function.
        - A boolean indicating whether the script ran without errors (RerunException and
            StopException don't count as errors).
        - The RerunData instance belonging to a RerunException if the script was
            interrupted by a RerunException.
        - A boolean indicating whether the script was stopped prematurely (False for
            RerunExceptions, True for all other exceptions).
        - The uncaught exception if one occurred, None otherwise
    TNF)r   
rerun_datacursorsclearr   setr	   r   r   	Exceptionr
   )	funcctxrun_without_errorsrerun_exception_datapremature_stopresultuncaught_exceptioneexs	            r   exec_func_with_error_handlingrA   D   s    H  .2 !N F ,0 B 	 ?   || 	79:   # "  "%b)	 s-    	B.AA--B.=B.B.B))B.)r8   zCallable[[], Any]r9   r   r,   zAtuple[Any | None, bool, RerunData | None, bool, Exception | None])
__future__r   r    typingr   r   r   	streamlitr   $streamlit.delta_generator_singletonsr   r	   streamlit.error_utilr
   streamlit.errorsr   /streamlit.runtime.scriptrunner_utils.exceptionsr   r   4streamlit.runtime.scriptrunner_utils.script_requestsr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   r   rA   r1   r   r   <module>rK      s`    # 
 / /  ? 5
 NX >[
["2[[r   