
    *Jf1              	          U d 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 ddlm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZmZmZ dd	lmZmZ dd
lmZ ej        Zej        Zej        Z ej        Z!ej        Z"ej        Z# ej$        e%          Z&ddee         de'dee(         defdZ)ddee         dee(         defdZ*	 ddee         de'dee(         defdZ+ddee         dee(         defdZ,ddee         de'dee(         defdZ- e.ed          rPeej/        ej0        z  ej1        z  eej/        ej2        z  ej1        z  e ej/        ej0        ej2        z  ej1        z  z  iZ3ni Z3ddee         de'dee(         defdZ4 e.ed          r eej5        eej6        e ej5        ej6        z  iZ7ni Z7ddee         de'dee(         defdZ8de9fdZ:erej;        Z;ee<d<   dej=        v rNej=        d         Z>e>?                    d          re> e@            vr eAde>           e@            e>         ZBdS erejC        dk    r e:            se;ZBdS ej         eDedd          u re-ZBdS  e.ed          re8ZBdS e)ZBdS ) z
Code concerned with waiting in different contexts (blocking, async, etc).

These functions are designed to consume the generators returned by the
`generators` module function and to return their final value.

    N)Optional)get_event_loopwait_forEventTimeoutError)DefaultSelector   )errors)RVPQGen	PQGenConnWaitFunc)WaitReady)_psycopggenfilenotimeoutreturnc                    	 t          |           }t                      5 }	 |                    ||           d}|s|                    |          }||                    |           |d         d         }||z  sJ |                     |          }r# 1 swxY w Y   dS # t          $ r"}|j        r|j        d         nd}|cY d}~S d}~ww xY w)a+  
    Wait for a generator using the best strategy available.

    :param gen: a generator performing database operations and yielding
        `Ready` values when it would block.
    :param fileno: the file descriptor to wait on.
    :param timeout: timeout (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C.
    :type timeout: float
    :return: whatever `!gen` returns on completion.

    Consume `!gen`, scheduling `fileno` for completion when it is reported to
    block. Once ready again send the ready state back to `!gen`.
    TNr   r   r	   )nextr   registerselect
unregistersendStopIterationargs)	r   r   r   sselrlistreadyexrvs	            R/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/psycopg/waiting.pywait_selectorr&   $   s#   II 
	$#	$VQ''' 8JJwJ77E   8v&&&$Qx{5y   HHUOO	$
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$ 
	$    !w0D						s;   B  A4BBB  BB   
C*CCCc                    	 t          |           \  }}|sd}t                      5 }	 |                    ||           |                    |          }|                    |           |st          j        d          |d         d         }|                     |          \  }}~# 1 swxY w Y   dS # t          $ r"}|j	        r|j	        d         nd}|cY d}~S d}~ww xY w)a  
    Wait for a connection generator using the best strategy available.

    :param gen: a generator performing database operations and yielding
        (fd, `Ready`) pairs when it would block.
    :param timeout: timeout (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C. If zero or None, wait indefinitely.
    :type timeout: float
    :return: whatever `!gen` returns on completion.

    Behave like in `wait()`, but take the fileno to wait from the generator
    itself, which might change during processing.
    NTr   connection timeout expiredr   r	   )
r   r   r   r   r   eConnectionTimeoutr   r   r   )	r   r   r   r   r    r!   r"   r#   r$   s	            r%   	wait_connr+   F   s,   II	 	G 	,#,VQ'''

7
33v&&& L-.JKKK$Qx{HHUOO	,	, 	, 	, 	, 	, 	, 	, 	, 	, 	,    !w0D						s;   $B3 B B&&B**B3 -B*.B3 3
C=CCCc                   
K   t                      
t                      }dt          ddf
fd}	 t          |           }	 |t          z  }|t
          z  }|s|st          j        d|           
                                 d|r|	                    ||t                     |r|                    ||t                     	 |
                                 d{V  n:	 t          
                                |           d{V  n# t          $ r Y nw xY w|r|                    |           |r|                    |           n4# |r|                    |           |r|                    |           w w xY w|                               }S# t&          $ r"}|j        r|j        d         nd}	|	cY d}~S d}~ww xY w)aW  
    Coroutine waiting for a generator to complete.

    :param gen: a generator performing database operations and yielding
        `Ready` values when it would block.
    :param fileno: the file descriptor to wait on.
    :return: whatever `!gen` returns on completion.

    Behave like in `wait()`, but exposing an `asyncio` interface.
    stater   Nc                 :    | z                                    d S Nsetr-   evr"   s    r%   wakeupzwait_async.<locals>.wakeup{   s    
    Tbad poll status: r   )r   r   r   r   WAIT_RWAIT_Wr)   InternalErrorclear
