
    BVh&                     r   d Z 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 ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ  G d d	 ej                  d	d
            Z G d d ej                  dd            Z G d d ej                  dd            Zd Z G d d      Z G d dej(                        Zd Zd Zd Zy)zJContainer for origin source code information before AutoGraph compilation.    N)anno)ast_util)parser)pretty_printer)
tf_inspectc                       e Zd ZdZy)LineLocationzrSimilar to Location, but without column information.

  Attributes:
    filename: Text
    lineno: int, 1-based
  N)__name__
__module____qualname____doc__     \/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/pyct/origin_info.pyr	   r	      s     r   r	   )filenamelinenoc                        e Zd ZdZed        Zy)LocationzEncodes code location information.

  Attributes:
    filename: Text
    lineno: int, 1-based
    col_offset: int
    line_loc: LineLocation
  c                 B    t        | j                  | j                        S N)r	   r   r   selfs    r   line_loczLocation.line_loc5   s    t{{33r   N)r
   r   r   r   propertyr   r   r   r   r   r   *   s     4 4r   r   )r   r   
col_offsetc                       e Zd ZdZd Zd Zy)
OriginInfozContainer for information about the source code before conversion.

  Attributes:
    loc: Location
    function_name: Optional[Text]
    source_code_line: Text
    comment: Optional[Text]
  c                     | j                   j                  | j                   j                  | j                  | j                  fS )zEReturns a 4-tuple consistent with the return of traceback.extract_tb.)locr   r   function_namesource_code_liner   s    r   as_framezOriginInfo.as_frameG   s4    HHtxx0B0B!!# #r   c                    | j                   j                  rodj                  t        j                  j                  | j                   j                        d   | j                   j                  | j                   j                        S dj                  | j                   j                  | j                   j                        S )Nz{}:{}:{}   z<no file>:{}:{})r   r   formatospathsplitr   r   r   s    r   __repr__zOriginInfo.__repr__L   s    xx
''--))
*1
-txx
((

  ##DHHOOTXX5H5HIIr   N)r
   r   r   r   r"   r)   r   r   r   r   r   :   s    #
Jr   r   )r   r    r!   commentc           	         t        j                  |dd      }|D ]%  }t        ||||j                  |j                         ' i }	 t        j                  | |      D ]B  \  }}t        j                  |t        j                  j                  d      }t        j                  |t        j                  j                  d      }	||	lt        |	j                  j                  |	j                  j                        }
|j                  |
      }||j                  j                  |j                  j                  k(  r/|j                  j                  |j                  j                  k\  r|j                  j                  |j                  j                  k  r>|||
<   E 	 |S # t         $ r}d}|t#        |      z  }|dz  }t%        | |      D ]  \  }}t'        j(                  |dd	      }t'        j(                  |dd	      }t+        j,                  |j/                  d
      |j/                  d
      ddd      }d
j1                  |      }||d
z   z  } t!        |      d}~ww xY w)a  Creates a source map between an annotated AST and the code it compiles to.

  Note: this function assumes nodes nodes, code and filepath correspond to the
  same code.

  Args:
    nodes: Iterable[ast.AST, ...], one or more AST modes.
    code: Text, the source code in which nodes are found.
    filepath: Text

  Returns:
    Dict[LineLocation, OriginInfo], mapping locations in code to locations
    indicated by origin annotations in node.
  r   F)preamble_lensingle_nodeNdefaultz3Inconsistent ASTs detected. This is a bug. Cause: 
zDiff:
T)colornoanno
zOriginal nodeszReparsed nodes   )fromfiletofilen)r   parseresolver   r   r   parallel_walkr   getannoBasicORIGINr	   r   r   getr   
ValueErrorstrzipr   fmtdifflibcontext_diffr(   join)nodescodefilepathreparsed_nodesnode
source_mapbeforeafterorigin_info
final_infor   existing_originerrnew_msgr6   rn	nodes_strreparsed_nodes_strdiffs                      r   create_source_maprV   U   s/    <<1%H. @dD$$++t?@ *-!//~F ) LL):):DIk<<tzz'8'8$Gj		
 2 jnn55z~~7L7LMh"x0o		$ '';??+C+CC  '';??+A+AA ))[__-G-GG
(j5)\ 
% 
 DGs3xGyGUN+ 
2 $$QeDAi)--bdK!!
//$


"
"4
(#!d YYt_dg
 W
!s   EF% %	I(.B5I##I(c                       e Zd Zd Zy)	_Functionc                     || _         y r   )name)r   rZ   s     r   __init__z_Function.__init__   s	    DIr   N)r
   r   r   r[   r   r   r   rX   rX      s    r   rX   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)OriginResolverzCAnnotates an AST with additional source information like file name.c                 2   || _         || _        t        |d      rG|j                  r;t        |j                  d   d      r"||j                  d   j                  z
  | _        n||j                  z
  | _        ||j                  z
  | _        || _        g | _	        y )Ndecorator_listr   r   )
