o
    0×¾gn&  ã                   @   sê   d Z ddlmZ ddlmZ ddlmZ g d¢ZzddlmZ W n ey/   ddl	mZ Y nw ddl
mZ ddlmZ eZeZG d	d
„ d
eƒZdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zeƒ ZdS )aÖ  
Cooperative ``contextvars`` module.

This module was added to Python 3.7. The gevent version is available
on all supported versions of Python. However, see an important note
about gevent 20.9.

Context variables are like greenlet-local variables, just more
inconvenient to use. They were designed to work around limitations in
:mod:`asyncio` and are rarely needed by greenlet-based code.

The primary difference is that snapshots of the state of all context
variables in a given greenlet can be taken, and later restored for
execution; modifications to context variables are "scoped" to the
duration that a particular context is active. (This state-restoration
support is rarely useful for greenlets because instead of always
running "tasks" sequentially within a single thread like `asyncio`
does, greenlet-based code usually spawns new greenlets to handle each
task.)

The gevent implementation is based on the Python reference implementation
from :pep:`567` and doesn't have much optimization. In particular, setting
context values isn't constant time.

.. versionadded:: 1.5a3
.. versionchanged:: 20.9.0
   On Python 3.7 and above, this module is no longer monkey-patched
   in place of the standard library version.
   gevent depends on greenlet 0.4.17 which includes support for context variables.
   This means that any number of greenlets can be running any number of asyncio tasks
   each with their own context variables. This module is only greenlet aware, not
   asyncio task aware, so its use is not recommended on Python 3.7 and above.

   On previous versions of Python, this module continues to be a solution for
   backporting code. It is also available if you wish to use the contextvar API
   in a strictly greenlet-local manner.
é    )Úabsolute_import)Údivision)Úprint_function)Ú
ContextVarÚContextÚcopy_contextÚToken)ÚMapping)Ú_NONE)Úlocalc                   @   s   e Zd Zdd„ ZdS )Ú_ContextStatec                 C   s   t ƒ | _d S ©N)r   Úcontext©Úself© r   ú\/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/contextvars.pyÚ__init__F   s   z_ContextState.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   D   s    r   c                 C   s   t dƒ‚)Nznot an acceptable base type)Ú	TypeError)Úclsr   r   r   Ú_not_base_typeJ   s   r   c                   @   sL   e 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S )Ú_ContextDataz—
    A copy-on-write immutable mapping from ContextVar
    keys to arbitrary values. Setting values requires a
    copy, making it O(n), not O(1).
    ©Ú_mappingc                 C   s
   i | _ d S r   r   r   r   r   r   r   b   ó   
z_ContextData.__init__c                 C   s
   | j | S r   r   ©r   Úkeyr   r   r   Ú__getitem__e   r   z_ContextData.__getitem__c                 C   s
   || j v S r   r   r   r   r   r   Ú__contains__h   r   z_ContextData.__contains__c                 C   ó
   t | jƒS r   )Úlenr   r   r   r   r   Ú__len__k   r   z_ContextData.__len__c                 C   r"   r   )Úiterr   r   r   r   r   Ú__iter__n   r   z_ContextData.__iter__c                 C   s    t ƒ }| j ¡ |_||j|< |S r   ©r   r   Úcopy)r   r   Úvaluer(   r   r   r   Úsetq   s   
z_ContextData.setc                 C   s   t ƒ }| j ¡ |_|j|= |S r   r'   )r   r   r(   r   r   r   Údeletew   s   z_ContextData.deleteN)r   r   r   Ú__doc__Ú	__slots__r   r    r!   r$   r&   r*   r+   r   r   r   r   r   Q   s    r   c                   @   sd   e Zd ZdZdZefdd„ZeeƒZ	edd„ ƒZ
edd„ ƒZefd	d
„Zdd„ Zdd„ Zdd„ ZdS )r   z<
    Implementation of :class:`contextvars.ContextVar`.
    ©Ú_nameÚ_defaultc                 C   s   || _ || _d S r   r.   )r   ÚnameÚdefaultr   r   r   r   ˆ   s   
zContextVar.__init__c                 C   s   | S r   r   )r   Ú_r   r   r   Ú__class_getitem__Ž   s   zContextVar.__class_getitem__c                 C   ó   | j S r   )r/   r   r   r   r   r1   •   s   zContextVar.namec                 C   sD   t j}z||  W S  ty   Y nw |tur|S | jtur | jS t‚r   )Ú_context_stater   ÚKeyErrorr
   r0   ÚLookupError)r   r2   r   r   r   r   Úget™   s   
