
[[\                @   s  d  Z  d d f 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
 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 m Z y d d l m Z WnN e k
 r8d d	 f \ Z Z d
 d f \ Z Z d d d f \ Z Z Z Yn5 Xe    Z! x( e j"   D] \ Z# Z$ e# e! d e$ <qOWd d >Z% d d   Z& d d   Z' d d   Z( d d   Z) d d   Z* e+ e d  rd d   Z, n d d   Z, e+ e d  rd  d!   Z- n d" d!   Z- d# d$   Z. d% d&   Z/ d' d(   Z0 d) d*   Z1 d+ d,   Z2 d- d.   Z3 d/ d0   Z4 d1 d2   Z5 d3 d4   Z6 d d5 d6  Z7 e d7 d8  Z8 d9 d:   Z9 d; d<   Z: d= d d> d?  Z; d@ dA   Z< dB dC   Z= dD dE   Z> dF dG   Z? e dH dI  Z@ dJ dK   ZA dL dM   ZB dN dO   ZC d dP dQ  ZD i  ZE i  ZF d dR dS  ZG dT dU   ZH dV dW   ZI GdX dY   dY eJ  ZK GdZ d[   d[  ZL d\ d]   ZM d^ d_   ZN d` da   ZO db dc   ZP dd de df  ZQ e dg dh  ZR di dj   ZS dk dl   ZT e dm dn  ZU do dp   ZV e dq dr  ZW ds dt   ZX e du dv  ZY dw dx   ZZ d dy dz  Z[ d{ d|   Z\ d d d f  i  i  e] d} d~   d d~   d d~   d d~   e[ d d  Z^ e] d d~   d d~   d d~   d d  Z_ d d   Z` d d   Za d d   Zb e d d  Zc d d   Zd e d d  Ze d d d  Zf d d   Zg d d d  Zh d d d  Zi d d   Zj d d d  Zk d d d  Zl em   Zn d d   Zo d d   Zp d d   Zq d d   Zr d d   Zs en d d  Zt d Zu d Zv d Zw d Zx d d   Zy d d   Zz e{ e{ j|  Z} e{ e~ j|  Z e{ e j d  Z e} e e e j f Z d d   Z f  d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z d d d  Z d d d  Z d d d d  Z d d   Z Gd d   d  Z Gd d   d  Z Gd d   d e  Z e d d d Z e d d d Z e d	 d d Z e d d d Z e d
 d d Z Gd d   d  Z Gd d   d  Z Gd d   d  Z d d   Z e d k re   n  d S)a(  Get useful information from live Python objects.

This module encapsulates the interface provided by the internal special
attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
It also provides some help for examining source code and class layout.

Here are some of the useful functions provided by this module:

    ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
        isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
        isroutine() - check object types
    getmembers() - get members of an object that satisfy a given condition

    getfile(), getsourcefile(), getsource() - find an object's source code
    getdoc(), getcomments() - get documentation on an object
    getmodule() - determine the module that an object came from
    getclasstree() - arrange classes so as to represent their hierarchy

    getargspec(), getargvalues(), getcallargs() - get info about function arguments
    getfullargspec() - same, with support for Python-3000 features
    formatargspec(), formatargvalues() - format an argument spec
    getouterframes(), getinnerframes() - get info about frames
    currentframe() - get the current stack frame
    stack(), trace() - get info about frames on the stack or in a traceback

    signature() - get a Signature object for the callable
zKa-Ping Yee <ping@lfw.org>z'Yury Selivanov <yselivanov@sprymix.com>    N)
attrgetter)
namedtupleOrderedDict)COMPILER_FLAG_NAMES                   @   ZCO_   c             C   s   t  |  t j  S)zReturn true if the object is a module.

    Module objects provide these attributes:
        __cached__      pathname to byte compiled file
        __doc__         documentation string
        __file__        filename (missing for built-in modules))
isinstancetypes
ModuleType)object r   /usr/lib/python3.4/inspect.pyismoduleD   s    r   c             C   s   t  |  t  S)zReturn true if the object is a class.

    Class objects provide these attributes:
        __doc__         documentation string
        __module__      name of module in which this class was defined)r   type)r   r   r   r   isclassM   s    r   c             C   s   t  |  t j  S)a_  Return true if the object is an instance method.

    Instance method objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this method was defined
        __func__        function object containing implementation of method
        __self__        instance to which this method is bound)r   r   
MethodType)r   r   r   r   ismethodU   s    r   c             C   sQ   t  |   s$ t |   s$ t |   r( d St |   } t | d  oP t | d  S)a  Return true if the object is a method descriptor.

    But not if ismethod() or isclass() or isfunction() are true.

    This is new in Python 2.2, and, for example, is true of int.__add__.
    An object passing this test has a __get__ attribute but not a __set__
    attribute, but beyond that the set of attributes varies.  __name__ is
    usually sensible, and __doc__ often is.

    Methods implemented via descriptors that also pass one of the other
    tests return false from the ismethoddescriptor() test, simply because
    the other tests promise more -- you can, e.g., count on having the
    __func__ attribute (etc) when an object passes ismethod().F__get____set__)r   r   
