
    1j4                    ,   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZmZmZ ddlmZmZmZmZ dd	lmZmZmZmZmZmZ dd
lmZ erddlmZmZ ddl m!Z! ddl"m#Z# ddlm$Z$  ed      Z% ed      Z& G d d      Z' G d d      Z(ddgZ)y)zEAsync wrapper around :class:`ReadWriteLock` for use with ``asyncio``.    )annotationsN)ThreadPoolExecutor)asynccontextmanager)TYPE_CHECKING	ParamSpecTypeVar   )_append_exception_context_ensure_current_process_fork_transition_register_fork_object)_BackendOutcome_capture_call_drain_future_future_result_raise_cancelled_error_wait_until_done)ReadWriteLock)AsyncGeneratorCallable)futures)TracebackType)AcquireReturnProxy_P_Rc                  h   e Zd ZdZ	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zed d       Zed!d       Zed"d	       Z	ed#d
       Z
ed$ddd%d       Zed$ddd%d       Zd&dZdddd'dZdddd'dZddd(d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y)-AsyncReadWriteLocka  
    Async wrapper around :class:`ReadWriteLock` for use in ``asyncio`` applications.

    This wrapper dispatches every blocking SQLite operation to a thread pool via ``loop.run_in_executor()`` because
    Python's :mod:`sqlite3` module has no async API. It delegates reentrancy, upgrade/downgrade rules, and singleton
    behavior to the underlying :class:`ReadWriteLock`.

    :param lock_file: path to the SQLite database file used as the lock
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable
    :param is_singleton: if ``True``, reuse existing :class:`ReadWriteLock` instances for the same resolved path
    :param loop: event loop for ``run_in_executor``; ``None`` uses the running loop
    :param executor: executor for ``run_in_executor``. When ``None`` this lock creates and owns a dedicated
        single-thread executor so every operation runs on the same thread (SQLite affinity requires this) and shuts it
        down in :meth:`close`. This lock uses a caller-supplied executor as-is and never shuts it down, so after passing
        no executor call :meth:`close` to release the owned one.

    .. versionadded:: 3.21.0

    TN)blockingis_singletonloopexecutorc               f   t        j                         }|| _        d| _        d| _        t        |        t               5  t        ||||      | _        || _	        |d u | _
        |xs t        d      | _        t        j                         |k7  rd}t        |      	 d d d        y # 1 sw Y   y xY w)NF)r   r   r	   )max_workersz:AsyncReadWriteLock construction cannot continue after fork)osgetpid_creator_pid_fork_invalidated_closedr   r   r   _lock_loop_owns_executorr   	_executorRuntimeError)	self	lock_filetimeoutr   r   r    r!   creator_pidmsgs	            /home/vela/workspace/domain/tn-silver/web/tn-site/.runtime/depth-eval/venv/lib/python3.12/site-packages/filelock/_async_read_write.py__init__zAsyncReadWriteLock.__init__>   s     iik'!&d#&y'H[ghDJDJ"*d"2D%J);)JDNyy{k)R"3'' *  s   AB''B0c                .    | j                   j                  S )zThe path to the lock file.)r)   r/   r.   s    r3   r/   zAsyncReadWriteLock.lock_fileV   s     zz###    c                .    | j                   j                  S )zThe default timeout.)r)   r0   r6   s    r3   r0   zAsyncReadWriteLock.timeout[   s     zz!!!r7   c                .    | j                   j                  S )z'Whether blocking is enabled by default.)r)   r   r6   s    r3   r   zAsyncReadWriteLock.blocking`   s     zz"""r7   c                    | j                   S )z2The event loop (or ``None`` for the running loop).)r*   r6   s    r3   r    zAsyncReadWriteLock.loope   s     zzr7   c                    | j                   S )z_The executor used for ``run_in_executor`` (a dedicated single-thread one if none was supplied).)r,   r6   s    r3   r!   zAsyncReadWriteLock.executorj   s     ~~r7   r   c              T  K   || j                   j                  }|| j                   j                  }| j                  ||       d{    d}	 d 	 | j                  |       d{    y7 '# t        $ r}|} d}~ww xY w7 # | j                  |       d{  7   w xY ww)a  
        Async context manager that acquires and releases a shared read lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        Nr<   )r)   r0   r   acquire_readBaseException_release_in_contextr.   r0   r   
