
    BVh*                         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Zddlm	Z	  ej                         Z ed ej                  j                         D              Zd Zd Zd Zd Zd	 Zd
 Zd ZddZd Zd Zd Zy)zgLive entity inspection utilities.

This module contains whatever inspect doesn't offer out of the box.
    N)
tf_inspectc              #   2   K   | ]  }t        |        y wN)id).0vs     ^/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/pyct/inspect_utils.py	<genexpr>r
   "   s     !LA"Q%!Ls   c                     t        j                  |       syt        | d      rt        | d      sy| j                  dk(  xs | j                  j
                  dk(  S )NF__name____code__z<lambda>)r   
isfunctionhasattrr   r   co_namefs    r	   islambdar   %   sN    			q	!
!Z
 WQ
%; ::#
J););z)IK    c                     t        j                  |       rt        | t              syt	        | d      syt        | d      }t        |t              syt        d |D              syy)z2Returns True if the argument is a namedtuple-like.F_fieldsc              3   <   K   | ]  }t        |t                y wr   )
isinstancestr)r   r   s     r	   r
   zisnamedtuple.<locals>.<genexpr>9   s     0AZ30s   T)r   isclass
issubclasstupler   getattrr   all)r   fieldss     r	   isnamedtupler    0   sV    


Q
Jq%$8	I	1i &	FE	"	00	0	r   c                     t        |       t        v ryt        | t        j                        ryt        j                  |       ry| t        u ryy)z4Returns True if the argument is a built-in function.TF)r   _BUILTIN_FUNCTION_IDSr   typesBuiltinFunctionTypeinspect	isbuiltinevalr   s    r	   r&   r&   >   sC    U##!U../Dyr   c                     t        j                  |       xr[ t        | j                  t              xr> t        | j                  d      xr& | j                  j                  t        j                  u S )a{  Returns True if the argument is an object constructor.

  In general, any object of type class is a constructor, with the exception
  of classes created using a callable metaclass.
  See below for why a callable metaclass is not a trivial combination:
  https://docs.python.org/2.7/reference/datamodel.html#customizing-class-creation

  Args:
    cls: Any

  Returns:
    Bool
  __call__)r%   r   r   	__class__typer   r)   )clss    r	   isconstructorr-   L   s[     //#
 <#--. ;s}}j1;}}%%T]]:<=r   c                 @   t        | d      rt        j                  |       }| j                  }t	        t
        j                  j                               }|D ]D  }t        |d      s|j                  |k(  s ||us%t        j                  ||j                         F yy)a  Fixes potential corruption of linecache in the presence of functools.wraps.

  functools.wraps modifies the target object's __module__ field, which seems
  to confuse linecache in special instances, for example when the source is
  loaded from a .par file (see https://google.github.io/subpar/subpar.html).

  This function simply triggers a call to linecache.updatecache when a mismatch
  was detected between the object's __module__ property and the object's source
  file.

  Args:
    obj: Any
  
__module____file__N)r   r%   getfiler/   r   sysmodulesvaluesr0   	linecacheupdatecache__dict__)objobj_file
obj_moduleloaded_modulesms        r	   _fix_linecache_recordr=   `   s     S,s#HJ 3;;--/0N 6	J	AJJ($:Q


!**
56  r   c                     t         5  t        |        t        j                  |       \  }}dj	                  t        j
                  ||d             cddd       S # 1 sw Y   yxY w)zEA variant of inspect.getsource that ignores the __wrapped__ property. N)_linecache_lockr=   r%   
findsourcejoingetblock)r8   lineslnums      r	   getimmediatesourcerF   {   sS     3#$$S)KE4777##E$%L123 3 3s   A
AA$c                     t        | j                        }| j                  }| j                  j                  }|r&|r$t        ||      D ]  \  }}	 |j                  ||<    |S # t        $ r Y %w xY w)a  Returns the complete namespace of a function.

  Namespace is defined here as the mapping of all non-local variables to values.
  This includes the globals and the closure variables. Note that this captures
  the entire globals collection of the function, and may contain extra symbols
  that it does not actually use.

  Args:
    f: User defined function.

  Returns:
    A dict mapping symbol names to values.
  )dict__globals____closure__r   co_freevarszipcell_contents
ValueError)r   	namespaceclosurefreevarsnamecells         r	   getnamespacerT      sy     1==!)MM'ZZ##('(G, 
d,,	$ 
  s   A""	A.-A.c                 4   |
t               }t        |       } | D ]  }|| |   u s|c S  t        j                  |      }|M||urI|| urEt	        | |d|      }|4t	        |j
                  |d|      }|J d       dj                  ||      S |r| j                         D ]v  }| |   }t        j                  |      st        |      |vs,|j                  t        |             t	        |j
                  ||dz
  |      }	|	ddj                  ||	      c S  y)a  Returns the name by which a value can be referred to in a given namespace.

  If the object defines a parent module, the function attempts to use it to
  locate the object.

  This function will recurse inside modules, but it will not search objects for
  attributes. The recursion depth is controlled by max_depth.

  Args:
    namespace: Dict[str, Any], the namespace to search into.
    object_: Any, the value to search.
    max_depth: Optional[int], a limit to the recursion depth when searching
      inside modules.
    visited: Optional[Set[int]], ID of modules to avoid visiting.
  Returns: Union[str, None], the fully-qualified name that resolves to the value
    o, or None if it couldn't be found.
  Nr   )	max_depthvisitedz4An object should always be found in its owner modulez{}.{}   )setrH   r   	getmodulegetqualifiednamer7   formatkeysismoduler   add)
rO   object_rV   rW   rR   parentparent_namename_in_parentvaluename_in_modules
             r	   r[   r[      sB   $ _eG 9o) d )D/!k (&V72vY7N"6Q9K'
//7aBn' B
@B'^^K88   6oe			U	#5	(@BuI)%..'*3a-B%n5
56 
r   c                     | j                   }t        j                  |      D ];  }t        |d      r||j                  v st        |d      s*||j
                  v s9|c S  |S )zHResolves the class (e.g. one of the superclasses) that defined a method.r7   	__slots__)r   r%   getmror   r7   rg   )r<   owner_classmethod_namesuper_classs       r	   getdefiningclassrl      sa    

+^^K0 k	j	)	,,	,	k	*	--	- 
r   c                    t        | d      s>t        | d      r2t        | d      r&t        | j                  t              r| j                  S t	        | dd      }|#t        j                  |      r|S |j                  S g }t        j                         j                  }	 t        j                  |j                  j                         |j                  j                               D ]u  }t        || j                        st	        || j                        }t        |d      r|j                   }t        | d      r| j                   } || u se|j#                  |       w 	 ~|rlt%        |      dk(  r|d   S t'        d	 |D              }|D ]1  }t        j                  |      st)        |t'        |            s/|c S  t+        d
| d|      y# ~w xY w)a  Resolves a function's owner, e.g.

  a method's class.

  Note that this returns the object that the function was retrieved from, not
  necessarily the class where it was defined.

  This function relies on Python stack frame support in the interpreter, and
  has the same limitations that inspect.currentframe.

  Limitations. This function will only work correctly if the owned class is
  visible in the caller's global or local variables.

  Args:
    m: A user defined function

  Returns:
    The class that this function was retrieved from, or None if the function
    is not an object or class method, or the class that owns the object or
    method is not visible to m.

  Raises:
    ValueError: if the class could not be resolved for any unexpected reason.
  r   r*   r)   __self__Nim_funcrX   r   c              3   `   K   | ]&  }t        j                  |      r|n
