a
    xd0d                     @   s  d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZmZ ddlmZ d	d
lmZmZ d	dlmZ zddlmZ W n ey   dZY n0 zddlmZm Z  W n$ ey   G dd dZeZ Y n0 dZ!dZ"dd Z#dd Z$G dd deZG dd dZ%G dd de&e%Z'G dd dZ(e	)e( G dd de	Z*G d d! d!e*e%Z+G d"d# d#Z,e
)e, G d$d% d%Z-G d&d' d'e-Z.e)e. G d(d) d)ee-Z/dS )*z8Custom maps, sets, sequences, and other data structures.    N)OrderedDict)deque)CallableMappingMutableMapping
MutableSetSequence)heapifyheappopheappush)chaincount)Empty   )firstuniq)
match_case)reversed_dict)
LazyObjectLazySettingsc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   P/var/www/html/Ranjet/env/lib/python3.9/site-packages/celery/utils/collections.pyr      s   r   )AttributeDictMixinAttributeDict	BufferMapChainMapConfigurationViewDictAttribute	Evictable
LimitedSetMessagebufferr   force_mappinglpmergezK<{name}({size}): maxlen={0.maxlen}, expires={0.expires}, minlen={0.minlen}>c                 C   s*   t | ttfr| j} t | ts&t| S | S )z?Wrap object into supporting the mapping interface if necessary.)
isinstancer   r   Z_wrappedr   r    mr   r   r   r$   '   s    r$   c                    s    | j   fdd| D  | S )zpIn place left precedent dictionary merge.

    Keeps values from `L`, if the value in `R` is :const:`None`.
    c                    s"   g | ]\}}|d ur ||qS Nr   ).0kvsetitemr   r   
<listcomp>6       zlpmerge.<locals>.<listcomp>)__setitem__items)LRr   r-   r   r%   /   s    r%   c                   @   s<   e Zd ZdZdd Zeeds8er.d
ddZn
dddZd	S )r   z#Dict where insertion order matters.c                 C   s   t t|  S r)   )nextiterkeysselfr   r   r   _LRUkey=   s    zOrderedDict._LRUkeymove_to_endTc                 C   s   |st d| || |< d S )Nzno last=True on PyPy)NotImplementedErrorpop)r9   keylastr   r   r   r;   F   s    zOrderedDict.move_to_endc                 C   s   | j | }|d }|d }||d< ||d< | j}|r^|d }||d< ||d< | |d< |d< n(|d }||d< ||d< | |d< |d< d S Nr   r   )Z_OrderedDict__mapZ_OrderedDict__root)r9   r>   r?   linkZ	link_prevZ	link_nextrootZ
first_noder   r   r   r;   Q   s    
N)T)T)	r   r   r   __doc__r:   hasattr_OrderedDict_dict_is_orderedr;   r   r   r   r   r   :   s   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z[Mixin for Mapping interface that adds attribute access.

    I.e., `d.key -> d[key]`).
    c                 C   s:   z
| | W S  t y4   tt| jd|Y n0 dS )z`d.key -> d[key]`.z object has no attribute N)KeyErrorAttributeErrortyper   )r9   r+   r   r   r   __getattr__k   s    
zAttributeDictMixin.__getattr__c                 C   s   || |< dS )z"`d[key] = value -> d.key = value`.Nr   r9   r>   valuer   r   r   __setattr__t   s    zAttributeDictMixin.__setattr__N)r   r   r   rC   rJ   rM   r   r   r   r   r   e   s   	r   c                   @   s   e Zd ZdZdS )r   z$Dict subclass with attribute access.N)r   r   r   rC   r   r   r   r   r   z   s   r   c                   @   s   e Zd ZdZdZdd Zdd Zdd Zdd	d
ZdddZ	dd Z
dd Zdd Zdd ZeZdd Zdd ZeZdd ZeZeZeZeZdS )r    z[Dict interface to attributes.

    `obj[k] -> obj.k`
    `obj[k] = val -> obj.k = val`
    Nc                 C   s   t | d| d S )Nobj)objectrM   )r9   rN   r   r   r   __init__   s    zDictAttribute.__init__c                 C   s   t | j|S r)   )getattrrN   r9   r>   r   r   r   rJ      s    zDictAttribute.__getattr__c                 C   s   t | j||S r)   setattrrN   rK   r   r   r   rM      s    zDictAttribute.__setattr__c                 C   s&   z
