o
    DfXc                     @   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mZmZ d
dlmZmZ d
dlmZ zddlm Z! W n e"ye   dZ!Y nw z
ddl#m$Z$m%Z% W n e"y   G dd dZ$e$Z%Y nw 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*Z0G d#d$ d$Z1e
.e1 G d%d& d&Z2G d'd( d(e2Z3e.e3 G d)d* d*ee2Z4dS )+z8Custom maps, sets, sequences, and other data structures.    N)OrderedDict)deque)CallableMappingMutableMapping
MutableSetSequence)heapifyheappopheappush)chaincount)Empty)AnyDictIterableList   )firstuniq)
match_case)reversed_dict)
LazyObjectLazySettingsc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   Q/home/ubuntu/webapp/venv/lib/python3.10/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   _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   s   " zlpmerge.<locals>.<listcomp>)__setitem__items)LRr   r2   r   r)   /   s   r)   c                   @   sB   e Zd ZdZdd Zeedserd
ddZd	S d
ddZd	S 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)r=   keylastr   r   r   r?   F   s   zOrderedDict.move_to_endc                 C   s   | j | }|d }|d }||d< ||d< | j}|r0|d }||d< ||d< | |d< |d< d S |d }||d< ||d< | |d< |d< d S Nr   r   )_OrderedDict__map_OrderedDict__root)r=   rB   rC   link	link_prev	link_nextroot
first_noder   r   r   r?   Q   s   
N)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eddfddZdS )	r   z[Mixin for Mapping interface that adds attribute access.

    I.e., `d.key -> d[key]`).
    c                 C   s2   z| | W S  t y   tt| jd|w )z`d.key -> d[key]`.z object has no attribute )KeyErrorAttributeErrortyper   )r=   r0   r   r   r   __getattr__k   s   
zAttributeDictMixin.__getattr__rB   returnNc                 C   s   || |< dS )z"`d[key] = value -> d.key = value`.Nr   r=   rB   valuer   r   r   __setattr__t      zAttributeDictMixin.__setattr__)r   r   r   rL   rS   strrW   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   rL   r   r   r   r   r    y   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)objectrW   )r=   rZ   r   r   r   __init__      zDictAttribute.__init__c                 C      t | j|S r.   )getattrrZ   r=   rB   r   r   r   rS      rX   zDictAttribute.__getattr__c                 C   s   t | j||S r.   setattrrZ   rU   r   r   r   rW         zDictAttribute.__setattr__c                 C   "   z| | W S  t y   | Y S w r.   rP   r=   rB   defaultr   r   r   get   
   
zDictAttribute.getc                 C   s   || vr
|| |< d S d S r.   r   rf   r   r   r   
setdefault   s   zDictAttribute.setdefaultc                 C   s&   zt | j|W S  ty   t|w r.   )r_   rZ   rQ   rP   r`   r   r   r   __getitem__   s
   zDictAttribute.__getitem__c                 C   s   t | j|| d S r.   ra   rU   r   r   r   r5      r]   zDictAttribute.__setitem__c                 C   r^   r.   )rM   rZ   r`   r   r   r   __contains__   rX   zDictAttribute.__contains__c                 C   s   t t| jS r.   )r:   dirrZ   r<   r   r   r   _iterate_keys   rc   zDictAttribute._iterate_keysc                 C      |   S r.   rn   r<   r   r   r   __iter__      zDictAttribute.__iter__c                 c   s&    |   D ]}|t| j|fV  qd S r.   rn   r_   rZ   r`   r   r   r   _iterate_items   s   zDictAttribute._iterate_itemsc                 c   s"    |   D ]	}t| j|V  qd S r.   rs   r`   r   r   r   _iterate_values   s   zDictAttribute._iterate_valuesr.   )r   r   r   rL   rZ   r\   rS   rW   rh   rj   rk   r5   rl   rn   iterkeysrq   rt   	iteritemsru   
itervaluesr6   r;   valuesr   r   r   r   r$   }   s*    

r$   c                   @   s  e Zd ZdZdZdZd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d4d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#d0d1 Z$e$Z%d2d3 Z&e Z'e"Z(e$Z)dS )5r"   z!Key lookup on a sequence of maps.Nr   c                 O   s:   t |pi g}| jj|d||d |dd  g d d S )Nkey_tr   r   )rz   mapschangesdefaults
_observers)list__dict__updaterh   )r=   r{   kwargsr   r   r   r\      s   

zChainMap.__init__c                 C   s(   t |}| jd| | jd| d S rD   )r(   r}   insertr{   )r=   dr   r   r   add_defaults   s   zChainMap.add_defaultsc                 G   s8   z| j d j|g|R  W S  ty   td|w )Nr   z$Key not found in the first mapping: )r{   rA   rP   rf   r   r   r   rA      s   zChainMap.popc                 C   s   t |r.   re   r`   r   r   r   __missing__   rr   zChainMap.__missing__c                 C   s   | j d ur
|  |S |S r.   )rz   r`   r   r   r   _key      zChainMap._keyc              	   C   s@   |  |}| jD ]}z|| W   S  ty   Y qw | |S r.   )r   r{   rP   r   )r=   rB   r   mappingr   r   r   rk      s   


