
    gi6                        U 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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 dd
l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        Z% ej&        e'          Z(ej)        dk    rd+dZ*nd+dZ*d,d-dZ+d,d.dZ,d,d-dZ-d,d.dZ.d,d-dZ/ e0ed          r8eej1        ej2        z  eej1        ej3        z  e ej1        ej2        z  ej3        z  iZ4ni Z4d,d-d Z5 e0ed!          r0eej6        eej7        e ej6        ej7        z  iZ8ej6        ej7        z   Z9ni Z8d,d-d"Z:d/d$Z;erej<        Z<d%e=d&<   d'ej>        v rNej>        d'         Z?e?@                    d(          re? eA            vr eBd)e?           eA            e?         ZCdS erej)        dk    r e;            se<ZCdS ej         eDed*d          u re/ZCdS  e0ed!          re:ZCdS e+ZCdS )0z
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.

    )annotationsN)EventTimeoutErrorget_event_loopwait_for)DefaultSelector   )errors)RVPQGen	PQGenConnWaitFunc)Ready)Wait)_psycopgwin32filenointreturnNonec                ~    	 t          j        |            dS # t          $ r}t          j        d          |d}~ww xY w)zC
        Raise OperationalError if the connection is lost.
        connection socket closedN)osfstat	ExceptioneOperationalError)r   exs     _/var/www/html/Pagina-Ingenieria-Requisitos/venv/lib/python3.11/site-packages/psycopg/waiting.py_check_fd_closedr    (   sU    	IHV 	I 	I 	I$%?@@bH	Is    
<7<c                    d S N )r   s    r   r    r    4   s                gen	PQGen[RV]intervalfloatr   c                   |t          d          	 t          |           }t                      5 }|                    ||           	 |                    |          x}s*t          |           |                     t                     C|d         d         }|                     |          }|                    ||           |# 1 swxY w Y   dS # t          $ r}|j	        }|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 interval: interval (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C.
    :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`.
    N%indefinite wait not supported anymoreTtimeoutr   r	   )

ValueErrornextr   registerselectr    send
READY_NONEStopIterationvalue)	r&   r   r(   sselrlistreadyr   rvs	            r   wait_selectorr;   8   s1    @AAAII 	(#LL###	(!$H!=!== $V,,,HHZ(((aHHUOOVQ'''	(	( 	( 	( 	( 	( 	( 	( 	( 	( 	(    						s;   C BCCC CC 
C.C)#C.)C.PQGenConn[RV]c                   |t          d          	 t          |           \  }}t                      5 }|                    ||           	 |                    |          x}s|                     t                     4|                    |           |d         d         }|                     |          \  }}|                    ||           # 1 swxY w Y   dS # t          $ r}|j	        }|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 interval: interval (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C.
    :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.
    Nr+   Tr,   r   r	   )
r.   r/   r   r0   r1   r2   r3   
unregisterr4   r5   )	r&   r(   r   r6   r7   r8   r9   r   r:   s	            r   	wait_connr?   \   s=    @AAAII	 
	(#LL###(!$H!=!== HHZ(((v&&&aHHUOO	VQ'''(
	( 
	( 
	( 
	( 
	( 
	( 
	( 
	( 
	( 
	(    						s;    C BCCC CC 
C:'C5/C:5C:c                  
K   |t          d          t                      
t                      }d
fd}	 t          |           }	 |t          z  }|t
          z  }|s|st          j        d	|           
                                 d
|r|	                    ||t                     |r|                    ||t                     	 	 t          
                                |           d{V  n# t          $ r Y nw xY w|r|                    |           |r|                    |           n4# |r|                    |           |r|                    |           w w xY w|                               }6# t&          $ r}t          j        d          |d}~wt*          $ r}|j        }	|	cY d}~S d}~ww xY w)a  
    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.
    :param interval: interval (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C.
    :return: whatever `!gen` returns on completion.

    Behave like in `wait()`, but exposing an `asyncio` interface.
    Nr+   stater   r   r   c                :    | z                                    d S r"   setrA   evr9   s    r   wakeupzwait_async.<locals>.wakeup   s    
r$   Tbad poll status: r   r   rA   r   r   r   )r.   r   r   r/   WAIT_RWAIT_Wr   InternalErrorclear
add_readerREADY_R
add_writerREADY_Wr   waitr   remove_readerremove_writerr2   OSErrorr   r4   r5   )r&   r   r(   looprG   r6   readerwriterr   r:   rF   r9   s             @@r   
wait_asyncrY   ~   sE      @AAA 
BD      
II	 ZFZF ?f ?o&=!&=&=>>>HHJJJE 9888 9888	/"27799h7777777777#   D  /&&v... /&&v...  /&&v... /&&v..../A+	 .  E E E !;<<"D   						s`   BF  (C6 5D5 6
D D5 DD5 /F  51E&&F   
F?
FF?,F:4F?:F?c                  
K   |t          d          t                      
t                      }d
fd}	 t          |           \  }}	 |t          z  }|t
          z  }|s|st          j        d	|           
                                 d
