o
    Yi:                     @   s>  d Z ddlZddlZddlZddlZddlZddlZddlmZ dZdd Z	e
ddZd	d
 ZedZG dd dZdd ZdddZzddlmZ W n ey\   ddlmZ Y nw G dd deZeejZeejZedkr~ejs~dd Zee_nedkrejrnedkrdd Zee_eeZdd Zdd ZdS ) zU
Decorator module, see https://pypi.python.org/pypi/decorator
for the documentation.
    N)getfullargspecz4.0.5c                 C   s   | j S N)__init__)cls r   ;/tmp/pip-target-1s0edx8b/lib/python/scipy/_lib/decorator.pyget_init.   s   r   ArgSpeczargs varargs varkw defaultsc                 C   s   t | }t|j|j|j|jS )z$A replacement for inspect.getargspec)r   r	   argsvarargsvarkwdefaults)fspecr   r   r   
getargspec7   s   r   z\s*def\s*([_\w][_\w\d]*)\s*\(c                   @   sJ   e Zd ZdZe Z		dddZdd Zddd	Z	e
		
dddZdS )FunctionMakerz
    An object with the ability to create functions with a given signature.
    It has attributes name, doc, module, signature, defaults, dict, and
    methods update and make.
    Nc                 C   s  || _ |r|j| _| jdkrd| _|j| _|j| _t|rt	|}t
|di | _dD ]}	t| |	t
||	 q+t| jD ]\}
}t| d|
 | q<t| j}t| j}| jrg|d| j  |d| j  n| jro|d | jD ]}	||	 d ||	 d|	  qr| jr|d	| j  |d	| j  d
|| _d
|| _ |j | _|r|| _|d ur|| _|r|| _|r|| _|r|| _|r|| _t| dsJ t| dstd| d S )N<lambda>_lambda___annotations__)r
   r   r   r   
kwonlyargskwonlydefaultszarg%d*z=None=z**, name	signaturez#You are decorating a non-function: )shortsignature__name__r   __doc__doc
__module__moduleinspect
isfunctionr   getattrannotationssetattr	enumerater
   listr   appendr   r   joinr   __dict__copydictr   hasattr	TypeError)selffuncr   r   r   r   r!   funcdictargspecaiargallargsallshortargsr   r   r   r   K   s\   






zFunctionMaker.__init__c                 K   s   | j |_t| dd|_t| di |_t| dd|_t| dd|_t| dd|_zt	d}W n t
y9   d	}Y nw |jd
d	}t| d||_|j| dS )z2Update the signature of func with the data in selfr   Nr-   r   r   r   r%      ?r   r!   )r   r   r$   r   r+   __defaults____kwdefaults__r   sys	_getframeAttributeError	f_globalsgetr    update)r0   r1   kwframecallermoduler   r   r   rB      s   zFunctionMaker.updateFc                 K   s  |t |  }|p	i }t|}|du rtd| |d}t|gdd | jdD  }|D ]}	|	dv r@t|	 d| q1|	d	sJ|d	7 }d
t
| jf }
zt||
d}t|| W n   tdtjd t|tjd  || }|r|||d< | j|fi | |S )zBMake a new function from a given template and update the signatureNznot a valid function template
   c                 S   s   g | ]}| d qS )z *)strip.0r6   r   r   r   
<listcomp>   s    z&FunctionMaker.make.<locals>.<listcomp>,)_func__call_z is overridden in

z<decorator-gen-%d>singlezError in generated code:)file
__source__)varsDEFmatchSyntaxErrorgroupsetr   split	NameErrorendswithnext_compile_countcompileexecprintr=   stderrrB   )r0   	src_templevaldict	addsourceattrssrcmor   namesnfilenamecoder1   r   r   r   make   s8   




zFunctionMaker.makeTc                 K   s   t |tr| dd\}	}
|
dd }d}nd}	d}|}| ||	||||}ddd | D }|jd| ||fi |S )	z
        Create a function from the strings name, signature, and body.
        evaldict is the evaluation dictionary. If addsource is true, an
        attribute __source__ is added to the result. The attributes attrs
        are added, if any.
        (rF   NrN   c                 s   s    | ]}d | V  qdS )z    Nr   )rI   liner   r   r   	<genexpr>       z'FunctionMaker.create.<locals>.<genexpr>zdef %(name)s(%(signature)s):
)
isinstancestrrG   rX   r*   
splitlinesrk   )r   objbodyrb   r   r   r!   rc   rd   r   restr   r1   r0   ibodyr   r   r   create   s   
	
zFunctionMaker.create)NNNNNNN)NF)NNNT)r   r    __qualname__r   	itertoolscountr\   r   rB   rk   classmethodrx   r   r   r   r   r   A   s    
4
!r   c                 C   sB   | j  }||d< | |d< tj| d|| d}t| dr| j|_|S )zE
    decorate(func, caller) decorates a function using a caller.
    rM   rL   z)return _call_(_func_, %(shortsignature)s)__wrapped__ry   )__globals__r,   r   rx   r.   ry   )r1   callerrb   funr   r   r   decorate   s   

r   c                 C   s   |dur	t || S t| r#| j }t| }d| j d| j d}n$t| r9| jdkr0d}n| j}| }| j}n| jj }| j	j
}| j	j}|j }| |d< t |d< tj| d	d
||| j| dS )z=decorator(caller) converts a caller function into a decoratorNz
decorator(z2) converts functions/generators into factories of z objectsr   r   rM   
_decorate_z(func)zreturn _decorate_(func, _call_))r   r!   r~   )r   r"   isclassr   lowerr   r#   r   	__class____call____func__r   r,   r   rx   r    )r   _funcr   
callerfuncr   rb   r   r   r   	decorator   s2   