zChainMap.__getitem__c                 C      || j | |< d S r.   r|   r   rU   r   r   r   r5         zChainMap.__setitem__c                 C   s2   z
| j | |= W d S  ty   td|w )Nz Key not found in first mapping: )r|   r   rP   r`   r   r   r   __delitem__  s
   zChainMap.__delitem__c                 C   s   | j   d S r.   r|   clearr<   r   r   r   r   	  rc   zChainMap.clearc                 C   s(   z| |  | W S  ty   | Y S w r.   )r   rP   rf   r   r   r   rh     s
   zChainMap.getc                 C   s   t t j| j S r.   )lensetunionr{   r<   r   r   r   __len__  r]   zChainMap.__len__c                 C   ro   r.   rp   r<   r   r   r   rq     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-   rB   r   r   	<genexpr>      z(ChainMap.__contains__.<locals>.<genexpr>)r   anyr{   r`   r   r   r   rl        
zChainMap.__contains__c                 C   
   t | jS r.   )r   r{   r<   r   r   r   __bool__      
zChainMap.__bool__c                 C   s"   |  |}|| vr|| |< d S d S r.   )r   rf   r   r   r   rj   %  s   
zChainMap.setdefaultc                 O   s0   | j j|i |}| jD ]	}||i | q|S r.   )r|   r   r~   )r=   argsr   resultcallbackr   r   r   r   +  s   
zChainMap.updatec                 C   s   d | dtt| jS )Nz{0.__class__.__name__}({1})z, )formatjoinmapreprr{   r<   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 rD   )	__class__r{   copyr<   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/   r   opr   r   r   H  r   z!ChainMap._iter.<locals>.<genexpr>)r   reversedr{   )r=   r   r   r   r   _iterB  s   zChainMap._iterc                 C   s   t | dd S )Nc                 S   ro   r.   r;   )r   r   r   r   <lambda>L  s    z(ChainMap._iterate_keys.<locals>.<lambda>)r   r   r<   r   r   r   rn   J  r]   zChainMap._iterate_keysc                        fdd D S )Nc                 3   s    | ]	}| | fV  qd S r.   r   r/   rB   r<   r   r   r   Q  s    z*ChainMap._iterate_items.<locals>.<genexpr>r   r<   r   r<   r   rt   O  r]   zChainMap._iterate_itemsc                    r   )Nc                 3   s    | ]} | V  qd S r.   r   r   r<   r   r   r   V  r   z+ChainMap._iterate_values.<locals>.<genexpr>r   r<   r   r<   r   ru   T  r]   zChainMap._iterate_valuesc                 C   s   | j | d S r.   )r~   append)r=   r   r   r   r   bind_toY  s   zChainMap.bind_tor.   )*r   r   r   rL   rz   r|   r}   r{   r~   r\   r   rA   r   r   rk   r5   r   r   rh   r   rq   rl   r   __nonzero__rj   r   r   classmethodr   r   __copy__r   rn   rv   rt   rw   ru   rx   r   r;   r6   ry   r   r   r   r   r"      sP    



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)superr\   r   r   rstrip)r=   r|   r}   r;   r   r   r   r   r\   o  s   
zConfigurationView.__init__c                 C   s4   | j }|r||s|| n|}t|||fS |fS r.   )r   
startswithr   )r=   rB   r   pkeyr   r   r   _to_keysx  s
   zConfigurationView._to_keysc              	      s   |   }t j}|| jrt fdd| jD nd D ]}z||W   S  ty.   Y qw z|  W S  tyJ   t|dkrItdj|  w )Nc                 3   s    | ]}| V  qd S r.   r   )r/   fr   r   r   r     r   z0ConfigurationView.__getitem__.<locals>.<genexpr>r   r   z)Key not found: {0!r} (with prefix: {0!r}))	r   r   rk   r   tuplerP   r   r   r   )r=   rB   r;   getitemr0   r   r   r   rk     s&   
 zConfigurationView.__getitem__c                 C   r   r.   r   rU   r   r   r   r5     r   zConfigurationView.__setitem__c                    s   t d  fdd|D S )Nc                 3   s    | ]}  |V  qd S r.   )rh   r   r<   r   r   r     s    z*ConfigurationView.first.<locals>.<genexpr>)r   )r=   r;   r   r<   r   r     r   zConfigurationView.firstc                 C   rd   r.   re   rf   r   r   r   rh     ri   zConfigurationView.getc                 C   s   | j   dS )z&Remove all changes, but keep defaults.Nr   r<   r   r   r   r     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/   r0   r,   r   r   r     r   z;ConfigurationView.__contains__.<locals>.<genexpr>.<genexpr>N)r   )r/   r   r,   r   r     s   $ z1ConfigurationView.__contains__.<locals>.<genexpr>)r   r   r{   r`   r   r   r   rl     r   zConfigurationView.__contains__c                 C   s@   |j d }|j d }| j j|||j d |j d |g| d d S )Nr|   r}   rz   r   )r|   r}   rz   r   r{   )r   r   )r=   otherr|   r}   r   r   r   	swap_with  s   


zConfigurationView.swap_with)NNNr.   )r   r   r   rL   r\   r   rk   r5   r   rh   r   rl   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e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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 rdn|| _i | _g | _|r(| | | j | j  kr7dks<td td| jdk rEt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_heapr   
ValueError)r=   r   r   datar   r   r   r   r\     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   r4     s    z,LimitedSet._refresh_heap.<locals>.<listcomp>N)r   ry   r   r	   r<   r   r   r   _refresh_heap  s   zLimitedSet._refresh_heapc                 C   s   | j | jkr|   d S d S r.   )_heap_overloadmax_heap_percent_overloadr   r<   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   r   r   r<   r   r   r   r     s   
zLimitedSet.clearc                 C   sh   |pt  }|| jv r| | ||f}|| j|< t| j| | jr0t| j| jkr2|   dS dS dS )z=Add a new item, or reset the expiry time of an existing item.N)	time	monotonicr   discardr   r   r   r   purge)r=   itemnowr   r   r   r   add  s   


zLimitedSet.addc                 C   s   |sdS t |tr| j|j |   |   dS t |trL| D ]&\}}t |tt	fr2|d }t |t
sCtdt|d| | || q#dS |D ]}| | qNdS )z8Update this set from other LimitedSet, dict or iterable.Nr   z$Expecting float timestamp, got type z with value: )r*   r&   r   r   r   r   r   r6   r   r   floatr   rR   r   )r=   r   rB   insertedrZ   r   r   r   r      s.   


zLimitedSet.updatec                 C   s   | j |d  |   d S r.   )r   rA   r   r=   r   r   r   r   r   :  s   zLimitedSet.discardc                 C   s   |pt  }t|tr| n|}| jr't| j| jkr'|   t| j| jks| jrat| j| j	  kr8dkrcn dS | j
d \}}|| j |krKdS |   t| j| j	  kr^dks;dS  dS dS d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   r   r   rA   r   r   r   )r=   r   inserted_timer   r   r   r   r   A  s   "$zLimitedSet.purgerg   rT   c                 C   sD   | j r t| j \}}z	| j| W |S  ty   Y nw | j s|S )z?Remove and return the oldest item, or :const:`None` when empty.)r   r
   r   rA   rP   )r=   rg   r   r   r   r   r   rA   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   rB   r   r   r   
<dictcomp>p  s    z&LimitedSet.as_dict.<locals>.<dictcomp>)r   ry   r<   r   r   r   as_dictb  s   zLimitedSet.as_dictc                 C   s   | j |j kS r.   r   )r=   r   r   r   r   __eq__r  rX   zLimitedSet.__eq__c                 C   s   t j| t| jt| dS )N)namesize)REPR_LIMITED_SETr   rR   r   r   r<   r   r   r   r   v  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   ~  r   z&LimitedSet.__iter__.<locals>.<genexpr>)sortedr   ry   r<   r   r   r   rq   |  r   zLimitedSet.__iter__c                 C   r   r.   )r   r   r<   r   r   r   r     r   zLimitedSet.__len__c                 C   
   || j v S r.   r   r`   r   r   r   rl     r   zLimitedSet.__contains__c                 C   s   | j | j| j|  | jffS r.   )r   r   r   r   r   r<   r   r   r   
