o
    ".h                     @   s:   d dl Z d dlZd dlZd dlZd dlZG dd dZdS )    Nc                       s\   e Zd ZdZddeddfddZdd	 Zd
d Zdd Zdd Z	 fddZ
dd Z  ZS )Locala  
    A drop-in replacement for threading.locals that also works with asyncio
    Tasks (via the current_task asyncio method), and passes locals through
    sync_to_async and async_to_sync.

    Specifically:
     - Locals work per-coroutine on any thread not spawned using asgiref
     - Locals work per-thread on any thread not spawned using asgiref
     - Locals are shared with the parent coroutine when using sync_to_async
     - Locals are shared with the parent thread when using async_to_sync
       (and if that thread was launched using sync_to_async, with its parent
       coroutine as well, with this working for indefinite levels of nesting)

    Set thread_critical to True to not allow locals to pass from an async Task
    to a thread it spawns. This is needed for code that truly needs
    thread-safety, as opposed to things used for helpful context (e.g. sqlite
    does not like being called from a different thread to the one it is from).
    Thread-critical code will still be differentiated per-Task within a thread
    as it is expected it does not like concurrent access.

    This doesn't use contextvars as it needs to support 3.6. Once it can support
    3.7 only, we can then reimplement the storage more nicely.
    Fthread_criticalreturnNc                 C   sD   || _ t | _t | _dt| d	dd t
dD | _d S )Nz_asgiref_local_impl_{}_{} c                 s   s    | ]	}t tjV  qd S N)randomchoicestringascii_letters).0i r   L/var/www/supernova/superenvnew/lib/python3.10/site-packages/asgiref/local.py	<genexpr>)   s    z!Local.__init__.<locals>.<genexpr>   )_thread_critical	threadingRLock_thread_lockweakrefWeakSet_context_refsformatidjoinrange
_attr_name)selfr   r   r   r   __init__!   s   


zLocal.__init__c              	   C   s   ddl m}m} | }d}|du rt }d}| jr|S tt	 D ] }z|r0|j
| }d}n|j
| }d}W q# tyC   Y  |S w td)zC
        Get the ID we should use for looking up variables
           )AsyncToSyncSyncToAsyncTNFzInfinite launch_map loops)syncr    r!   get_current_taskr   current_threadr   r   sysgetrecursionlimit
launch_mapKeyErrorRuntimeError)r   r    r!   
context_idcontext_is_asyncr   r   r   r   _get_context_id,   s*   

zLocal._get_context_idc                 C   s:   |   }t|| jst|| ji  | j| t|| jS r   )r,   hasattrr   setattrr   addgetattrr   context_objr   r   r   _get_storageP   s
   zLocal._get_storagec              	   C   sJ   z| j D ]}zt|| j W q ty   Y qw W d S  ty$   Y d S w r   )r   delattrr   AttributeError	TypeErrorr1   r   r   r   __del__W   s   
zLocal.__del__c                 C   sT   | j  |  }||v r|| W  d    S t| d|1 s#w   Y  d S Nz object has no attribute r   r3   r5   r   keystorager   r   r   __getattr__c   s   zLocal.__getattr__c                    sR   |dv rt  ||S | j |  }|||< W d    d S 1 s"w   Y  d S )N)r   r   r   r   )super__setattr__r   r3   )r   r;   valuer<   	__class__r   r   r?   k   s   
"zLocal.__setattr__c                 C   sV   | j  |  }||v r||= n	t| d|W d    d S 1 s$w   Y  d S r8   r9   r:   r   r   r   __delattr__r   s   "zLocal.__delattr__)F)__name__
__module____qualname____doc__boolr   r,   r3   r7   r=   r?   rC   __classcell__r   r   rA   r   r      s    $r   )r   r	   r%   r   r   r   r   r   r   r   <module>   s    