î
ù-XaI  ã               @   s"  d  Z  d d l m Z m Z m Z m Z m Z m Z m Z d d l	 m
 Z
 m Z d d l Z d d l Z d d l Z e e f Z d d d d d	 d
 d d d d d d d g Z Gd d „  d e ƒ Z Gd d „  d e j ƒ Z Gd d „  d e ƒ Z Gd d	 „  d	 e j ƒ Z Gd d „  d ƒ Z d S)z{Weak reference support for Python.

This module is an implementation of PEP 205:

http://www.python.org/dev/peps/pep-0205/
é    )ÚgetweakrefcountÚgetweakrefsÚrefÚproxyÚCallableProxyTypeÚ	ProxyTypeÚReferenceType)ÚWeakSetÚ_IterationGuardNr   r   r   r   ÚWeakKeyDictionaryr   r   r   Ú
ProxyTypesÚWeakValueDictionaryr	   Ú
WeakMethodÚfinalizec                   s^   e  Z d  Z d Z d Z d d d „ Z ‡  f d	 d
 †  Z d d „  Z d d „  Z e	 j
 Z
 ‡  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 } Wn0 t k
 rH t d j t | ƒ ƒ ƒ d  ‚ Yn X‡  ‡ f d d †  } t j |  | | ƒ } t | | ƒ | _ t | ƒ | _	 d | _
 t | ƒ ‰ | S)Nz)argument should be a bound method, not {}c                s;   ˆ ƒ  } | j  r7 d | _  ˆ  d  k	 r7 ˆ  | ƒ q7 n  d  S)NF)r   )ÚargÚself)ÚcallbackÚself_wr© ú7/home/kubikplay/www/django_dtb/lib/python3.4/weakref.pyÚ_cb2   s
    			zWeakMethod.__new__.<locals>._cbT)Ú__self__Ú__func__ÚAttributeErrorÚ	TypeErrorÚformatÚtyper   Ú__new__r   r   r   )ÚclsZmethr   ÚobjÚfuncr   r   r   )r   r   r   r!   +   s    			zWeakMethod.__new__c                sG   t  ƒ  j ƒ  } |  j ƒ  } | d  k s3 | d  k r7 d  S|  j | | ƒ S)N)ÚsuperÚ__call__r   r   )r   r#   r$   )Ú	__class__r   r   r&   A   s
    zWeakMethod.__call__c             C   sS   t  | t ƒ rO |  j s# | j r- |  | k St j |  | ƒ oN |  j | j k Sd S)NF)Ú
isinstancer   r   r   Ú__eq__r   )r   Úotherr   r   r   r)   H   s
    
"zWeakMethod.__eq__c             C   sS   t  | t ƒ rO |  j s# | j r- |  | k	 St j |  | ƒ pN |  j | j k Sd S)NT)r(   r   r   r   Ú__ne__r   )r   r*   r   r   r   r+   O   s
    
"zWeakMethod.__ne__)z	_func_refz
_meth_typez_alivez__weakref__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r!   r&   r)   r+   r   Ú__hash__r   r   )r'   r   r   #   s   c               @   s  e  Z d  Z 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 d d „  Z e Z d d „  Z d d d „ Z d d „  Z d d „  Z e Z d d „  Z d d  „  Z d! d" „  Z d# d$ „  Z d d% d& „ Z d d' d( „ Z d) d* „  Z d S)+r   z›Mapping class that references values weakly.

    Entries in the dictionary will be discarded when no strong
    reference to the value exists anymore
    c             O   sT   t  |  ƒ d d „ } | |  _ g  |  _ t ƒ  |  _ i  |  _ } |  j | | Ž  d  S)Nc             S   sH   | ƒ  } | d  k	 rD | j  r4 | j j |  j ƒ qD | j |  j =n  d  S)N)Ú