| | W S  t y    | Y S 0 d S r)   rG   r9   r>   defaultr   r   r   get   s    
zDictAttribute.getc                 C   s   || vr|| |< d S r)   r   rV   r   r   r   
setdefault   s    zDictAttribute.setdefaultc                 C   s.   zt | j|W S  ty(   t|Y n0 d S r)   )rQ   rN   rH   rG   rR   r   r   r   __getitem__   s    zDictAttribute.__getitem__c                 C   s   t | j|| d S r)   rS   rK   r   r   r   r1      s    zDictAttribute.__setitem__c                 C   s   t | j|S r)   )rD   rN   rR   r   r   r   __contains__   s    zDictAttribute.__contains__c                 C   s   t t| jS r)   )r6   dirrN   r8   r   r   r   _iterate_keys   s    zDictAttribute._iterate_keysc                 C   s   |   S r)   r]   r8   r   r   r   __iter__   s    zDictAttribute.__iter__c                 c   s$   |   D ]}|t| j|fV  qd S r)   r]   rQ   rN   rR   r   r   r   _iterate_items   s    zDictAttribute._iterate_itemsc                 c   s    |   D ]}t| j|V  qd S r)   r`   rR   r   r   r   _iterate_values   s    zDictAttribute._iterate_values)N)N)r   r   r   rC   rN   rP   rJ   rM   rX   rY   rZ   r1   r[   r]   iterkeysr_   ra   	iteritemsrb   
itervaluesr2   r7   valuesr   r   r   r   r    ~   s(   

r    c                   @   s  e Zd ZdZdZdZdZdZg 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d3ddZdd Zdd Zdd Zdd ZeZd4dd Zd!d" Zd#d$ Zed%d& Zd'd( ZeZd)d* Zd+d, Z e Z!d-d. Z"e"Z#d/d0 Z$e$Z%d1d2 Z&e Z'e"Z(e$Z)dS )5r   z!Key lookup on a sequence of maps.Nc                 O   s8   t |p
i g}| jj|d||d |dd  d d S )Nkey_tr   r   )rg   mapschangesdefaults)list__dict__updaterX   )r9   rh   kwargsr   r   r   rP      s    
zChainMap.__init__c                 C   s(   t |}| jd| | jd| d S r@   )r$   rj   insertrh   )r9   dr   r   r   add_defaults   s    zChainMap.add_defaultsc                 G   s@   z| j d j|g|R  W S  ty:   td|Y n0 d S )Nr   z$Key not found in the first mapping: )rh   r=   rG   rV   r   r   r   r=      s    zChainMap.popc                 C   s   t |d S r)   rU   rR   r   r   r   __missing__   s    zChainMap.__missing__c                 C   s   | j d ur|  |S |S r)   )rg   rR   r   r   r   _key   s    zChainMap._keyc              	   C   sB   |  |}| jD ]&}z|| W   S  ty4   Y q0 q| |S r)   )rs   rh   rG   rr   )r9   r>   rs   mappingr   r   r   rZ      s    

zChainMap.__getitem__c                 C   s   || j | |< d S r)   ri   rs   rK   r   r   r   r1      s    zChainMap.__setitem__c                 C   s8   z| j | |= W n  ty2   td|Y n0 d S )Nz Key not found in first mapping: )ri   rs   rG   rR   r   r   r   __delitem__  s    zChainMap.__delitem__c                 C   s   | j   d S r)   ri   clearr8   r   r   r   rx   	  s    zChainMap.clearc                 C   s,   z| |  | W S  ty&   | Y S 0 d S r)   )rs   rG   rV   r   r   r   rX     s    zChainMap.getc                 C   s   t t j| j S r)   )lensetunionrh   r8   r   r   r   __len__  s    zChainMap.__len__c                 C   s   |   S r)   r^   r8   r   r   r   r_     s    zChainMap.__iter__c                    s"   |    t fdd| jD S )Nc                 3   s   | ]} |v V  qd S r)   r   )r*   r(   r>   r   r   	<genexpr>  r0   z(ChainMap.__contains__.<locals>.<genexpr>)rs   anyrh   rR   r   r}   r   r[     s    
zChainMap.__contains__c                 C   s
   t | jS r)   )r   rh   r8   r   r   r   __bool__   s    zChainMap.__bool__c                 C   s   |  |}|| vr|| |< d S r)   )rs   rV   r   r   r   rY   %  s    
zChainMap.setdefaultc                 O   s0   | j j|i |}| jD ]}||i | q|S r)   )ri   rm   
_observers)r9   argsrn   resultcallbackr   r   r   rm   +  s    
zChainMap.updatec                 C   s   d | dtt| jS )Nz{0.__class__.__name__}({1})z, )formatjoinmapreprrh   r8   r   r   r   __repr__2  s    zChainMap.__repr__c                 G   s   | t j|g|R  S )z?Create a ChainMap with a single dict created from the iterable.)dictfromkeys)clsiterabler   r   r   r   r   7  s    zChainMap.fromkeysc                 C   s&   | j | jd  g| jdd  R  S r@   )	__class__rh   copyr8   r   r   r   r   =  s    zChainMap.copyc                    s   t  fddt| jD  S )Nc                 3   s   | ]} |V  qd S r)   r   )r*   rp   opr   r   r~   H  r0   z!ChainMap._iter.<locals>.<genexpr>)r   reversedrh   )r9   r   r   r   r   _iterB  s    zChainMap._iterc                 C   s   t | dd S )Nc                 S   s   |   S r)   r7   )rp   r   r   r   <lambda>L  r0   z(ChainMap._iterate_keys.<locals>.<lambda>)r   r   r8   r   r   r   r]   J  s    zChainMap._iterate_keysc                    s    fdd D S )Nc                 3   s   | ]}| | fV  qd S r)   r   r*   r>   r8   r   r   r~   Q  r0   z*ChainMap._iterate_items.<locals>.<genexpr>r   r8   r   r8   r   ra   O  s    zChainMap._iterate_itemsc                    s    fdd D S )Nc                 3   s   | ]} | V  qd S r)   r   r   r8   r   r   r~   V  r0   z+ChainMap._iterate_values.<locals>.<genexpr>r   r8   r   r8   r   rb   T  s    zChainMap._iterate_valuesc                 C   s   | j | d S r)   )r   append)r9   r   r   r   r   bind_toY  s    zChainMap.bind_to)N)N)*r   r   r   rC   rg   ri   rj   rh   r   rP   rq   r=   rr   rs   rZ   r1   rv   rx   rX   r|   r_   r[   r   __nonzero__rY   rm   r   classmethodr   r   __copy__r   r]   rc   ra   rd   rb   re   r   r7   r2   rf   r   r   r   r   r      sN   




r   c                       sh   e Zd ZdZd fdd	Zdd Z fddZd	d
 Zdd ZdddZ	dd Z
dd Zdd Z  ZS )r   a  A view over an applications configuration dictionaries.

    Custom (but older) version of :class:`collections.ChainMap`.

    If the key does not exist in ``changes``, the ``defaults``
    dictionaries are consulted.

    Arguments:
        changes (Mapping): Map of configuration changes.
        defaults (List[Mapping]): List of dictionaries containing
            the default configuration.
    Nc                    sJ   |d u rg n|}t  j|g|R   | jj|r<|dd n||d d S )N_)prefix_keys)superrP   rl   rm   rstrip)r9   ri   rj   r7   r   r   r   r   rP   o  s    zConfigurationView.__init__c                 C   s4   | j }|r.||s|| n|}t|||fS |fS r)   )r   
startswithr   )r9   r>   r   Zpkeyr   r   r   _to_keysx  s
    zConfigurationView._to_keysc              	      s   |   }t j}|| jr2t fdd| jD nd D ]&}z||W   S  ty\   Y q80 q8z|  W S  ty   t|dkrtdj|  Y n0 d S )Nc                 3   s   | ]}| V  qd S r)   r   )r*   fr}   r   r   r~     r0   z0ConfigurationView.__getitem__.<locals>.<genexpr>r   r   z)Key not found: {0!r} (with prefix: {0!r}))	r   r   rZ   r   tuplerG   rr   ry   r   )r9   r>   r7   getitemr+   r   r}   r   rZ     s"    
 zConfigurationView.__getitem__c                 C   s   || j | |< d S r)   ru   rK   r   r   r   r1     s    zConfigurationView.__setitem__c                    s   t d  fdd|D S )Nc                 3   s   | ]}  |V  qd S r)   )rX   r   r8   r   r   r~     r0   z*ConfigurationView.first.<locals>.<genexpr>)r   )r9   r7   r   r8   r   r     s    zConfigurationView.firstc                 C   s&   z
| | W S  t y    | Y S 0 d S r)   rU   rV   r   r   r   rX     s    
zConfigurationView.getc                 C   s   | j   dS )z&Remove all changes, but keep defaults.Nrw   r8   r   r   r   rx     s    zConfigurationView.clearc                    s"   |  | t fdd| jD S )Nc                 3   s$   | ] t  fd dD V  qdS )c                 3   s   | ]}| v V  qd S r)   r   )r*   r+   r'   r   r   r~     r0   z;ConfigurationView.__contains__.<locals>.<genexpr>.<genexpr>N)r   )r*   r   r'   r   r~     r0   z1ConfigurationView.__contains__.<locals>.<genexpr>)r   r   rh   rR   r   r   r   r[     s    
zConfigurationView.__contains__c                 C   s@   |j d }|j d }| j j|||j d |j d |g| d d S )Nri   rj   rg   r   )ri   rj   rg   r   rh   )rl   rm   )r9   otherri   rj   r   r   r   	swap_with  s    

zConfigurationView.swap_with)NNN)N)r   r   r   rC   rP   r   rZ   r1   r   rX   rx   r[   r   __classcell__r   r   r   r   r   a  s   	
r   c                   @   s   e Zd ZdZdZd+ddZdd Zd	d
 Zdd Zd,ddZ	dd Z
dd ZeZd-d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d'd( ZeZed)d* ZdS )/r"   a/  Kind-of Set (or priority queue) with limitations.

    Good for when you need to test for membership (`a in set`),
    but the set should not grow unbounded.

    ``maxlen`` is enforced at all times, so if the limit is reached
    we'll also remove non-expired items.

    You can also configure ``minlen``: this is the minimal residual size
    of the set.

    All arguments are optional, and no limits are enabled by default.

    Arguments:
        maxlen (int): Optional max number of items.
            Adding more items than ``maxlen`` will result in immediate
            removal of items sorted by oldest insertion time.

        expires (float): TTL for all items.
            Expired items are purged as keys are inserted.

        minlen (int): Minimal residual size of this set.
            .. versionadded:: 4.0

            Value must be less than ``maxlen`` if both are configured.

            Older expired items will be deleted, only after the set
            exceeds ``minlen`` number of items.

        data (Sequence): Initial data to initialize set with.
            Can be an iterable of ``(key, value)`` pairs,
            a dict (``{key: insertion_time}``), or another instance
            of :class:`LimitedSet`.

    Example:
        >>> s = LimitedSet(maxlen=50000, expires=3600, minlen=4000)
        >>> for i in range(60000):
        ...     s.add(i)
        ...     s.add(str(i))
        ...
        >>> 57000 in s  # last 50k inserted values are kept
        True
        >>> '10' in s  # '10' did expire and was purged from set.
        False
        >>> len(s)  # maxlen is reached
        50000
        >>> s.purge(now=time.monotonic() + 7200)  # clock + 2 hours
        >>> len(s)  # now only minlen items are cached
        4000
        >>>> 57000 in s  # even this item is gone now
        False
       r   Nc                 C   s   |d u rdn|| _ |d u rdn|| _|d u r0dn|| _i | _g | _|rP| | | j | j  krhdksrn td| jdk rtdd S )Nr   z:minlen must be a positive number, less or equal to maxlen.zexpires cannot be negative!)maxlenminlenexpires_data_heaprm   
ValueError)r9   r   r   datar   r   r   r   rP     s    

zLimitedSet.__init__c                 C   s,   dd | j  D | jdd< t| j dS )z=Time consuming recreating of heap.  Don't run this too often.c                 S   s   g | ]}|qS r   r   )r*   entryr   r   r   r/     r0   z,LimitedSet._refresh_heap.<locals>.<listcomp>N)r   rf   r   r	   r8   r   r   r   _refresh_heap  s    zLimitedSet._refresh_heapc                 C   s   | j | jkr|   d S r)   )_heap_overloadmax_heap_percent_overloadr   r8   r   r   r   _maybe_refresh_heap	  s    zLimitedSet._maybe_refresh_heapc                 C   s   | j   g | jdd< dS )z)Clear all data, start from scratch again.N)r   rx   r   r8   r   r   r   rx     s    
zLimitedSet.clearc                 C   s`   |p
t  }|| jv r | | ||f}|| j|< t| j| | jr\t| j| jkr\|   dS )z=Add a new item, or reset the expiry time of an existing item.N)	time	monotonicr   discardr   r   r   ry   purge)r9   itemnowr   r   r   r   add  s    


