
    ϑibB                         S SK r S SKrS SKrS SKrS SKrS SKrSSKJrJ	r	J
r
  SSKJrJr  / rSrSrSrSrS rSrS	rSS
 jr " S S\	5      r " S S\	5      r " S S5      r " S S\5      r " S S5      rg)    N   )Location
OriginInfoglobal_origin_info_map)RE_PYMODULEis_api_in_module_helperz@Error data about original source code information and traceback.TRANSLATOR_SIMPLIFY_NEW_ERRORTRANSLATOR_DISABLE_NEW_ERROR      c                     [         R                  " 5       u  p#n[        R                  " U5      SS n[	        X#U[
        5      nXl        [        U [        U5        U $ )aC  
Attaches 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   e_typee_valuee_tracebacktb
error_datas          Z/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/jit/dy2static/error.pyattach_error_datar   -   sQ     $'<<> F[			k	*12	.B6B0FGJ&E:z*L    c                   $    \ rS rSrSrS rS rSrg)TraceBackFrameC   
Traceback frame information.
c                 6    Xl         X l        X0l        SU l        g )N )locationfunction_namesource_code
error_line)selfr%   r&   r'   s       r   __init__TraceBackFrame.__init__H   s     *&r   c           
      4   S[         -  SR                  U R                  R                  U R                  R                  U R
                  [        U R                  [        5      (       a   U R                  R                  5       5      -   $ U R                  5      -   $ )N zFile "{}", line {}, in {}
	{})
BLANK_COUNT_BEFORE_FILE_STRformatr%   filepathlinenor&   
isinstancer'   strlstripr)   s    r   formatted_message TraceBackFrame.formatted_messageN   s     --/66&&$$"" "$"2"2C88 $$++-	
	
 ))	
	
