3
6ßL]  ã               @   sT   d Z ddlZddlZddlT ejZeedƒr.nG dd„ dejƒZG dd„ deƒZdS )aÊ   An implementation of weak references that works for bound methods.

This code is based on the code in the Python Cookbook, but you can call `ref`
for objects that are *not* bound methods too, in which case it just returns a
standard `weakref.ref`.

Weak references to bound methods are cached so that `ref(x) is ref(x)` as for
standard weakrefs, and the `ref` class defined here is therefore intended to be
used as a drop-in replacement for 'weakref.ref'.

é    N)Ú*Ú
WeakMethodc               @   s>   e Zd ZdZdZddd„Zd	d
„ Zdd„ Zdd„ Ze	j
jZdS )r   z£
        A custom `weakref.ref` subclass which simulates a weak reference to
        a bound method, working around the lifetime problem of bound methods.
        Ú	_func_refÚ
_meth_typeÚ_aliveÚ__weakref__Nc                s‚   y|j }|j}W n& tk
r6   tdjt|ƒƒƒ‚Y nX ‡ ‡fdd„}tjj| ||ƒ}tj||ƒ|_	t|ƒ|_
d|_tj|ƒ‰|S )Nz)argument should be a bound method, not {}c                s&   ˆƒ }|j r"d|_ ˆ d k	r"ˆ |ƒ d S )NF)r   )ÚargÚself)ÚcallbackÚself_wr© ú:/tmp/pip-install-5sebvq2j/envisage/envisage/safeweakref.pyÚ_cb-   s
    zWeakMethod.__new__.<locals>._cbT)Ú__self__Ú__func__ÚAttributeErrorÚ	TypeErrorÚformatÚtypeÚweakrefÚrefÚ__new__r   r   r   )ÚclsÚmethr
   ÚobjÚfuncr   r	   r   )r
   r   r   r   $   s    


zWeakMethod.__new__c             C   s4   t jj| ƒ}| jƒ }|d ks$|d kr(d S | j||ƒS )N)r   r   Ú__call__r   r   )r	   r   r   r   r   r   r   <   s
    zWeakMethod.__call__c             C   s@   t |tƒr<| j s|j r"| |kS tjj| |ƒo:| j|jkS dS )NF)Ú
isinstancer   r   r   r   Ú__eq__r   )r	   Úotherr   r   r   r   C   s
    
zWeakMethod.__eq__c             C   s@   t |tƒr<| j s|j r"| |k	S tjj| |ƒp:| j|jkS dS )NT)r   r   r   r   r   Ú__ne__r   )r	   r   r   r   r   r    J   s
    
zWeakMethod.__ne__)r   r   r   r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r    r   r   Ú__hash__r   r   r   r   r      s   
c               @   s"   e Zd ZdZejƒ Zddd„ZdS )r   zX An implementation of weak references that works for bound methods and     caches them. Nc             C   s\   t |dd ƒd k	rL| jj|ji ƒ}|j|jƒ}|d krHt||ƒ}|||j< |S tj||ƒS d S )Nr   )	ÚgetattrÚ_cacheÚ
setdefaultr   Úgetr   r   r   r   )r   r   r
   Z
func_cacher	   r   r   r   r   Y   s    

zref.__new__)N)r!   r"   r#   r$   r   ÚWeakKeyDictionaryr(   r   r   r   r   r   r   T   s   r   )r$   Úsysr   Ú__all__Úhasattrr   r   Úobjectr   r   r   r   Ú<module>   s   
8