B
    ѻd<              
   @   s|  d Z ddlZddlZddlZddlZddlmZ ddddZej	Z	ej
Z
ejZeedrbejZnedd	d
dddddgZdd ZeedrejZdd ZnejZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% ZdId&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@dA Z'dBdC Z(dDdE Z)dJdGdHZ*dS )Kz6TFDecorator-aware replacements for the inspect module.    N)tf_decoratorT)follow_wrappedc            C   s   t jt| d |dS )z4TFDecorator-aware replacement for inspect.signature.   )r   )_inspect	signaturer   unwrap)objr    r	   S/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/util/tf_inspect.pyr      s    r   FullArgSpecargsvarargsvarkwdefaults
kwonlyargskwonlydefaultsannotationsc          	   C   s,   t | tr| S t| j| j| j| jg d i dS )N)r   r   r   r   r   r   r   )
isinstancer   r   r   keywordsr   )argspecr	   r	   r
   %_convert_maybe_argspec_to_fullargspec2   s    
r   getfullargspecc             C   s$   t | }t|j|j|j|jd}|S )a  A python3 version of getargspec.

    Calls `getfullargspec` and assigns args, varargs,
    varkw, and defaults to a python 2/3 compatible `ArgSpec`.

    The parameter name 'varkw' is changed to 'keywords' to fit the
    `ArgSpec` struct.

    Args:
      target: the target object to inspect.

    Returns:
      An ArgSpec with args, varargs, keywords, and defaults parameters
      from FullArgSpec.
    )r   r   r   r   )r   ArgSpecr   r   r   r   )targetZfullargspecsZargspecsr	   r	   r
   _getargspecA   s    
r   c             C   s   t t| S )zA python2 version of getfullargspec.

    Args:
      target: the target object to inspect.

    Returns:
      A FullArgSpec with empty kwonlyargs, kwonlydefaults and annotations.
    )r   
getargspec)r   r	   r	   r
   _getfullargspec[   s    	r   c               C   s   t  d d S )z7TFDecorator-aware replacement for inspect.currentframe.r   r   )r   stackr	   r	   r	   r
   currentframeg   s    r   c             C   s   t | tjrt| S t| \}}tdd |D d}|r>|S yt|S  tk
rZ   Y nX t |t	ry
t|j
S  tk
r   Y nX y
t|jS  tk
r   Y nX tt	|jS )aJ  TFDecorator-aware replacement for `inspect.getargspec`.

  Note: `getfullargspec` is recommended as the python 2/3 compatible
  replacement for this function.

  Args:
    obj: A function, partial function, or callable object, possibly decorated.

  Returns:
    The `ArgSpec` that describes the signature of the outermost decorator that
    changes the callable's signature, or the `ArgSpec` that describes
    the object if not decorated.

  Raises:
    ValueError: When callable's signature can not be expressed with
      ArgSpec.
    TypeError: For objects of unsupported types.
  c             s   s   | ]}|j d k	r|j V  qd S )N)decorator_argspec).0dr	   r	   r
   	<genexpr>   s   zgetargspec.<locals>.<genexpr>N)r   	functoolspartial_get_argspec_for_partialr   r   nextr   	TypeErrortype__init____new____call__)r   
decoratorsr   specr	   r	   r
   r   l   s,    


r   c                s(  t | j}| jpi }t| j\ }}} |d  t gt   }|r\||t | d< xJt|D ]<\}}| kr |}	|||	< qh|sht	|  d| dqhW t
fddt|D ddkrt ||dS  fddt|D }
|
rt	|  d|
 dt ||t|d S )	zImplements `getargspec` for `functools.partial` objects.

  Args:
    obj: The `functools.partial` object
  Returns:
    An `inspect.ArgSpec`
  Raises:
    ValueError: When callable's signature can not be expressed with
      ArgSpec.
  NzM does not have a **kwargs parameter, but contains an unknown partial keyword .c             3   s   | ]\}}| k	r|V  qd S )Nr	   )r    idxx)
no_defaultr	   r
   r"      s    z+_get_argspec_for_partial.<locals>.<genexpr>c                s(   g | ] \}}|kr|kr | qS r	   r	   )r    ij)r   first_defaultr1   r	   r
   
<listcomp>   s    z,_get_argspec_for_partial.<locals>.<listcomp>z; has some keyword-only arguments, which are not supported: )lenr   r   r   funcobjectsix	iteritemsindex
ValueErrorr&   	enumerater   tuple)r   Zn_prune_argspartial_keywordsr   r   r   Zall_defaultskwdefaultr/   Zinvalid_default_valuesr	   )r   r4   r1   r
   r%      s,    (



r%   c             C   s8   t | \}}x |D ]}|jdk	rt|jS qW t|S )a  TFDecorator-aware replacement for `inspect.getfullargspec`.

  This wrapper emulates `inspect.getfullargspec` in[^)]* Python2.

  Args:
    obj: A callable, possibly decorated.

  Returns:
    The `FullArgSpec` that describes the signature of
    the outermost decorator that changes the callable's signature. If the
    callable is not decorated, `inspect.getfullargspec()` will be called
    directly on the callable.
  N)r   r   r   r   r   )r   r,   r   r!   r	   r	   r
   r      s
    

c                 s   | d }| dd }t |}|  t|ddp:t|dd}t|rR|rR|f| } fdd|jD } tt|| |jsdnt	|j}|rx4t|j| d |jD ]\}}	| kr|	 |< qW |j
dk	rx&|j
 D ]\}
}|
 kr| |