_iteratingÚ_pending_removalsÚappendÚkeyÚdata)ÚwrÚselfrefr   r   r   r   Úremovef   s
    		z,WeakValueDictionary.__init__.<locals>.remove)r   Ú_remover3   Úsetr2   r6   Úupdate)r   ÚargsÚkwr9   Údr   r   r   Ú__init__e   s    		zWeakValueDictionary.__init__c             C   s0   |  j  } |  j } x | r+ | | j ƒ  =q Wd  S)N)r3   r6   Úpop)r   Úlr?   r   r   r   Ú_commit_removalst   s    			z$WeakValueDictionary._commit_removalsc             C   s3   |  j  | ƒ  } | d  k r+ t | ƒ ‚ n | Sd  S)N)r6   ÚKeyError)r   r5   Úor   r   r   Ú__getitem__|   s    zWeakValueDictionary.__getitem__c             C   s$   |  j  r |  j ƒ  n  |  j | =d  S)N)r3   rC   r6   )r   r5   r   r   r   Ú__delitem__ƒ   s    	zWeakValueDictionary.__delitem__c             C   s   t  |  j ƒ t  |  j ƒ S)N)Úlenr6   r3   )r   r   r   r   Ú__len__ˆ   s    zWeakValueDictionary.__len__c             C   s7   y |  j  | ƒ  } Wn t k
 r, d SYn X| d  k	 S)NF)r6   rD   )r   r5   rE   r   r   r   Ú__contains__‹   s
    	z WeakValueDictionary.__contains__c             C   s   d t  |  ƒ S)Nz<WeakValueDictionary at %s>)Úid)r   r   r   r   Ú__repr__’   s    zWeakValueDictionary.__repr__c             C   s6   |  j  r |  j ƒ  n  t | |  j | ƒ |  j | <d  S)N)r3   rC   ÚKeyedRefr:   r6   )r   r5   Úvaluer   r   r   Ú__setitem__•   s    	zWeakValueDictionary.__setitem__c             C   sO   t  ƒ  } x? |  j j ƒ  D]. \ } } | ƒ  } | d  k	 r | | | <q q W| S)N)r   r6   Úitems)r   Únewr5   r7   rE   r   r   r   Úcopyš   s    		zWeakValueDictionary.copyc             C   sk   d d l  m } |  j ƒ  } xH |  j j ƒ  D]7 \ } } | ƒ  } | d  k	 r, | | | | | ƒ <q, q, W| S)Nr   )Údeepcopy)rR   rS   r'   r6   rP   )r   ÚmemorS   rQ   r5   r7   rE   r   r   r   Ú__deepcopy__¤   s    	z WeakValueDictionary.__deepcopy__Nc             C   sK   y |  j  | } Wn t k
 r) | SYn X| ƒ  } | d  k rC | S| Sd  S)N)r6   rD   )r   r5   Údefaultr7   rE   r   r   r   Úget­   s    		zWeakValueDictionary.getc          
   c   sZ   t  |  ƒ H x@ |  j j ƒ  D]/ \ } } | ƒ  } | d  k	 r | | f Vq q WWd  QXd  S)N)r
   r6   rP   )r   Úkr7   Úvr   r   r   rP   º   s
    	zWeakValueDictionary.itemsc          
   c   sN   t  |  ƒ < x4 |  j j ƒ  D]# \ } } | ƒ  d  k	 r | Vq q WWd  QXd  S)N)r
   r6   rP   )r   rX   r7   r   r   r   ÚkeysÁ   s    zWeakValueDictionary.keysc          	   c   s)   t  |  ƒ  |  j j ƒ  Dd HWd QXd S)a  Return an iterator that yields the weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        N)r
   r6   Úvalues)r   r   r   r   ÚitervaluerefsÉ   s    
z!WeakValueDictionary.itervaluerefsc          
   c   sN   t  |  ƒ < x4 |  j j ƒ  D]# } | ƒ  } | d  k	 r | Vq q WWd  QXd  S)N)r
   r6   r[   )r   r7   r#   r   r   r   r[   Ö   s
    	zWeakValueDictionary.valuesc             C   sU   |  j  r |  j ƒ  n  x8 |  j j ƒ  \ } } | ƒ  } | d  k	 r | | f Sq Wd  S)N)r3   rC   r6   Úpopitem)r   r5   r7   rE   r   r   r   r]   Ý   s    		zWeakValueDictionary.popitemc             G   sx   |  j  r |  j ƒ  n  y |  j j | ƒ ƒ  } Wn# t k
 rT | rM | d S‚  Yn X| d  k rp t | ƒ ‚ n | Sd  S)Nr   )r3   rC   r6   rA   rD   )r   r5   r=   rE   r   r   r   rA   æ   s    	zWeakValueDictionary.popc             C   sg   y |  j  | } WnH t k
 r[ |  j r7 |  j ƒ  n  t | |  j | ƒ |  j  | <| SYn X| ƒ  Sd  S)N)r6   rD   r3   rC   rM   r:   )r   r5   rV   r7   r   r   r   Ú