zLimitedSet.addc                 C   s   |sdS t |tr2| j|j |   |   nvt |tr| D ]L\}}t |tt	frb|d }t |t
stdt|d| | || qDn|D ]}| | qdS )z8Update this set from other LimitedSet, dict or iterable.Nr   z$Expecting float timestamp, got type z with value: )r&   r"   r   rm   r   r   r   r2   r   rk   floatr   rI   r   )r9   r   r>   insertedrN   r   r   r   rm      s*    



zLimitedSet.updatec                 C   s   | j |d  |   d S r)   )r   r=   r   r9   r   r   r   r   r   :  s    zLimitedSet.discardc                 C   s   |p
t  }t|tr| n|}| jr@t| j| jkr@|   q&| jrt| j| j	  krbdkrn n(| j
d \}}|| j |krq|   qFdS )zCheck oldest items and remove them if needed.

        Arguments:
            now (float): Time of purging -- by default right now.
                This can be useful for unit testing.
        r   N)r   r   r&   r   r   ry   r   r=   r   r   r   )r9   r   Zinserted_timer   r   r   r   r   A  s    
 zLimitedSet.purgec                 C   sB   | j r>t| j \}}z| j| W n ty6   Y q 0 |S q |S )z?Remove and return the oldest item, or :const:`None` when empty.)r   r
   r   r=   rG   )r9   rW   r   r   r   r   r   r=   V  s    zLimitedSet.popc                 C   s   dd | j  D S )a6  Whole set as serializable dictionary.

        Example:
            >>> s = LimitedSet(maxlen=200)
            >>> r = LimitedSet(maxlen=200)
            >>> for i in range(500):
            ...     s.add(i)
            ...
            >>> r.update(s.as_dict())
            >>> r == s
            True
        c                 S   s   i | ]\}}||qS r   r   )r*   r   r>   r   r   r   