add_readerREADY_R
add_writerREADY_Wwaitr   r   remove_readerremove_writerr   r   r   )r   r   r   loopr4   r   readerwriterr#   r$   r3   r"   s             @@r%   
wait_asyncrE   g   s>      
BDe        
II	 ZFZF ?& ?o&=!&=&=>>>HHJJJE 9888 9888/?''))OOOOOOOO&rwwyy'::::::::::'     /&&v... /&&v...  /&&v... /&&v..../A1	 4    !w0D						sZ   BF E
 "(D 
E
 
DE
 DE
 /F 
1E;;F 
GF<6G<Gc                 x  
K   t                      
t                      }dt          ddf
fd}	 t          |           \  }}|sd}	 |t          z  }|t
          z  }|s|st          j        d|           
                                 d|r|	                    ||t                     |r|                    ||t                     	 t          
                                |           d{V  |r|                    |           |r|                    |           n4# |r|                    |           |r|                    |           w w xY w|                               \  }}'# t$          $ r t          j        d          t(          $ r"}|j        r|j        d         nd}	|	cY d}~S d}~ww xY w)	a  
    Coroutine waiting for a connection generator to complete.

    :param gen: a generator performing database operations and yielding
        (fd, `Ready`) pairs when it would block.
    :param timeout: timeout (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C. If zero or None, wait indefinitely.
    :return: whatever `!gen` returns on completion.

    Behave like in `wait()`, but take the fileno to wait from the generator
    itself, which might change during processing.
    r-   r   Nc                 4    |                                   d S r/   r0   r2   s    r%   r4   zwait_conn_async.<locals>.wakeup   s    
r5   Tr6   r   r(   )r   r   r   r   r7   r8   r)   r9   r:   r;   r<   r=   r>   r   r?   r@   rA   r   r   r*   r   r   )r   r   rB   r4   r   r   rC   rD   r#   r$   r3   r"   s             @@r%   wait_conn_asyncrH      s0      
BDe        
II	 	G	(ZFZF ?& ?o&=!&=&=>>>HHJJJE 9888 9888/rwwyy'222222222 /&&v... /&&v...  /&&v... /&&v..../IFA%	((  @ @ @!">???   !w0D						s6   BE0 (D" 3/E0 "1EE0 0'F9F4.F94F9c                 R   	 t          |           }d}|f}	 t          j        |t          z  r|n||t          z  r|n|||          \  }}}d}	|rt          }	|r
|	t
          z  }	|	sN|                     |	          }c# t          $ r"}
|
j        r|
j        d         nd}|cY d}
~
S d}
~
ww xY w)z
    Wait for a generator using select where supported.

    BUG: on Linux, can't select on FD >= 1024. On Windows it's fine.
     Tr   N)	r   r   r7   r8   r<   r>   r   r   r   )r   r   r   r   emptyfnlistrlwlxlr"   r#   r$   s               r%   wait_selectrP      s    II	 f*/%f*/%	 JBB E   !  A	 "    !w0D						s   A8A: :