setdefaultô   s    		zWeakValueDictionary.setdefaultc             K   s¨   |  j  r |  j ƒ  n  |  j } | d  k	 rˆ t | d ƒ sO t i  ƒ | ƒ } n  x6 | j ƒ  D]% \ } } t | |  j | ƒ | | <q\ Wn  t | ƒ r¤ |  j	 | ƒ n  d  S)NrP   )
r3   rC   r6   Úhasattrr    rP   rM   r:   rH   r<   )r   ÚdictÚkwargsr?   r5   rE   r   r   r   r<   ÿ   s    		 zWeakValueDictionary.updatec             C   s   t  |  j j ƒ  ƒ S)a~  Return a list of weak references to the values.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the values around longer than needed.

        )Úlistr6   r[   )r   r   r   r   Ú	valuerefs  s    
zWeakValueDictionary.valuerefs)r,   r-   r.   r/   r@   rC   rF   rG   rI   rJ   rL   rO   rR   Ú__copy__rU   rW   rP   rZ   Ú__iter__r\   r[   r]   rA   r^   r<   rc   r   r   r   r   r   Y   s.   		c                   s:   e  Z d  Z d Z d Z d d „  Z ‡  f d d †  Z ‡  S)rM   a[  Specialized reference that includes a key corresponding to the value.

    This is used in the WeakValueDictionary to avoid having to create
    a function object for each key stored in the mapping.  A shared
    callback object can use the 'key' attribute of a KeyedRef instead
    of getting a reference to the key from an enclosing scope.

    r5   c             C   s"   t  j |  | | ƒ } | | _ | S)N)r   r!   r5   )r    Úobr   r5   r   r   r   r   r!   $  s    	zKeyedRef.__new__c                s   t  ƒ  j | | ƒ d  S)N)r%   r@   )r   rf   r   r5   )r'   r   r   r@   )  s    zKeyedRef.__init__)zkey)r,   r-   r.   r/   r0   r!   r@   r   r   )r'   r   rM     s   rM   c               @   s  e  Z d  Z d Z d 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 e Z d d „  Z d d d „ Z d d „  Z d d „  Z d d „  Z e Z d d  „  Z d! d" „  Z d# d$ „  Z d% d& „  Z d d' d( „ Z d d) d* „ Z d S)+r   a‰   Mapping class that references keys weakly.

    Entries in the dictionary will be discarded when there is no
    longer a strong reference to the key. This can be used to
    associate additional data with an object owned by other parts of
    an application without adding attributes to those objects. This
    can be especially useful with objects that override attribute
    accesses.
    Nc             C   se   i  |  _  t |  ƒ d d „ } | |  _ g  |  _ t ƒ  |  _ d |  _ | d  k	 ra |  j | ƒ n  d  S)Nc             S   sB   | ƒ  } | d  k	 r> | j  r1 | j j |  ƒ q> | j |  =n  d  S)N)r2   r3   r4   r6   )rX   r8   r   r   r   r   r9   :  s
    		z*WeakKeyDictionary.__init__.<locals>.removeF)r6   r   r:   r3   r;   r2   Ú