__reduce__  s   zLimitedSet.__reduce__c                 C   r   r.   )boolr   r<   r   r   r   r     r   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   )r   r   maxr   r<   r   r   r   r     s   "zLimitedSet._heap_overload)r   r   Nr   r.   )r   r   r   rL   r   r\   r   r   r   r   r   r   	pop_valuer   r   rA   r   r   r   rq   r   rl   r   r   r   propertyr   r   r   r   r   r&     s0    5


r&   c                   @   s@   e Zd ZdZeZdddZdefdeddfdd	Zdd
dZ	dS )r%   z2Mixin for classes supporting the ``evict`` method.rT   Nc                 C   s   | j td dS )z&Force evict until maxsize is enforced.)rangeN)_evictr   r<   r   r   r   evict  s   zEvictable.evictr   limitc                    s2   z fdd||D  W d S  t y   Y d S w )Nc                    s   g | ]}   qS r   )_evict1)r/   r   r<   r   r   r4     s    z$Evictable._evict.<locals>.<listcomp>)
IndexError)r=   r   r   r   r<   r   r     s
   zEvictable._evictc                 C   s8   | j | jkr	t z|   W d S  | jy   t w r.   )_evictcountmaxsizer   _pop_to_evictr   r<   r   r   r   r     s   zEvictable._evict1)rT   N)
