
    4j"G                     z   S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	J
r
  SSKJrJr  SSKJrJrJr  SSKJr   " S	 S
\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS!SS.S jjrS!SS.S jjrS rS rS r S r!S  r"\" 5       \"l#        g)"aI  Synchronization decorators and calling-convention markers/bridges.

Provides ``synchronized`` for thread and async locking, ``mark_as_sync``
and ``mark_as_async`` for declaring the effective calling convention of a
wrapped callable (without converting it), and ``async_to_sync`` /
``sync_to_async`` for bridging between the two.
    N)partial)CO_ASYNC_GENERATORCO_COROUTINECO_GENERATORCO_ITERABLE_COROUTINEiscoroutinefunction)LockRLock   )BoundFunctionWrapperCallableObjectProxyFunctionWrapper)	decoratorc                   <   ^  \ rS rSrSU 4S jjr\S 5       rSrU =r$ )_SyncCodeProxy    c                 0   > [         TU ]  U5        X l        g Nsuper__init___self_generatorselfwrapped	generator	__class__s      T/root/lseg-credit/backend/venv/lib/python3.13/site-packages/wrapt/synchronization.pyr   _SyncCodeProxy.__init__"       !(    c                     U R                   R                  nU[        [        -  [        -  ) -  nU R
                  SL a  U[        -  nU$ U R
                  SL a  U[        ) -  nU$ U[        -  (       a	  U[        -  nU$ NTF)__wrapped__co_flagsr   r   r   r   r   r   originalflagss      r   r%   _SyncCodeProxy.co_flags&   s    ##,, \,>>AVVWW4'\!E  !!U*l]"E  ,,%r!   r   r   	__name__
__module____qualname____firstlineno__r   propertyr%   __static_attributes____classcell__r   s   @r   r   r       s    )  r!   r   c                   <   ^  \ rS rSrSU 4S jjr\S 5       rSrU =r$ )_SyncFunctionSurrogate9   c                 0   > [         TU ]  U5        X l        g r   r   r   s      r   r   _SyncFunctionSurrogate.__init__;   r    r!   c                 V    [        U R                  R                  U R                  5      $ r   r   r$   __code__r   r   s    r   r;   _SyncFunctionSurrogate.__code__?   !    d..779M9MNNr!   r*   r   	r,   r-   r.   r/   r   r0   r;   r1   r2   r3   s   @r   r5   r5   9   s    ) O Or!   r5   c                   8   ^  \ rS rSrU 4S jr\S 5       rSrU =r$ )_BoundSyncFunctionWrapperD   c                 4   > [         TU ]  " U0 UD6  SU l        g NT)r   r   _self_is_not_coroutine)r   argskwargsr   s      r   r   "_BoundSyncFunctionWrapper.__init__F   s    $)&)&*#r!   c                 j    [        U R                  R                  U R                  R                  5      $ r   )r5   r$   __func___self_parentr   r<   s    r   rJ   "_BoundSyncFunctionWrapper.__func__J   s,    %%%t'8'8'H'H
 	
r!   )rE   )	r,   r-   r.   r/   r   r0   rJ   r1   r2   r3   s   @r   rA   rA   D   s    + 
 
