o
    "jA                     @   s   d dl 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m	Z	m
Z
 ddlmZmZ g ZdZdZdZdZd ZdZd	ZdddZG dd de	ZG dd de	ZG dd dZG dd deZG dd dZdS )    N   )Location
OriginInfoglobal_origin_info_map)RE_PYMODULE_is_api_in_module_helperz@Error data about original source code information and traceback.ZTRANSLATOR_SIMPLIFY_NEW_ERRORZTRANSLATOR_DISABLE_NEW_ERROR      Fc                 C   sD   t  \}}}t|dd }t|||t}||_t| t| | S )a^  
    Attachs error data about original source code information and traceback to an error.

    Args:
        error(Exception): An native error.
        in_runtime(bool): `error` is raised in runtime if in_runtime is True, otherwise in compile time
    Returns:
        An error attached data about original source code information and traceback.
    r   N)	sysexc_info	traceback
extract_tb	ErrorDatar   
in_runtimesetattr
ERROR_DATA)errorr   Ze_typeZe_valueZe_tracebacktbZ
error_data r   [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/jit/dy2static/error.pyattach_error_data-   s   r   c                   @       e Zd ZdZdd Zdd ZdS )TraceBackFrame&
    Traceback frame information.
    c                 C   s   || _ || _|| _d| _d S )N )locationfunction_namesource_code
error_line)selfr   r   r   r   r   r   __init__H   s   
zTraceBackFrame.__init__c              	   C   s>   dt  d| jj| jj| jt| jtr| j	  S | j S )N zFile "{}", line {}, in {}
	{})
BLANK_COUNT_BEFORE_FILE_STRformatr   filepathlinenor   
isinstancer   strlstripr   r   r   r   formated_messageN   s   

zTraceBackFrame.formated_messageN__name__
__module____qualname____doc__r    r*   r   r   r   r   r   C   s    r   c                   @   r   )TraceBackFrameRanger   c           
      C   s  || _ || _g | _d| _g }td| j jttd  }t||t D ]N}t	
| j j|d}| }| j| |sA|d n|t|t|  || j jkrp| jd | _dt| jd  d }| j| ||d  q"t	  t| jdkr| jd s| jd |d t| jdkr| jd rt| jdkr| jd s| jd |d t| jdkr| jd rtd	d
 |D }	tt| jD ]}| j| rd|| |	 td   | j|  | j|< qd S )Nr   r      
~z
 <--- HEREr   c                 S   s   g | ]}|d kr|qS )r   r   .0ir   r   r   
<listcomp>   s    z0TraceBackFrameRange.__init__.<locals>.<listcomp>r!   )r   r   r   r   maxr%   intSOURCE_CODE_RANGErange	linecachegetliner$   rstripr(   appendlen
clearcachepopminr"   )
r   r   r   Zblank_countZbegin_linenor7   lineZline_lstripZhint_msgZmin_black_countr   r   r   r    c   sV   


zTraceBackFrameRange.__init__c                 C   s0   dt  d| jj| jj| j }|d| j S )Nr!   zFile "{}", line {}, in {}
r2   )r"   r#   r   r$   r%   r   joinr   )r   msgr   r   r   r*      s   z$TraceBackFrameRange.formated_messageNr+   r   r   r   r   r0   ^   s    -r0   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SuggestionDictc                 C   s   ddi| _ d S )N)zis not initialized.zHint:ZIsInitialized)z>Please ensure all your sublayers are inheritted from nn.Layer.zPlease ensure there is no tensor created explicitly depended on external data, we suggest to register it as buffer tensor. See https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/jit/principle_cn.html#buffers for detailssuggestion_dictr)   r   r   r   r       s   
zSuggestionDict.__init__c                 C   s
   | j  S N)rJ   keysr)   r   r   r   rL         
zSuggestionDict.keysc                 C   s
   | j | S rK   rI   )r   keyr   r   r   __getitem__   rM   zSuggestionDict.__getitem__N)r,   r-   r.   r    rL   rO   r   r   r   r   rH      s    rH   c                   @   s   e Zd ZdS )Dy2StKeyErrorN)r,   r-   r.   r   r   r   r   rP      s    rP   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )r   zU
    Error data attached to an exception which is raised in un-transformed code.
    c                 C   s*   || _ || _|| _|| _d| _t | _d S )NF)
error_typeerror_valueorigin_tracebackorigin_info_mapr   rH   rJ   )r   rQ   rR   rS   rT   r   r   r   r       s   zErrorData.__init__c                 C   s6   |   }| jtu rt|}n| |}t|t|  |S rK   )create_messagerQ   KeyErrorrP   r   r   )r   messagenew_exceptionr   r   r   create_exception   s   