|r|	                    ||t                     |r|                    ||t                     	 |r:	 t          
                                |           d{V  n*# t          $ r Y nw xY w
                                 d{V  |r|                    |           |r|                    |           n4# |r|                    |           |r|                    |           w w xY w|                               \  }}U# t&          $ r}|j        }	|	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 interval: interval (in seconds) to check for other interrupt, e.g.
        to allow Ctrl-C.
    :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.
    Nr+   rA   r   r   r   c                4    |                                   d S r"   rC   rE   s    r   rG   zwait_conn_async.<locals>.wakeup   s    
r$   TrH   r   rI   )r.   r   r   r/   rJ   rK   r   rL   rM   rN   rO   rP   rQ   r   rR   r   rS   rT   r2   r4   r5   )r&   r(   rV   rG   r   r6   rW   rX   r   r:   rF   r9   s             @@r   wait_conn_asyncr\      sA      @AAA 
BD      
II		(ZFZF ?f ?o&=!&=&=>>>HHJJJE 9888 9888/ $&rwwyy(;;;;;;;;;;'    ''))OOOOOOO /&&v... /&&v...  /&&v... /&&v..../IFA1	(4    						sZ   BF" E (C; :E ;
DE DE %/F" 1FF" "
F?,F:4F?:F?c                   |t          d          	 t          |           }d}|f}	 t          j        |t          z  r|n||t          z  r|n|||          \  }}}|r#t          |           t          j        d          d}	|rt          }	|r
|	t          z  }	| 
                    |	          }# t          $ r}
t          j        d          |
d}
~
wt          $ r}
|
j        }|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.
    Nr+   r#   Tr   r   )r.   r/   r1   rJ   rK   r    r   r   rO   rQ   r2   rU   r4   r5   )r&   r   r(   r6   emptyfnlistrlwlxlr9   r   r:   s               r   wait_selectrc      s>    @AAAII	 f*/%f*/%	 JBB  E (((()CDDDE   ! A#	 &  E E E !;<<"D   						s*   BB- -
C,7CC,C'!C,'C,EpollSelectorc                   |t          d          	 t          |           }|dk     rd}t          j                    5 }t          |         }|                    ||           	 |                    |          x}s*t          |           |                     t                     B|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        }
|
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+   r   r%   Tr	   )r.   r/   r1   epoll_epoll_evmasksr0   pollr    r2   r3   EPOLLINrO   EPOLLOUTrQ   modifyr4   r5   )r&   r   r(   r6   rf   evmaskfileevsrF   r9   r   r:   s              r   
wait_epollrn   -  s     @AAAIIa<<H\^^ 	-u#A&FNN66***-#(::h#7#77 $V,,,HHZ(((QZ]& $#E' %W$EHHUOO'*VV,,,-	- 	- 	- 	- 	- 	- 	- 	- 	- 	-$    						s;   *D( CDDD( "D#D( (
E2E :E EPollSelectorc                   |t          d          	 t          |           }|dk     rd}nt          |dz            }t          j                    }t
          |         }|                    ||           	 |                    |          x}s|                     t                     3|d         d         }d}|t          j	        z  rt          }|t          j        z  r
|t          z  }|s-|t          z  r#t          |           t          j        d          |                     |          }t
          |         }|                    ||           # t$          $ r}	|	j        }
|
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+   r   g     @@Tr	   r   )r.   r/   r   r1   rh   _poll_evmasksr0   r2   r3   POLLINrO   POLLOUTrQ   POLL_BADr    r   r   rk   r4   r5   )r&   r   r(   r6   rh   rl   rm   rF   r9   r   r:   s              r   	wait_pollru   g  s    @AAA#IIa<<HH8f,--H{}}q!ff%%%	(#yy222G $$$ABEFM!  FN" !  ER(] E (((()CDDDA"1%FKK''')	(,    						s   D:E 
E*E%E*%E*boolc                     t           j                            d          x} 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.monkeyr1   Tz+failed to detect gevent monkey-patching: %sNF)sysmodulesgetis_module_patchedr   loggerwarning)mr   s     r   _is_select_patchedr     s     KOOO,,,q N	N""8,, t 	N 	N 	NNNH"MMMMMMMM	N 5s   ; 
A*A%%A*r   rR   PSYCOPG_WAIT_FUNCwait_zHPSYCOPG_WAIT_FUNC should be the name of an available wait function; got SelectSelector)r   r   r   r   )r%   )r&   r'   r   r   r(   r)   r   r   )r&   r<   r(   r)   r   r   )r   rv   )E__doc__
__future__r   r   rx   r1   logging	selectorsasyncior   r   r   r   r    r
   r   abcr   r   r   r   _enumsr   r   _cmoduler   RrJ   WrK   RWWAIT_RWNONEr3   rO   rQ   READY_RW	getLogger__name__r|   platformr    r;   r?   rY   r\   rc   hasattrEPOLLONESHOTri   rj   rg   rn   rr   rs   rq   rt   ru   r   wait_c__annotations__environfname
startswithglobalsImportErrorrR   getattrr#   r$   r   <module>r      s     # " " " " " 				 



       A A A A A A A A A A A A % % % % % %       / / / / / / / / / / / / " " " " " "                  		
'Z

'
'8		8	$	$ <7I I I I I   ! ! ! ! !H    D: : : : :z: : : : :@% % % % %P 79o&& #fn4#fo5$v~5GNN N, , , , ,^ 79n%% /M
 /0HHM+ + + + +\   (  _F  "*$$J*+EG$$ 
WWYY(>(>k 
 
 	
 799UDDD
  #,'))2D2D2F2F)DDD'')5Et"L"LLLDDDWY''  DDD DDDr$   