B&B!B&!B&EpollSelectorc                 n   	 t          |           }||dk     rd}nt          |dz            }t          j                    5 }t          |         }|                    ||           	 d}|s|                    |          }||d         d         }d}|t          j         z  rt          }|t          j	         z  r
|t          z  }|                     |          }t          |         }|                    ||           # 1 swxY w Y   dS # t          $ r"}	|	j        r|	j        d         nd}
|
cY d}	~	S d}	~	ww xY w)a$  
    Wait for a generator using epoll where supported.

    Parameters are like for `wait()`. If it is detected that the best selector
    strategy is `epoll` then this function will be used instead of `wait`.

    See also: https://linux.die.net/man/2/epoll_ctl

    BUG: if the connection FD is closed, `epoll.poll()` hangs. Same for
    EpollSelector. For this reason, wait_poll() is currently preferable.
    To reproduce the bug:

        export PSYCOPG_WAIT_FUNC=wait_epoll
        pytest tests/test_concurrency.py::test_concurrent_close
    Nr        @@Tr	   )r   intr   epoll_epoll_evmasksr   pollEPOLLOUTr<   EPOLLINr>   r   modifyr   r   )r   r   r   r   rU   evmaskfileevsr3   r"   r#   r$   s              r%   
wait_epollr]   	  s    II?gkkGG'F*++G\^^ 	-u#A&FNN66***-! 2#jj11G " 2QZ](( $#E' %W$EHHUOO'*VV,,,-	- 	- 	- 	- 	- 	- 	- 	- 	- 	-$    !w0D						s<   ?D B:C;;C??D C?D 
D4D/)D4/D4PollSelectorc                 R   	 t          |           }||dk     rd}nt          |dz            }t          j                    }t          |         }|                    ||           	 d}|s|                    |          }||d         d         }d}|t          j         z  rt          }|t          j         z  r
|t          z  }| 
                    |          }t          |         }|                    ||           # t          $ r"}	|	j        r|	j        d         nd}
|
cY d}	~	S d}	~	ww xY w)za
    Wait for a generator using poll where supported.

    Parameters are like for `wait()`.
    Nr   rS   Tr	   )r   rT   r   rW   _poll_evmasksr   POLLOUTr<   POLLINr>   r   rZ   r   r   )r   r   r   r   rW   r[   r\   r3   r"   r#   r$   s              r%   	wait_pollrc   B  sI   II?gkkGG'F*++G{}}q!ff%%%	(G -))G,,  -ABEV^O#  V]N" ! A"1%FKK'''	(    !w0D						s   C8C: :
D&D!D&!D&c                      t           j                            d          } | rK	 |                     d          rdS n2# t          $ r%}t
                              d|           Y d}~nd}~ww xY wdS )z
    Detect if some greenlet library has patched the select library.

    If this is the case, avoid to use the wait_c function as it doesn't behave
    in a collaborative way.

    Currently supported: gevent.
    zgevent.monkeyr   Tz+failed to detect gevent monkey-patching: %sNF)sysmodulesgetis_module_patched	Exceptionloggerwarning)mr#   s     r%   _is_select_patchedrm   g  s     	((A N	N""8,, t 	N 	N 	NNNH"MMMMMMMM	N 5s   ; 
A*A%%A*r?   PSYCOPG_WAIT_FUNCwait_zHPSYCOPG_WAIT_FUNC should be the name of an available wait function; got win32SelectSelectorr/   )E__doc__osre   r   logging	selectorstypingr   asyncior   r   r   r   r    r
   r)   abcr   r   r   r   _enumsr   r   _cmoduler   Rr7   Wr8   RWWAIT_RWr<   r>   READY_RW	getLogger__name__rj   rT   floatr&   r+   rE   rH   rP   hasattrEPOLLONESHOTrY   EPOLLERRrX   rV   r]   rb   ra   r`   rc   boolrm   wait_c__annotations__environfname
startswithglobalsImportErrorr?   platformgetattrrJ   r5   r%   <module>r      s     
			 



             A A A A A A A A A A A A % % % % % %       / / / / / / / / / / / / 0 0 0 0 0 0 0 0      		
'
'
'8		8	$	$ uRy #  SU    D 9R= 8E? b    D =A7 7	r77*25/77 7 7 7t6 6y} 6x 6RT 6 6 6 6x U2Y  huo QS    B 79o&& #fn4vF#fo5G$>FO+fo=?NN N, ,E"I ,s ,Xe_ ,PR , , , ,^ 79n%% /MM M" "59 "c "HUO "r " " " "JD    *  _F  "*$$J*+EG$$ 
WWYY(>(>k 
 
 	
 799UDDD
  #,'))2D2D2F2F)DDD'')5Et"L"LLLDDDWY''  DDD DDDr5   