<dictcomp>q  r0   z&LimitedSet.as_dict.<locals>.<dictcomp>)r   rf   r8   r   r   r   as_dictc  s    zLimitedSet.as_dictc                 C   s   | j |j kS r)   r   r9   r   r   r   r   __eq__s  s    zLimitedSet.__eq__c                 C   s   |  | S r)   )r   r   r   r   r   __ne__w  s    zLimitedSet.__ne__c                 C   s   t j| t| jt| dS )N)namesize)REPR_LIMITED_SETr   rI   r   ry   r8   r   r   r   r   {  s    zLimitedSet.__repr__c                 C   s   dd t | j D S )Nc                 s   s   | ]\}}|V  qd S r)   r   )r*   r   ir   r   r   r~     r0   z&LimitedSet.__iter__.<locals>.<genexpr>)sortedr   rf   r8   r   r   r   r_     s    zLimitedSet.__iter__c                 C   s
   t | jS r)   )ry   r   r8   r   r   r   r|     s    zLimitedSet.__len__c                 C   s
   || j v S r)   r   rR   r   r   r   r[     s    zLimitedSet.__contains__c                 C   s   | j | j| j|  | jffS r)   )r   r   r   r   r   r8   r   r   r   
__reduce__  s    zLimitedSet.__reduce__c                 C   s
   t | jS r)   )boolr   r8   r   r   r   r     s    zLimitedSet.__bool__c                 C   s"   t | jd tt | jd d S )z5Compute how much is heap bigger than data [percents].d   r   )ry   r   maxr   r8   r   r   r   r     s    zLimitedSet._heap_overload)r   r   Nr   )N)N)N)r   r   r   rC   r   rP   r   r   rx   r   rm   r   Z	pop_valuer   r=   r   r   r   r   r_   r|   r[   r   r   r   propertyr   r   r   r   r   r"     s0   5



