o
    wZh"(                     @   s   d dl mZ d dl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)OptionalNc                  c   s\   zd V  W d S  t y }  z| j}g }|d ur|jjj}|jjd}|dkr|d urtjdddd}|	| W d    n1 sDw   Y  |j}t
d|jd}|j|jjd	}t|jd
rl|j|jj|jjd}t||ji |jdtji}td ||j|j}	||	 n|| |j}|d usd }
t|D ]}|
|_|}
q| |
d } ~ ww )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+   E/var/www/auris/lib/python3.10/site-packages/torch/utils/_traceback.pyreport_compile_source_on_error/   sX   

F
r-   basec                C   s\   |du rt jt jt}z
t j| |g}W n ty#   |  Y S w | t|d d S )ziShorten a source filepath, with the assumption that torch/ subdirectories don't need to be shown to user.N   )ospathdirname__file__
commonpath
ValueErrorlen)fnr/   prefixr+   r+   r,   shorten_filename   s   r:   F)r/   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)   r/   r;   Z
extra_liner+   r+   r,   format_frame   s   &r?   c                 C   s   t t| d S )zJFormat a TracebackType in a short way, printing only the inner-most frame.)r?   	traceback
extract_tbr$   r+   r+   r,   format_traceback_short   s   rD   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$   rF   )selfr$   rF   r+   r+   r,   __init__   s   
zCapturedTraceback.__init__c                 C   s
   d | _ d S rG   rC   rI   r+   r+   r,   cleanup   s   
zCapturedTraceback.cleanupc                 C   s8   dd l }| jd u rt S t|jj| jgd | jS )Nr   )	torch._C._profilerr$   rA   StackSummary_extract_symbolized_tb_C	_profilersymbolize_tracebacksrF   )rI   torchr+   r+   r,   summary   s   
zCapturedTraceback.summaryc                 C   s   d d | j dfS )NrH   )rF   rK   r+   r+   r,   __getstate__   s   zCapturedTraceback.__getstate__F)scriptcpprF   c                 C   sL   ddl }| s|r|dksJ dt|jjjd| |d| s|r!dS |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)pythonrV   rW   r0   )rM   rE   rP   rQ   Zgather_traceback)rV   rW   rF   rS   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
        )rA   format_listrT   rK   r+   r+   r,   format   s   zCapturedTraceback.formatc                    s   ddl }g }g }t D ]\}}|jdu r|g  q|d || q|jj fdd|D  |D ]}t | 	 ||< q6|S )z_
        Bulk version of CapturedTraceback.format.  Returns a list of list of strings.
        r   Nc                    s   g | ]} | j qS r+   rC   ).0itbsr+   r,   
<listcomp>   s    z0CapturedTraceback.format_all.<locals>.<listcomp>)
rM   	enumerater$   r   rP   rQ   rR   rA   rZ   rT   )r_   rS   rsZdelayed_idxsr]   r$   r+   r^   r,   
format_all   s   

zCapturedTraceback.format_allN)r   )__name__
__module____qualname__	__slots__rJ   rL   rT   rU   staticmethodrY   r[   rc   r+   r+   r+   r,   rE      s    

rE   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   )rA   rN   r!   r   FrameSummary)r$   rF   r%   r(   r+   r+   r,   rO      s   "rO   )typesr   typingr   r   rA   
contextlibr   Zos.pathr1   contextmanagerr-   r:   r?   rD   rE   rO   r+   r+   r+   r,   <module>   s   '
VV