o
    0×¾gÑS  ã                   @   sJ  d Z ddlmZ ddlmZ ddlmZ edƒjeƒ d< dd„ eƒ d	< d
gZ	d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G dd„ deƒZdd„ Zeƒ Zdd„ Zdd„ Zh d£ZG dd
„ d
eƒZd d!„ Zd"d#„ Zd$d%„ Zejd&kr†eeƒe_nejd'kr”ed(ejef ƒ‚eƒ  dd)l m!Z! e!e"ƒ d*ƒ d+S ),aR  
Greenlet-local objects.

This module is based on `_threading_local.py`__ from the standard
library of Python 3.4.

__ https://github.com/python/cpython/blob/3.4/Lib/_threading_local.py

Greenlet-local objects support the management of greenlet-local data.
If you have data that you want to be local to a greenlet, simply create
a greenlet-local object and use its attributes:

  >>> import gevent
  >>> from gevent.local import local
  >>> mydata = local()
  >>> mydata.number = 42
  >>> mydata.number
  42

You can also access the local-object's dictionary:

  >>> mydata.__dict__
  {'number': 42}
  >>> mydata.__dict__.setdefault('widgets', [])
  []
  >>> mydata.widgets
  []

What's important about greenlet-local objects is that their data are
local to a greenlet. If we access the data in a different greenlet:

  >>> log = []
  >>> def f():
  ...     items = list(mydata.__dict__.items())
  ...     items.sort()
  ...     log.append(items)
  ...     mydata.number = 11
  ...     log.append(mydata.number)
  >>> greenlet = gevent.spawn(f)
  >>> greenlet.join()
  >>> log
  [[], 11]

we get different data.  Furthermore, changes made in the other greenlet
don't affect data seen in this greenlet:

  >>> mydata.number
  42

Of course, values you get from a local object, including a __dict__
attribute, are for whatever greenlet was current at the time the
attribute was read.  For that reason, you generally don't want to save
these values across greenlets, as they apply only to the greenlet they
came from.

You can create custom local objects by subclassing the local class:

  >>> class MyLocal(local):
  ...     number = 2
  ...     initialized = False
  ...     def __init__(self, **kw):
  ...         if self.initialized:
  ...             raise SystemError('__init__ called too many times')
  ...         self.initialized = True
  ...         self.__dict__.update(kw)
  ...     def squared(self):
  ...         return self.number ** 2

This can be useful to support default values, methods and
initialization.  Note that if you define an __init__ method, it will be
called each time the local object is used in a separate greenlet.  This
is necessary to initialize each greenlet's dictionary.

Now if we create a local object:

  >>> mydata = MyLocal(color='red')

Now we have a default number:

  >>> mydata.number
  2

an initial color:

  >>> mydata.color
  'red'
  >>> del mydata.color

And a method that operates on the data:

  >>> mydata.squared()
  4

As before, we can access the data in a separate greenlet:

  >>> log = []
  >>> greenlet = gevent.spawn(f)
  >>> greenlet.join()
  >>> log
  [[('color', 'red'), ('initialized', True)], 11]

without affecting this greenlet's data:

  >>> mydata.number
  2
  >>> mydata.color
  Traceback (most recent call last):
  ...
  AttributeError: 'MyLocal' object has no attribute 'color'

Note that subclasses can define slots, but they are not greenlet
local. They are shared across greenlets::

  >>> class MyLocal(local):
  ...     __slots__ = 'number'

  >>> mydata = MyLocal()
  >>> mydata.number = 42
  >>> mydata.color = 'red'

So, the separate greenlet:

  >>> greenlet = gevent.spawn(f)
  >>> greenlet.join()

affects what we see:

  >>> mydata.number
  11

>>> del mydata

.. versionchanged:: 1.1a2
   Update the implementation to match Python 3.4 instead of Python 2.5.
   This results in locals being eligible for garbage collection as soon
   as their greenlet exits.

.. versionchanged:: 1.2.3
   Use a weak-reference to clear the greenlet link we establish in case
   the local object dies before the greenlet does.

.. versionchanged:: 1.3a1
   Implement the methods for attribute access directly, handling
   descriptors directly here. This allows removing the use of a lock
   and facilitates greatly improved performance.

.. versionchanged:: 1.3a1
   The ``__init__`` method of subclasses of ``local`` is no longer
   called with a lock held. CPython does not use such a lock in its
   native implementation. This could potentially show as a difference
   if code that uses multiple dependent attributes in ``__slots__``
   (which are shared across all greenlets) switches during ``__init__``.

é    )Úprint_function)Úcopy)ÚrefÚgreenletÚ
getcurrentc                   C   s   d S ©N© r   r   r   úV/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/local.pyÚ<lambda>£   s    r
   Úgreenlet_initÚlocalÚ_gevent_local_localimpl_c                 C   s|   g }t | ƒ}| j}| ¡ D ].\}}| t¡sq|ƒ }|du rq|j |¡}|du r*q| ¡ | u s2J ‚| |j	|j
