
    \j                    t   U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlmZmZmZmZmZmZ d dl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  ejB                  d
k\  rd dlm"Z" nd dl#m"Z"  ejH                  d      Z%de&d<   ejN                  Z(de&d<   ejR                  jT                  dk(  Z+de&d<    e,ed      xr* ejR                  jT                  dk(  xr ejB                  dk  Z-de&d<   e.e/z  ej`                  e.   z  ej`                  e/   z  e1z  e2z  e3ejh                     z  dz  Z5de&d<   e6e2e2f   Z7de&d<   dZ8de&d<   dZ9de&d<    G d  d!ejt                        Z; e;       Z<d"e&d#<    G d$ d%      Z= e=       Z>d"e&d&<    G d' d(      Z? e?       Z@d"e&d)<    G d* d+ejh                        ZA G d, d-e3      ZB G d. d/eB0      ZCd8d1ZDe
d9d2       ZEd:d3ZFd;d4ZGd<d5ZH ee>        ee@        eeC       e+r ej                  eG        e,ed      r ej                  eF6       d/d7gZKy)=    )annotationsN)contextmanagersuppress)TYPE_CHECKINGClassVarFinalLiteral	TypeAliascast)WeakValueDictionary   )AcquireReturnProxy_ensure_current_process_fork_transition_raise_chained_errors_register_fork_class_register_fork_object)Timeout)Callable	Generator)      )SelffilelockzFinal[logging.Logger]_LOGGERzFinal[Callable[[], int]]_GETPIDpypyzFinal[bool]_IS_PYPYregister_at_forkcpython)r      _NEEDS_CONNECTION_ESCROWr
   _ConnectionParameter_DatabaseIdentityi5wz
Final[int]_MAX_SQLITE_TIMEOUT_MSF   _UNSAFE_FORK_EXIT_STATUSc                      e Zd ZU dZded<   y)_SQLiteTransitionContextr   intdepthN)__name__
__module____qualname__r+   __annotations__     G/root/.hermes/venv/lib/python3.12/site-packages/filelock/_read_write.pyr)   r)   0   s    E3Nr1   r)   r   _SQLITE_TRANSITION_CONTEXTc                  (    e Zd ZddZ	 	 ddZddZy)_ConnectionEscrowc                D    t        j                         | _        d | _        y N)	threadingRLock_lock
_functionsselfs    r2   __init__z_ConnectionEscrow.__init__8   s    __&
txr1   c                    t         sy | j                  5  | j                  dd l}|j	                  d |j
                        }|j                  |j                  j                  |j                        j                  }|j                  |j                  j                  |j                        j                  }||d}t        |      t        d ||            t        d ||            f| _        | j                  cd d d        S # 1 sw Y   y xY w)Nr   z+CPython reference functions have no addressz$Callable[[sqlite3.Connection], None])r"   r:   r;   ctypes