r   )_GeneratorContextManager)GeneratorContextManagerc                   @   s   e Zd Zdd ZdS )ContextManagerc                 C   s   t j|dt| |d|dS )zContext manager decoratorz.with _self_: return _func_(%(shortsignature)s))_self_rL   r}   )r   rx   r-   )r0   r1   r   r   r   r      s   zContextManager.__call__N)r   r    ry   r   r   r   r   r   r      s    r      c                 O   s   t | ||i |S r   r   r   r0   gr4   kr   r   r   r   	  s   r      c                 O   s   t | |||S r   r   r   r   r   r   r     s   c                 C   sT   d}t |D ]\}}t|| rd} nt| |r| ||< d}q|r(||  dS dS )z_
    Append ``a`` to the list of the virtual ancestors, unless it is already
    included.
    TFN)r'   
issubclassr)   )r4   
vancestorsaddjvar   r   r   r)     s   

r)   c                     sP   sJ ddd  dtjdffdd	  fdd	}d
 |_|S )zr
    Factory of decorators turning a function into a generic function
    dispatching on the given arguments.
    zNo dispatch args passedrl   r   z,) c                    s*   |t | t  rtdt   ddS )z5Make sure one passes the expected number of argumentsz	Expected z% arguments, got {len(arguments)}{msg}N)lenr/   )	argumentswrongmsg)dispatch_argsr   r   check2  s   zdispatch_on.<locals>.checkc                    s   t tj}t |kstd i fddfdd fdd} fdd	} fd
d}tjd dt|d| |d
S )z4Decorator turning a function into a generic functionzUnknown dispatch arguments c                     sl    |  dd t tD }D ]}t| ||D ]\}}}t||r-||jvr-t|| qqdd |D S )zU
            Get a list of sets of virtual ancestors for the given types
            c                 S   s   g | ]}g qS r   r   )rI   _r   r   r   rJ   G  s    zIdispatch_on.<locals>.gen_func_dec.<locals>.vancestors.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   )rW   )rI   rar   r   r   rJ   L  s    )ranger   zipr   __mro__r)   )typesrastypes_ttype_r   )r   r   typemapr   r   r   B  s   
z5dispatch_on.<locals>.gen_func_dec.<locals>.vancestorsc                     s    |  g }t | |  D ]7\}}t|}|dkr#td| d| |dkr8|\}td||fi jdd }n|j}||dd  q|S )zG
            Get a list of virtual MROs, one for each type
            rF   zAmbiguous dispatch for z: r   Nrm   )r   r   RuntimeErrortyper   r)   )r   listsr   vasn_vasr   mro)r   r   r   r   	ancestorsN  s   z4dispatch_on.<locals>.gen_func_dec.<locals>.ancestorsc                     s      fdd}|S )zU
            Decorator to register an implementation for the given types
            c                    s&    t | jtjd| j  | < | S )Nz in )r   r
   operatorltr   )r   )r   r   r   r   r   decg  s   z@dispatch_on.<locals>.gen_func_dec.<locals>.register.<locals>.decr   )r   r   )r   r   )r   r   registera  s   z3dispatch_on.<locals>.gen_func_dec.<locals>.registerc                     s$   |  dd t j |   D }|S )zI
            An utility to introspect the dispatch algorithm
            c                 S   s   g | ]}t d d |D qS )c                 s   s    | ]}|j V  qd S r   )r   )rI   r4   r   r   r   ro   r  s    zVdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<listcomp>.<genexpr>)tuple)rI   ancr   r   r   rJ   r  s    zLdispatch_on.<locals>.gen_func_dec.<locals>.dispatch_info.<locals>.<listcomp>)rz   product)r   lst)r   r   r   r   dispatch_infom  s
   z8dispatch_on.<locals>.gen_func_dec.<locals>.dispatch_infoc                    s   t dd | D }z| }W n	 ty   Y nw ||i |S tj |  }t| |D ]}|}|d urA||i |  S q-|i |S )Nc                 s   s    | ]}t |V  qd S r   )r   rH   r   r   r   ro   w  rp   zGdispatch_on.<locals>.gen_func_dec.<locals>._dispatch.<locals>.<genexpr>)r   KeyErrorrz   r   r[   rA   )r   r
   rC   r   r   combinationsr   )r   r1   r   r   r   	_dispatchv  s   
z4dispatch_on.<locals>.gen_func_dec.<locals>._dispatchzreturn _f_(z, %%(shortsignature)s))_f_)r   defaultr   r   r   r   r~   )rW   r   r
   rY   r   rx   r-   )r1   argsetr   r   r   r   r   dispatch_str)r   r1   r   r   r   gen_func_dec8  s   	z!dispatch_on.<locals>.gen_func_decdispatch_on)r*   r   ner   )r   r   r   r   r   r   *  s   
Vr   r   ) r   rer=   r"   r   rz   collectionsr   __version__r   
namedtupler	   r   r]   rS   r   r   r   
contextlibr   ImportErrorr   r   r   initr   r
   n_argsr   contextmanagerr)   r   r   r   r   r   <module>   sL   
 
!