t        |       ( y wr   )r   r   r+   )r   os     r	   r
   z!getmethodclass.<locals>.<genexpr>+  s&     PAZ//2Q?Ps   ,.zFound too many owners of z: )r   r   r*   r+   r   r%   r   r   currentframef_back	itertoolschainf_localsr4   	f_globalsr   ro   appendlenr   r   rN   )r<   m_selfownerscaller_framer   	candidateowner_typesrq   s           r	   getmethodclassr      s   6 !Z
 WQ%<a!++t$[[ 1j$'&vm &((*11, __\2299;)33::<> 	AJJ	Aqzz*	 9i('')1i ii!>
--
 	
6{aAY PPPK 			A	:a{1C#D !VD
EE	 	s   AG >A
G 	G Gc                     t        j                  |       st        j                  |       s
t               S t        t	        d | j
                  j                         D                    S )zDetects what future imports are necessary to safely execute entity source.

  Args:
    entity: Any object

  Returns:
    A tuple of future strings
  c              3   F   K   | ]  \  }}t        |d d      dk(  r|  yw)r/   N
__future__)r   )r   rR   rd   s      r	   r
   z#getfutureimports.<locals>.<genexpr>@  s.      DkdE|T2lB  Ds   !)r   r   ismethodr   sortedrI   items)entitys    r	   getfutureimportsr   4  s[     


':+>+>v+F7N	 DV%7%7%=%=%? D D
E Er   )   N)__doc__builtinsr%   rt   r5   r2   	threadingr#   tensorflow.python.utilr   Lockr@   	frozensetr7   r4   r"   r   r    r&   r-   r=   rF   rT   r[   rl   r   r    r   r	   <module>r      s   
     
   - !).." "!L1B1B1I1I1K!LL L=(6636;|	IXEr   