PYFUNCTYPE	py_objectr   	pythonapi	Py_IncRefc_void_pvalue	Py_DecRefRuntimeError)r=   r@   function_typeincrement_addressdecrement_addressmsgs         r2   	functionsz_ConnectionEscrow.functions<   s     (ZZ 	#& & 1 1$8H8H I$*KK0@0@0J0JFOO$\$b$b!$*KK0@0@0J0JFOO$\$b$b!$,0A0IGC&s++?O`Aab?O`Aab# ??	# 	# 	#s   C&DDc                6    t        j                         | _        y r7   )r8   r9   r:   r<   s    r2   _reset_after_fork_in_childz,_ConnectionEscrow._reset_after_fork_in_childQ   s    __&
r1   NreturnNone)rQ   Xtuple[Callable[[sqlite3.Connection], None], Callable[[sqlite3.Connection], None]] | None)r,   r-   r.   r>   rM   rO   r0   r1   r2   r5   r5   7   s    y#	a#*'r1   r5   _CONNECTION_ESCROWc                  F    e Zd ZddZd	dZd
dZddZddZedd       Z	y)_ForkedDatabaseRegistryc                    t        j                         | _        t               | _        t               | _        d| _        d| _        y NF)r8   r9   r:   set_paths_identities_sqlite_used_all_paths_poisonedr<   s    r2   r>   z _ForkedDatabaseRegistry.__init__Y   s3    __&
),365!#( r1   c                    | j                  |      }| j                  5  | j                  }|xs$ || j                  v xs |d uxr || j                  v }d d d        rrdnd|d}t        |      y # 1 sw Y   !xY w)NzOReadWriteLock is unavailable in a PyPy fork child; exec or exit before using itzSQLite database zD was active across fork(); exec or exit before using it in the child)identityr:   r]   rZ   r[   rH   )r=   pathr_   all_paths_poisonedpoisonedrL   s         r2   raise_if_poisonedz)_ForkedDatabaseRegistry.raise_if_poisoned`   s    ==&ZZ 	!%!9!9"tddkk&9thd>R>sW_cgcscsWs 	
  & b'x/st 
 s## 	 	s   5A33A<c                v    | j                   j                  |       || j                  j                  |       y y r7   )rZ   addr[   )r=   r`   r_   s      r2   poison_after_forkz)_ForkedDatabaseRegistry.poison_after_forko   s1      *  r1   c                b    t         r| j                  5  d| _        d d d        y y # 1 sw Y   y xY wNT)r   r:   r\   r<   s    r2   note_sqlite_usez'_ForkedDatabaseRegistry.note_sqlite_uset   s3     )$(!) ) ) )s   %.c                    t        j                         | _        | j                  xs t        xr | j
                  | _        d| _        y rX   )r8   r9   r:   r]   r   r\   r<   s    r2   rO   z2_ForkedDatabaseRegistry._reset_after_fork_in_childy   s6    __&
#'#;#;#_@^TM^M^ !r1   c                r    	 | j                         }|j                  |j                  fS # t        $ r Y y w xY wr7   )statOSErrorst_devst_ino)r`   stat_results     r2   r_   z _ForkedDatabaseRegistry.identity~   s?    	))+K !!;#5#555  		s   * 	66NrP   )r`   pathlib.PathrQ   rR   )r`   rq   r_   _DatabaseIdentity | NonerQ   rR   )r`   rq   rQ   rr   )
r,   r-   r.   r>   rc   rf   ri   rO   staticmethodr_   r0   r1   r2   rV   rV   X   s/    )$+
)
"
 6 6r1   rV   _FORKED_DATABASESc                  b     e Zd ZU ded<   ded<   	 	 	 	 	 	 d	 fdZd
dZ	 	 	 	 ddZd
dZ xZS )_ForkSafeConnectionr*   _creator_pidz+Callable[[sqlite3.Connection], None] | None_decrement_escrowc                R    t         |   |       }t               |_        d |_        |S r7   )super__new__r   rw   rx   )cls_args_kwargs
connection	__class__s       r2   r{   z_ForkSafeConnection.__new__   s+    
 W_S)
"))
'+
$r1   c                :   t               5  t               | j                  k7  r
	 d d d        y t               5  t        j
                  j                  |        | j                  x}d | _         ||        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wr7   )_sqlite_transitionr   rw   r   sqlite3
Connectioncloserx   )r=   	decrements     r2   r   z_ForkSafeConnection.close   s    ! 	$yD---	$ 	$ "# $""((.!%!7!77ID-1D*dO	$	$ 	$$ $	$ 	$s'   B
B=B4BB	
BBc                2    ||\  }} ||        || _         y y r7   )rx   )r=   rM   	incrementr   s       r2   acquire_escrowz"_ForkSafeConnection.acquire_escrow   s(      #, IydO%.D" !r1   c                    t        t        j                  t              5  | j	                          d d d        y # 1 sw Y   y xY wr7   )r   r   ErrorrH   r   r<   s    r2   __del__z_ForkSafeConnection.__del__   s.    gmm\2 	JJL	 	 	s	   9A)r}   r#   r~   r#   rQ   r   rP   )rM   rS   rQ   rR   )	r,   r-   r.   r/   r{   r   r   r   __classcell__r   s   @r2   rv   rv      sQ    BB$ ( 
	$/k/ 