isfunctionr   hasattr)r   tpr   r   r   ismethoddescriptor_   s    $r   c             C   sP   t  |   s$ t |   s$ t |   r( d St |   } t | d  oO t | d  S)a  Return true if the object is a data descriptor.

    Data descriptors have both a __get__ and a __set__ attribute.  Examples are
    properties (defined in Python) and getsets and members (defined in C).
    Typically, data descriptors will also have __name__ and __doc__ attributes
    (properties, getsets, and members have both of these attributes), but this
    is not guaranteed.Fr   r   )r   r   r   r   r   )r   r   r   r   r   isdatadescriptors   s    $r   MemberDescriptorTypec             C   s   t  |  t j  S)zReturn true if the object is a member descriptor.

        Member descriptors are specialized descriptors defined in extension
        modules.)r   r   r    )r   r   r   r   ismemberdescriptor   s    r!   c             C   s   d S)zReturn true if the object is a member descriptor.

        Member descriptors are specialized descriptors defined in extension
        modules.Fr   )r   r   r   r   r!      s    GetSetDescriptorTypec             C   s   t  |  t j  S)zReturn true if the object is a getset descriptor.

        getset descriptors are specialized descriptors defined in extension
        modules.)r   r   r"   )r   r   r   r   isgetsetdescriptor   s    r#   c             C   s   d S)zReturn true if the object is a getset descriptor.

        getset descriptors are specialized descriptors defined in extension
        modules.Fr   )r   r   r   r   r#      s    c             C   s   t  |  t j  S)a(  Return true if the object is a user-defined function.

    Function objects provide these attributes:
        __doc__         documentation string
        __name__        name with which this function was defined
        __code__        code object containing compiled function bytecode
        __defaults__    tuple of any default values for arguments
        __globals__     global namespace in which this function was defined
        __annotations__ dict of parameter annotations
        __kwdefaults__  dict of keyword only parameters with defaults)r   r   FunctionType)r   r   r   r   r      s    r   c             C   s,   t  t |   s t |   o( |  j j t @ S)zReturn true if the object is a user-defined generator function.

    Generator function objects provides same attributes as functions.

    See help(isfunction) for attributes listing.)boolr   r   __code__co_flagsCO_GENERATOR)r   r   r   r   isgeneratorfunction   s    r)   c             C   s   t  |  t j  S)a  Return true if the object is a generator.

    Generator objects provide these attributes:
        __iter__        defined to support iteration over container
        close           raises a new GeneratorExit exception inside the
                        generator to terminate the iteration
        gi_code         code object
        gi_frame        frame object or possibly None once the generator has
                        been exhausted
        gi_running      set to 1 when generator is executing, 0 otherwise
        next            return the next item from the container
        send            resumes the generator and "sends" a value that becomes
                        the result of the current yield-expression
        throw           used to raise an exception inside the generator)r   r   GeneratorType)r   r   r   r   isgenerator   s    r+   c             C   s   t  |  t j  S)ab  Return true if the object is a traceback.

    Traceback objects provide these attributes:
        tb_frame        frame object at this level
        tb_lasti        index of last attempted instruction in bytecode
        tb_lineno       current line number in Python source code
        tb_next         next inner traceback object (called by this level))r   r   TracebackType)r   r   r   r   istraceback   s    r-   c             C   s   t  |  t j  S)a`  Return true if the object is a frame object.

    Frame objects provide these attributes:
        f_back          next outer frame object (this frame's caller)
        f_builtins      built-in namespace seen by this frame
        f_code          code object being executed in this frame
        f_globals       global namespace seen by this frame
        f_lasti         index of last attempted instruction in bytecode
        f_lineno        current line number in Python source code
        f_locals        local namespace seen by this frame
        f_trace         tracing function for this frame, or None)r   r   	FrameType)r   r   r   r   isframe   s    r/   c             C   s   t  |  t j  S)au  Return true if the object is a code object.

    Code objects provide these attributes:
        co_argcount     number of arguments (not including * or ** args)
        co_code         string of raw compiled bytecode
        co_consts       tuple of constants used in the bytecode
        co_filename     name of file in which this code object was created
        co_firstlineno  number of first line in Python source code
        co_flags        bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
        co_lnotab       encoded mapping of line numbers to bytecode indices
        co_name         name with which this code object was defined
        co_names        tuple of names of local variables
        co_nlocals      number of local variables
        co_stacksize    virtual machine stack space required
        co_varnames     tuple of names of arguments and local variables)r   r   CodeType)r   r   r   r   iscode   s    r1   c             C   s   t  |  t j  S)a,  Return true if the object is a built-in function or method.

    Built-in functions and methods provide these attributes:
        __doc__         documentation string
        __name__        original name of this function or method
        __self__        instance to which a method is bound, or None)r   r   BuiltinFunctionType)r   r   r   r   	isbuiltin   s    r3   c             C   s.   t  |   p- t |   p- t |   p- t |   S)z<Return true if the object is any kind of function or method.)r3   r   r   r   )r   r   r   r   	isroutine   s    r4   c             C   s    t  t |  t  o |  j t @ S)z:Return true if the object is an abstract base class (ABC).)r%   r   r   	__flags__TPFLAGS_IS_ABSTRACT)r   r   r   r   
isabstract  s    r7   c             C   s  t  |   r" |  f t |   } n f  } g  } t   } t |   } yZ xS |  j D]H } x? | j j   D]. \ } } t | t j	  rf | j
 |  qf qf WqP WWn t k
 r Yn Xx | D] }	 y( t |  |	  }
 |	 | k r t  n  WnF t k
 r/x1 | D]& } |	 | j k r | j |	 }
 Pq q Ww Yn X| sC| |
  rY| j
 |	 |
 f  n  | j |	  q W| j d d d    | S)zReturn all members of an object as (name, value) pairs sorted by name.
    Optionally, only return members that satisfy a given predicate.keyc             S   s   |  d S)Nr   r   )Zpairr   r   r   <lambda>1  s    zgetmembers.<locals>.<lambda>)r   getmrosetdir	__bases____dict__itemsr   r   DynamicClassAttributeappendAttributeErrorgetattraddsort)r   Z	predicatemroresults	processednamesbasekvr8   valuer   r   r   
getmembers  s:    	rN   	Attributezname kind defining_class objectc             C   s  t  |   } t  t |    } t d d   | D  } |  f | } | | } t |   } xP | D]H } x? | j j   D]. \ } } t | t j  rw | j	 |  qw qw Wqa Wg  }	 t
   }
 xU| D]M} d } d } d } | |
 k ry. | d k rt d   n  t |  |  } Wn% t k
 r<} z WYd d } ~ XqXt | d |  } | | k rd } d } x5 | D]- } t | | d  } | | k rn| } qnqnWxQ | D]I } y | j |  |  } Wn t k
 rwYn X| | k r| } qqW| d k	 r| } qqn  xC | D]; } | | j k r| j | } | | k rL| } n  PqqW| d k rfq n  | po| } t | t  rd } | } nW t | t  rd } | } n9 t | t  rd	 } | } n t |  rd
 } n d } |	 j	 t | | | |   |
 j |  q W|	 S)aN  Return list of attribute-descriptor tuples.

    For each name in dir(cls), the return list contains a 4-tuple
    with these elements:

        0. The name (a string).

        1. The kind of attribute this is, one of these strings:
               'class method'    created via classmethod()
               'static method'   created via staticmethod()
               'property'        created via property()
               'method'          any other flavor of method or descriptor
               'data'            not a method

        2. The class which defined this attribute (a class).

        3. The object as obtained by calling getattr; if this fails, or if the
           resulting object does not live anywhere in the class' mro (including
           metaclasses) then the object is looked up in the defining class's
           dict (found by walking the mro).

    If one of the items in dir(cls) is stored in the metaclass it will now
    be discovered and not have None be listed as the class in which it was
    defined.  Any items whose home class cannot be discovered are skipped.
    c             S   s(   g  |  ] } | t  t f k r |  q Sr   )r   r   ).0clsr   r   r   
<listcomp>S  s   	 z(classify_class_attrs.<locals>.<listcomp>Nr>   z)__dict__ is special, don't want the proxy__objclass__zstatic methodzclass methodpropertymethoddata)r:   r   tupler<   r>   r?   r   r   r@   rA   r;   	ExceptionrC   __getattr__rB   staticmethodclassmethodrT   r4   rO   rD   )rQ   rF   ZmetamroZclass_basesZ	all_basesrI   rJ   rK   rL   resultrH   nameZhomeclsZget_objZdict_objexcZlast_clsZsrch_clsZsrch_objobjkindr   r   r   classify_class_attrs6  s    
	
					ra   c             C   s   |  j  S)zHReturn tuple of base classes (including cls) in method resolution order.)__mro__)rQ   r   r   r   r:     s    r:   stopc               s     d k r d d   } n   f d d   } |  } t  |  h } xV | |   r |  j }  t  |   } | | k r t d j |    n  | j |  qE W|  S)an  Get the object wrapped by *func*.

   Follows the chain of :attr:`__wrapped__` attributes returning the last
   object in the chain.

   *stop* is an optional callback accepting an object in the wrapper chain
   as its sole argument that allows the unwrapping to be terminated early if
   the callback returns a true value. If the callback never returns a true
   value, the last object in the chain is returned as usual. For example,
   :func:`signature` uses this to stop unwrapping if any object in the
   chain has a ``__signature__`` attribute defined.

   :exc:`ValueError` is raised if a cycle is encountered.

    Nc             S   s   t  |  d  S)N__wrapped__)r   )fr   r   r   _is_wrapper  s    zunwrap.<locals>._is_wrapperc                s   t  |  d  o   |   S)Nrd   )r   )re   )rc   r   r   rf     s    z!wrapper loop when unwrapping {!r})idrd   
ValueErrorformatrD   )funcrc   rf   re   memoZid_funcr   )rc   r   unwrap  s    	rl   c             C   s&   |  j    } t |  t | j    S)zBReturn the indent size, in spaces, at the start of a line of text.)
expandtabslenlstrip)lineZexpliner   r   r   
indentsize  s    rq   c             C   sC   y |  j  } Wn t k
 r% d SYn Xt | t  s9 d St |  S)zGet the documentation string for an object.

    All tabs are expanded to spaces.  To clean up docstrings that are
    indented to line up with blocks of code, any whitespace than can be
    uniformly removed from the second line onwards is removed.N)__doc__rB   r   strcleandoc)r   docr   r   r   getdoc  s    	rv   c             C   sO  y |  j    j d  } Wn t k
 r1 d SYnXt j } xR | d d  D]@ } t | j    } | rL t |  | } t | |  } qL qL W| r | d j   | d <n  | t j k  r x8 t d t |   D] } | | | d  | | <q Wn  x | r| d r| j	   q Wx" | r=| d r=| j	 d  qWd j
 |  Sd S)zClean up indentation from docstrings.

    Any whitespace that can be uniformly removed from the second line
    onwards is removed.
Nr   r   )rm   splitUnicodeErrorsysmaxsizern   ro   minrangepopjoin)ru   linesZmarginrp   Zcontentindentir   r   r   rt     s(    		 rt   c             C   s'  t  |   r: t |  d  r" |  j St d j |     n  t |   r t |  d  r t j j |  j	  }  t |  d  r |  j Sn  t d j |     n  t
 |   r |  j }  n  t |   r |  j }  n  t |   r |  j }  n  t |   r |  j }  n  t |   r|  j St d j |     d S)z@Work out which source or compiled file an object was defined in.__file__z{!r} is a built-in module
__module__z{!r} is a built-in classzO{!r} is not a module, class, method, function, traceback, frame, or code objectN)r   r   r   	TypeErrorri   r   r{   modulesgetr   r   __func__r   r&   r-   tb_framer/   f_coder1   co_filename)r   r   r   r   getfile  s,    
r   
ModuleInfozname suffix mode module_typec             C   s   t  j d t d  t  j   ! t  j d t  d d l } Wd QXt j j	 |   } d d   | j
   D } | j   xM | D]E \ } } } } | | d  | k r~ t | d |  | | |  Sq~ Wd S)zDGet the module name, suffix, mode, and module type for a given file.z%inspect.getmoduleinfo() is deprecatedr   ignorer   Nc             S   s2   g  |  ]( \ } } } t  |  | | | f  q Sr   )rn   )rP   suffixmodemtyper   r   r   rR   $  s   	z!getmoduleinfo.<locals>.<listcomp>)warningswarnDeprecationWarningcatch_warningssimplefilterPendingDeprecationWarningimpospathbasenameZget_suffixesrE   r   )r   r   filenamesuffixesneglenr   r   r   r   r   r   getmoduleinfo  s    	
r   c             C   sp   t  j j |   } d d   t j j   D } | j   x1 | D]) \ } } | j |  r? | d |  Sq? Wd S)z1Return the module name for a given file, or None.c             S   s#   g  |  ] } t  |  | f  q Sr   )rn   )rP   r   r   r   r   rR   /  s   	z!getmodulename.<locals>.<listcomp>N)r   r   r   	importlib	machineryall_suffixesrE   endswith)r   Zfnamer   r   r   r   r   r   getmodulename+  s    	
r   c                s   t  |     t j j d d  } | t j j d d  7} t   f d d   | D  r t j j    d t j j	 d   n) t   f d d   t j j
 D  r d St j j    r   St t |     d d  d k	 r   S  t j k r   Sd S)zReturn the filename that can be used to locate an object's source.
    Return None if no way can be identified to get the source.
    Nc             3   s   |  ] }   j  |  Vq d  S)N)r   )rP   s)r   r   r   	<genexpr>>  s    z getsourcefile.<locals>.<genexpr>r   c             3   s   |  ] }   j  |  Vq d  S)N)r   )rP   r   )r   r   r   r   A  s    
__loader__)r   r   r   DEBUG_BYTECODE_SUFFIXESOPTIMIZED_BYTECODE_SUFFIXESanyr   r   splitextSOURCE_SUFFIXESEXTENSION_SUFFIXESexistsrC   	getmodule	linecachecache)r   Zall_bytecode_suffixesr   )r   r   getsourcefile7  s    !r   c             C   sC   | d k r' t  |   p! t |   } n  t j j t j j |   S)zReturn an absolute path to the source or compiled file for an object.

    The idea is for each object to have a unique origin, so this routine
    normalizes the result as much as possible.N)r   r   r   r   normcaseabspath)r   	_filenamer   r   r   
getabsfileM  s    r   c       
      C   s  t  |   r |  St |  d  r2 t j j |  j  S| d k	 r^ | t k r^ t j j t |  Sy t |  |  } Wn t k
 r d SYn X| t k r t j j t |  Sx t	 t j j
    D] \ } } t  |  r t | d  r | j } | t j | d  k rq n  | t | <t |  } | j t | <t t j j |  <q q W| t k rlt j j t |  St j d } t |  d  sd St | |  j  rt | |  j  } | |  k r| Sn  t j d } t | |  j  rt | |  j  }	 |	 |  k r| Sn  d S)zAReturn the module an object was defined in, or None if not found.r   Nr   __main____name__builtins)r   r   r{   r   r   r   modulesbyfiler   r   listr?   r   _filesbymodnamer   r   r   realpathrC   )
r   r   filemodnamemodulere   mainZ
mainobjectZbuiltinZbuiltinobjectr   r   r   r   Y  sD    	"	
(r   c             C   s  t  |   } t |   } | rR | d d  | d d  d k rR t d   n  | r^ | n | } t |  |  } | r t j | | j  } n t j |  } | s t d   n  t |   r | d f St |   r|  j	 } t
 j d | d  } g  } xp t t |   D]\ } | j | |  }	 |	 r| | d d	 k rM| | f S| j |	 j d  | f  qqW| r| j   | | d d f St d
   n  t |   r|  j }  n  t |   r|  j }  n  t |   r|  j }  n  t |   r|  j }  n  t |   rt |  d  s+t d   n  |  j d }
 t
 j d  } x1 |
 d k rz| j | |
  rmPn  |
 d }
 qJW| |
 f St d   d S)ab  Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An OSError
    is raised if the source code cannot be retrieved.Nr   z<>zsource code not availablezcould not get source coder   z^(\s*)class\s*z\bczcould not find class definitionco_firstlinenoz"could not find function definitionz+^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)zcould not find code objectrx   )r   r   OSErrorr   r   getlinesr>   r   r   r   recompiler~   rn   matchrA   grouprE   r   r   r   r&   r-   r   r/   r   r1   r   r   )r   r   Z
sourcefiler   r   r]   ZpatZ
candidatesr   r   lnumr   r   r   
findsource  s\    +
	
#
 
r   c             C   s  y t  |   \ } } Wn t t f k
 r4 d SYn Xt |   rEd } | rp | d d d  d k rp d } n  x6 | t |  k  r | | j   d k r | d } qs W| t |  k  r| | d d  d k rg  } | } xQ | t |  k  r4| | d d  d k r4| j | | j    | d } q Wd j |  Sn| d k rt	 | |  } | d } | d k r| | j
   d d  d k rt	 | |  | k r| | j   j
   g } | d k rk| d } | | j   j
   } xv | d d  d k rgt	 | |  | k rg| g | d d  <| d } | d k  rNPn  | | j   j
   } qWn  x0 | r| d j   d k rg  | d d  <qnWx0 | r| d	 j   d k rg  | d
 d  <qWd j |  Sn  d S)zwGet lines of comments immediately preceding an object's source code.

    Returns None when source can't be found.
    Nr   r   z#!r    #)r   r   rx   rx   )r   r   r   r   rn   striprA   rm   r   rq   ro   )r   r   r   startZcommentsendr   Zcommentr   r   r   getcomments  sJ    	  	+,/
,
/
 r   c               @   s   e  Z d  Z d S)
EndOfBlockN)r   r   __qualname__r   r   r   r   r     s    r   c               @   s.   e  Z d  Z d Z d d   Z d d   Z d S)BlockFinderz@Provide a tokeneater() method to detect the end of a code block.c             C   s1   d |  _  d |  _ d |  _ d |  _ d |  _ d  S)Nr   Fr   )r   islambdastartedpasslinelast)selfr   r   r   __init__  s
    				zBlockFinder.__init__c             C   s$  |  j  sE | d k r9 | d k r- d |  _ n  d |  _  n  d |  _ n | t j k r d |  _ | d |  _ |  j r t  q n |  j r n | t j k r |  j d |  _ d |  _ nj | t j	 k r |  j d |  _ |  j d k r t  q n0 |  j d k r | t j
 t j f k r t  n  d  S)	NdefclasslambdaTFr   r   )zdefzclasszlambda)r   r   r   tokenizeNEWLINEr   r   INDENTr   DEDENTCOMMENTNL)r   r   tokenZsrowcolZerowcolrp   r   r   r   
tokeneater  s,    				'zBlockFinder.tokeneaterN)r   r   r   rr   r   r   r   r   r   r   r     s   r   c             C   so   t    } y: t j t |   j  } x | D] } | j |   q+ WWn t t f k
 r] Yn X|  d | j  S)z@Extract the block of code at the top of the given list of lines.N)	r   r   generate_tokensiter__next__r   r   IndentationErrorr   )r   ZblockfindertokensZ_tokenr   r   r   getblock   s    	r   c             C   sJ   t  |   \ } } t |   r( | d f St | | d   | d f Sd S)a  Return a list of source lines and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of the lines
    corresponding to the object and the line number indicates where in the
    original source file the first line of code was found.  An OSError is
    raised if the source code cannot be retrieved.r   Nr   )r   r   r   )r   r   r   r   r   r   getsourcelines+  s     
r   c             C   s   t  |   \ } } d j |  S)a  Return the text of the source code for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    OSError is raised if the source code cannot be retrieved.r   )r   r   )r   r   r   r   r   r   	getsource8  s    r   c             C   sv   g  } |  j  d t d d   xP |  D]H } | j | | j f  | | k r& | j t | | | |   q& q& W| S)z-Recursive helper function for getclasstree().r8   r   r   )rE   r   rA   r=   walktree)classeschildrenparentrG   r   r   r   r   r   B  s    $r   Fc             C   s   i  } g  } x |  D] } | j  r x | j  D]Y } | | k rK g  | | <n  | | | k ro | | j |  n  | r, | |  k r, Pq, q, Wq | | k r | j |  q q Wx* | D]" } | |  k r | j |  q q Wt | | d  S)a  Arrange the given list of classes into a hierarchy of nested lists.

    Where a nested list appears, it contains classes derived from the class
    whose entry immediately precedes the list.  Each entry is a 2-tuple
    containing a class and a tuple of its base classes.  If the 'unique'
    argument is true, exactly one entry appears in the returned structure
    for each class in the given list.  Otherwise, classes using multiple
    inheritance and their descendants will appear multiple times.N)r=   rA   r   )r   Zuniquer   rootsr   r   r   r   r   getclasstreeL  s"    		 r   	Argumentszargs, varargs, varkwc             C   s,   t  |   \ } } } } t | | | |  S)a  Get information about the arguments accepted by a code object.

    Three things are returned: (args, varargs, varkw), where
    'args' is the list of argument names. Keyword-only arguments are
    appended. 'varargs' and 'varkw' are the names of the * and **
    arguments or None.)_getfullargsr   )coargsvarargs
kwonlyargsvarkwr   r   r   getargsi  s    r   c       	      C   s   t  |   s$ t d j |     n  |  j } |  j } |  j } t | d |   } t | | | |   } d } | | 7} d } |  j t @r |  j | } | d } n  d } |  j t	 @r |  j | } n  | | | | f S)a  Get information about the arguments accepted by a code object.

    Four things are returned: (args, varargs, kwonlyargs, varkw), where
    'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
    and 'varkw' are the names of the * and ** arguments or None.z{!r} is not a code objectNr   r   )
r1   r   ri   co_argcountco_varnamesco_kwonlyargcountr   r'   
CO_VARARGSCO_VARKEYWORDS)	r   nargsrI   Znkwargsr   r   stepr   r   r   r   r   r   s  s"    			
r   ArgSpeczargs varargs keywords defaultsc             C   sO   t  |   \ } } } } } } } | s- | r< t d   n  t | | | |  S)aS  Get the names and default values of a function's arguments.

    A tuple of four things is returned: (args, varargs, varkw, defaults).
    'args' is a list of the argument names.
    'args' will include keyword-only argument names.
    'varargs' and 'varkw' are the names of the * and ** arguments or None.
    'defaults' is an n-tuple of the default values of the last n arguments.

    Use the getfullargspec() API for Python-3000 code, as annotations
    and keyword arguments are supported. getargspec() will raise ValueError
    if the func has either annotations or keyword arguments.
    zcFunction has keyword-only arguments or annotations, use getfullargspec() API which can support them)getfullargspecrh   r  )rj   r   r   r   defaultsr   kwonlydefaultsannr   r   r   
getargspec  s    !r	  FullArgSpeczGargs, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotationsc             C   s  y t  |  d d d d } Wn4 t k
 rR } z t d  |  WYd d } ~ Xn Xg  } d } d } g  } f  } i  } f  } i  }	 | j | j k	 r | j | d <n  x| j j   D] }
 |
 j } |
 j } | t	 k r | j
 |  n | t k r*| j
 |  |
 j |
 j k	 r| |
 j f 7} qnh | t k r?| } nS | t k r}| j
 |  |
 j |
 j k	 r|
 j |	 | <qn | t k r| } n  |
 j |
 j k	 r |
 j | | <q q W|	 sd }	 n  | sd } n  t | | | | | |	 |  S)a  Get the names and default values of a callable object's arguments.

    A tuple of seven things is returned:
    (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
    'args' is a list of the argument names.
    'varargs' and 'varkw' are the names of the * and ** arguments or None.
    'defaults' is an n-tuple of the default values of the last n arguments.
    'kwonlyargs' is a list of keyword-only argument names.
    'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
    'annotations' is a dictionary mapping argument names to annotations.

    The first four items in the tuple correspond to getargspec().
    follow_wrapper_chainsFskip_bound_argzunsupported callableNreturn)_signature_internalrX   r   return_annotationempty
parametersvaluesr`   r]   _POSITIONAL_ONLYrA   _POSITIONAL_OR_KEYWORDdefault_VAR_POSITIONAL_KEYWORD_ONLY_VAR_KEYWORD
annotationr
  )rj   sigexr   r   r   r   r  annotations