_dirty_lenr<   )r   r`   r9   r   r   r   r@   8  s    				zWeakKeyDictionary.__init__c             C   sI   |  j  } |  j } x0 | rD y | | j ƒ  =Wq t k
 r@ Yq Xq Wd  S)N)r3   r6   rA   rD   )r   rB   r?   r   r   r   rC   I  s    			z"WeakKeyDictionary._commit_removalsc                s5   |  j  ‰  ‡  f d d †  |  j Dƒ |  _ d |  _ d  S)Nc                s"   g  |  ] } | ˆ  k r | ‘ q Sr   r   )Ú.0rX   )r?   r   r   ú
<listcomp>X  s   	 z5WeakKeyDictionary._scrub_removals.<locals>.<listcomp>F)r6   r3   rg   )r   r   )r?   r   Ú_scrub_removalsV  s    	z!WeakKeyDictionary._scrub_removalsc             C   s   d |  _  |  j t | ƒ =d  S)NT)rg   r6   r   )r   r5   r   r   r   rG   [  s    	zWeakKeyDictionary.__delitem__c             C   s   |  j  t | ƒ S)N)r6   r   )r   r5   r   r   r   rF   _  s    zWeakKeyDictionary.__getitem__c             C   s9   |  j  r |  j r |  j ƒ  n  t |  j ƒ t |  j ƒ S)N)rg   r3   rj   rH   r6   )r   r   r   r   rI   b  s    zWeakKeyDictionary.__len__c             C   s   d t  |  ƒ S)Nz<WeakKeyDictionary at %s>)rK   )r   r   r   r   rL   i  s    zWeakKeyDictionary.__repr__c             C   s   | |  j  t | |  j ƒ <d  S)N)r6   r   r:   )r   r5   rN   r   r   r   rO   l  s    zWeakKeyDictionary.__setitem__c             C   sO   t  ƒ  } x? |  j j ƒ  D]. \ } } | ƒ  } | d  k	 r | | | <q q W| S)N)r   r6   rP   )r   rQ   r5   rN   rE   r   r   r   rR   o  s    		zWeakKeyDictionary.copyc             C   sk   d d l  m } |  j ƒ  } xH |  j j ƒ  D]7 \ } } | ƒ  } | d  k	 r, | | | ƒ | | <q, q, W| S)Nr   )rS   )rR   rS   r'   r6   rP   )r   rT   rS   rQ   r5   rN   rE   r   r   r   rU   y  s    	zWeakKeyDictionary.__deepcopy__c             C   s   |  j  j t | ƒ | ƒ S)N)r6   rW   r   )r   r5   rV   r   r   r   rW   ‚  s    zWeakKeyDictionary.getc             C   s6   y t  | ƒ } Wn t k
 r( d SYn X| |  j k S)NF)r   r   r6   )r   r5   r7   r   r   r   rJ   …  s
    	zWeakKeyDictionary.__contains__c          
   c   sZ   t  |  ƒ H x@ |  j j ƒ  D]/ \ } } | ƒ  } | d  k	 r | | f Vq q WWd  QXd  S)N)r
   r6   rP   )r   r7   rN   r5   r   r   r   rP   Œ  s
    	zWeakKeyDictionary.itemsc          
   c   sH   t  |  ƒ 6 x. |  j D]# } | ƒ  } | d  k	 r | Vq q WWd  QXd  S)N)r
   r6   )r   r7   r#   r   r   r   rZ   “  s
    	zWeakKeyDictionary.keysc          
   c   sN   t  |  ƒ < x4 |  j j ƒ  D]# \ } } | ƒ  d  k	 r | Vq q WWd  QXd  S)N)r
   r6   rP   )r   r7   rN   r   r   r   r[   œ  s    zWeakKeyDictionary.valuesc             C   s   t  |  j ƒ S)az  Return a list of weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        )rb   r6   )r   r   r   r   Úkeyrefs¢  s    
zWeakKeyDictionary.keyrefsc             C   sH   d |  _  x8 |  j j ƒ  \ } } | ƒ  } | d  k	 r | | f Sq Wd  S)NT)rg   r6   r]   )r   r5   rN   rE   r   r   r   r]   ®  s    		zWeakKeyDictionary.popitemc             G   s"   d |  _  |  j j t | ƒ | Œ S)NT)rg   r6   rA   r   )r   r5   r=   r   r   r   rA   ¶  s    	zWeakKeyDictionary.popc             C   s   |  j  j t | |  j ƒ | ƒ S)N)r6   r^   r   r:   )r   r5   rV   r   r   r   r^   º  s    zWeakKeyDictionary.setdefaultc             K   s   |  j  } | d  k	 ro t | d ƒ s9 t i  ƒ | ƒ } n  x3 | j ƒ  D]" \ } } | | t | |  j ƒ <qF Wn  t | ƒ r‹ |  j | ƒ n  d  S)NrP   )r6   r_   r    rP   r   r:   rH   r<   )r   r`   ra   r?   r5   rN   r   r   r   r<   ½  s    	zWeakKeyDictionary.update)r,   r-   r.   r/   r@   rC   rj   rG   rF   rI   rL   rO   rR   rd   rU   rW   rJ   rP   rZ   re   r[   rk   r]   rA   r^   r<   r   r   r   r   r   -  s.   		c               @   sï   e  Z d  Z d Z f  Z i  Z d Z e j ƒ  Z	 d Z
 d Z Gd d „  d ƒ Z d d „  Z d d d	 „ Z d
 d „  Z d d „  Z e d d „  ƒ Z e d d „  ƒ Z e j d d „  ƒ Z d d „  Z e d d „  ƒ Z e d d „  ƒ Z d S)r   a  Class for finalization of weakrefable objects

    finalize(obj, func, *args, **kwargs) returns a callable finalizer
    object which will be called when obj is garbage collected. The
    first time the finalizer is called it evaluates func(*arg, **kwargs)
    and returns the result. After this the finalizer is dead, and
    calling it just returns None.

    When the program exits any remaining finalizers for which the
    atexit attribute is true will be run in reverse order of creation.
    By default atexit is true.
    Fc               @   s   e  Z d  Z d Z d S)	zfinalize._InfoÚweakrefr$   r=   ra   ÚatexitÚindexN)zweakrefzfunczargszkwargszatexitzindex)r,   r-   r.   r0   r   r   r   r   Ú_Infoá  s   ro   c             O   s¥   |  j  s1 d d  l } | j |  j ƒ d t _  n  |  j ƒ  } t | |  ƒ | _ | | _ | | _	 | pj d  | _
 d | _ t |  j ƒ | _ | |  j |  <d t _ d  S)Nr   T)Ú_registered_with_atexitrm   ÚregisterÚ	_exitfuncr   ro   r   rl   r$   r=   ra   ÚnextÚ_index_iterrn   Ú	_registryÚ_dirty)r   r#   r$   r=   ra   rm   Úinfor   r   r   r@   ä  s    				zfinalize.__init__Nc             C   sE   |  j  j |  d ƒ } | rA |  j rA | j | j | j p= i  Ž  Sd S)zZIf alive then mark as dead and return func(*args, **kwargs);
        otherwise return NoneN)ru   rA   Ú	_shutdownr$   r=   ra   )r   Ú_rw   r   r   r   r&   õ  s    zfinalize.__call__c             C   sh   |  j  j |  ƒ } | o! | j ƒ  } | d k	 rd |  j  j |  d ƒ rd | | j | j | j p` i  f Sd S)z^If alive then mark as dead and return (obj, func, args, kwargs);
        otherwise return NoneN)ru   rW   rl   rA   r$   r=   ra   )r   rw   r#   r   r   r   Údetachü  s    !zfinalize.detachc             C   sS   |  j  j |  ƒ } | o! | j ƒ  } | d k	 rO | | j | j | j pK i  f Sd S)zMIf alive then return (obj, func, args, kwargs);
        otherwise return NoneN)ru   rW   rl   r$   r=   ra   )r   rw   r#   r   r   r   Úpeek  s    zfinalize.peekc             C   s   |  |  j  k S)zWhether finalizer is alive)ru   )r   r   r   r   Úalive  s    zfinalize.alivec             C   s%   |  j  j |  ƒ } t | ƒ o$ | j S)z*Whether finalizer should be called at exit)ru   rW   Úboolrm   )r   rw   r   r   r   rm     s    zfinalize.atexitc             C   s.   |  j  j |  ƒ } | r* t | ƒ | _ n  d  S)N)ru   rW   r}   rm   )r   rN   rw   r   r   r   rm     s    c             C   sƒ   |  j  j |  ƒ } | o! | j ƒ  } | d  k rM d t |  ƒ j t |  ƒ f Sd t |  ƒ j t |  ƒ t | ƒ j t | ƒ f Sd  S)Nz<%s object at %#x; dead>z!<%s object at %#x; for %r at %#x>)ru   rW   rl   r    r,   rK   )r   rw   r#   r   r   r   rL     s    zfinalize.__repr__c             C   sC   d d „  |  j  j ƒ  Dƒ } | j d d d „  ƒ d d „  | Dƒ S)Nc             S   s+   g  |  ]! \ } } | j  r | | f ‘ q Sr   )rm   )rh   ÚfÚir   r   r   ri   )  s   	 z-finalize._select_for_exit.<locals>.<listcomp>r5   c             S   s   |  d j  S)Né   )rn   )Úitemr   r   r   Ú<lambda>*  s    z+finalize._select_for_exit.<locals>.<lambda>c             S   s   g  |  ] \ } } | ‘ q Sr   r   )rh   r~   r   r   r   r   ri   +  s   	 )ru   rP   Úsort)r"   ÚLr   r   r   Ú_select_for_exit&  s    zfinalize._select_for_exitc             C   sý   d } zÓ |  j  rØ d d  l } | j ƒ  r= d } | j ƒ  n  d  } x’ | d  k s[ t j rs |  j ƒ  } d t _ n  | s} Pn  | j ƒ  } y | ƒ  Wn% t k
 r» t	 j
 t	 j ƒ  Œ  Yn X| |  j  k sF t ‚ qF Wn  Wd  d t _ | rø | j ƒ  n  Xd  S)NFr   T)ru   ÚgcZ	isenabledZdisabler   rv   r…   rA   Ú	ExceptionÚsysÚ
excepthookÚexc_infoÚAssertionErrorrx   Zenable)r"   Zreenable_gcr†   Zpendingr~   r   r   r   rr   -  s.    	 	zfinalize._exitfunc)r,   r-   r.   r/   r0   ru   rx   Ú	itertoolsÚcountrt   rv   rp   ro   r@   r&   rz   r{   Úpropertyr|   rm   ÚsetterrL   Úclassmethodr…   rr   r   r   r   r   r   È  s$   	)r/   Ú_weakrefr   r   r   r   r   r   r   Ú_weakrefsetr	   r
   Úcollectionsrˆ   rŒ   r   Ú__all__r   ÚMutableMappingr   rM   r   r   r   r   r   r   Ú<module>   s   4			6¿›