o
    ;i^                     @   sT  d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ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eef Zeeeef ef Zd
ededeeef fddZd
edededdfddZde
e dedefddZde
e defddZd
e
ee  de
e de
e fddZ de	ej! fddZ"dZ#G d d! d!Z$dS )"zHelper functions related to operating on exceptions, warnings, and traceback objects.

Functions related to *displaying* tracebacks should go in `modal/cli/_traceback.py`
so that Rich is not a dependency of the container Client.
    N)TracebackType)AnyIterableOptional)config)api_pb2   )	Traceback)ServerWarningexctask_idreturnc                 C   s   t | j}|}|dur(|jjj}|drnd| d| |jj_|j}|dus| }t| di }t	
| jD ]}|j||j|jf< q8||fS )a6  Given an exception, extract a serializable traceback (with task ID markers included),
    and a line cache that maps (filename, lineno) to line contents. The latter is used to show
    a helpful traceback to the user, even if they don't have packages installed locally that
    are referenced in the traceback.N<z>:__line_cache__)TBLibTraceback__traceback__tb_framef_codeco_filename
startswithtb_nextto_dictgetattr	traceback
extract_tblinefilenamelineno)r   r   tbcurfiletb_dict
line_cacheframe r$   D/home/ubuntu/.local/lib/python3.10/site-packages/modal/_traceback.pyextract_traceback   s   


r&   r!   r"   c                 C   sJ   t | }|d urd|jjjvrn|j}|d us|| _t| d| d S )Nz/pkg/modal/r   )	r   	from_dictas_tracebackr   r   r   r   r   setattr)r   r!   r"   r   r$   r$   r%   append_modal_tb<   s   r*   r   user_sourcec                 C   s   dt dtfdd}| }| dur/| jdur(|| jjjjr"| jj| _nn| jdus| j} | dus|} |drP| durO| jjj|krO| j} | durO| jjj|ksAn| duri| jjjdkri| j} | duri| jjjdks[| du ro|} | S )zRReturn a traceback that does not contain modal entrypoint or synchronicity frames.r   r   c                 S   s   d| v pd| v S )Nz/site-packages/synchronicity/zmodal/_utils/deprecationr$   )r   r$   r$   r%   
skip_frameQ   s   z1reduce_traceback_to_user_code.<locals>.skip_frameNz.pyz<module>)strboolr   r   r   r   endswithco_name)r   r+   r,   tb_rootr$   r$   r%   reduce_traceback_to_user_codeM   s,   


r2   c                 C   s0   | durt d| jjjrdS | j} | dusdS )zYInspect the traceback stack to determine whether an error was raised locally or remotely.Nz^<ta-[0-9A-Z]{26}>:TF)rematchr   r   r   r   )r   r$   r$   r%   traceback_contains_remote_calll   s   r5   valuec                 C   sJ   t | || tjdk r!|dur#t|dg }t|dtjd dS dS dS )zQAdd backwards compatibility for printing exceptions with "notes" for Python<3.11.)      N	__notes__
)sepr    )r   print_exceptionsysversion_infor   printstderr)r   r6   r   notesr$   r$   r%   r<   u   s
   r<   server_warningsc                 C   s    | D ]}t |jtdd qdS )zIssue a warning originating from the server with empty metadata about local origin.

    When using the Modal CLI, these warnings should get caught and coerced into Rich panels.
    z<modal-server>r   N)warningswarn_explicitmessager
   )rB   warningr$   r$   r%   print_server_warnings}   s   rG   zoInternal Modal traceback frames are suppressed for readability. Use MODAL_TRACEBACK=1 to show a full traceback.c                	   @   sD   e Zd Zdd Zdeee  dee dee dej	d fdd	Z
d
S )suppress_tb_framec                 C   s   d S )Nr$   )selfr$   r$   r%   	__enter__   s   zsuppress_tb_frame.__enter__exc_typer   r   r   Fc                 C   sd   t |t }tds|d u s|rdS |d ur|jn|}|| t|dg }t|vr0|t dS )Nr   Fr9   )	
isinstance	Exceptionr   getr   with_tracebackr   traceback_suppression_noteappend)rI   rK   r   r   is_base_exceptionfinal_tbrA   r$   r$   r%   __exit__   s   

zsuppress_tb_frame.__exit__N)__name__
__module____qualname__rJ   r   typeBaseExceptionr   typingLiteralrT   r$   r$   r$   r%   rH      s    
rH   )%__doc__r3   r=   r   rZ   rC   typesr   r   r   r   modal.configr   modal_protor   _vendor.tblibr	   r   	exceptionr
   dictr-   
TBDictTypetupleLineCacheTyperY   r&   r*   r2   r.   r5   rX   r<   WarningrG   rP   rH   r$   r$   r$   r%   <module>   s.   "&	