kwdefaultsparamr`   r]   r   r   r   r    sR    	"						r  ArgInfozargs varargs keywords localsc             C   s.   t  |  j  \ } } } t | | | |  j  S)a9  Get information about arguments passed into a particular frame.

    A tuple of four things is returned: (args, varargs, varkw, locals).
    'args' is a list of the argument names.
    'varargs' and 'varkw' are the names of the * and ** arguments or None.
    'locals' is the locals dictionary of the given frame.)r   r   r  f_locals)framer   r   r   r   r   r   getargvalues  s    r"  c             C   sG   t  |  t  r= |  j d | f k r+ |  j S|  j d |  j St |   S)Nr   .)r   r   r   r   repr)r  Zbase_moduler   r   r   formatannotation  s
    r%  c                s(   t  |  d d       f d d   } | S)Nr   c                s   t  |     S)N)r%  )r  )r   r   r   _formatannotation  s    z5formatannotationrelativeto.<locals>._formatannotation)rC   )r   r&  r   )r   r   formatannotationrelativeto  s    r'  c             C   s   d |  S)N*r   )r]   r   r   r   r9     s    r9   c             C   s   d |  S)Nz**r   )r]   r   r   r   r9     s    c             C   s   d t  |   S)N=)r$  )rM   r   r   r   r9     s    c             C   s   d |  S)Nz -> r   )textr   r   r   r9      s    c                s      f d d   } g  } | r= t  |   t  |  } n  x` t |   D]R \ } } | |  } | r | | k r | |
 | | |  } n  | j |  qJ W| d k	 r | j | | |    n | r | j d  n  | r:xS | D]H } | |  } | r&| | k r&| |
 | |  7} n  | j |  q Wn  | d k	 rb| j |	 | |    n  d d j |  d } d   k r| |    d   7} n  | S)	a  Format an argument spec from the values returned by getargspec
    or getfullargspec.

    The first seven arguments are (args, varargs, varkw, defaults,
    kwonlyargs, kwonlydefaults, annotations).  The other five arguments
    are the corresponding optional formatting functions that are called to
    turn names and values into strings.  The last argument is an optional
    function to format the sequence of arguments.c                s7    |   } |    k r3 | d    |   7} n  | S)Nz: r   )argr\   )r  r%  	formatargr   r   formatargandannotation*  s    z-formatargspec.<locals>.formatargandannotationNr(  (z, )r  )rn   	enumeraterA   r   )r   r   r   r  r   r  r  r,  formatvarargsformatvarkwformatvalueZformatreturnsr%  r-  specsZfirstdefaultr   r+  specZ	kwonlyargr\   r   )r  r%  r,  r   formatargspec  s2    r6  c             C   s   d |  S)Nr(  r   )r]   r   r   r   r9   K  s    c             C   s   d |  S)Nz**r   )r]   r   r   r   r9   L  s    c             C   s   d t  |   S)Nr)  )r$  )rM   r   r   r   r9   M  s    c             C   s   | | | d d  } g  }	 x1 t  t |    D] }
 |	 j | |  |
   q. W| ry |	 j | |  | | |   n  | r |	 j | |  | | |   n  d d j |	  d S)af  Format an argument spec from the 4 values returned by getargvalues.

    The first four arguments are (args, varargs, varkw, locals).  The
    next four arguments are the corresponding optional formatting functions
    that are called to turn names and values into strings.  The ninth
    argument is an optional function to format the sequence of arguments.c             S   s   | |   | | |   S)Nr   )r]   localsr,  r3  r   r   r   convertT  s    z formatargvalues.<locals>.convertr.  z, r/  )r~   rn   rA   r   )r   r   r   r7  r,  r1  r2  r3  r8  r4  r   r   r   r   formatargvaluesI  s    $$r9  c                s     f d d   | D } t  |  } | d k r> | d } nW | d k r\ d j |   } n9 d j | d d     } | d d   =d j |  | } t d	 |  | | r d
 n d | d k r d n d | f   d  S)Nc                s(   g  |  ] } |   k r t  |   q Sr   )r$  )rP   r]   )r  r   r   rR   a  s   	 z&_missing_arguments.<locals>.<listcomp>r   r   r   z	{} and {}z, {} and {}z, z*%s() missing %i required %s argument%s: %s