r   r(   r&   r%   r'   N__name__
__module____qualname____firstlineno____doc__r*   r6   __static_attributes__ r   r   r    r    C   s    
r   r    c                   $    \ rS rSrSrS rS rSrg)TraceBackFrameRange`   r"   c                 `   Xl         X l        / U l        SU l        / n[	        SU R                   R
                  [        [        S-  5      -
  5      n[        XD[        -   5       GH$  n[        R                  " U R                   R                  U5      R                  S5      nUR                  5       nU R                  R                  U5        U(       d  UR                  S5        O&UR                  [        U5      [        U5      -
  5        XPR                   R
                  :X  d  M  U R                  S   U l        S[        U R                  S   5      -  S-   nU R                  R                  U5        UR                  US   5        GM'     [        U R                  5      S:  ao  U R                  S   (       d[  U R                  R!                  S5        UR!                  S5        [        U R                  5      S:  a  U R                  S   (       d  M[  [        U R                  5      S:  ao  U R                  S   (       d[  U R                  R!                  S5        UR!                  S5        [        U R                  5      S:  a  U R                  S   (       d  M[  [#        U Vs/ s H  oUS:  d  M
  UPM     sn5      n	[        [        U R                  5      5       HJ  nU R                  U   (       d  M  S	X5   U	-
  [$        S-  -   -  U R                  U   -   U R                  U'   ML     g s  snf )
Nr$   r      
~z
 <--- HEREr   r-   )r%   r&   r'   r(   maxr1   intSOURCE_CODE_RANGErange	linecachegetliner0   rstripr4   appendlenpopminr.   )
r)   r%   r&   blank_countbegin_linenoilineline_lstriphint_msgmin_black_counts
             r   r*   TraceBackFrameRange.__init__e   s    *1dmm22S9JQ9N5OOP|4E%EFA$$T]]%;%;Q?FFtLD++-K##K0""2&""3t9s;/?#?@MM((("&"2"22"6T%5%5b%9!::\I  ''1"";r?3 G( $""#a'0@0@0C  #OOA $""#a'0@0@0C0C $""#a'0@0@0D  $OOB $""#a'0@0@0D0D +@+Qaq+@As4++,-A""#)*59: &&q)*   # . As   $	L+1L+c                     S[         -  SU R                  R                   SU R                  R                   SU R                   S3-   nUSR                  U R                  5      -   $ )Nr-   zFile "z", line z, in rF   )r.   r%   r0   r1   r&   joinr'   )r)   msgs     r   r6   %TraceBackFrameRange.formatted_message   si    --t}}--.ht}}7K7K6LERVRdRdQeeghi 	
 TYYt//000r   r8   Nr9   r@   r   r   rB   rB   `   s    /b1r   rB   c                   &    \ rS rSrS rS rS rSrg)SuggestionDict   c                     SS0U l         g )N)zis not initialized.zHint:IsInitialized)z=Please ensure all your sublayers are inherited 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_dictr5   s    r   r*   SuggestionDict.__init__   s     > @ 
r   c                 6    U R                   R                  5       $ N)rf   keysr5   s    r   rj   SuggestionDict.keys   s    ##((**r   c                      U R                   U   $ ri   re   )r)   keys     r   __getitem__SuggestionDict.__getitem__   s    ##C((r   re   N)r:   r;   r<   r=   r*   rj   rn   r?   r@   r   r   ra   ra      s    	
+)r   ra   c                       \ rS rSrSrg)Dy2StKeyError   r@   N)r:   r;   r<   r=   r?   r@   r   r   rq   rq      s    r   rq   c                   B    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
rg)r      zM
Error data attached to an exception which is raised in un-transformed code.
c                 `    Xl         X l        X0l        X@l        SU l        [        5       U l        g )NF)
error_typeerror_valueorigin_tracebackorigin_info_mapr   ra   rf   )r)   rv   rw   rx   ry   s        r   r*   ErrorData.__init__   s-     %& 0.-/r   c                     U R                  5       nU R                  [        L a  [        U5      nOU R                  U5      n[	        U[
        U 5        U$ ri   )create_messagerv   KeyErrorrq   r   r   )r)   messagenew_exceptions      r   create_exceptionErrorData.create_exception   sF    %%'??h&)'2M OOG4Mz40r   c                    U R                   [        La  U$ U R                  nS nU HH  n[        R                  " S[
         SUR                   3U5      nU(       d  M7  UR                  S5      n  O    S[        0n[        XG5      n[        US5      n	U	=(       d)    UR                  S5      =(       d    UR                  S5      n
U
(       a  U(       a  S	U S
U S3S/$ U$ ! [         a    Sn
 N)f = f)N(z)*r   npnumpyznumpy.znp.FzTypeError: Code 'z' called numpy API z&, please use Paddle API to replace it.z_           values will be changed to variables by dy2static, numpy api can not handle variables)rv   	TypeErrorrx   researchr   namegroupr   evalr   
startswith	Exception)r)   format_exceptionr(   r   func_strframesearched_nameglobalsfnmodule_resultis_numpy_api_errs              r   numpy_api_checkErrorData.numpy_api_check   s   ??)+##""EII[MEJJ<0M }(..q1 	%RjGh(B3B@M,  ##H-K1D1DU1K  #J</B8*Lrsq 
 $#  	%$	%s   3AC C.-C.c                    / nSnUR                  U5        UR                  S5        SnU R                  (       as   [        [        R                  " [
        [        5      5      (       a  U R                  5         UR                  [        U R                  5      5        SR                  U5      $ / n[        U R                  5       H@  u  nu  pgpU R                  R                  Xg4S5      n
U
(       d  M/  UR                  U5        MB     U H  nU R                  U   u  pgpU R                  R                  Xg4S5      n
XTS   :X  a!  [        U
R                   U
R"                  5      nO+[%        U
R                   U
R"                  U
R&                  5      nUR                  UR)                  5       5        UR*                  nM     UR                  S5        U(       a  US   S-   OSnU R                  US  H:  u  pgp[%        [-        Xg5      X5      nUR                  UR)                  5       5        M<     UR                  S5        [.        R0                  " U R2                  U R                  5      nUb  U R5                  X5      nU Vs/ s H  nS[6        -  U-   PM     nnUR9                  U5        SR                  U5      $ !    GN= fs  snf )	zn
Creates a custom error message which includes trace stack with source code information of dygraph from user.
zIn transformed code:r$   NrF   rG   r   r   r-   )rP   r   rJ   osgetenvSIMPLIFY_ERROR_ENV_NAMEDEFAULT_SIMPLIFY_NEW_ERROR_simplify_error_valuer3   rw   r]   	enumeraterx   ry   getrB   r%   r&   r    r'   r6   r(   r   r   format_exception_onlyrv   r   r.   extend)r)   message_linesheader_messager(   user_code_traceback_indexrV   r0   r1   funcnamecodedygraph_func_infotraceback_framepaddle_traceback_start_indexr   rW   error_messages                   r   r|   ErrorData.create_message   s     0^,R 
 ??0II/1K 
 ..0 $$S)9)9%:;yy// %'!5>!!6
1A1( !% 4 4 8 8"D! ! )0036
 +A/3/D/DQ/G,Hh $ 4 4 8 8"D! b11"5%..0A0O0O# #1%..%33%11#   !B!B!DE(33J# +$ 	R 
 ) &b)A- 	%
 150E0E()1
,Hh -*HO   !B!B!DE1
 	R 
 %::OOT--
 !#33   )
( --4( 	 
 	]+yy''M~
s   =J< K<Kc                    SS[         -  S-   /nU R                  R                  5        H  nU Vs/ s H  oDSR                  U5      ;   d  M  SPM     nn[	        U5      [	        U5      :X  d  ME  U R                  U    H4  nS[         -  S-  [	        U5      S-
   SU 3-   nUR                  U5        M6     M     [	        U5      S:  a  U$ / $ s  snf )Nr$   r-   zRevise suggestion: TrE   r   z. )r.   rf   rj   r]   rQ   rP   )r)   bottom_error_messagerevise_suggestionskeywordsrV   contain_keywords
suggestionsuggestion_msgs           r   _create_revise_suggestion#ErrorData._create_revise_suggestionK  s    --0EE
 ,,113H& &rww7K/L*Lh    #$)  #'"6"6x"@J99A= !34q89J<HI # '--n= #A 4 &));%<q%@!HbH s   CCc                    U R                   SL d   e[        U R                  5      R                  S5      nU Vs/ s H  o"R	                  S5      PM     nnSnUR                  U5      nXS-   S nX5S-   S nUR                  S5      nXS-   S nU R                  U5      n/ n	/ n
Sn0 nU R                  R                  5        H7  u  pUR                  R                  nUR                  R                  nXUU4'   M9     [        S	[        U5      S
5       H  nUU   R                  S5      (       d  M  [        R                   " XU   5      nUR#                  5       u  nnnUS-   [        U5      :  a  UUS-      OSnUR%                  U['        U5      45      (       a  U
R)                  [        U	5      5        U	R)                  U['        U5      UU45        M     / nU
 Hc  nU	U   u  nnnnUU
S   :X  a  [+        [-        UU5      U5      nO[/        [-        UU5      UU5      nUR)                  UR1                  5       5        Me     UR)                  S5        U
(       a  U
S   S-   OS	nU	US  H>  u  nnnn[/        [-        UU5      UU5      nUR)                  UR1                  5       5        M@     UR)                  S5        UR3                  U5        UR3                  U5        SR5                  U5      nU R7                  U5      U l        gs  snf )ab  
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
TrF   r-   zoutputs = static_func(*inputs)r   Nr$   zFFile "(?P<filepath>.+)", line (?P<lineno>.+), in (?P<function_name>.+)r   rE   zFile rG   )r   r3   rw   splitr4   indexr   ry   itemsr%   r0   r1   rL   rQ   r   r   r   groupsr   rJ   rP   rB   r   r    r6   r   r]   rv   )r)   error_value_linesmeserror_value_lines_stripstart_trace	start_idxempty_line_idxr   revise_suggestionerror_tracebackr   patternstatic_info_mapkvorigin_filepathorigin_linenorV   	re_resulttmp_filepath
lineno_strr&   r   error_framer0   r1   r   r   r   error_value_strs                                 r   r   ErrorData._simplify_error_value_  s3    $&&& 0 0177=>O"P>Os::c?>O"P6+11+>	-!mo>"9a-/"J 166r:0!1C1EF ::;OP$&!Z ((..0DAjj11OJJ--M@A_m<= 1
 q#56:A&q)44W==IIgq/IJ	:C:J:J:L7j- 1us#:;; ,AE2  #&&c*o'FGG-44S5IJ&&!3z?M4H ;" *A/>q/A,Hfh-b11"5Xv.# #1Xv.$# @@BC + 	2
 ) &b)A- 	%
 1@()1
,Hfh -6*HdO @@BC1
 	2/0,-))K0???;U #Qs   Lc                     [        [        R                  " [        [        5      5      (       a  U R
                  eU R                  5       nUS eri   )rJ   r   r   DISABLE_ERROR_ENV_NAMEDEFAULT_DISABLE_NEW_ERRORrw   r   )r)   r   s     r   raise_new_exceptionErrorData.raise_new_exception  s>    ryy/1JKLL"""--/ %r   )rv   rw   r   ry   rx   rf   N)r:   r;   r<   r=   r>   r*   r   r   r|   r   r   r   r?   r@   r   r   r   r      s0    0$@[(zI(X<t&r   r   )F)rM   r   r   r   r   r   r   origin_infor   r   r   utilsr   r   __all__r   r   r   r   r   rK   r.   r   r    rB   ra   r   rq   r   r@   r   r   <module>r      s     	 	 
   E E
 O
 :   8    ,
Z 
:<1* <1~) )&	I 	P& P&r   