o
    ßñhÅ  ã                   @  s¤   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rDd d	lmZ d d
lmZ G dd„ dƒZddd„ZdS )é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚCallable)Úutil)Úcontext_dg_stackÚget_default_dg_stack_value)Úhandle_uncaught_app_exception)ÚFragmentHandledException)ÚRerunExceptionÚStopException)Ú	RerunData)ÚScriptRunContextc                   @  s4   e Zd ZdZddd„Zddd„Zd	d
„ Zdd„ ZdS )Úmodified_sys_pathzÁA 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
    Úmain_script_pathÚstrc                 C  s   || _ d| _d S ©NF)Ú_main_script_pathÚ_added_path)Úselfr   © r   ú\/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/exec_code.pyÚ__init__,   s   
zmodified_sys_path.__init__Úreturnc                 C  s
   t  | ¡S )N)r   Úrepr_©r   r   r   r   Ú__repr__0   s   
zmodified_sys_path.__repr__c                 C  s*   | j tjvrtj d| j ¡ d| _d S d S )Nr   T)r   ÚsysÚpathÚinsertr   r   r   r   r   Ú	__enter__3   s   
þzmodified_sys_path.__enter__c                 C  s4   | j rz
tj | j¡ W dS  ty   Y dS w dS r   )r   r   r   Úremover   Ú
ValueError)r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__8   s   ûûzmodified_sys_path.__exit__N)r   r   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r&   r   r   r   r   r   %   s    

r   ÚfuncúCallable[[], Any]Úctxr   r   úAtuple[Any | None, bool, RerunData | None, bool, Exception | None]c           	   
   C  sÔ   d}d}d}d}d}z| ƒ }W nS t y1 } z|j}|j ¡  t tƒ ¡ d}W Y d}~n6d}~w ty;   d}Y n( tyG   d}d}Y n t	yb } zd}d}t
|ƒ |}W Y d}~nd}~ww |||||fS )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	   )	r+   r-   Úrun_without_errorsÚrerun_exception_dataÚpremature_stopÚresultÚuncaught_exceptionÚeÚexr   r   r   Úexec_func_with_error_handlingD   s>   $

€€üûr;   )r+   r,   r-   r   r   r.   )Ú
__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   r;   r   r   r   r   Ú<module>   s   