r!   rA   c                   @   ^  \ rS rSr\rSU 4S jjr\S 5       rSr	U =r
$ )_SyncFunctionWrapperQ   c                 >   > [         TU ]  X5        SU l        X0l        g rD   )r   r   rE   r   r   r   wrapperr   r   s       r   r   _SyncFunctionWrapper.__init__U   s    *&*#(r!   c                 V    [        U R                  R                  U R                  5      $ r   r:   r<   s    r   r;   _SyncFunctionWrapper.__code__Z   r>   r!   )r   rE   r   )r,   r-   r.   r/   rA   __bound_function_wrapper__r   r0   r;   r1   r2   r3   s   @r   rN   rN   Q   s$    !:)
 O Or!   rN   c                   <   ^  \ rS rSrSU 4S jjr\S 5       rSrU =r$ )_AsyncCodeProxy_   c                 0   > [         TU ]  U5        X l        g r   r   r   s      r   r   _AsyncCodeProxy.__init__a   r    r!   c                 *   U R                   R                  nU[        [        -  [        -  [
        -  ) -  nU R                  SL a  U[
        -  nU$ U R                  SL a  U[        -  nU$ U[        [
        -  -  (       a  U[
        -  nU$ U[        -  nU$ r#   )r$   r%   r   r   r   r   r   r&   s      r   r%   _AsyncCodeProxy.co_flagse   s    ##,,<'*??BTT
 
 4'''E  !!U*\!E 	 <*<<=++  %r!   r*   r   r+   r3   s   @r   rX   rX   _   s    )  r!   rX   c                   <   ^  \ rS rSrSU 4S jjr\S 5       rSrU =r$ )_AsyncFunctionSurrogatez   c                 0   > [         TU ]  U5        X l        g r   r   r   s      r   r    _AsyncFunctionSurrogate.__init__|   r    r!   c                 V    [        U R                  R                  U R                  5      $ r   rX   r$   r;   r   r<   s    r   r;    _AsyncFunctionSurrogate.__code__   !    t//88$:N:NOOr!   r*   r   r?   r3   s   @r   r_   r_   z   s    ) P Pr!   r_   c                   $    \ rS rSr\S 5       rSrg)_BoundAsyncFunctionWrapper   c                 j    [        U R                  R                  U R                  R                  5      $ r   )r_   r$   rJ   rK   r   r<   s    r   rJ   #_BoundAsyncFunctionWrapper.__func__   s,    &%%t'8'8'H'H
 	
r!    N)r,   r-   r.   r/   r0   rJ   r1   rl   r!   r   rh   rh      s    
 
r!   rh   c                   @   ^  \ rS rSr\rSU 4S jjr\S 5       rSr	U =r
$ )_AsyncFunctionWrapper   c                 0   > [         TU ]  X5        X0l        g r   r   rQ   s       r   r   _AsyncFunctionWrapper.__init__   s    *(r!   c                 V    [        U R                  R                  U R                  5      $ r   rd   r<   s    r   r;   _AsyncFunctionWrapper.__code__   rf   r!   r*   r   )r,   r-   r.   r/   rh   rV   r   r0   r;   r1   r2   r3   s   @r   rn   rn      s$    !;) P Pr!   rn   r   c               *   ^ U4S jnU c  U$ U" U 5      $ )a  Mark a callable as synchronous from the perspective of calling
convention detection. The returned wrapper is a pass-through that
reports `inspect.iscoroutinefunction()` as False regardless of
whether the underlying callable is declared `async def`. Useful
when a stacked decorator has already collapsed an async function
into a synchronous one (for example by using `asyncio.run()`).

The `generator` keyword toggles the sync generator bit
(`CO_GENERATOR`) on the resulting wrapper. Tri-state:

- `None` (default): auto. Preserve generator-ness from the input --
  if the input was an async generator, the wrapper reports as a sync
  generator; otherwise CO_GENERATOR is copied through unchanged.
- `True`: force CO_GENERATOR on. Wrapper reports as a sync generator.
- `False`: force CO_GENERATOR off. Wrapper reports as a plain sync
  function even if the input had CO_GENERATOR set.

Regardless of `generator`, CO_COROUTINE, CO_ASYNC_GENERATOR, and
CO_ITERABLE_COROUTINE are all cleared (sync means none of those).
c                    > S n[        XTS9$ )Nc                     U " U0 UD6$ r   rl   r   instancerF   rG   s       r   _wrapper2mark_as_sync.<locals>._decorator.<locals>._wrapper   s    D+F++r!   rt   rN   r   rz   r   s     r   
_decorator mark_as_sync.<locals>._decorator   s    	, $GKKr!   rl   )r   r   r~   s    ` r   mark_as_syncr      s!    ,L gr!   c               4   ^^ S mUU4S jnU c  U$ U" U 5      $ )a  Mark a callable as asynchronous from the perspective of calling
convention detection. The returned wrapper reports
`inspect.iscoroutinefunction()` as True regardless of whether the
underlying callable is declared `async def`. Useful when a stacked
decorator returns a coroutine from a plain `def` wrapper.

The `generator` keyword chooses between coroutine function and
async generator reporting. Tri-state:

- `None` (default): auto. If the input was a sync or async
  generator, the wrapper reports as an async generator
  (`CO_ASYNC_GENERATOR`); otherwise it reports as a coroutine
  function (`CO_COROUTINE`).
- `True`: force async generator reporting (`CO_ASYNC_GENERATOR` set,
  `CO_COROUTINE` cleared). These two flags are mutually exclusive at
  the CPython code-object level.
- `False`: force coroutine function reporting (`CO_COROUTINE` set,
  `CO_ASYNC_GENERATOR` cleared).

CO_GENERATOR and CO_ITERABLE_COROUTINE are always cleared (the
async path does not use either).
c                    #    U " U0 UD6$ 7fr   rl   rx   s       r   rz   mark_as_async.<locals>._wrapper   s     '''s   	c                    > [        U TTS9$ )Nrt   rn   r}   s    r   r~   !mark_as_async.<locals>._decorator   s    $Wh)LLr!   rl   )r   r   r~   rz   s    ` @r   mark_as_asyncr      s&    0(M gr!   c                     S n[        X5      $ )zAdapt an async callable so it can be called synchronously. Each
call runs the coroutine to completion via `asyncio.run()`. The
returned wrapper reports as synchronous under
`inspect.iscoroutinefunction()`. Naming follows the asgiref
convention.c                 :    [         R                  " U " U0 UD65      $ r   )asynciorunrx   s       r   rR   async_to_sync.<locals>.wrapper   s    {{7D3F344r!   r|   r   rR   s     r   async_to_syncr      s    5  11r!   c                     S n[        X5      $ )a  Adapt a sync callable so it can be awaited. Each call dispatches
the synchronous work to the default executor via
`loop.run_in_executor()`. The returned wrapper reports as
asynchronous under `inspect.iscoroutinefunction()`. Naming follows
the asgiref convention.c                    #    [         R                  " 5       nUR                  S [        U /UQ70 UD65      I S h  vN $  N7fr   )r   get_running_looprun_in_executorr   )r   ry   rF   rG   loops        r   rR   sync_to_async.<locals>.wrapper   s;     '')))$0Q$0Q&0QRRRRs   8A?Ar   r   s     r   sync_to_asyncr      s    S !22r!   c                 .    [        [        U SS 5      5      $ )Nacquire)r   getattr)objs    r   _synchronized_is_async_lockr      s    wsIt<==r!   c                 0   [        U 5      U 0n[        R                  " 5       nU n [        U[        [
        45      (       a]  [        USS 5      nUc  UR                  nUn[        U5      nXQ;   d  [        U5      U:  a  [        SR                  U 5      5      eX1U'   My  [        USS5      (       a  g[        U5      (       a  g[        USS 5      nUb  XcL a  gUn[        U5      nXQ;   d  [        U5      U:  a  [        SR                  U 5      5      eX1U'   M  )NTr$   z!wrapper loop when unwrapping {!r}rE   F)idsysgetrecursionlimit
isinstanceclassmethodstaticmethodr   rJ   len
ValueErrorformatr   )r   memorecursion_limittargetinner	id_targetnext_targets          r   _synchronized_is_async_callabler      s    sGS>D++-OF
f{L9::FM48E}F6
I CI$@ !D!K!KC!PQQ$O63U;;v&&fmT:+"7vJ	D	_ <@GGLMM Y3 r!   c                   ^^^	 [        U S5      (       aq  [        U S5      (       a`  U m	[        T	5      (       a'  [        U	4S j5       n " U	4S jS[        5      nU" US9$ [        U	4S j5       n " U	4S jS	[        5      nU" US9$ S
 mS mU4S jnU4S jn " UU4S jS[        5      n[        U 5      (       a  U" XS9$ U" XS9$ )a  Depending on the nature of the `wrapped` object, will either return a
decorator which can be used to wrap a function or method, or a context
manager, both of which will act accordingly depending on how used, to
synchronize access to calling of the wrapped function, or the block of
code within the context manager. If it is an object which is a
synchronization primitive, such as a threading Lock, RLock, Semaphore,
Condition, or Event, then it is assumed that the object is to be used
directly as the synchronization primitive, otherwise a lock is created
automatically and attached to the wrapped object and used as the
synchronization primitive.

Async functions are supported: if the wrapped callable is an async
function, an `asyncio.Lock` is created for the context and the wrapper
awaits the lock. The returned object also exposes `__aenter__` and
`__aexit__` so it can be used with `async with` to synchronise a block
of code using an independent per-context `asyncio.Lock`. If an object
with coroutine `acquire`/`release` methods (such as an `asyncio.Lock`)
is supplied directly, the returned decorator and context manager will
use it via the async protocol.
r   releasec                    >#    T IS h  vN   U " U0 UD6I S h  vN sS S S 5      IS h  vN   $  N% N N	! , IS h  vN  (       d  f       g = f7fr   rl   r   ry   rF   rG   locks       r   _synchronized#synchronized.<locals>._synchronizedX  s2     4!($!9&!99  449  444sE   A0A626A4A6AA?A	Ac                   .   > \ rS rSrU 4S jrU 4S jrSrg)1synchronized.<locals>._AsyncSynchronizedLockProxyi]  c                 D   >#    TR                  5       I S h  vN   T$  N7fr   r   r   r   s    r   
__aenter__<synchronized.<locals>._AsyncSynchronizedLockProxy.__aenter___  s     ,,.((K )s     c                 .   >#    TR                  5         g 7fr   r   r   rF   r   s     r   	__aexit__;synchronized.<locals>._AsyncSynchronizedLockProxy.__aexit__c  s     LLNs   rl   N)r,   r-   r.   r/   r   r   r1   r   s   r   _AsyncSynchronizedLockProxyr   ]  s     # #r!   r   )r   c                 N   > T   U " U0 UD6sS S S 5        $ ! , (       d  f       g = fr   rl   r   s       r   r   r   h  s    
 // s   
$c                   .   > \ rS rSrU 4S jrU 4S jrSrg),synchronized.<locals>._SynchronizedLockProxyip  c                 (   > TR                  5         T$ r   r   r   s    r   	__enter__6synchronized.<locals>._SynchronizedLockProxy.__enter__r  s    r!   c                 &   > TR                  5         g r   r   r   s     r   __exit__5synchronized.<locals>._SynchronizedLockProxy.__exit__v  s    r!   rl   N)r,   r-   r.   r/   r   r   r1   r   s   r   _SynchronizedLockProxyr   p  s     r!   r   c                    [        U 5      R                  SS 5      nUcP  [        R                     [        U 5      R                  SS 5      nUc  [	        5       n[        U SU5        S S S 5        U$ U$ ! , (       d  f       U$ = f)N_synchronized_lock)varsgetsynchronized_synchronized_meta_lockr
   setattrcontextr   s     r   r   (synchronized.<locals>._synchronized_lock  s}     G}  !5t<< 55 G}(()=tD< 7DG%94@ 6 t 65 s   6A11
B c                    [        U 5      R                  SS 5      nUc[  [        R                     [        U 5      R                  SS 5      nUc"  [        R
                  " 5       n[        U SU5        S S S 5        U$ U$ ! , (       d  f       U$ = f)N_synchronized_async_lock)r   r   r   r   r   r	   r   r   s     r   r   .synchronized.<locals>._synchronized_async_lock  s}     G}  !;TB<55G}(()CTJ<"<<>DG%?F 6 t 65 s   AA<<
Bc                 d   > T" Ub  UOU 5         U " U0 UD6sS S S 5        $ ! , (       d  f       g = fr   rl   )r   ry   rF   rG   r   s       r   _synchronized_wrapper+synchronized.<locals>._synchronized_wrapper  s.    
  H,@gND+F+ ONNs   !
/c                    >#    T" Ub  UOU 5       IS h  vN   U " U0 UD6I S h  vN sS S S 5      IS h  vN   $  N% N N	! , IS h  vN  (       d  f       g = f7fr   rl   )r   ry   rF   rG   r   s       r   _synchronized_async_wrapper1synchronized.<locals>._synchronized_async_wrapper  sW     + ,H'
 
 !$1&11
 
 
 2
 
 
 
sL   A;AA=AA?AAAAA
AAc                   :   > \ rS rSrU4S jrS rU 4S jrS rSrg)2synchronized.<locals>._SynchronizedFunctionWrapperi  c                 ~   > T" U R                   5      U l        U R                  R                  5         U R                  $ r   )r$   
_self_lockr   )r   r   s    r   r   <synchronized.<locals>._SynchronizedFunctionWrapper.__enter__  s/    01A1ABDOOO##%??"r!   c                 8    U R                   R                  5         g r   )r   r   r   rF   s     r   r   ;synchronized.<locals>._SynchronizedFunctionWrapper.__exit__  s    OO##%r!   c                    >#    T" U R                   5      U l        U R                  R                  5       I S h  vN   U R                  $  N7fr   )r$   _self_async_lockr   )r   r   s    r   r   =synchronized.<locals>._SynchronizedFunctionWrapper.__aenter__  sB     $<T=M=M$ND!''//111((( 2s   5AA	Ac                 @   #    U R                   R                  5         g 7fr   )r   r   r   s     r   r   <synchronized.<locals>._SynchronizedFunctionWrapper.__aexit__  s     !!))+s   )r   r   N)	r,   r-   r.   r/   r   r   r   r   r1   )r   r   s   r   _SynchronizedFunctionWrapperr     s    	#
	&	)
	,r!   r   r   )hasattrr   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   s
          @@@r   r   r   1  s    8 w	""ww	'B'B &t,,: :#.A # /}EE		0 
	0	%8 	 &m<<@$,2, , ,$ 'w//+
 	
 (WWr!   r   )$__doc__r   r   	functoolsr   inspectr   r   r   r   r   	threadingr	   r
   	__wrapt__r   r   r   
decoratorsr   r   r5   rA   rN   rX   r_   rh   rn   r   r   r   r   r   r   r   r   rl   r!   r   <module>r      s     
   " Q Q !( 2O0 O

 4 

O? O) 6P1 P
!5 

PO 
Pt B   F
23>%!fgXT (,v $r!   