r   r   r   rL   r   r   r   intr   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ef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)r=   r   r   r   r   r   r   r\     s   


zMessagebuffer.__init__c                 C   "   |  | | jo|   d S  d S r.   )r   r   r   r   r   r   r   put  r   zMessagebuffer.putc                 C   r   r.   )r   r   r   )r=   itr   r   r   r     r   zMessagebuffer.extendc                 G   s2   z|   W S  ty   |r|d  Y S |  w )Nr   )r   r   r   r=   rg   r   r   r   take  s   
zMessagebuffer.takec                 C   ro   r.   )r  r<   r   r   r   r     rr   zMessagebuffer._pop_to_evictc                 C   s$   dt | j dt|  d| j dS N<z: />)rR   r   r   r   r<   r   r   r   r     s   $zMessagebuffer.__repr__c                 c   s*    	 z|   V  W n
 ty   Y d S w qr.   )r   r   r<   r   r   r   rq     s   zMessagebuffer.__iter__c                 C   ro   r.   )r   r<   r   r   r   r     rr   zMessagebuffer.__len__rT   c                 C   r   r.   r   r   r   r   r   rl     s   
zMessagebuffer.__contains__c                 C   r   r.   )r   r   r<   r   r   r   __reversed__  r   zMessagebuffer.__reversed__c                 C   s
   | j | S r.   r  )r=   indexr   r   r   rk     r   zMessagebuffer.__getitem__c                 C   s   t | S r.   r   r<   r   r   r   r     s   zMessagebuffer._evictcount)r   r   r   rL   r   r   r\   r   r   r  r   r   rq   r   r   rl   r  rk   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     r   z%BufferMap.__init__.<locals>.<genexpr>)r   r\   r   
bufmaxsizer   sumr6   total)r=   r   r   r  r   r   r   r\     s   

zBufferMap.__init__c                 C   s@   |  || |  jd7  _| | | jo|   d S  d S )Nr   )_get_or_create_bufferr   r  r?   r   r   )r=   rB   r   r   r   r   r     s   
zBufferMap.putc                 C   s:   |  || |  jt|7  _| jo|   d S  d S r.   )r  r   r  r   r   r   )r=   rB   r   r   r   r   r     s   zBufferMap.extendc                 G   s   d\}}z| | }W n t y   d}Y n w z| }|  jd8  _W n | jy/   d}Y nw | | |rA|r=|d S |  |S )N)NFTr   r   )rP   r  r  r   r?   )r=   rB   rg   r   throwr  r   r   r   r    s$   
zBufferMap.takec                 C   s2   z| | W S  t y   |   }| |< | Y S w r.   )rP   _new_buffer)r=   rB   r  r   r   r   r  3  s   
zBufferMap._get_or_create_bufferc                 C   s   | j | jdS )N)r   )Bufferr  r<   r   r   r   r  ;  rc   zBufferMap._new_bufferc                 G   s   | |    j| S r.   )r>   r  r  r   r   r   _LRUpop?  r]   zBufferMap._LRUpopc              
   C   s   t dD ]<}|  }| | }z|  W n t| jfy%   | | Y qw |  jd8  _t|s9| |  d S | |  d S d S )Nr   r   )	r   r>   r  r   r   rA   r  r   r?   )r=   r   rB   r  r   r   r   r   C  s   

zBufferMap._pop_to_evictc                 C   s"   dt | j d| j d| j dS r  )rR   r   r  r   r<   r   r   r   r   X  s   "zBufferMap.__repr__c                 C   s   | j S r.   )r  r<   r   r   r   r   \  s   zBufferMap._evictcount)Nr  )r   r   r   rL   r'   r  r   r   r  r  r\   r   r   r  r  r  r  r   r   r   r   r   r   r   r   r   r!     s$    	r!   )5rL   r   collectionsr   rN   r   collections.abcr   r   r   r   r   heapqr	   r
   r   	itertoolsr   r   queuer   typingr   r   r   r   
functionalr   r   textr   __pypy__r   rO   ImportErrordjango.utils.functionalr   r   __all__r   r(   r)   r   r   r    r$   registerr"   r#   r&   r%   r'   r!   r   r   r   r   <module>   sR    +
J X 
a
F