o
    h"(                     @   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 )N__compile_source__z<string>wFz.py)modedeletesuffixz__inspect_currentframe()eval)co_nameco_linetable)r   co_firstlineno__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code
fake_framefake_tbr"    r/   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/utils/_traceback.pyreport_compile_source_on_error/   sX   

F
r1   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)fnr3   prefixr/   r/   r0   shorten_filename   s   r>   F)r3   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  # r2   :z in )r?   r>   r(   linenor   )r+   r3   r?   
extra_liner/   r/   r0   format_frame   s   &rD   c                 C   s   t t| d S )zJFormat a TracebackType in a short way, printing only the inner-most frame.)rD   	traceback
extract_tbr&   r/   r/   r0   format_traceback_short   s   rI   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&   rK   )selfr&   rK   r/   r/   r0   __init__   s   
zCapturedTraceback.__init__c                 C   s
   d | _ d S rL   rH   rN   r/   r/   r0   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&   rF   StackSummary_extract_symbolized_tb_C	_profilersymbolize_tracebacksrK   )rN   torchr/   r/   r0   summary   s   
zCapturedTraceback.summaryc                 C   s   d d | j dfS )NrM   )rK   rP   r/   r/   r0   __getstate__   s   zCapturedTraceback.__getstate__F)scriptcpprK   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)pythonr[   r\   r4   )rR   rJ   rU   rV   gather_traceback)r[   r\   rK   rX   r/   r/   r0   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
        )rF   format_listrY   rP   r/   r/   r0   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/   rH   ).0itbsr/   r0   
<listcomp>   s    z0CapturedTraceback.format_all.<locals>.<listcomp>)
rR   	enumerater&   r!   rU   rV   rW   rF   r`   rY   )re   rX   rsdelayed_idxsrc   r&   r/   rd   r0   
format_all   s   

zCapturedTraceback.format_allN)r   )__name__
__module____qualname__	__slots__rO   rQ   rY   rZ   staticmethodr_   ra   rj   r/   r/   r/   r0   rJ      s    

rJ   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   )rF   rS   r#   r!   FrameSummary)r&   rK   r'   r*   r/   r/   r0   rT      s   "rT   )typesr   typingr   r   rF   
contextlibr   os.pathr5   contextmanagerr1   r>   rD   rI   rJ   rT   r/   r/   r/   r0   <module>   s   '
VV