body_errorerrors        r3   	read_lockzAsyncReadWriteLock.read_locko   s      ?jj((Gzz**H(;;;+/
	7
 **:666 	<  	J	 7$**:666`   AB(	A1
B(A3 B(+B,B(3	B<A??BB	 B(	B%B!B%%B(c              T  K   || j                   j                  }|| j                   j                  }| j                  ||       d{    d}	 d 	 | j                  |       d{    y7 '# t        $ r}|} d}~ww xY w7 # | j                  |       d{  7   w xY ww)a  
        Async context manager that acquires and releases an exclusive write lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        Nr<   )r)   r0   r   acquire_writer?   r@   rA   s        r3   
write_lockzAsyncReadWriteLock.write_lock   s      ?jj((Gzz**H  8 <<<+/
	7
 **:666 	=  	J	 7$**:666rE   c                   K   	 | j                          d {    y 7 # t        $ r}|t        ||        d }~ww xY wwN)releaser?   r
   )r.   rB   release_errors      r3   r@   z&AsyncReadWriteLock._release_in_context   s<     	,,.   	%)-D	s(   ?  ? 	<7<?c                  K   | j                          | j                  t        j                  | j                  j
                  ||             d{    t        |       S 7 w)a7  
        Acquire a shared read lock.

        See :meth:`ReadWriteLock.acquire_read` for full semantics.

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as an async context manager to release the lock

        :raises RuntimeError: if a write lock is already held on this instance
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r<   Nlock)_raise_if_unusable_run_acquire	functoolspartialr)   r>    AsyncAcquireReadWriteReturnProxyr.   r0   r   s      r3   r>   zAsyncReadWriteLock.acquire_read   sS      	!	 1 1$**2I2I7]e fggg/T:: 	h   AA$A"A$c                  K   | j                          | j                  t        j                  | j                  j
                  ||             d{    t        |       S 7 w)aZ  
        Acquire an exclusive write lock.

        See :meth:`ReadWriteLock.acquire_write` for full semantics.

        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: a proxy that can be used as an async context manager to release the lock

        :raises RuntimeError: if a read lock is already held, or a write lock is held by a different thread
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r<   NrN   )rP   rQ   rR   rS   r)   rG   rT   rU   s      r3   rG   z AsyncReadWriteLock.acquire_write   sS      	!	 1 1$**2J2JG^f ghhh/T:: 	irV   Fforcec                  K   t                | j                  ry| j                  | j                  j                  |       d{    y7 w)a2  
        Release one level of the current lock.

        See :meth:`ReadWriteLock.release` for full semantics.

        :param force: if ``True``, release the lock completely regardless of the current lock level

        :raises RuntimeError: if no lock is currently held and *force* is ``False``

        NrX   )r   
_inherited_runr)   rK   )r.   rY   s     r3   rK   zAsyncReadWriteLock.release   s8      	 !??ii

**%i888s   AAA
Ac                n  K   t                | j                  ry| j                  ry| j                  | j                  j
                        }	 t        |       d{    t        |       d| _        | j                  rGt        j                  t!        j"                  | j$                  j&                  d             d{    yy7 j# t        j                  $ rU}	 t        |       d{  7   n"# t        $ r}t        ||       Y d}~nd}~ww xY wd| _        | j                           d}~ww xY w7 sw)z
        Release the lock (if held) and close the underlying SQLite connection.

        After calling this method, the lock instance is no longer usable.

        NTwait)r   r[   r(   _submitr)   closer   asyncioCancelledErrorr   r?   r   _shutdown_owned_executorr   r+   	to_threadrR   rS   r,   shutdown)r.   close_futurecancellationrC   s       r3   ra   zAsyncReadWriteLock.close   s     	 !??<<||DJJ$4$45		"<000 	|$ ##I$5$5dnn6M6MTX$YZZZ  1%% 	<#L111  <&|U;;<DL))+	 [sy   A
D5C CC  AD5?D3 D5C D0C1*C-+C10D+1	D:DD+DD++D00D5c                  K   | j                  |      }	 t        |       d {    t        |       y 7 # t        j                  $ r}	 t	        |       d {  7   n"# t
        $ r}t        ||       Y d }~nd }~ww xY w	 t	        | j                  | j                  j                               d {  7    # t
        $ r}t        ||       Y d }~ d }~ww xY wd }~ww xY wwrJ   )
r`   r   rb   rc   r   r?   r   r)   rK   r   )r.   acquireacquire_futurerh   rC   s        r3   rQ   zAsyncReadWriteLock._run_acquire   s     g.	">222 	~& 3%% 		<#N333  <&|U;;<<#DLL1C1C$DEEE  ! <&|U;;<		s   C$6 46 C$6 C!
AAAC	A>(A94C9A>>C1B:3B64B:9C:	CCCCCC!!C$c                  K    | j                   |g|i |}	 t        |       d {    t        |      S 7 # t        j                  $ r=}	 t	        |       d {  7    # t
        $ r}t        ||       Y d }~ d }~ww xY wd }~ww xY wwrJ   )r`   r   rb   rc   r   r?   r   r   )r.   funcargskwargsfuturerh   rC   s          r3   r\   zAsyncReadWriteLock._run  s     d4T4V4	"6*** f%% +%% 	<#F+++  ! <&|U;;<	sa   B: 8: B: B
A#AA#"B#	B,A=8B=BBB

Bc           	         | j                   xs t        j                         j                  | j                  t
        t        j                  |g|i |      S rJ   )r*   rb   get_running_looprun_in_executorr,   r   rR   rS   )r.   rm   rn   ro   s       r3   r`   zAsyncReadWriteLock._submit  sK     

