
    Dslj                     x    d dl Z d dlZd dlZd dlZd dlmZmZ  G d d      Zd
dZ G d d      Z	 G d d	      Z
y)    N)AnyUnionc                   2    e Zd ZdZdZdedeeef   ddfdZ	y)_Storagean  Thread-tagged storage for a non-thread-critical ``Local``.

    The data is tagged with the identity of the thread that owns it. This lets
    ``_CVar`` ignore data that leaked into an unrelated thread.

    Python 3.14 added ``sys.flags.thread_inherit_context``, which is enabled by
    default on free-threaded builds. When set, a new thread starts with a copy
    of the spawning thread's context instead of an empty one, so the contextvar
    backing a ``Local`` would otherwise be visible in any thread spawned from
    one that had set it -- breaking the documented "thread-local in sync
    threads" behaviour. asgiref re-homes the storage to the current thread at
    the points where it *intentionally* moves work between threads (see
    ``asgiref.sync._restore_context``); data merely inherited by an unrelated
    thread is never re-homed and so stays isolated.
    	thread_iddatar   r	   returnNc                      || _         || _        y Nr   )selfr   r	   s      F/var/www/eggtrainer/venv/lib/python3.12/site-packages/asgiref/local.py__init__z_Storage.__init__   s    "	    )
__name__
__module____qualname____doc__	__slots__intdictstrr   r    r   r   r   r      s0      &I# T#s(^  r   r   c                 R    t        t        j                         | j                        S )z7Return a copy of *storage* owned by the current thread.)r   	threading	get_identr	   )storages    r   _rehomer       s    I'')7<<88r   c                   V     e Zd ZdZddZddZd Zdededdf fd	Z	deddfd
Z
 xZS )_CVarzStorage utility for Local.r
   Nc                 8    t        j                  d      | _        y )Nzasgiref.local)contextvars
ContextVar_data)r   s    r   r   z_CVar.__init__(   s    9D9O9O:

r   c                     | j                   j                  d       }|!|j                  t        j                         k7  rt        t        j                         i       S |S r   )r$   getr   r   r   r   )r   r   s     r   _storagez_CVar._storage-   sL     **..&?g//93F3F3HHI//1266r   c                 x    	 | j                         j                  |   S # t        $ r t        | d|      w xY wNz object has no attribute )r'   r	   KeyErrorAttributeError)r   keys     r   __getattr__z_CVar.__getattr__6   sG    	N==?'',, 	N D8+DSG!LMM	Ns    9r,   valuec                     |dk(  rt         |   ||      S | j                         j                  j	                         }|||<   | j
                  j                  t        t        j                         |             y )Nr$   )
super__setattr__r'   r	   copyr$   setr   r   r   )r   r,   r.   r	   	__class__s       r   r1   z_CVar.__setattr__<   s_    '>7&sE22}}##((*S	

x	 3 3 5t<=r   c                     | j                         j                  j                         }||v r;||= | j                  j	                  t        t        j                         |             y t        | d|      r)   )	r'   r	   r2   r$   r3   r   r   r   r+   )r   r,   r	   s      r   __delattr__z_CVar.__delattr__D   sa    }}##((*$;S	JJNN8I$7$7$94@A D8+DSG!LMMr   )r
   N)r
   r   )r   r   r   r   r   r'   r-   r   r   r1   r6   __classcell__r4   s   @r   r    r    %   sF    $

N>s >3 >4 >Ns Nt Nr   r    c                   d     e Zd ZdZd
deddfdZej                  d        Zd Z	 fdZ
d	 Z xZS )Locala  Local storage for async tasks.

    This is a namespace object (similar to `threading.local`) where data is
    also local to the current async task (if there is one).

    In async threads, local means in the same sense as the `contextvars`
    module - i.e. a value set in an async frame will be visible:

    - to other async code `await`-ed from this frame.
    - to tasks spawned using `asyncio` utilities (`create_task`, `wait_for`,
      `gather` and probably others).
    - to code scheduled in a sync thread using `sync_to_async`

    In "sync" threads (a thread with no async event loop running), the
    data is thread-local, but additionally shared with async code executed
    via the `async_to_sync` utility, which schedules async code in a new thread
    and copies context across to that thread.

    If `thread_critical` is True, then the local will only be visible per-thread,
    behaving exactly like `threading.local` if the thread is sync, and as
    `contextvars` if the thread is async. This allows genuinely thread-sensitive
    code (such as DB handles) to be kept strictly to their initial thread and
    disable the sharing across `sync_to_async` and `async_to_sync` wrapped calls.

    Unlike plain `contextvars` objects, this utility is threadsafe.
    thread_criticalr
   Nc                     || _         t        j                         | _        |  |rt        j                         | _        y t               | _        y r   )_thread_criticalr   RLock_thread_locklocalr'   r    )r   r;   s     r   r   zLocal.__init__i   s8     /%OO-%OO-DM "GDMr   c              #     K   | j                   rpd}	 t        j                          |s| j                   y t        | j                  d      st               | j                  _        | j                  j                   y | j                  5  | j                   d d d        y # t        $ r d}Y w xY w# 1 sw Y   y xY ww)NTFcvar)	r=   asyncioget_running_loopRuntimeErrorr'   hasattrr    rB   r?   )r   is_asyncs     r   _lock_storagezLocal._lock_storagev   s        H! ((* 
 mm# t}}f5).DMM&
 mm((( "" $mm#$ $3   ! !2$ $s@   C B# A%C B4	C #B1.C 0B11C 4B=9C c                 f    | j                         5 }t        ||      cd d d        S # 1 sw Y   y xY wr   )rH   getattrr   r,   r   s      r   r-   zLocal.__getattr__   s/    ! 	)W7C(	) 	) 	)s   '0c                     |dv rt         |   ||      S | j                         5 }t        |||       d d d        y # 1 sw Y   y xY w)N)_localr'   r=   r?   )r0   r1   rH   setattr)r   r,   r.   r   r4   s       r   r1   zLocal.__setattr__   sM    LL7&sE22! 	)WGS%(	) 	) 	)s	   =Ac                 f    | j                         5 }t        ||       d d d        y # 1 sw Y   y xY wr   )rH   delattrrK   s      r   r6   zLocal.__delattr__   s/    ! 	"WGS!	" 	" 	"s   '0)F)r   r   r   r   boolr   
contextlibcontextmanagerrH   r-   r1   r6   r7   r8   s   @r   r:   r:   M   sE    6$ $ $ #$ #$J))"r   r:   )r   r   r
   r   )rC   rR   r"   r   typingr   r   r   r   r    r:   r   r   r   <module>rU      s<         09
%N %NP[" ["r   