< qW  S )aX  TFDecorator-aware replacement for inspect.getcallargs.

  Args:
    *func_and_positional: A callable, possibly decorated, followed by any
      positional arguments that would be passed to `func`.
    **named: The named argument dictionary that would be passed to `func`.

  Returns:
    A dictionary mapping `func`'s named arguments to the values they would
    receive if `func(*positional, **named)` were called.

  `getcallargs` will use the argspec from the outermost decorator that provides
  it. If no attached decorators modify argspec, the final unwrapped target's
  argspec will be used.
  r   r   NZim_self__self__c                s   g | ]}| kr|qS r	   r	   )r    arg)	call_argsr	   r
   r5   $  s    zgetcallargs.<locals>.<listcomp>)r   copygetattrismethodr   updatedictzipr   r6   r   items)func_and_positionalnamedr7   
positionalr   thisZremaining_positionalsZdefault_countrC   valuekvr	   )rD   r
   getcallargs  s&    
"
rS   c              O   s   t j| |S )N)r   getframeinfo)r   kwargsr	   r	   r
   rT   2  s    rT   c             C   s
   t | S )a(  TFDecorator-aware replacement for inspect.getdoc.

  Args:
    object: An object, possibly decorated.

  Returns:
    The docstring associated with the object.

  The outermost-decorated object is intended to have the most complete
  documentation, so the decorated parameter is not unwrapped.
  )r   getdoc)r8   r	   r	   r
   rV   6  s    rV   c             C   s6   t | d }t|dr,d|jkr,|jd S t|S )z2TFDecorator-aware replacement for inspect.getfile.r   	f_globals__file__)r   r   hasattrrW   r   getfile)r8   Zunwrapped_objectr	   r	   r
   rZ   E  s
    


rZ   c             C   s   t | |S )z5TFDecorator-aware replacement for inspect.getmembers.)r   
getmembers)r8   	predicater	   r	   r
   r[   S  s    r[   c             C   s
   t | S )z4TFDecorator-aware replacement for inspect.getmodule.)r   	getmodule)r8   r	   r	   r
   r]   X  s    r]   c             C   s
   t | S )z1TFDecorator-aware replacement for inspect.getmro.)r   getmro)clsr	   r	   r
   r^   ]  s    r^   c             C   s   t t| d S )z4TFDecorator-aware replacement for inspect.getsource.r   )r   	getsourcer   r   )r8   r	   r	   r
   r`   b  s    r`   c             C   s   t t| d S )z8TFDecorator-aware replacement for inspect.getsourcefile.r   )r   getsourcefiler   r   )r8   r	   r	   r
   ra   g  s    ra   c             C   s   t t| d S )z9TFDecorator-aware replacement for inspect.getsourcelines.r   )r   getsourcelinesr   r   )r8   r	   r	   r
   rb   l  s    rb   c             C   s   t t| d S )z4TFDecorator-aware replacement for inspect.isbuiltin.r   )r   	isbuiltinr   r   )r8   r	   r	   r
   rc   q  s    rc   c             C   s   t t| d S )z2TFDecorator-aware replacement for inspect.isclass.r   )r   isclassr   r   )r8   r	   r	   r
   rd   v  s    rd   c             C   s   t t| d S )z5TFDecorator-aware replacement for inspect.isfunction.r   )r   
isfunctionr   r   )r8   r	   r	   r
   re   {  s    re   c             C   s   t t| d S )z3TFDecorator-aware replacement for inspect.ismodule.r   )r   isframer   r   )r8   r	   r	   r
   rf     s    rf   c             C   s   t t| d S )z6TFDecorator-aware replacement for inspect.isgenerator.r   )r   isgeneratorr   r   )r8   r	   r	   r
   rg     s    rg   c             C   s   t t| d S )z>TFDecorator-aware replacement for inspect.isgeneratorfunction.r   )r   isgeneratorfunctionr   r   )r8   r	   r	   r
   rh     s    rh   c             C   s   t t| d S )z3TFDecorator-aware replacement for inspect.ismethod.r   )r   rG   r   r   )r8   r	   r	   r
   rG     s    rG   c             C   sX   t | \}}x|D ]}t|jrdS qW xt|tjrB|j}q.W t	|oVt
| S )a  Checks if `object` or a TF Decorator wrapped target contains self or cls.

  This function could be used along with `tf_inspect.getfullargspec` to
  determine if the first argument of `object` argspec is self or cls. If the
  first argument is self or cls, it needs to be excluded from argspec when we
  compare the argspec to the input arguments and, if provided, the tf.function
  input_signature.

  Like `tf_inspect.getfullargspec` and python `inspect.getfullargspec`, it
  does not unwrap python decorators.

  Args:
    obj: An method, function, or functool.partial, possibly decorated by
    TFDecorator.

  Returns:
    A bool indicates if `object` or any target along the chain of TF decorators
    is a method.
  T)r   r   r   rG   decorated_targetr   r#   r$   r7   callablere   )r8   r,   r   	decoratorr	   r	   r
   isanytargetmethod  s    

rl   c             C   s   t t| d S )z3TFDecorator-aware replacement for inspect.ismodule.r   )r   ismoduler   r   )r8   r	   r	   r
   rm     s    rm   c             C   s   t t| d S )z4TFDecorator-aware replacement for inspect.isroutine.r   )r   	isroutiner   r   )r8   r	   r	   r
   rn     s    rn   r   c             C   s   t | dd S )z0TFDecorator-aware replacement for inspect.stack.r   N)r   r   )contextr	   r	   r
   r     s    r   )N)r   )+__doc__collectionsr#   inspectr   r9   tensorflow.python.utilr   r   	Parameter	Signaturer   rY   r   
namedtupler   r   r   r   r   r   r%   rS   rT   rV   rZ   r[   r]   r^   r`   ra   rb   rc   rd   re   rf   rg   rh   rG   rl   rm   rn   r   r	   r	   r	   r
   <module>   sZ   


4W%
$