positionalzkeyword-onlyr   r   r;  )rn   ri   r   r   )f_nameZargnamesposr  rI   missingr   tailr   )r  r   _missing_arguments`  s    r@  c          	      s1  t  |  | } t    f d d   | D  } | rQ | d k }	 d | f }
 nI | rv d }	 d | t  |  f }
 n$ t  |  d k }	 t t  |   }
 d } | r d } | | d k r d	 n d | | d k r d	 n d f } n  t d
 |  |
 |	 r d	 n d | | | d k r | r d n d f   d  S)Nc                s"   g  |  ] } |   k r |  q Sr   r   )rP   r+  )r  r   r   rR   r  s   	 z_too_many.<locals>.<listcomp>r   zat least %dTzfrom %d to %dr   z7 positional argument%s (and %d keyword-only argument%s)r   z5%s() takes %s positional argument%s but %d%s %s givenZwasZwere)rn   rs   r   )r<  r   Zkwonlyr   ZdefcountZgivenr  ZatleastZkwonly_givenZpluralr  Z
kwonly_sigmsgr   )r  r   	_too_manyp  s$    rB  c              O   s  |  d } |  d d  } t  |  } | \ } } } } }	 }
 } | j } i  } t |  r~ | j d k	 r~ | j f | } n  t |  } t |  } | r t |  n d } t | |  } x& t |  D] } | | | | | <q W| r	t | | d   | | <n  t | |	  } | r,i  | | <n  x | j	   D]z \ } } | | k r| spt
 d | | f   n  | | | | <q9n  | | k rt
 d | | f   n  | | | <q9W| | k r| rt | | |	 | | | |  n  | | k  r| d | |  } x0 | D]( } | | k rt | | d |  qqWxH t | | | d   D]) \ } } | | k rW| | | | <qWqWWn  d } xJ |	 D]B } | | k r|
 r| |
 k r|
 | | | <q| d 7} qqW| rt | |	 d |  n  | S)zGet the mapping of arguments to values.

    A dict is returned, with keys the function argument names (including the
    names of the * and ** arguments, if any), and values the respective bound
    values from 'positional' and 'named'.r   r   Nz*%s() got an unexpected keyword argument %rz(%s() got multiple values for argument %rTF)r  r   r   __self__rn   r}   r~   rW   r;   r?   r   rB  r@  r0  )Zfunc_and_positionalZnamedrj   r:  r5  r   r   r   r  r   r  r  r<  Z	arg2valueZnum_posZnum_argsZnum_defaultsnr   Zpossible_kwargskwrM   Zreqr+  r>  kwargr   r   r   getcallargs  sd    
	'rG  ClosureVarsz"nonlocals globals builtins unboundc       	      C   s^  t  |   r |  j }  n  t |   s< t d j |     n  |  j } |  j d k r] i  } n" d d   t | j |  j  D } |  j	 } | j
 d t j  } t |  r | j } n  i  } i  } t   } x~ | j D]s } | d	 k r q n  y | | | | <Wq t k
 rFy | | | | <Wn t k
 rA| j |  Yn XYq Xq Wt | | | |  S)
a  
    Get the mapping of free variables to their current values.

    Returns a named tuple of dicts mapping the current nonlocal, global
    and builtin references as seen by the body of the function. A final
    set of unbound names that could not be resolved is also provided.
    z'{!r}' is not a Python functionNc             S   s"   i  |  ] \ } } | j  |  q Sr   )cell_contents)rP   ZvarZcellr   r   r   
<dictcomp>  s   	z"getclosurevars.<locals>.<dictcomp>__builtins__NoneTrueFalse)zNonezTruezFalse)r   r   r   r   ri   r&   __closure__zipco_freevars__globals__r   r   r>   r   r;   co_namesKeyErrorrD   rH  )	rj   codeZnonlocal_varsZ	global_nsZ
builtin_nsZglobal_varsZbuiltin_varsZunbound_namesr]   r   r   r   getclosurevars  s8    							rV  	Tracebackz+filename lineno function code_context indexc             C   s5  t  |   r! |  j } |  j }  n	 |  j } t |   sN t d j |     n  t |   pc t |   } | d k r| d | d } y t	 |   \ } } Wn t
 k
 r d } } YqXt | d  } t d t | t |  |   } | | | |  } | d | } n
 d } } t | | |  j j | |  S)a  Get information about a frame or traceback object.

    A tuple of five things is returned: the filename, the line number of
    the current line, the function name, a list of lines of context from
    the source code, and the index of the current line within that list.
    The optional second argument specifies the number of lines of context
    to return, which are centered around the current line.z'{!r} is not a frame or traceback objectr   r   r   N)r-   	tb_linenor   f_linenor/   r   ri   r   r   r   r   maxr}   rn   rW  r   co_name)r!  contextlinenor   r   r   r   indexr   r   r   getframeinfo  s&    		"
r_  c             C   s   |  j  S)zCGet the line number from a frame object, allowing for optimization.)rY  )r!  r   r   r   	getlineno  s    r`  c             C   s=   g  } x0 |  r8 | j  |  f t |  |   |  j }  q	 W| S)zGet a list of records for a frame and all higher (calling) frames.

    Each record contains a frame object, filename, line number, function
    name, a list of lines of context, and index within the context.)rA   r_  f_back)r!  r\  	framelistr   r   r   getouterframes"  s
    	rc  c             C   s@   g  } x3 |  r; | j  |  j f t |  |   |  j }  q	 W| S)zGet a list of records for a traceback's frame and all lower frames.

    Each record contains a frame object, filename, line number, function
    name, a list of lines of context, and index within the context.)rA   r   r_  tb_next)tbr\  rb  r   r   r   getinnerframes-  s
    	 rf  c               C   s    t  t d  r t j d  Sd S)z?Return the frame of the caller or None if this is not possible.	_getframer   N)r   r{   rg  r   r   r   r   currentframe8  s    rh  c             C   s   t  t j d  |   S)z@Return a list of records for the stack above the caller's frame.r   )rc  r{   rg  )r\  r   r   r   stack<  s    ri  c             C   s   t  t j   d |   S)zCReturn a list of records for the stack below the current exception.r   )rf  r{   exc_info)r\  r   r   r   trace@  s    rk  c             C   s   t  j d j |   S)Nrb   )r   r>   r   )klassr   r   r   _static_getmroI  s    rm  c             C   sD   i  } y t  j |  d  } Wn t k
 r0 Yn Xt j | | t  S)Nr>   )r   __getattribute__rB   dictr   	_sentinel)r_   attrZinstance_dictr   r   r   _check_instanceL  s    rr  c             C   sZ   xS t  |   D]E } t t |   t k r y | j | SWqR t k
 rN YqR Xq q Wt S)N)rm  _shadowed_dictr   rp  r>   rT  )rl  rq  entryr   r   r   _check_classU  s    ru  c             C   s+   y t  |   Wn t k
 r& d SYn Xd S)NFT)rm  r   )r_   r   r   r   _is_type^  s
    	rv  c             C   s   t  j d } xw t |   D]i } y | j |  d } Wn t k
 rK Yq Xt  |  t j k o| | j d k o| | j | k s | Sq Wt	 S)Nr>   )
