
    BVhA              	           d Z ddlZddlmZ ddlmZ  G d d ej                  dd            Zd Ze	e
eeeeeeef	Z G d	 d
e      Zej*                  e_         G d de      Zy)zCode transformation exceptions.    N)origin_info)traceback_utilsc                       e Zd ZdZy)	FrameInfo N)__name__
__module____qualname__	__slots__r       \/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/pyct/error_utils.pyr   r      s	    
 )r   r   filenamelinenofunction_namecodeis_convertedis_allowlistedc           	      T   g }t        |       D ]  \  }}}}t        j                  ||      }||v ro||   }	t        |	j                  j
                  |	j                  j                  |	j                  |	j                  dd      }
|j                  |
        t        |      S ||k(  rT|rQ|d   }|j                  rJ t        |j
                  |j                  |j                  |j                  dd      }
|
|d<   t        ||||dd      }
|j                  |
        t        |      S )a`  Summarizes inner traceback frames up to the call to a given function.

  This functions locates the innermost (i.e. most recent) frame that corresponds
  to code that can be mapped by source_map originated from, and returns a
  translated stack trace ending at that frame. If no such frame is found, the
  entire stack trace is summarized.

  For example, the following code:

    def f():
      for i in tf.range(1):
        z = y + i  # z only defined here

  Would generate this traceback:

    <converted code>
        ag__.for_stmt(...)
    <for_stmt>
        return _known_len_tf_for_stmt(iter_, extra_test, body, init_state)
    <_known_len_tf_for_stmt>
        _disallow_undefs_into_loop(*init_state)
    <_disallow_undefs_into_loop>
        raise ...

  Which is then processed into:

    <f>
        for i in tf.range(1):
    <for_stmt>
        return _known_len_tf_for_stmt(iter_, extra_test, body, init_state)
    <_known_len_tf_for_stmt>
        _disallow_undefs_into_loop(*init_state)
    <_disallow_undefs_into_loop>
        raise ...

  Args:
    tb: traceback.FrameSummary, The traceback corresponding to an error.
      Typically, the output of traceback.Summary.extract(capture_locals=True).
    source_map: Dict[LineLocation, OriginInfo], a source map as created by
      origin_info.create_source_map.
    converter_filename: str, the file path of the converted module. Call frames
      corresponding to this module are elided and their preceding frames are
      marked as allowlisted. Note that frames enclosing converted code are
      dropped using a different mechanism.

  Returns:
    List[FrameInfo]
  )r   r   TFr   )reversedr   LineLocationr   locr   r   r   source_code_lineappendr   r   tuple)tb
source_mapconverter_filenameresult_framesr   line_numberr   textr   originfiprevs               r   _stack_trace_inside_mapped_coder&      sD   b -4<RL $0h]D

"
"H[
IC
j#f::&&"",,&& b 22 
}	/ %%	R $$$$]];;,,! b	#
B I$L 
}	r   c                   $     e Zd Z fdZd Z xZS )MultilineMessageKeyErrorc                 :    t         t        |   |       || _        y N)superr(   __init__"_MultilineMessageKeyError__message)selfmessageoriginal_key	__class__s      r   r,   z!MultilineMessageKeyError.__init__   s    	
"D2<@DNr   c                     | j                   S r*   )r-   )r.   s    r   __str__z MultilineMessageKeyError.__str__   s    >>r   )r   r	   r
   r,   r3   __classcell__)r1   s   @r   r(   r(      s    r   r(   c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)ErrorMetadataBasea  Container objects attached to exceptions raised in user code.

  This metadata allows re-raising exceptions that occur in generated code, with
  a custom error message that includes a stack trace relative to user-readable
  code from which the generated code originated.
  )translated_stackcause_messagec                     t        |||      }||| _        || _        y |j                  |d   fz   | _        |j                  | _        y )Nr   )r&   r7   r8   )r.   callsite_tbcause_metadatar8   r   r   r7   s          r   r,   zErrorMetadataBase.__init__   s\    6Z!35 .d(d 
)
)-=b-A,C
C )77dr   c                 8   g }|j                  d       |j                  d       t        | j                        D ]  }t        j                         r t        j
                  |j                        s7d|j                   d|j                   d|j                   }|j                  r|dz  }n|j                  r|dz  }|j                  |       |j                  d}n|j                  j                         }|j                  d	j                  |              |j                  d       | j                  j                  d
      }t!        t#        |            D ]  }d||   z   ||<    |j%                  |       |j                  d       d
j'                  |      S )z1Returns the message for the underlying exception.zin user code: z
    File "z", line z, in z  *z  **z<source unavailable>z
        {}
z    )r   r   r7   r   is_traceback_filtering_enabledinclude_framer   r   r   r   r   r   stripformatr8   splitrangelenextendjoin)r.   lines
frame_infoformatted_linecode_snippetmessage_linesis          r   get_messagezErrorMetadataBase.get_message   sq   E	LL!	LLt445 6


8
8
:++J,?,?@ %Z%8%8$9%,,-U:3K3K2LNn		 	 %$$& ll>"		 -!,,.ll<&&|45%6( 
LL&&,,T2M3}%& 3-"22mA3	LL	LL99Ur   c                 R   t        |      }d}|j                  t        j                  u r || j                               }|t        v r || j                               }n,|t
        u r$t        | j                         | j                        }||j                  |j                        S y)z$Creates exception from source_error.N)
typer,   	ExceptionrN   KNOWN_STRING_CONSTRUCTOR_ERRORSKeyErrorr(   r8   with_traceback__traceback__)r.   source_errorpreferred_typeto_rets       r   create_exceptionz"ErrorMetadataBase.create_exception   s    ,'NF)"4"44d..01f88d..01f	8	#'(8(8(:D<N<NOf""<#=#=>> r   c                 D    | j                  |      }d|_        | |_        |S )NT)rY   __suppress_context__ag_error_metadata)r.   rV   excs      r   to_exceptionzErrorMetadataBase.to_exception   s'    



-C#C CJr   N)	r   r	   r
   __doc__r   r,   rN   rY   r^   r   r   r   r6   r6      s#     4)8$L?r   r6   )r_   collections tensorflow.python.autograph.pyctr   tensorflow.python.utilr   
namedtupler   r&   AssertionErrorAttributeError	NameErrorNotImplementedErrorRuntimeErrorStopIteration	TypeErrorUnboundLocalError
ValueErrorrR   rS   r(   r   objectr6   r   r   r   <module>rn      s    &  8 2K;>?Xx 
#  x  %-$5$5  !P Pr   