U
    9%eL(                     @   s   d dl mZ d dlmZmZ d dlZd dlZd dlZd dlZd dl	Z
ejdd ZddddZdd	d
ddZdd ZG dd dZdd ZdS )    )TracebackType)ListOptionalNc                  c   sV  zd V  W nD t k
rP }  z"| j}g }|d k	r|jjj}|jjd}|dkr|d k	rtjdddd}|	| W 5 Q R X |j}t
d|jd}|j|jjd	}t|jd
r|j|jj|jjd}t||j|jdtji}td ||j|j}	||	 n
|| |j}q,d }
t|D ]}|
|_|}
q$| |
W 5 d } ~ X Y nX d S )NZ__compile_source__z<string>wFz.py)modedeletesuffixz__inspect_currentframe()eval)co_nameco_linetable)r   co_firstlinenoZ__inspect_currentframe)	Exception__traceback__tb_framef_codeco_filename	f_globalsgettempfileNamedTemporaryFilewritecompilenamereplacer
   hasattrr   r   r	   f_localsinspectcurrentframer   tb_lasti	tb_linenoappendtb_nextreversedwith_traceback)exctbstackfilenamesourcefframecodeZ
fake_frameZfake_tbr!    r,   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/utils/_traceback.pyreport_compile_source_on_error.   sR    

    
r.   basec                C   sb   |dkrt jt jt}zt j| |g}W n tk
rH   |  Y S X | t|d d S dS )z
    Shorten a source filepath, under the assumption that anything under torch/
    directory is "obvious" and doesn't need to be shown to user.
    N   )ospathdirname__file__
commonpath
ValueErrorlen)fnr0   prefixr,   r,   r-   shorten_filename   s    
r;   F)r0   linec                C   s:   d}|r| j  d}| t| j|d d| j d| j S )z
    Format a FrameSummary in a short way, without printing full absolute path
    or code.  The idea is the result fits on a single line.
     z  # r/   :z in )r<   r;   r'   linenor   )r*   r0   r<   Z
extra_liner,   r,   r-   format_frame   s    r@   c                 C   s   t t| d S )zT
    Format a TracebackType in a short way, printing only the inner-most frame.
    )r@   	traceback
extract_tbr%   r,   r,   r-   format_traceback_short   s    rE   c                   @   s`   e Zd ZddgZdddZdd Zdd	 Zd
d ZeddddddZ	dd Z
edd ZdS )CapturedTracebackr%   skipr   c                 C   s   || _ || _d S Nr%   rG   )selfr%   rG   r,   r,   r-   __init__   s    zCapturedTraceback.__init__c                 C   s
   d | _ d S rH   rD   rJ   r,   r,   r-   cleanup   s    zCapturedTraceback.cleanupc                 C   s8   dd l }| jd krt S t|jj| jgd | jS )Nr   )	torch._C._profilerr%   rB   StackSummary_extract_symbolized_tb_C	_profilersymbolize_tracebacksrG   )rJ   torchr,   r,   r-   summary   s    
zCapturedTraceback.summaryc                 C   s   d d | j dfS )NrI   )rG   rL   r,   r,   r-   __getstate__   s    zCapturedTraceback.__getstate__F)scriptcpprG   c                 C   sJ   ddl }| s|r |dks tdt|jjjd| |d| s<|r@dn|d S )a  
        Like traceback.extract_stack(), but faster (approximately 20x faster); it
        is fast enough that you can unconditionally log stacks this way as part of
        normal execution.  It returns a torch._C._profiler.CapturedTraceback
        object that must be formatted specially with format_captured_tb.

        By default, this only reports Python backtraces (like extract_stack).  You
        can set the script/cpp kwargs to also turn on TorchScript/C++ trace
        reporting.
        r   Nzskip with script/cpp NYIT)pythonrW   rX   r1   )rN   AssertionErrorrF   rQ   rR   Zgather_traceback)rW   rX   rG   rT   r,   r,   r-   extract   s    zCapturedTraceback.extractc                 C   s   t |  S )al  
        Formats a single torch._C._profiler.CapturedTraceback into a list of
        strings equivalent to the output of traceback.format_list.  Note that if
        pass it CapturedTraceback with C++ traces,  it is better not to use this
        function and use the batch formatting API format_captured_tbs to amortize
        the cost of symbolization
        )rB   format_listrU   rL   r,   r,   r-   format   s    zCapturedTraceback.formatc                    s   ddl }g }g }t D ]2\}}|jdkr6|g  q|d || q|jj fdd|D }t||D ]\}}t	 | 
 ||< qr|S )z_
        Bulk version of CapturedTraceback.format.  Returns a list of list of strings.
        r   Nc                    s   g | ]} | j qS r,   rD   ).0itbsr,   r-   
<listcomp>   s     z0CapturedTraceback.format_all.<locals>.<listcomp>)rN   	enumerater%   r    rQ   rR   rS   ziprB   r\   rU   )ra   rT   rsZdelayed_idxsr_   r%   ZstbsZstbr,   r`   r-   
format_all   s    

zCapturedTraceback.format_allN)r   )__name__
__module____qualname__	__slots__rK   rM   rU   rV   staticmethodr[   r]   rf   r,   r,   r,   r-   rF      s   

rF   c              	   C   sB   t  }t| |d D ]$}|t |d |d |d  q|S )z
    Given a symbolized traceback from symbolize_tracebacks, return a StackSummary object of
    pre-processed stack trace entries.
    Nr'   r<   r   )rB   rO   r"   r    FrameSummary)r%   rG   r&   r)   r,   r,   r-   rP      s    "rP   )typesr   typingr   r   r   rB   
contextlibr   os.pathr2   contextmanagerr.   r;   r@   rE   rF   rP   r,   r,   r,   r-   <module>   s   '
V
V