o
    Z¾±hˆ  ã                   @  s´   d dl mZ d dlZd dl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mZ erLd d	lmZ d 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ÚLiteral)Úutil)Úcontext_dg_stackÚget_default_dg_stack_value)Úhandle_uncaught_app_exception)ÚFragmentHandledException)ÚRerunExceptionÚStopException)ÚTracebackType)Ú	RerunData)ÚScriptRunContextc                   @  s8   e Zd ZdZddd„Zddd	„Zdd
d„Zd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ÚstrÚreturnÚNonec                 C  s   || _ d| _d S ©NF)Ú_main_script_pathÚ_added_path)Úselfr   © r   úi/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/exec_code.pyÚ__init__.   s   
zmodified_sys_path.__init__c                 C  s
   t  | ¡S )N)r   Úrepr_©r   r   r   r   Ú__repr__2   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__5   s   
þzmodified_sys_path.__enter__Útypútype[BaseException] | NoneÚexcúBaseException | NoneÚtbúTracebackType | NoneúLiteral[False]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   r$   r&   r(   r   r   r   Ú__exit__:   s   ûûzmodified_sys_path.__exit__N)r   r   r   r   )r   r   )r   r   )r$   r%   r&   r'   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
   )	r2   r4   Úrun_without_errorsÚrerun_exception_dataÚpremature_stopÚresultÚuncaught_exceptionÚeÚexr   r   r   Úexec_func_with_error_handlingK   s>   $

€€üûrB   )r2   r3   r4   r   r   r5   )Ú
__future__r   r    Útypingr   r   r   r   Ú	streamlitr   Ú$streamlit.delta_generator_singletonsr   r	   Ústreamlit.error_utilr
   Ústreamlit.errorsr   Ú/streamlit.runtime.scriptrunner_utils.exceptionsr   r   Útypesr   Ú4streamlit.runtime.scriptrunner_utils.script_requestsr   Ú7streamlit.runtime.scriptrunner_utils.script_run_contextr   r   rB   r   r   r   r   Ú<module>   s   $