r   r>   rm  r   rT  r   r"   r   rS   rp  )rl  	dict_attrrt  Z
class_dictr   r   r   rs  e  s    rs  c             C   su  t  } t |   s` t |   } t |  } | t  k sK t |  t j k rf t |  |  } qf n |  } t | |  } | t  k	 r | t  k	 r t t |  d  t  k	 r t t |  d  t  k	 r | Sn  | t  k	 r | S| t  k	 r | S|  | k rUx\ t t |   D]E } t t |   t  k r	y | j	 | SWqNt
 k
 rJYqNXq	q	Wn  | t  k	 re| St |   d S)a  Retrieve attributes without triggering dynamic lookup via the
       descriptor protocol,  __getattr__ or __getattribute__.

       Note: this function may not be able to retrieve all attributes
       that getattr can fetch (like dynamically created attributes)
       and may find attributes that getattr can't (like descriptors
       that raise AttributeError). It can also return descriptor objects
       instead of instance members in some cases. See the
       documentation for details.
    r   r   N)rp  rv  r   rs  r   r    rr  ru  rm  r>   rT  rB   )r_   rq  r  Zinstance_resultrl  rw  Zklass_resultrt  r   r   r   getattr_statics  s6    rx  GEN_CREATEDGEN_RUNNINGGEN_SUSPENDED
GEN_CLOSEDc             C   s:   |  j  r t S|  j d k r  t S|  j j d k r6 t St S)a#  Get current state of a generator-iterator.

    Possible states are:
      GEN_CREATED: Waiting to start execution.
      GEN_RUNNING: Currently being executed by the interpreter.
      GEN_SUSPENDED: Currently suspended at a yield expression.
      GEN_CLOSED: Execution has completed.
    Nr   rx   )
gi_runningrz  gi_framer|  f_lastiry  r{  )	generatorr   r   r   getgeneratorstate  s    		r  c             C   sT   t  |   s$ t d j |     n  t |  d d  } | d k	 rL |  j j Si  Sd S)z
    Get the mapping of generator local variables to their current values.

    A dict is returned, with the keys the local variable names and values the
    bound values.z '{!r}' is not a Python generatorr~  N)r+   r   ri   rC   r~  r   )r  r!  r   r   r   getgeneratorlocals  s    
r  
from_bytesc             C   sC   y t  |  |  } Wn t k
 r+ d  SYn Xt | t  s? | Sd  S)N)rC   rB   r   _NonUserDefinedCallables)rQ   Zmethod_namemethr   r   r   "_signature_get_user_defined_method  s    	r  c             C   sE  |  j  } t | j    } | j p' f  } | j p6 i  } | rL | | } n  y |  j | |   } WnC t k
 r } z# d j |  }	 t |	  |  WYd  d  } ~ Xn Xd }
 x~| j   D]p\ } } y | j	 | } Wn t
 k
 r Yn X| j t k r| j |  q n  | j t k r_| | k rId }
 | j d |  | | <q_| j | j  q n  | j t k r| j d |  | | <n  |
 r | j t k	 st  | j t k r| | j d t  } | | | <| j |  q+| j t t f k r| j |  q+| j t k r+| j | j  q+q q W|  j d | j    S)Nz+partial object {!r} has incorrect argumentsFTr  r`   r  )r  r   r?   r   keywordsbind_partialr   ri   rh   	argumentsrT  r`   r  r   r  replacer]   r  AssertionErrormove_to_endr  r  r  )wrapped_sigpartialZ
extra_argsZ
old_params
new_paramsZpartial_argsZpartial_keywordsZbar  rA  Ztransform_to_kwonly
param_namer  Z	arg_valueZ	new_paramr   r   r   _signature_get_partial  sN    	"
r  c             C   s   t  |  j j    } | s5 | d j t t f k rD t d   n  | d j } | t t f k rv | d d   } n | t	 k	 r t d   n  |  j
 d |  S)Nr   zinvalid method signaturer   zinvalid argument typer  )rW   r  r  r`   r  r  rh   r  r  r  r  )r  paramsr`   r   r   r   _signature_bound_method0  s     r  c             C   s7   t  |   p6 t |   p6 t |  t  p6 |  t t f k S)N)r3   r   r   r  r   r   )r_   r   r   r   _signature_is_builtinI  s    r  c             C   s   t  |   s t |   r d St |  d d   } t |  d d   } t |  d t  } t |  d t  } t |  d d   } t | t j  o t | t  o | d  k s t | t  o | d  k s t | t	  o t | t	  S)NFr   r&   __defaults____kwdefaults____annotations__)
callabler   rC   _voidr   r   r0   rs   rW   ro  )r_   r]   rU  r  r  r  r   r   r   _signature_is_functionlikeT  s    r  c             C   s   |  j  d  s t  |  j d  } | d k rB |  j d  } n  |  j d  } | d	 k so | | k so t  |  j d  } | d
 k s | | k s t  |  d |  S)Nz($,r   r/  :r)  r   rx   rx   rx   )
startswithr  find)r5  r=  Zcposr   r   r   _signature_get_bound_paraml  s    r  c             C   s  |  s |  d d f Sd } d } d d   |  j  d  D } t |  j } t j |  } d } d } g  } | j }	 d }
 t j } t j } t |  } | j	 t j
 k s t  x<| D]4} | j	 | j } } | | k r^| d k r| r d } q | st  d } |
 d	 7}
 q n  | d
 k r^| s3t  | d k sEt  d } |
 d	 } q q^n  | | k r| d k r| d k st  |
 } q n  | rd } | | k o| d k s|	 d  qn  |	 |  | d k r |	 d  q q Wd j |  } | | | f S)a  
    Takes a signature in Argument Clinic's extended signature format.
    Returns a tuple of three things:
      * that signature re-rendered in standard Python syntax,
      * the index of the "self" parameter (generally 0), or None if
        the function does not have a "self" parameter, and
      * the index of the last "positional only" parameter,
        or None if the signature has no positional-only parameters.
    Nc             S   s   g  |  ] } | j  d    q S)ascii)encode)rP   lr   r   r   rR     s   	 z6_signature_strip_non_python_syntax.<locals>.<listcomp>rw   Fr   r  Tr   /$r/  z,  r   )ry   r   r   r   rA   r   OP
ERRORTOKENnextr   ENCODINGr  stringr   )	signatureself_parameterlast_positional_onlyr   r  Ztoken_streamZdelayed_commaZskip_next_commar*  rD   Zcurrent_parameterr  r  tr   r  clean_signaturer   r   r   "_signature_strip_non_python_syntax  sZ    				

	
r  Tc                sM  |  j    t |  \ } } } d | d } y t j |  } Wn t k
 rY d  } Yn Xt | t j  s t d j |    n  | j	 d }	 g     j
  t    d  } i   t | d d   }
 |
 r t j j |
 d   } | r | j  q n  t j  d d      f d d	   	 G	 f d
 d   d t j            f d d  } t |	 j j  } t |	 j j  } t j | | d d  } | d  k	 r  j  n	   j  xQ t t t |    D]7 \ } \ } } | | |  | | k r  j  qqW|	 j j rC  j  | |	 j j   n    j  x6 t |	 j j |	 j j   D] \ } } | | |  qhW|	 j j! r  j"  | |	 j j!   n  | d  k	 r: st#  t | d d   } | d  k	 } t$ |  } | r| s| r j% d  q: d j& d   j  } |  d <n  |   d |  j
 S)Nzdef fooz: passz"{!r} builtin has invalid signaturer   r   c             S   s=   t  |  t j  s t  |  j d  k r6 t d   n  |  j S)Nz'Annotations are not currently supported)r   astr+  r  r  rh   )noder   r   r   
parse_name  s    z&_signature_fromstr.<locals>.parse_namec                s   y t  |     } WnC t k
 rX y t  |    } Wn t k
 rS t    Yn XYn Xt | t  ru t j |  St | t t f  r t j	 |  St | t
  r t j |  S| d k r t j |  St    d  S)NTF)TFN)eval	NameErrorRuntimeErrorr   rs   r  ZStrintfloatZNumbytesZBytesZNameConstant)r   rM   )module_dictsys_module_dictr   r   
wrap_value  s     z&_signature_fromstr.<locals>.wrap_valuec                   s4   e  Z d  Z   f d d   Z   f d d   Z d S)z,_signature_fromstr.<locals>.RewriteSymbolicsc                s   g  } | } x/ t  | t j  r= | j | j  | j } q Wt  | t j  s\ t    n  | j | j  d j	 t
 |   }   |  S)Nr#  )r   r  rO   rA   rq  rM   Namer  rg   r   reversed)r   r  arD  rM   )r  r   r   visit_Attribute  s    z<_signature_fromstr.<locals>.RewriteSymbolics.visit_Attributec                s.   t  | j t j  s! t    n    | j  S)N)r   Zctxr  ZLoadrh   rg   )r   r  )r  r   r   
visit_Name  s    z7_signature_fromstr.<locals>.RewriteSymbolics.visit_NameN)r   r   r   r  r  r   )r  r   r   RewriteSymbolics  s   r  c                s    |   } |  k r d  S| r | t  k	 r y%    j |  } t j |  } Wn t k
 rm  } Yn X|  k r~ d  S|  k	 r | n | } n   j   |  d | d   d  S)Nr  r  )_emptyZvisitr  Zliteral_evalrh   rA   )Z	name_nodeZdefault_noder  r]   o)	Parameterr  r  invalidr`   r  r  r   r   p  s    z_signature_fromstr.<locals>.p	fillvaluerC  r`   r  )'_parameter_clsr  r  parseSyntaxErrorr   ZModulerh   ri   Zbodyr  r   rC   r{   r   r   r>   ZNodeTransformerr  r   r  	itertoolszip_longestPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORDr0  r   ZvarargVAR_POSITIONALKEYWORD_ONLYrP  r   Zkw_defaultsrF  VAR_KEYWORDr  r   r   r  )rQ   r_   r   r  r  r  r  Zprogramr   re   Zmodule_namer  r   r  r   r   r]   r  _selfZself_isboundZself_ismoduler   )
r  r  r  r  r`   r  r  r  r  r  r   _signature_fromstr  sl    				'	+		(	r  c             C   sg   t  |  s$ t d j |    n  t | d d   } | sT t d j |    n  t |  | | |  S)Nz%{!r} is not a Python builtin function__text_signature__z#no signature found for builtin {!r})r  r   ri   rC   rh   r  )rQ   rj   r  r   r   r   r   _signature_from_builtinS  s    r  c          !   C   s3  t  |   s$ t d j |     n  t |  t j  rb t |  j | |  } | r[ t |  S| Sn  | r t	 |  d d d   }  n  y |  j
 } Wn t k
 r Yn8 X| d  k	 r t | t  s t d j |    n  | Sy |  j } Wn t k
 r Yn Xt | t j  r|t | j | |  } t | | d  } t | j j    d } | f t | j j    } | j d |  St |   st |   rt j |   St |   rt t |  d | St |  t j  rt |  j | |  } t | |   Sd  } t |  t  r5t t |   d	  } | d  k	 r?t | | |  } n` t |  d
  }	 |	 d  k	 rot |	 | |  } n0 t |  d  }
 |
 d  k	 rt |
 | |  } n  | d  k rxS |  j d  d  D]> } y | j } Wn t k
 rYqX| rt  t |  |  SqWt |  j k r2|  j! t" j! k r/t# t"  Sq2qn t |  t$  st t |   d	  } | d  k	 ry t | | |  } Wqt% k
 r} z# d j |   } t% |  |  WYd  d  } ~ XqXqn  | d  k	 r| rt |  S| Sn  t |  t j&  rd j |   } t% |   n  t% d j |     d  S)Nz{!r} is not a callable objectrc   c             S   s   t  |  d  S)N__signature__)r   )re   r   r   r   r9   s  s    z%_signature_internal.<locals>.<lambda>z1unexpected object {!r} in __signature__ attributer   r  r  __call____new__r   r   zno signature found for {!r}z,no signature found for builtin function {!r}z+callable {!r} is not supported by signature)Nrx   )'r  r   ri   r   r   r   r  r   r  rl   r  rB   	Signature_partialmethod	functoolspartialmethodrj   r  rW   r  r  r  r   r  from_functionr  r  r  r   r  rb   r  r  r   r   r  r  rh   r2   )r_   r  r  r  r  r  Zfirst_wrapped_paramr  callnewZinitrJ   Ztext_sigr  rA  r   r   r   r  a  s    		
				(
r  c             C   s
   t  |   S)z/Get a signature object for the passed callable.)r  )r_   r   r   r   r    s    r  c               @   s   e  Z d  Z d Z d S)r  z0A private marker - used in Parameter & SignatureN)r   r   r   rr   r   r   r   r   r    s   r  c               @   s   e  Z d  Z d S)r  N)r   r   r   r   r   r   r   r    s   r  c               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)_ParameterKindc            G   s   t  j |  |  } | | _ | S)N)r  r  _name)r   r]   r   r_   r   r   r   r    s    	z_ParameterKind.__new__c             C   s   |  j  S)N)r  )r   r   r   r   __str__  s    z_ParameterKind.__str__c             C   s   d j  |  j  S)Nz<_ParameterKind: {!r}>)ri   r  )r   r   r   r   __repr__  s    z_ParameterKind.__repr__N)r   r   r   r  r  r  r   r   r   r   r    s   r  r]   r  r  r     r  r  c            
   @   s   e  Z d  Z d Z d Z e Z e Z e	 Z
 e Z e Z e Z d e d e d d	  Z e d
 d    Z e d d    Z e d d    Z e d d    Z d e d e d e d e d d  Z d d   Z d d   Z d d   Z d d   Z d S) r  a  Represents a parameter in a function signature.

    Has the following public attributes:

    * name : str
        The name of the parameter as a string.
    * default : object
        The default value for the parameter if specified.  If the
        parameter has no default value, this attribute is set to
        `Parameter.empty`.
    * annotation
        The annotation for the parameter if specified.  If the
        parameter has no annotation, this attribute is set to
        `Parameter.empty`.
    * kind : str
        Describes how argument values are bound to the parameter.
        Possible values: `Parameter.POSITIONAL_ONLY`,
        `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
        `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
    r  _kind_default_annotationr  r  c            C   s   | t  t t t t f k r* t d   n  | |  _ | t k	 rr | t t f k rr d j |  } t |   qr n  | |  _	 | |  _
 | t k r t d   n  t | t  s t d j |    n  | j   s t d j |    n  | |  _ d  S)Nz,invalid value for 'Parameter.kind' attributez({} parameters cannot have default valuesz*name is a required attribute for Parameterzname must be a str, not a {!r}z"{!r} is not a valid parameter name)r  r  r  r  r  rh   r  r  ri   r  r  r   rs   r   isidentifierr  )r   r]   r`   r  r  rA  r   r   r   r   @  s"    				zParameter.__init__c             C   s   |  j  S)N)r  )r   r   r   r   r]   Y  s    zParameter.namec             C   s   |  j  S)N)r  )r   r   r   r   r  ]  s    zParameter.defaultc             C   s   |  j  S)N)r  )r   r   r   r   r  a  s    zParameter.annotationc             C   s   |  j  S)N)r  )r   r   r   r   r`   e  s    zParameter.kindr]   r`   c            C   s   | t  k r |  j } n  | t  k r0 |  j } n  | t  k rH |  j } n  | t  k r` |  j } n  t |   | | d | d | S)z+Creates a customized copy of the Parameter.r  r  )r  r  r  r  r  r   )r   r]   r`   r  r  r   r   r   r  i  s    zParameter.replacec             C   s   |  j  } |  j } |  j t k	 r? d j | t |  j   } n  |  j t k	 rl d j | t |  j   } n  | t k r d | } n | t	 k r d | } n  | S)Nz{}:{}z{}={}r(  z**)