/r1   rv   c                  t     e Zd ZU dZded<   ded<   ded<   ded	<   	 dd
d
d	 	 	 	 	 	 	 	 	 d fdZddZ xZS )_ReadWriteLockMetaa
  
    Resolve singleton instances for ``is_singleton=True`` construction.

    This logic lives here rather than in ReadWriteLock.get_lock so ``ReadWriteLock(path)`` returns cached instances
    without a 2-arg ``super()`` call that type checkers cannot verify.

    0WeakValueDictionary[pathlib.Path, ReadWriteLock]
_instanceszthreading.RLock_instances_lockr*   _instances_pidzset[pathlib.Path]_instances_under_constructionTblockingis_singletonc          	        t                | j                  t               k7  r| j                          t               }|s/t        
|   ||||      }t               |k7  rd}t        |      |S t        j                  |      j                         }| j                  5  || j                  vr|| j                  v rd|}t        |      | j                  }	|	j                  |       	 t        
|   ||||      }t               |k(  r|	j                  |       	 t               |k7  rd}t        |      || j                  |<   n| j                  |   }|j                  |k7  s|j                   |k7  r-d|j                   d|j                    d| d| }t#        |      |cd d d        S # t               |k(  r|	j                  |       w w xY w# 1 sw Y   y xY w)Nr   z5ReadWriteLock construction cannot continue after forkz2Singleton lock construction is already active for z$Singleton lock created with timeout=z, blocking=z, cannot be changed to timeout=)r   r   r   _reset_class_after_forkrz   __call__rH   pathlibPathresolver   r   r   re   discardtimeoutr   
ValueError)r|   	lock_filer   r   r   construction_pidinstancerL   
normalizedconstruction_registryr   s             r2   r   z_ReadWriteLockMeta.__call__   s    	 !*'')"9w'	7X\h'iHy,,M"3''O\\),446
   	/!B!BBNym\C&s++(+(I(I%%))*5B$w/	7Xdp/qHy$44-55jA9 00QC&s++-5z*>>*57*h.?.?8.K:8;K;K:LKX`XiXiWj k55<I[
T  !o%3	 	 y$44-55jA 5	 	s&   A
G(F);B$G)!G

GGc                    t               | _        t        j                         | _        t               | _        t               | _        y r7   )	r   r   r8   r9   r   r   r   rY   r   )r|   s    r2   r   z*_ReadWriteLockMeta._reset_class_after_fork   s/    ,.'oo/$Y,/E)r1   )
r   str | os.PathLike[str]r   floatr   boolr   r   rQ   ReadWriteLockrP   )r,   r-   r.   __doc__r/   r   r   r   r   s   @r2   r   r      sp     A@$$#44
 -
 !-)- -
 - - 
-^2r1   r   c                      e Zd ZU dZ e       Zded<    ej                         Z	 e
       Z e       Zded<   d% fdZe	 d&dd	 	 	 	 	 	 	 d'd	       Z	 d&ddd
	 	 	 	 	 	 	 	 	 d(dZd&ddd)dZd&ddd)dZed*ddd+d       Ze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 	 	 	 	 	 	 	 	 	 	 	 	 d0dZ!d1dZ"	 	 	 	 	 	 	 	 	 	 d2dZ#d3dZ$d%d Z%d4d!Z&d5d"Z'd6d#Z(d7d$Z) xZ*S )8r   ap  
    Cross-process read-write lock backed by SQLite.

    Allows concurrent shared readers or a single exclusive writer. The lock is reentrant within the same mode (multiple
    ``acquire_read`` calls nest, as do multiple ``acquire_write`` calls from the same thread), but upgrading from read
    to write or downgrading from write to read raises :class:`RuntimeError`. Write locks are pinned to the thread that
    acquired them.

    By default, ``is_singleton=True``: calling ``ReadWriteLock(path)`` with the same resolved path returns the same
    instance. The path is handed to :func:`sqlite3.connect` as given, so a ``.db`` extension is a convention rather
    than a requirement; the filesystem must be one the active SQLite VFS supports.

    :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 instances for the same resolved path

    .. versionadded:: 3.21.0

    r   r   zClassVar[set[pathlib.Path]]r   c                    t         |           t               | _        t	        j
                         | _        t               | _        t               | _
        t        |        y r7   )rz   __init_subclass__r   r   r8   r9   r   r   r   rY   r   r   )r|   r   s    r2   r   zReadWriteLock.__init_subclass__  sC    !#,.'oo/$Y,/E)S!r1   Tr   c                    | |||      S )a	  
        Return the singleton :class:`ReadWriteLock` for *lock_file*.

        :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

        :returns: the singleton lock instance

        :raises ValueError: if an instance already exists for this path with different *timeout* or *blocking* values

        r   r0   )r|   r   r   r   s       r2   get_lockzReadWriteLock.get_lock  s      9g99r1   r   c                  t        j                  |      | _        t        j                  |      j                         | _        t        j                  | j                         || _	        || _
        t        j                         | _        t        j                         | _        d| _        d | _        d | _        t%               | _        d | _        d| _        d | _        d| _        t1               | _        d| _        t7        |        t9               5  t;               5  | j=                  d      }|j?                          d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   TFg      @sqlite_timeout) osfspathr   r   r   r   _canonical_pathrt   rc   r   r   r8   Lock_transaction_lock_internal_lock_lock_level_current_mode_write_thread_idrY   _acquisition_thread_ids_con _connection_transaction_released_connection_identity_closedr   rw   _fork_invalidatedr   r   r   _open_connectionr   )r=   r   r   r   r   validation_connections         r2   r>   zReadWriteLock.__init__)  s    9-&||I6>>@++D,@,@A !*!1'nn.>B,014$04	04->B!#I!&d# 	*!3!5 	*$($9$9$9$M!!'')	* 	* 	* 	* 	* 	*s$   E#E;EE	EE!c               *    | j                  d||      S )a  
        Acquire a shared read lock.

        If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a
        read lock while holding a write lock raises :class:`RuntimeError` (downgrade not allowed).

        :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 a 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

        readr   _acquirer=   r   r   s      r2   acquire_readzReadWriteLock.acquire_readG  s      }}VWx}@@r1   c               *    | j                  d||      S )a  
        Acquire an exclusive write lock.

        If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant).
        Attempting to acquire a write lock while holding a read lock raises :class:`RuntimeError` (upgrade not allowed).
        Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raises
        :class:`RuntimeError`.

        :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 a 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

        writer   r   r   s      r2   acquire_writezReadWriteLock.acquire_writeY  s    $ }}Wg}AAr1   Nc             #     K   || j                   }|| j                  }| j                  ||       	 d | j                          y# | j                          w xY ww)a}  
        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   r   r   releaser   s      r2   	read_lockzReadWriteLock.read_lockm  sR      ?llG}}H'H5	LLNDLLN   0AA AAAc             #     K   || j                   }|| j                  }| j                  ||       	 d | j                          y# | j                          w xY ww)a  
        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   r   r   r   r   s      r2   
write_lockzReadWriteLock.write_lock  sR      ?llG}}H7X6	LLNDLLNr   F)forcec                   t               5  t                | j                  r
	 ddd       y| j                  d       | j	                  |d       ddd       y# 1 sw Y   yxY w)al  
        Release one level of the current lock.

        When the lock level reaches zero the underlying SQLite transaction is rolled back, releasing the database lock.

        :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``

        Nr   Fr   r   r   r   
_inherited_raise_if_acquiring_release)r=   r   s     r2   r   zReadWriteLock.release  sY      	4#%	4 	4 $$Y/MMUM3	4 	4 	4   A$AA"c                    t               5  t                | j                  r
	 ddd       y| j                  d       | j	                  dd       ddd       y# 1 sw Y   yxY w)z
        Release the lock (if held) and close the underlying SQLite connection.

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

        Nr   Tr   r   r<   s    r2   r   zReadWriteLock.close  sY      	2#%	2 	2 $$W-MMDM1	2 	2 	2r   c               v   | j                   5  | j                  5  | j                  dk(  rR|r'| j                  |rd| _        	 d d d        d d d        y |s'd| j
                   dt        |        d}t        |      |s6| j                  dkD  r'| xj                  dz  c_        	 d d d        d d d        y 	 | j                          | j                          |rd| _        d d d        d d d        y # t        j                  $ r | j                  r| j                           w xY w# 1 sw Y   FxY w# 1 sw Y   y xY w)Nr   TzCannot release a lock on  (lock id: z) that is not heldr   )r   r   r   r   r   r   idrH   _finish_connectionr   r   r   _clear_lock_state)r=   r   r   rL   s       r2   r   zReadWriteLock._release  s3   ## 	$T%8%8 	$1$TYY.'+	$ 	$ 	$ 5dnn5E[QSTXQYPZZlmC&s++T--1  A% 	$ 	$ 	$'')
 ""$#)	$ 	$ 	$ == 88**,	$ 	$ 	$ 	$sM   D/(D#D/AD##D/5C/D#D//1D  D##D,	(D//D8c                .    d| _         d | _        d | _        y )Nr   )r   r   r   r<   s    r2   r   zReadWriteLock._clear_lock_state  s    ! $r1   c                    t               t        | dd       k(  rHt        | dd       x}8t        t        j                  t
              5  |j                          d d d        y y y # 1 sw Y   y xY w)Nrw   r   )r   getattrr   r   r   rH   r   )r=   r   s     r2   r   zReadWriteLock.__del__  si    9nd;;wW[]ceiOjAj@w'--6 #  "# # Ax;# #s   A""A+c                J   | j                   *t        j                  | j                  | j                         d | _         d| _        d | _        t        j                         | _        t        j                         | _	        | j                          t               | _        d| _        y rh   )r   rt   rf   r   r   r   r8   r   r   r   r   rY   r   r   r<   s    r2   rO   z(ReadWriteLock._reset_after_fork_in_child  s{    99 //0D0DdF_F_`	04-$(!!*!1'nn. '*u$!%r1   c                L    | j                   xs t               | j                  k7  S r7   )r   r   rw   r<   s    r2   r   zReadWriteLock._inherited  s     %%Gd6G6G)GGr1   c                    t                | j                  rd| j                   d}t        |      | j                  rd}t        j                  |      y )NReadWriteLock on 4 was invalidated by fork(); construct a new instancez$Cannot operate on a closed database.)r   r   r   rH   r   r   ProgrammingError)r=   rL   s     r2   _raise_if_unusablez ReadWriteLock._raise_if_unusable  sP    !??%dnn%55ijCs##<<8C**3// r1   c          	        t               5  | j                          t               }t        j                         }| j
                  5  | j                  dkD  r#| j                  |      cd d d        cd d d        S || j                  v rd| j                   d}t        |      | j                  j                  |       d d d        	 t        j                         }| j                  ||       	 | j                          | j                  |||||      | j                   j#                          | j
                  5  | j                  j%                  |       d d d        cd d d        S # 1 sw Y   xY w# 1 sw Y   xY w# | j                   j#                          w xY w# | j
                  5  | j                  j%                  |       d d d        w # 1 sw Y   w xY wxY w# 1 sw Y   y xY w)Nr   z Cannot acquire ReadWriteLock on + while acquisition is active in this threadr   r   r   operation_pid
