U
    •Ê¹jL(  ã                   @   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/bwave/python/venv/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