r"   c                   @   s2   e Zd ZdZeZdd ZdefddZdd Zd	S )
r!   z2Mixin for classes supporting the ``evict`` method.c                 C   s   | j td dS )z&Force evict until maxsize is enforced.)rangeN)_evictr   r8   r   r   r   evict  s    zEvictable.evictr   c                    s2   z fdd||D  W n t y,   Y n0 d S )Nc                    s   g | ]}   qS r   )_evict1)r*   r   r8   r   r   r/     r0   z$Evictable._evict.<locals>.<listcomp>)
IndexError)r9   limitr   r   r8   r   r     s    zEvictable._evictc                 C   s>   | j | jkrt z|   W n | jy8   t Y n0 d S r)   )_evictcountmaxsizer   _pop_to_evictr   r8   r   r   r   r     s    zEvictable._evict1N)	r   r   r   rC   r   r   r   r   r   r   r   r   r   r!     s
   r!   c                   @   s~   e Zd ZdZeZdef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dd Zdd Zedd ZdS )r#   zA buffer of pending messages.Nc                 C   s@   || _ ||pg | _| jj| _| jj| _| jj| _| jj| _	d S r)   )
r   r   r   _appendpopleft_popr|   _lenextend_extend)r9   r   r   r   r   r   r   rP     s    