ÿ
zContextVar.getc                 C   s   t j}| | |¡S r   )r6   r   Ú
_set_value)r   r)   r   r   r   r   r*   ¨   s   zContextVar.setc                 C   s   |  | ¡ d S r   )Ú_reset)r   Útokenr   r   r   Úreset¬   s   zContextVar.resetc                 C   s&   dt | ƒjt | ƒj| j| jt| ƒf S )Nz"<%s.%s name=%r default=%r at 0x%x>)Útyper   r   r/   r0   Úidr   r   r   r   Ú__repr__¯   s   ûzContextVar.__repr__N)r   r   r   r,   r-   r
   r   Úclassmethodr   Ú__init_subclass__r4   Úpropertyr1   r9   r*   r=   r@   r   r   r   r   r   ~   s    

r   c                   @   sP   e Zd ZdZeZdZdd„ Zee	ƒZ
edd„ ƒZedd„ ƒZd	d
„ Zdd„ ZdS )r   z>
    Opaque implementation of :class:`contextvars.Token`.
    ©Ú_contextÚ_varÚ
_old_valueÚ_usedc                 C   s   || _ || _|| _d| _d S )NFrD   )r   r   ÚvarÚ	old_valuer   r   r   r   È   s   
zToken.__init__c                 C   r5   )zW
        A read-only attribute pointing to the variable that created the token
        )rF   r   r   r   r   rI   Ð   s   z	Token.varc                 C   r5   )z­
        A read-only attribute set to the value the variable had before
        the ``set()`` call, or to :attr:`MISSING` if the variable wasn't set
        before.
        )rG   r   r   r   r   rJ   ×   s   zToken.old_valuec                 C   sj   | j rtdƒ‚| j|urtdƒ‚| jtjurtdƒ‚d| _ | j| ju r+| j 	|¡ d S | j 
|| j¡ d S )Nz Taken has already been used oncez+Token was created by a different ContextVarz(Token was created in a different ContextT)rH   ÚRuntimeErrorrF   Ú
ValueErrorrE   r6   r   rG   ÚMISSINGÚ_deleteÚ_reset_value©r   rI   r   r   r   r;   à   s   
zToken._resetc                 C   s.   dt | ƒjt | ƒj| jrdnd| jt| ƒf S )Nz<%s.%s%s var=%r at 0x%x>z usedÚ )r>   r   r   rH   rF   r?   r   r   r   r   r@   ð   s   ûzToken.__repr__N)r   r   r   r,   r
   rM   r-   r   rA   r   rB   rC   rI   rJ   r;   r@   r   r   r   r   r   º   s    

r   c                   @   sx   e Zd ZdZdZdd„ ZeeƒZdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )r   z8
    Implementation of :class:`contextvars.Context`
    )Ú_dataÚ_prev_contextc                 C   s   t ƒ | _d| _dS )z+
        Creates an empty context.
        N)r   rR   rS   r   r   r   r   r     s   
zContext.__init__c                 O   sV   | j d urtd| f ƒ‚tj| _ z| t_||i |¤ŽW | j t_d | _ S | j t_d | _ w )Nz+Cannot enter context; %s is already entered)rS   rK   r6   r   )r   ÚfunctionÚargsÚkwargsr   r   r   Úrun  s   
ÿÿzContext.runc                 C   s   t ƒ }| j|_|S )z(
        Return a shallow copy.
        )r   rR   )r   Úresultr   r   r   r(     s   zContext.copyc                 C   sD   z| j | }W n ty   tj}Y nw | j  ||¡| _ t| ||ƒS r   )rR   r7   r   rM   r*   )r   rI   r)   rJ   r   r   r   r:   '  s   
ÿzContext._set_valuec                 C   s   | j  |¡| _ d S r   )rR   r+   rP   r   r   r   rN   0  s   zContext._deletec                 C   s   | j  ||¡| _ d S r   )rR   r*   )r   rI   rJ   r   r   r   rO   3  s   zContext._reset_valuec                 C   s   t | ƒtur
tdƒ‚d S )NzContextVar key was expected)r>   r   r   )r   r   r   r   Ú__check_keyA  s   ÿzContext.__check_keyc                 C   s   |   |¡ | j| S r   ©Ú_Context__check_keyrR   r   r   r   r   r    F  ó   

zContext.__getitem__c                 C   s   |   |¡ || jv S r   rZ   r   r   r   r   r!   J  r\   zContext.__contains__c                 C   r"   r   )r#   rR   r   r   r   r   r$   N  r   zContext.__len__c                 C   r"   r   )r%   rR   r   r   r   r   r&   Q  r   zContext.__iter__N)r   r   r   r,   r-   r   rA   r   rB   rW   r(   r:   rN   rO   Ústaticmethodr[   r    r!   r$   r&   r   r   r   r   r   ú   s     	
r   c                   C   s
   t j ¡ S )z7
    Return a shallow copy of the current context.
    )r6   r   r(   r   r   r   r   r   U  s   
r   N)r,   Ú
__future__r   r   r   Ú__all__Úcollections.abcr	   ÚImportErrorÚcollectionsÚgevent._utilr
   Úgevent.localr   Ú__stdlib_expected__Ú__implements__r   r   Úobjectr   r   r   r   r   r6   r   r   r   r   Ú<module>   s,   %ÿ-<@[