zErrorData.create_exceptionc           	      C   s   | j tur|S | j}d }|D ]}tdt d|j |}|r&|d} nqztd	|d}|p;|
dp;|
d}W n tyH   d}Y nw |rU|rUd		||d
gS |S )N(z)*r   z"_is_api_in_module_helper({}, '{}')numpyznumpy.znp.FzNTypeError: Code '{}' called numpy API {}, please use Paddle API to replace it.z_           values will be changed to variables by dy2static, numpy api can not handle variables)rQ   	TypeErrorrS   researchr   namegroupevalr#   
startswith	Exception)	r   format_exceptionr   r   Zfunc_strframeZsearched_nameZmodule_resultZis_numpy_api_errr   r   r   numpy_api_check   s<   


zErrorData.numpy_api_checkc                 C   s  g }d}| | | d d}| jr4zttttr |   W n   Y n| t| j	 d
|S g }t| jD ]\}\}}}}	| j||fd}
|
rS| | q;|D ]5}| j| \}}}}	| j||fd}
||d krxt|
j|
j}n	t|
j|
j|
j}| |  |j}qV| d |r|d d nd}| j|d D ]\}}}}	tt||||	}| |  q| d t| j| j	}|dur| ||}dd	 |D }|| d
|S )
z~
        Creates a custom error message which includes trace stack with source code information of dygraph from user.
        zIn transformed code:r   Nr2   r3   r   r   c                 S   s   g | ]}d t  | qS r!   )r"   )r6   rE   r   r   r   r8   A  s    
z,ErrorData.create_message.<locals>.<listcomp>)r@   r   r:   osgetenvSIMPLIFY_ERROR_ENV_NAMEDEFAULT_SIMPLIFY_NEW_ERROR_simplify_error_valuer'   rR   rF   	enumeraterS   rT   getr0   r   r   r   r   r*   r   r   r   format_exception_onlyrQ   rf   extend)r   Zmessage_linesZheader_messager   user_code_traceback_indexr7   r$   r%   funcnamecodeZdygraph_func_infotraceback_framepaddle_traceback_start_indexrd   error_messager   r   r   rU      s   







zErrorData.create_messagec                    s   ddt  d g}| j D ]3} fdd|D }t|t|kr@| j| D ]}dt  d tt|d  d|  }|| q%qt|dkrI|S g S )	Nr   r!   zRevise suggestion: c                    s   g | ]}|d   v rdqS )r   T)rF   r5   bottom_error_messager   r   r8   O  s    z7ErrorData._create_revise_suggestion.<locals>.<listcomp>r1   r   z. )r"   rJ   rL   rA   r'   r@   )r   rx   Zrevise_suggestionskeywordsZcontain_keywordsZ
suggestionZsuggestion_msgr   rw   r   _create_revise_suggestionI  s$   


z#ErrorData._create_revise_suggestionc                 C   sB  | j du sJ t| jd}dd |D }d}||}||d d }||d d }|d}||d d }| |}g }g }	d	}
i }| j D ]\}}|jj	}|jj
}||||f< qLtd
t|dD ]C}|| drt|
|| }| \}}}|d t|k r||d  nd}||t|fr|	t| ||t|||f qgg }|	D ])}|| \}}}}||	d krtt|||}n	tt||||}||  q|d |	r|	d d nd
}||d D ]\}}}}tt||||}||  q|d || || d|}| || _dS )a  
        Simplifies error value to improve readability if error is raised in runtime.

        NOTE(liym27): The op callstack information about transformed static code has been replaced with original dygraph code.

        TODO(liym27):
            1. Need a more robust way because the code of start_trace may change.
            2. Set the switch to determine whether to simplify error_value
        Tr2   c                 S   s   g | ]}| d qS rg   )r(   )r6   Zmesr   r   r   r8   k  s    z3ErrorData._simplify_error_value.<locals>.<listcomp>zoutputs = static_func(*inputs)r   Nr   zFFile "(?P<filepath>.+)", line (?P<lineno>.+), in (?P<function_name>.+)r   r1   zFile r3   )r   r'   rR   splitindexrz   rT   itemsr   r$   r%   r<   rA   rb   r]   r^   groupsrn   r:   r@   r0   r   r   r*   rp   rF   rQ   )r   Zerror_value_linesZerror_value_lines_stripZstart_traceZ	start_idxZempty_line_idxrx   Zrevise_suggestionZerror_tracebackrq   patternZstatic_info_mapkvZorigin_filepathZorigin_linenor7   Z	re_resultZtmp_filepathZ
lineno_strr   rs   Zerror_framer$   r%   rr   rt   ru   Zerror_value_strr   r   r   rl   ]  sx   








zErrorData._simplify_error_valuec                 C   s    t tttr	 |  }|d rK   )r:   rh   ri   DISABLE_ERROR_ENV_NAMEDEFAULT_DISABLE_NEW_ERRORrY   )r   rX   r   r   r   raise_new_exception  s   zErrorData.raise_new_exceptionN)r,   r-   r.   r/   r    rY   rf   rU   rz   rl   r   r   r   r   r   r      s    
	"]Zr   )F)r=   rh   r]   r
   r   r[   npZorigin_infor   r   r   utilsr   r   __all__r   rj   rk   r   r   r;   r"   r   r   r0   rH   rc   rP   r   r   r   r   r   <module>   s,   
=