r`   r  r  r  ri   r%  r  r$  r  r  )r   r`   	formattedr   r   r   r  {  s    			zParameter.__str__c             C   s"   d j  |  j j t |   |  j  S)Nz<{} at {:#x} {!r}>)ri   	__class__r   rg   r]   )r   r   r   r   r    s    zParameter.__repr__c             C   sX   t  | j t  oW |  j | j k oW |  j | j k oW |  j | j k oW |  j | j k S)N)
issubclassr  r  r  r  r  r  )r   otherr   r   r   __eq__  s
    zParameter.__eq__c             C   s   |  j  |  S)N)r  )r   r  r   r   r   __ne__  s    zParameter.__ne__N)z_namez_kindz_defaultz_annotation)r   r   r   rr   	__slots__r  r  r  r  r  r  r  r  r  r  r  r  r   rT   r]   r  r  r`   r  r  r  r  r  r  r   r   r   r   r     s&   r  c               @   sp   e  Z d  Z d Z d d   Z e d d    Z e d d    Z e d d	    Z d
 d   Z	 d d   Z
 d S)BoundArgumentsa  Result of `Signature.bind` call.  Holds the mapping of arguments
    to the function's parameters.

    Has the following public attributes:

    * arguments : OrderedDict
        An ordered mutable mapping of parameters' names to arguments' values.
        Does not contain arguments' default values.
    * signature : Signature
        The Signature object that created this instance.
    * args : tuple
        Tuple of positional arguments values.
    * kwargs : dict
        Dict of keyword arguments values.
    c             C   s   | |  _  | |  _ d  S)N)r  
_signature)r   r  r  r   r   r   r     s    	zBoundArguments.__init__c             C   s   |  j  S)N)r  )r   r   r   r   r    s    zBoundArguments.signaturec             C   s   g  } x |  j  j j   D]x \ } } | j t t f k r> Pn  y |  j | } Wn t k
 rd PYq X| j t k r | j	 |  q | j
 |  q Wt |  S)N)r  r  r?   r`   r  r  r  rT  r  extendrA   rW   )r   r   r  r  r+  r   r   r   r     s    zBoundArguments.argsc             C   s   i  } d } x |  j  j j   D] \ } } | sm | j t t f k rO d } qm | |  j k rm d } q qm n  | sy q n  y |  j | } Wn t k
 r Yq X| j t k r | j |  q | | | <q W| S)NFT)	r  r  r?   r`   r  r  r  rT  update)r   kwargsZkwargs_startedr  r  r+  r   r   r   r    s&    		zBoundArguments.kwargsc             C   s4   t  | j t  o3 |  j | j k o3 |  j | j k S)N)r  r  r  r  r  )r   r  r   r   r   r    s    zBoundArguments.__eq__c             C   s   |  j  |  S)N)r  )r   r  r   r   r   r    s    zBoundArguments.__ne__N)r   r   r   rr   r   rT   r  r   r  r  r  r   r   r   r   r    s   r  c               @   s   e  Z d  Z d Z d# Z e Z e Z e	 Z
 d d e	 d d d d	 Z e d
 d    Z e d d    Z e d d    Z e d d    Z d e d e d d  Z d d   Z d d   Z d d d d  Z d d   Z d d    Z d! d"   Z d S)$r  a  A Signature object represents the overall signature of a function.
    It stores a Parameter object for each parameter accepted by the
    function, as well as information specific to the function itself.

    A Signature object has the following public attributes and methods:

    * parameters : OrderedDict
        An ordered mapping of parameters' names to the corresponding
        Parameter objects (keyword-only arguments are in the same order
        as listed in `code.co_varnames`).
    * return_annotation : object
        The annotation for the return type of the function if specified.
        If the function has no annotation for its return type, this
        attribute is set to `Signature.empty`.
    * bind(*args, **kwargs) -> BoundArguments
        Creates a mapping from positional and keyword arguments to
        parameters.
    * bind_partial(*args, **kwargs) -> BoundArguments
        Creates a partial mapping from positional and keyword arguments
        to parameters (simulating 'functools.partial' behavior.)
    _return_annotation_parametersNr  __validate_parameters__Tc            C   sg  | d k r t    } n0| r/t    } t } d } xt |  D] \ } } | j }	 | j }
 |	 | k  r d } | j | |	  } t |   n |	 | k r d } |	 } n  |	 t t f k r | j t	 k r | r d } t |   q q d } n  |
 | k rd j |
  } t |   n  | | |
 <q@ Wn t  d d   | D  } t
 j |  |  _ | |  _ d S)	zConstructs Signature from the given list of Parameter
        objects and 'return_annotation'.  All arguments are optional.
        NFz'wrong parameter order: {!r} before {!r}z-non-default argument follows default argumentTzduplicate parameter name: {!r}c             s   s   |  ] } | j  | f Vq d  S)N)r]   )rP   r  r   r   r   r   ?	  s   z%Signature.__init__.<locals>.<genexpr>)r   r  r0  r`   r]   ri   rh   r  r  r  r   MappingProxyTyper  r  )r   r  r  r  r  Ztop_kindZkind_defaultsidxr  r`   r]   rA  r   r   r   r   	  s<    					zSignature.__init__c             C   s  d } t  |  s? t |  r' d } q? t d j |    n  |  j } | j } | j } | j } t | d |   } | j	 } | | | |  }	 | j
 }
 | j } | j } | r t |  } n d } g  } | | } xI | d |  D]7 } |
 j | t  } | j | | d | d t  q Wx_ t | | d   D]G \ } } |
 j | t  } | j | | d | d t d | |  q?W| j t @r| | | } |
 j | t  } | j | | d | d t  n  xl |	 D]d } t } | d k	 r| j | t  } n  |
 j | t  } | j | | d | d t d |  qW| j t @r| | } | j t @ry| d	 7} n  | | } |
 j | t  } | j | | d | d t  n  |  | d
 |
 j d t  d | S)z2Constructs Signature for the given python functionFTz{!r} is not a Python functionNr   r  r`   r  r   r  r  r  )r   r  r   ri   r  r&   r   r   rW   r   r  r  r  rn   r   r  rA   r  r0  r'   r   r  r  r  r  )rQ   rj   Zis_duck_functionr  Z	func_codeZ	pos_countZ	arg_namesr:  Zkeyword_only_countZkeyword_onlyr  r  r  Zpos_default_countr  Znon_default_countr]   r  offsetr  r^  r   r   r   r  E	  sj    									