f¡ q|S )at  
    Internal debug helper for getting the local values associated
    with a greenlet. This is subject to change or removal at any time.

    :return: A list of ((type, id), {}) pairs, where the first element
      is the type and id of the local object and the second object is its
      instance dictionary, as seen from this greenlet.

    .. versionadded:: 1.3a2
    N)ÚidÚ__dict__ÚitemsÚ
startswithÚ
key_prefixÚdictsÚgetÚ
wrgreenletÚappendÚlocaltypeidÚ	localdict)r   ÚresultÚid_greenletÚgreenlet_dictÚkÚvÚ
local_implÚentryr   r   r	   Úall_local_dicts_for_greenletµ   s   
r    c                   @   s   e Zd ZdZdS )Ú	_wrefdictz"A dict that can be weak referencedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r!   Ô   s    r!   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )Ú_greenlet_deletedzÁ
    A weakref callback for when the greenlet
    is deleted.

    If the greenlet is a `gevent.greenlet.Greenlet` and
    supplies ``rawlink``, that will be used instead of a
    weakref.
    ©ÚidtÚwrdictsc                 C   ó   || _ || _d S r   r'   )Úselfr(   r)   r   r   r	   Ú__init__â   ó   
z_greenlet_deleted.__init__c                 C   s"   |   ¡ }|r| | jd ¡ d S d S r   )r)   Úpopr(   )r+   Ú_unusedr   r   r   r	   Ú__call__æ   s   ÿz_greenlet_deleted.__call__N)r"   r#   r$   r%   Ú	__slots__r,   r0   r   r   r   r	   r&   ×   s
    r&   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_local_deleted©ÚkeyÚwrthreadÚgreenlet_deletedc                 C   s   || _ || _|| _d S r   r3   )r+   r4   r5   r6   r   r   r	   r,   î   s   
z_local_deleted.__init__c                 C   sJ   |   ¡ }|d ur#z|j}W n	 ty   Y nw || jƒ |j| j= d S d S r   )r5   ÚunlinkÚAttributeErrorr6   r   r4   )r+   r/   Úthreadr7   r   r   r	   r0   ó   s   
ÿ
ùz_local_deleted.__call__N)r"   r#   r$   r1   r,   r0   r   r   r   r	   r2   ë   s    r2   c                   @   ó   e Zd ZdZdZdd„ ZdS )Ú
_localimplz#A class managing thread-local dicts)r4   r   Ú	localargsÚlocalkwargsr   Ú__weakref__c                 C   sJ   t tt| ƒƒ | _tƒ | _|| _|| _||f| _t	ƒ }t
| |t|ƒƒ d S r   )r   Ústrr   r4   r!   r   r<   r=   r   r   Ú_localimpl_create_dict)r+   ÚargsÚkwargsÚ
local_typeÚid_localr   r   r   r	   r,     s   
z_localimpl.__init__N©r"   r#   r$   r%   r1   r,   r   r   r   r	   r;   þ   s    r;   c                   @   r:   )Ú_localimpl_dict_entryz]
    The object that goes in the ``dicts`` of ``_localimpl``
    object for each thread.
    ©r   r   c                 C   r*   r   rG   )r+   r   r   r   r   r	   r,     r-   z_localimpl_dict_entry.__init__NrE   r   r   r   r	   rF     s    rF   c                 C   s‚   i }| j }t| jƒ}t||ƒ}t|ddƒ}|dur"||ƒ t|ƒ}nt||ƒ}t|||ƒ}	t| |	ƒ}