start_time)r   r   r   r8   	get_identr   r   _validate_reentrantr   r   rH   re   timeperf_counter_acquire_transaction_lock_do_acquire_innerr   r   r   )r=   moder   r   r   	thread_idrL   r   s           r2   r   zReadWriteLock._acquire  s    	D##%#IM!++-I$$ <##a'33D9< <		D 	D  < <<<T^^<LLwxC&s++,,00;<D!..0
..'.R
5++-11!)&3#- 2  **224(( D0088CD5	D 	D< <,D D **224(( D0088CD D D5	D 	Ds   ;G E'&	G9AE'=G'F.%E?F-G9E3G'E0	,G3E<8G?FFG+G		GGGGG(c               L   | j                   5  | j                  dkD  r| j                  |      cd d d        S 	 d d d        | j                  | j	                          	 | j                  ||||       | j                  |||||f       | j                  |       | j                   5  | j                  |       || _        d| _        |dk(  rt%        j&                         | _        d d d        t+        |       S # 1 sw Y   xY w# t        $ r}t        |t        j                        r#dt        |      v rt        | j                        }n|}	 | j	                          n,# t        j                  $ r}t!        ||       Y d }~nd }~ww xY w||ur|d  d }~ww xY w# 1 sw Y   xY w)Nr   r   )r   	operationzdatabase is lockedr   r   lock)r   r   r   r   r   _open_for_acquisition_configure_and_begin_raise_if_process_changedBaseException
isinstancer   OperationalErrorstrr   r   r   r   r   r8   r   r   r   )	r=   r   r   r   r   r   erroracquisition_errorcleanup_errors	            r2   r   zReadWriteLock._do_acquire_inner  s       	6!#//5	6 	6#	6 99 ##%	&&!+%	 '  %%!(*5	 &  **=9    	>**=9!%D Dw(1(;(;(=%	> "t,,M	6 	6&  	%!9!9:?SWZ[`Wa?a$+DNN$;!$)!H'')== H%&7GGH -'T1		> 	>sY    C8=D &>F8D	F?FEFF1F=FFFFF#c               :   t               5  t        ||t        j                         |z
        dz  }| j	                  |      }|dt
        j                  | j                        c| _        | _	        | _
        | j                  |       d d d        y # 1 sw Y   y xY w)Nr   already_waited  r   F)r   timeout_for_sqliter   r   r   rt   r_   r   r   r   r   r  )r=   r   r   r   r   r   r   s          r2   r  z#ReadWriteLock._open_for_acquisitionA  s    ! 	:"%#'#4#4#6#C
   ..n.MJ!**4+?+?@ XDIt<d>W
 **=9	: 	: 	:s   A=BBc                  t               5  |\  }}t        d| j                        }t        j                         |z
  }t        |||      }	| j                  |       |j                  d|	 d      j                          t        j                         |z
  }t        |||      }