zMessagebuffer.__init__c                 C   s   |  | | jo|   d S r)   )r   r   r   r   r   r   r   put  s    
zMessagebuffer.putc                 C   s   |  | | jo|   d S r)   )r   r   r   )r9   itr   r   r   r     s    
zMessagebuffer.extendc                 G   s:   z
|   W S  ty4   |r(|d  Y S |  Y n0 d S )Nr   )r   r   r   r9   rW   r   r   r   take  s    
zMessagebuffer.takec                 C   s   |   S r)   )r   r8   r   r   r   r     s    zMessagebuffer._pop_to_evictc                 C   s$   dt | j dt|  d| j dS N<z: />)rI   r   ry   r   r8   r   r   r   r     s    zMessagebuffer.__repr__c                 c   s,   z|   V  W q  ty$   Y q(Y q 0 q d S r)   )r   r   r8   r   r   r   r_     s    zMessagebuffer.__iter__c                 C   s   |   S r)   )r   r8   r   r   r   r|     s    zMessagebuffer.__len__c                 C   s
   || j v S r)   r   r   r   r   r   r[     s    zMessagebuffer.__contains__c                 C   s
   t | jS r)   )r   r   r8   r   r   r   __reversed__  s    zMessagebuffer.__reversed__c                 C   s
   | j | S r)   r   )r9   indexr   r   r   rZ     s    zMessagebuffer.__getitem__c                 C   s   t | S r)   ry   r8   r   r   r   r     s    zMessagebuffer._evictcount)r   r   r   rC   r   r   rP   r   r   r   r   r   r_   r|   r[   r   rZ   r   r   r   r   r   r   r#     s   		r#   c                       s   e Zd ZdZeZeZdZdZdZ	d f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dd Z  ZS )r   zMap of buffers.Nr     c                    s@   t    || _d| _|r$| | tdd |  D | _d S )Nr   c                 s   s   | ]}t |V  qd S r)   r   )r*   bufr   r   r   r~     r0   z%BufferMap.__init__.<locals>.<genexpr>)r   rP   r   
bufmaxsizerm   sumr2   total)r9   r   r   r   r   r   r   rP     s    