|
|j|< t||ƒ| j|< |S )z8Create a new dict for the current thread, and return it.ÚrawlinkN)r4   r   r   r&   Úgetattrr2   r   rF   )r+   r   r   r   r4   r)   r6   rH   r5   Úlocal_deletedÚwrlocalr   r   r	   r@   (  s   





r@   c                 C   s`   | j }tƒ }t|ƒ}z|j| }|j}W |S  ty/   t|||ƒ}| j|ji |j	¤Ž Y |S w r   )
Ú_local__implr   r   r   r   ÚKeyErrorr@   r,   r<   r=   )r+   Úimplr   Úidgr   Údctr   r   r	   Ú_local_get_dictO  s   
ýýrQ   c                   C   s
   t ƒ  d S r   )r   r   r   r   r	   Ú_init\  s   
rR   >	   Ú	__cinit__Ú	__class__Ú_local_typerL   Ú_local_type_varsÚ_local_type_del_descriptorsÚ_local_type_get_descriptorsÚ_local_type_set_descriptorsÚ"_local_type_set_or_del_descriptorsc                   @   sH   e Zd ZdZeeddh ƒZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ ZdS )r   z8
    An object whose attributes are greenlet-local.
    rT   rS   c                 O   s†   |s|rt | ƒjtjkrtd||ƒ‚t||t | ƒt| ƒƒ}|| _t| ƒ\}}}}|| _|| _	|| _
|| _t | ƒ| _tt| jƒƒ| _d S )Nz*Initialization arguments are not supported)Útyper,   ÚobjectÚ	TypeErrorr;   r   rL   Ú_local_find_descriptorsrX   rZ   rW   rY   rU   ÚsetÚdirrV   )r+   rA   ÚkwrN   r   ÚdelsÚsets_or_delsÚsetsr   r   r	   rS   q  s   
zlocal.__cinit__c                 C   s<  |t v r
t | |¡S t| ƒ}|dkr|S | jtu r'||v r!|| S t | |¡S ||v rV|| jvr4|| S || jvr=|| S || jv rRt	| j|ƒ}t
|ƒ || | j¡S || S || jv r‡|| jvrft	| j|ƒS | j ¡ D ]}|j}||v r†|| }t
|ƒ || | j¡}|  S qkt| jdƒr”| j | |¡S td| jj|f ƒ‚)Nr   Ú__getattr__z%r object has no attribute '%s')Ú_local_attrsr\   Ú__getattribute__rQ   rU   r   rV   rX   rZ   rI   r[   Ú__get__Úmror   Úhasattrre   r8   r"   )r+   ÚnamerP   Ú	type_attrÚbaseÚbdÚattr_on_typer   r   r   r	   rg   €  s>   


	


ý	
ÿzlocal.__getattribute__c                 C   s”   |dkrt dt| ƒ ƒ‚|tv rt | ||¡ d S t| ƒ}| jtu r(|||< d S || jv rD|| j	v rDt
| j|tƒ}t|ƒ || |¡ d S |||< d S ©Nr   z+%r object attribute '__dict__' is read-only)r8   r[   rf   r\   Ú__setattr__rQ   rU   r   rV   rY   rI   Ú_markerÚ__set__)r+   rk   ÚvaluerP   rl   r   r   r	   rq   Ö  s&   ÿÿ


zlocal.__setattr__c                 C   sz   |dkrt d| jj ƒ‚|| jv r'|| jv r't| j|tƒ}t|ƒ 	|| ¡ d S t
| ƒ}z||= W d S  ty<   t |ƒ‚w rp   )r8   rT   r"   rV   rW   rI   rU   rr   r[   Ú
__delete__rQ   rM   )r+   rk   rl   rP   r   r   r	   Ú__delattr__ñ  s"   ÿÿ

ÿzlocal.__delattr__c                 C   sN   | j }|jttƒ ƒ }|j}t|ƒ}t| ƒ}||ji |j¤Ž}t	|||ƒ |S r   )
rL   r   r   r   r   r   r[   r<   r=   Ú_local__copy_dict_from)r+   rN   r   rP   Ú	duplicateÚclsÚinstancer   r   r	   Ú__copy__  s   zlocal.__copy__N)r"   r#   r$   r%   Útuplerf   r1   rS   rg   rq   rv   r{   r   r   r   r	   r   k  s    Vc                 C   s@   t ƒ }t|ƒ}| j}||usJ ‚|j| }t|j|ƒ|j|< d S r   )r   r   rL   r   rF   r   )r+   rN   rx   ÚcurrentÚ	currentIdÚnew_implr   r   r   r	   rw     s   
rw   c                 C   sÀ   t | ƒ}tƒ }tƒ }tƒ }tƒ }t| ¡ ƒ}t|ƒD ]?}|D ]}|j}	||	v r-|	| }
 nqt|ƒ‚t |
ƒ}t|dƒr@| |¡ t|dƒrO| |¡ | |¡ t|dƒrY| |¡ q||||fS )Nrh   ru   rs   )	r[   r_   Úlistri   r`   r   r8   rj   Úadd)r+   Ú	type_selfÚgetsrb   Ú
set_or_delrd   ri   Ú	attr_namerm   rn   Úattrrl   r   r   r	   r^     s0   þ






€r^   c                 O   s,   t t| ƒ | ¡}|j|dd … i |¤Ž |S )Né   )Úsuperr   Ú__new__rS   )ry   rA   ra   r+   r   r   r	   r‰   @  s   r‰   zgevent.localzgevent._gevent_clocalzAModule names changed (local: %r; __name__: %r); revisit this code)Úimport_c_accelzgevent._localN)#r%   Ú
__future__r   r   Úweakrefr   Ú
__import__r   ÚlocalsÚ__all__r   r    Údictr!   r\   r&   r2   r;   rF   r@   rr   rQ   rR   rf   r   rw   r^   r‰   r#   ÚclassmethodÚAssertionErrorr"   Úgevent._utilrŠ   Úglobalsr   r   r   r	   Ú<module>   sF    ÿ	% )%

ÿ