8g668IINNd4T4V4
 	
r7   c                V    | j                   r| j                  j                  d       y y )NFr^   )r+   r,   rf   r6   s    r3   rd   z+AsyncReadWriteLock._shutdown_owned_executor"  s%    NN###/ r7   c                `    | j                   xs! t        j                         | j                  k7  S rJ   )r'   r$   r%   r&   r6   s    r3   r[   zAsyncReadWriteLock._inherited&  s$    %%I8I8I)IIr7   c                    t                | j                  rd| j                   d}t        |      | j                  rd}t        j                  |      y )NzAsyncReadWriteLock on z4 was invalidated by fork(); construct a new instancez$Cannot operate on a closed database.)r   r[   r/   r-   r(   sqlite3ProgrammingError)r.   r2   s     r3   rP   z%AsyncReadWriteLock._raise_if_unusable*  sP    !??*4>>*::noCs##<<8C**3// r7   c                    d| _         y )NT)r'   r6   s    r3   _reset_after_fork_in_childz-AsyncReadWriteLock._reset_after_fork_in_child3  s
    !%r7   c                    t        j                         t        | dd       k(  r+t        | dd      r| j                  j	                  d       y y y )Nr&   r+   Fr^   )r$   r%   getattrr,   rf   r6   s    r3   __del__zAsyncReadWriteLock.__del__6  sG     99;'$=='$P`bgBhNN###/ Ci=r7   ))r/   zstr | os.PathLike[str]r0   floatr   boolr   r   r     asyncio.AbstractEventLoop | Noner!   zfutures.Executor | NonereturnNone)r   str)r   r   )r   r   )r   r   )r   zfutures.ExecutorrJ   )r0   zfloat | Noner   zbool | Noner   zAsyncGenerator[None])rB   BaseException | Noner   r   )r0   r   r   r   r   rT   )rY   r   r   r   )r   r   )rj   z Callable[[], AcquireReturnProxy]r   r   )rm   Callable[_P, _R]rn   _P.argsro   	_P.kwargsr   r   )rm   r   rn   r   ro   r   r   z#asyncio.Future[_BackendOutcome[_R]])__name__
__module____qualname____doc__r4   propertyr/   r0   r   r    r!   r   rD   rH   r@   r>   rG   rK   ra   rQ   r\   r`   rd   r[   rP   rz   r}    r7   r3   r   r   (   s   0 (
 !15,0()( (
 ( ( /( *( 
(0 $ $ " " # #     7W[ 7 70 7X\ 7 70;$ ;&;4 ;& .3 9 [<' 
&
$
-4
@I
	,
0 J J0&0r7   r   c                  8    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 ddZy)	rT   zEContext-aware object that releases the async read/write lock on exit.c                    || _         y rJ   rN   )r.   rO   s     r3   r4   z)AsyncAcquireReadWriteReturnProxy.__init__@  s	    	r7   c                "   K   | j                   S wrJ   rN   r6   s    r3   
__aenter__z+AsyncAcquireReadWriteReturnProxy.__aenter__C  s     yys   c                T   K   | j                   j                          d {    y 7 wrJ   )rO   rK   )r.   exc_type	exc_value	tracebacks       r3   	__aexit__z*AsyncAcquireReadWriteReturnProxy.__aexit__F  s      ii!!!s   (&(N)rO   r   r   r   )r   r   )r   ztype[BaseException] | Noner   r   r   zTracebackType | Noner   r   )r   r   r   r   r4   r   r   r   r7   r3   rT   rT   =  s:    O"," (" (	"
 
"r7   rT   )*r   
__future__r   rb   rR   r$   rw   concurrent.futuresr   
contextlibr   typingr   r   r   _apir
   r   r   r   _asyncr   r   r   r   r   r   _read_writer   collections.abcr   r   
concurrentr   typesr   r   r   r   r   rT   __all__r   r7   r3   <module>r      s    K "   	  1 * 4 4   '8"#(t_T]R0 R0j" "& 'r7   