zBufferMap.__init__c                 C   s:   |  || |  jd7  _| | | jo4|   d S )Nr   )_get_or_create_bufferr   r   r;   r   r   )r9   r>   r   r   r   r   r     s    
zBufferMap.putc                 C   s4   |  || |  jt|7  _| jo.|   d S r)   )r   r   r   ry   r   r   )r9   r>   r   r   r   r   r      s    zBufferMap.extendc                 G   s   d\}}z| | }W n t y*   d}Y n@0 z| }|  jd8  _W n | jy^   d}Y n0 | | |r|rz|d S |  |S )N)NFTr   r   )rG   r   r   r   r;   )r9   r>   rW   r   throwr   r   r   r   r   &  s     


zBufferMap.takec                 C   s6   z
| | W S  t y0   |   }| |< | Y S 0 d S r)   )rG   _new_buffer)r9   r>   r   r   r   r   r   <  s
    
zBufferMap._get_or_create_bufferc                 C   s   | j | jdS )N)r   )Bufferr   r8   r   r   r   r   D  s    zBufferMap._new_bufferc                 G   s   | |    j| S r)   )r:   r   r   r   r   r   _LRUpopH  s    zBufferMap._LRUpopc              
   C   s   t dD ]t}|  }| | }z|  W n" t| jfyJ   | | Y q0 |  jd8  _t|sn| | n
| |  q~qd S )Nr   r   )	r   r:   r   r   r   r=   r   ry   r;   )r9   r   r>   r   r   r   r   r   L  s    
zBufferMap._pop_to_evictc                 C   s"   dt | j d| j d| j dS r   )rI   r   r   r   r8   r   r   r   r   a  s    zBufferMap.__repr__c                 C   s   | j S r)   )r   r8   r   r   r   r   e  s    zBufferMap._evictcount)Nr   )r   r   r   rC   r#   r   r   r   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s"   	r   )0rC   r   collectionsr   rE   r   collections.abcr   r   r   r   r   heapqr	   r
   r   	itertoolsr   r   queuer   Z
functionalr   r   textr   Z__pypy__r   rF   ImportErrorZdjango.utils.functionalr   r   __all__r   r$   r%   r   r   r   r    registerr   r   r"   r!   r#   r   r   r   r   r   <module>   sJ   

+J
 X f
G