| j                  |       |
|	k7  rd|
 dnd}||dk(  rdnd	z  }|d
k(  r|dz  }|j                  |      j                          d d d        y # 1 sw Y   y xY w)Nrv   r  zPRAGMA busy_timeout=z; PRAGMA journal_mode=MEMORY;z;  r   zBEGIN EXCLUSIVE TRANSACTION;zBEGIN TRANSACTION;r   z( SELECT name FROM sqlite_schema LIMIT 1;)	r   r   r   r   r   r  r  executescriptr   )r=   r   r   r   r   r   r   r   waited
timeout_ms
recomputed
statementss               r2   r  z"ReadWriteLock._configure_and_beginS  s     ! 	9(1%M:3TYY?J&&(:5F+GhW]^J**=9$$';J<Gd%eflln &&(:5F+GhW]^J**=9BLPZBZ/
|2>`bJDGO8QeeJv~ HH
$$Z0668-	9 	9 	9s   C-DD
c                  t               5  t               }t        j                         }t               |k7  rd}t	        |      t        t        j                  | j                        t        |      }||j                  |       t               |k7  rJt        j                  | j                  t        j                  | j                               d}t	        |      |cd d d        S # 1 sw Y   y xY w)Nz9SQLite connection construction cannot continue after fork)factoryr   )r   r   rT   rM   rH   _connectr   r   r   rv   r   rt   rf   r_   )r=   r   creator_pidrM   rL   r   s         r2   r   zReadWriteLock._open_connections  s    ! 	!)K*446IyK'Q"3''!		$../+&J
 $)))4yK'!33((%..t/C/CD R"3'')	 	 	s   CC((C1c                   t               5  | j                  x}
	 d d d        y d }| j                  s,|j                  r	 |j	                          d| _        nd| _        	 |j                          d | _        d| _        d | _	        ||	 d d d        y # t
        j                  $ r }|j                  r d| _        |}Y d }~bd }~ww xY w# t
        j                  $ r}|t        ||        d }~ww xY w# 1 sw Y   y xY wrh   )
r   r   r   in_transactionrollbackr   r   r   r   r   )r=   r   rollback_errorr	  close_errors        r2   r   z ReadWriteLock._finish_connection  s   ! 	%"ii'
0	% 	% 48N88,,E"++- AE=<@D9  "
 DI48D1(,D%)$$ *5	% 	% #== /%44!@D=).	/ == !-).+F'	% 	%s]   C2C2BC2C/C2C%C ;C2 CC2C/C**C//C22C;c                   | j                   |k7  rB|dk(  rdnd}|dk(  rdnd}d| d| j                   dt        |        d| d	| d
}t        |      |dk(  rYt	        j
                         x}| j                  k7  r6d| j                   dt        |        d| d| j                   }t        |      | xj                  dz  c_        t        |       S )Nr   r   	downgradeupgradezCannot acquire z	 lock on r   z): already holding a z lock (z not allowed)zCannot acquire write lock on z) from thread z while it is held by thread r   r   )	r   r   r   rH   r8   r   r   r   r   )r=   r   opposite	directionrL   curs         r2   r   z!ReadWriteLock._validate_reentrant  s    %"&&.wfH'+v~9I!$y0@BtH: V%%-Jgi[O  s##7?y':':'< <AVAVV//?{2d8* U"e#?@U@U?VX  s##A!t,,r1   c                   |s| j                   j                  d      }n?|dk(  r| j                   j                  d      }n| j                   j                  d|      }|st        | j                        d y )NFr   r   Tr   )r   acquirer   r   )r=   r   r   acquireds       r2   r   z'ReadWriteLock._acquire_transaction_lock  sr    --55u5EH]--55t5DH--55tW5UH$..)t3 r1   c                    | j                   5  t        j                         | j                  v }d d d        rd| d| j                   d}t        |      y # 1 sw Y   *xY w)NzCannot z ReadWriteLock on r   )r   r8   r   r   r   rH   )r=   r   active_in_current_threadrL   s       r2   r   z!ReadWriteLock._raise_if_acquiring  sl       	]'0':':'<@\@\'\$	]#I;&88HHstCs## $	] 	]s   !AA c                l    t               |k7  s| j                  rd| j                   d}t        |      y )Nr   r   )r   r   r   rH   )r=   r   rL   s      r2   r  z'ReadWriteLock._raise_if_process_changed  s6    9%%dnn%55ijCs## *9r1   rP   r   )r   r   r   r   r   r   rQ   r   )
r   r   r   r   r   r   r   r   rQ   rR   )r   r   r   r   rQ   r   r7   )r   zfloat | Noner   zbool | NonerQ   Generator[None])r   r   rQ   rR   )r   r   r   r   rQ   rR   )rQ   r   )r   Literal['read', 'write']r   r   r   r   rQ   r   )r   r.  r   r   r   r   r   r*   r   r   rQ   r   )
r   r   r   r   r   r*   r   r   rQ   rR   )
r   r.  r   r   r   r   r   ztuple[int, float]rQ   rR   )r   r   rQ   rv   )r   r.  rQ   r   )r   r   r   r   rQ   rR   )r   z&Literal['acquire', 'close', 'release']rQ   rR   )r   r*   rQ   rR   )+r,   r-   r.   r   r   r   r/   r8   r9   r   r   r   rY   r   r   classmethodr   r>   r   r   r   r   r   r   r   r   r   r   rO   propertyr   r   r   r   r  r  r   r   r   r   r   r  r   r   s   @r2   r   r      s   * DWCXJ@X%ioo'OYNAD!#>F" AC:Y]:.:9>:RV:	: :( *
 !*)* *
 * * 
*<AD A$BT B( QU  ( RV  ( (- 4$2$.%
#

& H H0D<0-&0- 0-
 0- 0- 0- 
0-d:$9&9 9
 9 %9 
9@.%<-$4$$r1   r   )	metaclassc               ^    t         j                          t        j                  | d|d|      S )NFr   )check_same_threadr  cached_statementsr   )rt   ri   r   connect)databaser  r   s      r2   r  r    s.    %%'?? r1   c               #     K   t         xj                  dz  c_        	 d  t         xj                  dz  c_        y # t         xj                  dz  c_        w xY ww)Nr   )r3   r+   r0   r1   r2   r   r     sB     $$)$."((A-("((A-(s   A; AAAc                 X    t         j                  rt        j                  t               y y r7   )r3   r+   r   _exitr'   r0   r1   r2   _abort_forked_sqlite_transitionr:    s    !''
)* (r1   c                8    | dk(  rt         j                          y y )Nzsqlite3.connect)rt   ri   )eventr}   s     r2   _track_sqlite_user=    s    !!))+ "r1   c                   |du ry| dk(  rt         S | dk  rd}t        |      t        | dkD  rt        | |z
  d      n| dz        }|t         kD  s|dk  r!t        j                  d| t                t         S |S )NFr   r   z+timeout must be a non-negative number or -1r  z7timeout %s is too large for SQLite, using %s ms instead)r%   r   r*   maxr   warning)r   r   r  rL   r  s        r2   r  r    s    5"}%%{;o'A+c'N2A67VZZ[J**j1nQSZ\rs%%r1   )after_in_childr  )r6  r  r  ztype[_ForkSafeConnection]r   r   rQ   rv   )rQ   r-  rP   )r<  r  r}   ztuple[object, ...]rQ   rR   )r   r   r   r   r  r   rQ   r*   )L
__future__r   loggingr   r   r   sysr8   r   
contextlibr   r   typingr   r   r   r	   r
   r   weakrefr   _apir   r   r   r   r   r   _errorr   collections.abcr   r   version_infor   typing_extensions	getLoggerr   r/   getpidr   implementationnamer   hasattrr"   r  bytesPathLiker   r*   typer   r#   tupler$   r%   r'   localr)   r3   r5   rT   rV   rt   rv   r   r   r  r   r:  r=  r  addaudithookr   __all__r0   r1   r2   <module>rY     s[   "  	   
   / K K '  3
7"*!2!2!2:!>	 >$&II	! -**//69+ 9B"#k(:(:(?(?9(LkQTQaQadkQk +  %K"++c""R[[%77%?#EWM_M_H``cgg i   %S#X 9 . &7 
 6') * )y  %=$> E >' '< ./ E /,6 ,6^ 34 5 4#',, #LA2 A2HZ$0 Z$z . .+
,
$ ( ) ' ( ] #C&'
2!"B'FG r1   