_source_lines_comments_maphasattrr_   r   _lineno_offsetr   _col_offset	_filepath_function_stack)r   	root_nodesource_linescomments_mapcontext_linenocontext_col_offsetrG   s          r   r[   zOriginResolver.__init__   s     &D%D	+,1I1I	((+X6 +Y-E-Ea-H-O-OOd +Y-=-==d)I,@,@@DDNDr   c                      || j                   z   S r   )rc   )r   r   s     r   _absolute_linenozOriginResolver._absolute_lineno   s    D''''r   c                 &    |y|| j                   z   S )Nr   )rd   )r   r   s     r   _absolute_col_offsetz#OriginResolver._absolute_col_offset   s    ((((r   c                    t        |dd       }t        |dd       }|y | j                  r| j                  d   j                  }nd }| j                  |dz
     }| j                  j                  |      }t        | j                  | j                  |      | j                  |            }t        ||||      }t        j                  |d|       t        j                  |t        j                  j                  |       y )Nr   r   r$   )getattrrf   rZ   r`   ra   r=   r   re   rm   ro   r   r   setannor;   r<   )	r   rI   r   r   r    r!   r*   r   origins	            r   _attach_origin_infoz"OriginResolver._attach_origin_info   s    T8T*F|T2J~**2.33mm))&1*5  $$V,G
4>>4#8#8#@,,Z8:C],<gFFLLx(LLtzz((&1r   c                    d}t        |t        j                        r0d}| j                  j	                  t        |j                               | j                  |       | j                  |       |r| j                  j                          y y )NFT)

isinstancegastFunctionDefrf   appendrX   rZ   ru   generic_visitpop)r   rI   entered_functions      r   visitzOriginResolver.visit   sq    $(()
!!)DII"67T"t
  r   N)	r
   r   r   r   r[   rm   ro   ru   r~   r   r   r   r]   r]      s    K,()
2*
!r   r]   c                    t        j                  |      }i }	 t        j                  |j                        D ]G  }|\  }}	}
}}|
\  }}|t        j
                  k(  s$|	j                         dd j                         ||<   I 	 |j                  d      }t        | |||||      }|j                  |        y# t        j                  $ r t        | t        j                        rn Y dw xY w)a3  Adds origin information to an AST, based on the source it was loaded from.

  This allows us to map the original source code line numbers to generated
  source code.

  Note: the AST may be a part of a larger context (e.g. a function is part of
  a module that may contain other things). However, this function does not
  assume the source argument contains the entire context, nor that it contains
  only code corresponding to node itself. However, it assumes that node was
  parsed from the given source code.
  For this reason, two extra arguments are required, and they indicate the
  location of the node in the original context.

  Args:
    node: gast.AST, the AST to annotate.
    source: Text, the source code representing node.
    context_filepath: Text
    context_lineno: int
    context_col_offset: int
  r$   Nr2   )ioStringIOtokenizegenerate_tokensreadlineCOMMENTstrip
TokenErrorrw   rx   Lambdar(   r]   r~   )rI   sourcecontext_filepathrj   rk   code_readerri   tokentok_type
tok_stringr   _srowrh   visitors                  r   r8   r8      s    , F#+,))+*>*>? <(-%h
CAgdA	X%%	%'--/399;T	< d#,4|)+=+-' 
-- 
		 $$  	s   AB6 &B6 6/C('C(c                     t        j                  |      \  }}t        j                  |      }|d   }t        |      t        |j	                               z
  }t        | ||||       y)zBLike resolve, but extracts the context information from an entity.r   N)r   getsourcelinesgetsourcefilelenlstripr8   )rI   r   entitylinesr   rG   definition_liner   s           r   resolve_entityr     s^    ++F3-%%%f-( !H/?#c/*@*@*B&CC*	$&*5r   c                 2   t         j                  j                  j                  | d      }|yt	        |t
        t        f      s|f}|D ]K  }t        j                  |      D ]1  }t        j                  |t         j                  j                  |       3 M y)z;Copies the origin info from a node to another, recursively.Nr.   )
r   r;   r<   ofrw   listtuplerx   walkrs   )	from_nodeto_nodert   rI   r6   s        r   copy_originr     s~    ::	48&^
	GdE]	+jG 1dYYt_ 1
ll1djj''011r   )r   collectionsrB   r   r&   r   rx    tensorflow.python.autograph.pyctr   r   r   r   tensorflow.python.utilr   
namedtupler	   r   r   rV   rX   NodeVisitorr]   r8   r   r   r   r   r   <module>r      s    Q   	 	   1 5 3 ; -K>+AB4K:'KL4 JK?AJ6DN @!T%% @!F*Z
6	1r   