U
    Pe}                     @   s   d Z ddlZG dd dZdS )a  
Weak Method
===========

The :class:`WeakMethod` is used by the :class:`~kivy.clock.Clock` class to
allow references to a bound method that permits the associated object to
be garbage collected. Please refer to
`examples/core/clock_method.py` for more information.

This WeakMethod class is taken from the recipe
http://code.activestate.com/recipes/81253/, based on the nicodemus version.
Many thanks nicodemus!
    Nc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
WeakMethodzzImplementation of a
    `weakref <http://en.wikipedia.org/wiki/Weak_reference>`_
    for functions and bound methods.
    c                 C   sf   d | _ d | _z4|jd k	r2|jj| _t|j| _n|| _ d | _W n  tk
r`   || _ d | _Y nX d S )N)methodmethod_name__self____func____name__weakrefproxyAttributeError)selfr    r   3/tmp/pip-unpacked-wheel-xzebddm3/kivy/weakmethod.py__init__   s    


zWeakMethod.__init__c                 C   s8   | j dk	r2zt| j | jW S  tk
r0   Y dS X | jS )zReturn a new bound-method like the original, or the
        original function if it was just a function or unbound
        method.
        Returns None if the original object doesn't exist.
        N)r	   getattrr   ReferenceErrorr   r   r   r   r   __call__%   s    
zWeakMethod.__call__c                 C   s>   | j dkrdS zt| j | j W dS  tk
r8   Y dS X dS )zReturns True if the referenced callable was a bound method and
        the instance no longer exists. Otherwise, return False.
        NFT)r	   r   r   r   r   r   r   r   is_dead3   s    
zWeakMethod.is_deadc                 C   s,   t | t |k	rdS |  }|d k	o*|| kS )NF)type)r   othersr   r   r   __eq__@   s    zWeakMethod.__eq__c                 C   s   d | j| j| jS )Nz.<WeakMethod proxy={} method={} method_name={}>)formatr	   r   r   r   r   r   r   __repr__F   s
      zWeakMethod.__repr__N)	r   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r      s   r   )r   r   r   r   r   r   r   <module>   s   