#

	zSignature.from_functionc             C   s   t  |  |  S)N)r  )rQ   rj   r   r   r   from_builtin	  s    zSignature.from_builtinc             C   s   |  j  S)N)r  )r   r   r   r   r  	  s    zSignature.parametersc             C   s   |  j  S)N)r  )r   r   r   r   r  	  s    zSignature.return_annotationr  c            C   sL   | t  k r |  j j   } n  | t  k r6 |  j } n  t |   | d | S)zCreates a customized copy of the Signature.
        Pass 'parameters' and/or 'return_annotation' arguments
        to override them in the new copy.
        r  )r  r  r  r  r   )r   r  r  r   r   r   r  	  s    zSignature.replacec             C   s2  t  t |  t  sF |  j | j k sF t |  j  t | j  k rJ d Sd d   t | j j    D } x t |  j j    D] \ } \ } } | j	 t
 k r y | j | } Wn t k
 r d SYq*X| | k r*d Sq y | | } Wn t k
 rd SYq X| | k s&| | j | k r d Sq Wd S)NFc             S   s   i  |  ] \ } } | |  q Sr   r   )rP   r  r  r   r   r   rJ  	  s   	z$Signature.__eq__.<locals>.<dictcomp>T)r  r   r  r  rn   r  r0  keysr?   r`   r  rT  )r   r  Zother_positionsr  r  r  Zother_paramZ	other_idxr   r   r   r  	  s,    	(		zSignature.__eq__c             C   s   |  j  |  S)N)r  )r   r  r   r   r   r  	  s    zSignature.__ne__r  Fc            C   sa  t    } t |  j j    } f  } t |  } xy t |  } Wnt k
 rPy t |  }	 Wn t k
 rx PYn X|	 j t k r Pn |	 j | k r |	 j t	 k r d }
 |
 j
 d |	 j  }
 t |
  d  n  |	 f } Pnh |	 j t k s|	 j t k	 r|	 f } Pn= | r"|	 f } Pn* d }
 |
 j
 d |	 j  }
 t |
  d  Yq3 Xy t |  }	 Wn! t k
 rt d  d  Yq3 X|	 j t t f k rt d   n  |	 j t k r| g } | j |  t |  | |	 j <Pn  |	 j | k rt d j
 d |	 j    n  | | |	 j <q3 d } x t j | |  D] }	 |	 j t k r\|	 } q;n  |	 j t k rqq;n  |	 j } y | j |  } WnU t k
 r| r|	 j t k r|	 j t k rt d j
 d |   d  n  Yq;X|	 j t	 k rt d j
 d |	 j    n  | | | <q;W| rQ| d k	 rB| | | j <qQt d   n  |  j |  |  S)z$Private method.  Don't use directly.zA{arg!r} parameter is positional only, but was passed as a keywordr+  Nz'{arg!r} parameter lacking default valueztoo many positional argumentsz$multiple values for argument {arg!r}ztoo many keyword arguments)r   r   r  r  r  StopIterationr`   r  r]   r  ri   r   r  r  r  r  r  rW   r  chainr   rT  _bound_arguments_cls)r   r   r  r  r  r  Zparameters_exZarg_valsZarg_valr  rA  r  Zkwargs_paramr  r   r   r   _bind	  s    						 zSignature._bindc              O   s   |  d j  |  d d  |  S)zGet a BoundArguments object, that maps the passed `args`
        and `kwargs` to the function's signature.  Raises `TypeError`
        if the passed arguments can not be bound.
        r   r   N)r  )r   r  r   r   r   bindQ
  s    zSignature.bindc              O   s$   |  d j  |  d d  | d d S)zGet a BoundArguments object, that partially maps the
        passed `args` and `kwargs` to the function's signature.
        Raises `TypeError` if the passed arguments can not be bound.
        r   r   Nr  T)r  )r   r  r   r   r   r  X
  s    zSignature.bind_partialc       	      C   s"  g  } d } d } x |  j  j   D] } t |  } | j } | t k rR d } n | rn | j d  d } n  | t k r d } n( | t k r | r | j d  d } n  | j |  q" W| r | j d  n  d j d j	 |   } |  j
 t k	 rt |  j
  } | d j |  7} n  | S)NFTr  r(  z({})z, z -> {})r  r  rs   r`   r  rA   r  r  ri   r   r  r  r%  )	r   r\   Zrender_pos_only_separatorZrender_kw_only_separatorr  r  r`   ZrenderedZannor   r   r   r  _
  s0    					zSignature.__str__)z_return_annotationz_parameters)r   r   r   rr   r  r  r  r  r   r  r  r   r[   r  r  rT   r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s$   2Qr  c              C   sg  d d l  }  d d l } |  j   } | j d d d | j d d d d	 d d
 | j   } | j } | j d  \ } } } y | j |  } }	 Wn` t k
 r }
 z@ d j	 | t
 |
  j |
  } t | d t j t d  WYd d }
 ~
 Xn X| r8| j d  } |	 } x  | D] } t | |  } qWn  |	 j t j k rjt d d t j t d  n  | j rSt d j	 |   t d j	 t |	    t d j	 |	 j   | |	 k rt d j	 t |	 j    t |	 d  rFt d j	 |	 j   qFn> y t |  \ } } Wn t k
 r2Yn Xt d j	 |   t d  n t t |   d S)z6 Logic for inspecting an object given at command line r   Nr   helpzCThe object to be analysed. It supports the 'module:qualname' syntaxz-dz	--detailsaction
store_truez9Display info about the module rather than its source coder  zFailed to import {} ({}: {})r   r   r#  z#Can't get info for builtin modules.r   z
Target: {}z
Origin: {}z
Cached: {}z
Loader: {}__path__zSubmodule search path: {}zLine: {}rw   )argparser   ArgumentParseradd_argument
parse_argsr   	partitionimport_modulerX   ri   r   r   printr{   stderrexitry   rC   builtin_module_namesZdetailsr   
__cached__r$  r   r   r  r   r   )r  r   parserr   targetZmod_nameZ	has_attrsattrsr_   r   r^   rA  partspart__r]  r   r   r   _main
  sV    				r  r   )rr   
__author__r  importlib.machineryr   r  r   r   r   r{   r   r   r   r   r  r   operatorr   collectionsr   r   Zdisr   Z_flag_namesImportErrorZCO_OPTIMIZEDZCO_NEWLOCALSr   r  Z	CO_NESTEDr(   Z	CO_NOFREEglobalsZmod_dictr?   rK   rL   r6   r   r   r   r   r   r   r!   r#   r   r)   r+   r-   r/   r1   r3   r4   r7   rN   rO   ra   r:   rl   rq   rv   rt   r   r   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   r   r   r   r   r   r   r  r	  r
  r  r  r"  r%  r'  rs   r6  r9  r@  rB  rG  rH  rV  rW  r_  r`  rc  rf  rh  ri  rk  r   rp  rm  rr  ru  rv  rs  rx  ry  rz  r{  r|  r  r  r   r  Z_WrapperDescriptorallZ_MethodWrapperr  r>   Z_ClassMethodWrapperr2   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r   <module>   s8  		
	
	
	,t!	.C-'


	X
						)		>5!			0KF}W :