
    )Jfj                     `   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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  ej        e          ZdZi Ze
j        j        rd ee<   d eej        <   d ee<   d	 ee<    ee                                          Zd
 Z  G d de          Z! G d de"          Z# G d de"          Z$ G d de"          Z% G d de          Z& G d de
j        j'                  Z( G d de(          Z) G d de(          Z* G d de(          Z+ G d de+          Z,dS )zeA connection adapter that tries to use the best polling method for the
platform pika is running on.

    N)nbio_interface)BaseConnection)SelectorIOServicesAdapterAbstractSelectorIOLoopc                     dS )NT es    b/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/pika/adapters/select_connection.py<lambda>r   !   s         c                 8    | j         d         t          j        k    S Nr   )argserrnoEINTRr	   s    r   r   r   #   s    ek1I r   c                 ,    | j         t           j        k    S Nr   r   r	   s    r   r   r   $       AGu{,B r   c                 ,    | j         t           j        k    S r   r   r	   s    r   r   r   %   r   r   c                 `    t                               | j        d          }| ||           S dS )z{Check if caught exception represents EINTR error.
    :param exc: exception; must be one of classes in _SELECT_ERRORS

    NF)_SELECT_ERROR_CHECKERSget	__class__)exccheckers     r   _is_resumabler   1   s4    
 %((==Gws||ur   c                   R     e Zd ZdZ	 	 	 	 	 	 d fd	Ze	 	 dd            Zd Z xZS )	SelectConnectionzxAn asynchronous connection adapter that attempts to use the fastest
    event loop adapter for the given platform.

    NTc                     t          |t          j                  r|}nt          |pt	                                }t          t          |                               ||||||           dS )a  Create a new instance of the Connection object.

        :param pika.connection.Parameters parameters: Connection parameters
        :param callable on_open_callback: Method to call on connection open
        :param None | method on_open_error_callback: Called if the connection
            can't be established or connection establishment is interrupted by
            `Connection.close()`: on_open_error_callback(Connection, exception).
        :param None | method on_close_callback: Called when a previously fully
            open connection is closed:
            `on_close_callback(Connection, exception)`, where `exception` is
            either an instance of `exceptions.ConnectionClosed` if closed by
            user or broker or exception of another type that describes the cause
            of connection failure.
        :param None | IOLoop | nbio_interface.AbstractIOServices custom_ioloop:
            Provide a custom I/O Loop object.
        :param bool internal_connection_workflow: True for autonomous connection
            establishment which is default; False for externally-managed
            connection workflow via the `create_connection()` factory.
        :raises: RuntimeError

        )internal_connection_workflowN)
isinstancer   AbstractIOServicesr   IOLoopsuperr    __init__)	self
parameterson_open_callbackon_open_error_callbackon_close_callbackcustom_ioloopr"   nbior   s	           r   r'   zSelectConnection.__init__C   s    : m^%FGG 	H DD,]-FfhhGGD%%..")E 	/ 	G 	G 	G 	G 	Gr   c                      t          |pt                                 fd}                     ||||          S )z`Implement
        :py:classmethod::`pika.adapters.BaseConnection.create_connection()`.

        c                 B    | t          d           | d          S )zConnection factory.NzIExpected pika.connection.Parameters instance, but got None in params arg.F)r)   r-   r"   )
ValueError)paramsclsr.   s    r   connection_factoryz>SelectConnection.create_connection.<locals>.connection_factoryy   sE    ~  "I J J J3!"-24 4 4 4r   )connection_configsr4   r.   workflowon_done)r   r%   _start_connection_workflow)r3   r5   r7   r-   r6   r4   r.   s   `     @r   create_connectionz"SelectConnection.create_connectionm   sh     ))B&((CC	4 	4 	4 	4 	4 	4 --11 .   	r   c                 4    | j                                         S )ze
        :returns: Current size of output data buffered by the transport
        :rtype: int
        )
_transportget_write_buffer_sizer(   s    r   _get_write_buffer_sizez'SelectConnection._get_write_buffer_size   s    
 44666r   )NNNNNT)NN)	__name__
__module____qualname____doc__r'   classmethodr9   r>   __classcell__r   s   @r   r    r    =   s          !#'")-(G (G (G (G (G (GT  )-#'	   [87 7 7 7 7 7 7r   r    c                   @    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
S )_TimeoutzRepresents a timeout)deadlinecallbackc                     |dk     rt          d|          t          |          st          d|          || _        || _        dS )z
        :param float deadline: timer expiration as non-negative epoch number
        :param callable callback: callback to call when timeout expires
        :raises ValueError, TypeError:
        r   z4deadline must be non-negative epoch number, but got %callback must be a callable, but got N)r1   callable	TypeErrorrH   rI   )r(   rH   rI   s      r   r'   z_Timeout.__init__   sq     a<<*   !! 	I)=EXGI I I ! r   c                 Z    t          |t                    r| j        |j        k    S t          S z#NOTE: not supporting sort stabilityr#   rG   rH   NotImplementedr(   others     r   __eq__z_Timeout.__eq__   )    eX&& 	3=EN22r   c                 R    |                      |          }|t          ur| S t          S rO   )rT   rQ   )r(   rS   results      r   __ne__z_Timeout.__ne__   s,    U##'':r   c                 Z    t          |t                    r| j        |j        k     S t          S rO   rP   rR   s     r   __lt__z_Timeout.__lt__   )    eX&& 	2=5>11r   c                 Z    t          |t                    r| j        |j        k    S t          S rO   rP   rR   s     r   __gt__z_Timeout.__gt__   r[   r   c                 Z    t          |t                    r| j        |j        k    S t          S rO   rP   rR   s     r   __le__z_Timeout.__le__   rU   r   c                 Z    t          |t                    r| j        |j        k    S t          S rO   rP   rR   s     r   __ge__z_Timeout.__ge__   rU   r   N)r?   r@   rA   rB   	__slots__r'   rT   rX   rZ   r]   r_   ra   r   r   r   rG   rG      s        I
! ! !&              r   rG   c                   :    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	S )
_Timerz!Manage timeouts for use in ioloopi   c                 "    g | _         d| _        d S r   )_timeout_heap_num_cancellationsr=   s    r   r'   z_Timer.__init__   s     #$r   c                 H    | j         | j         D ]	}d|_        
d| _         dS dS )zTRelease resources. Don't use the `_Timer` instance after closing
        it
        N)rf   rI   r(   timeouts     r   closez_Timer.close   s@    
 )- ( (#'  !%D *)r   c                 J   | j         t          d          |dk     rt          d|          t          j                                        }t          ||z   |          }t          j        | j         |           t          	                    d||j
        |j        ||           |S )a  Schedule a one-shot timeout given delay seconds.

        NOTE: you may cancel the timer before dispatch of the callback. Timer
            Manager cancels the timer upon dispatch of the callback.

        :param float delay: Non-negative number of seconds from now until
            expiration
        :param callable callback: The callback method, having the signature
            `callback()`

        :rtype: _Timeout
        :raises ValueError, TypeError

        NzTimeout closed before callr   z0call_later: delay must be non-negative, but got zOcall_later: added timeout %r with deadline=%r and callback=%r; now=%s; delay=%s)rf   r1   pikacompattime_nowrG   heapqheappushLOGGERdebugrH   rI   )r(   delayrI   nowrj   s        r   
call_laterz_Timer.call_later   s     %9:::199*HMOQ Q Q k""$$3;11t)7333,-4g6Fc5	* 	* 	*
 r   c                     |j         t                              d|           dS t                              d||j        |j                    d|_         | xj        dz  c_        dS )zRCancel the timeout

        :param _Timeout timeout: The timer to cancel

        Nz8remove_timeout: timeout was already removed or called %rzDremove_timeout: removing timeout %r with deadline=%r and callback=%r   )rI   rr   rs   rH   rg   ri   s     r   remove_timeoutz_Timer.remove_timeout  s     #LLJ     LL"#*G,<g>NP P P  $G##q(####r   c                     | j         rBt          j                                        }t	          d| j         d         j        |z
            }nd}|S )zGet the interval to the next timeout expiration

        :returns: non-negative number of seconds until next timer expiration;
                  None if there are no timers
        :rtype: float

        r   N)rf   rm   rn   ro   maxrH   )r(   ru   intervals      r   get_remaining_intervalz_Timer.get_remaining_interval"  sM      	+&&((C1d03<sBCCHHHr   c                    | j         r@t          j                                        }g }| j         ry| j         d         j        |k    rct          j        | j                   }|j        |                    |           n| xj	        dz  c_	        | j         r| j         d         j        |k    c|D ]5}|j        | xj	        dz  c_	        |                                 d|_        6| j	        | j
        k    rZ| j	        t          | j                   dz	  k    r<d| _	        d | j         D             | _         t          j        | j                    dS dS dS dS )z\Process pending timeouts, invoking callbacks for those whose time has
        come

        r   Nrx   c                      g | ]}|j         	|S r   )rI   ).0ts     r   
<listcomp>z+_Timer.process_timeouts.<locals>.<listcomp>S  s&     & & &QZ5KA5K5K5Kr   )rf   rm   rn   ro   rH   rp   heappoprI   appendrg   _GC_CANCELLATION_THRESHOLDlenheapify)r(   ru   ready_timeoutsrj   s       r   process_timeoutsz_Timer.process_timeouts2  s   
  	2+&&((C  N$ 1);A)>)G3)N)N-(:;;#/"))'2222++q0++ $ 1);A)>)G3)N)N * ( (#+++q0++  """#'   '4+JJJ+s43E/F/F!/KLL*+'& &#1& & &" d011111?	2 	22 KJLLr   N)r?   r@   rA   rB   r   r'   rk   rv   ry   r}   r   r   r   r   rd   rd      s}        ++ "&$ $ $& & &! ! !F) ) )(   $2 $2 $2 $2 $2r   rd   c                   `    e Zd ZdZ eedd          Z eedd          Z eedd          ZdS )	
PollEventszEvent flags for I/OPOLLINrx   POLLOUT   POLLERR   N)	r?   r@   rA   rB   getattrselectREADWRITEERRORr   r   r   r   r   Y  sO         768T**DGFIt,,EGFIt,,EEEr   r   c                       e Zd ZdZej        Zej        Zej        Zd Zd Z	e
d             Zd Zd Zd ZeZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZdS )r%   zI/O loop implementation that picks a suitable poller (`select`,
     `poll`, `epoll`, `kqueue`) to use based on platform.

     Implements the
     `pika.adapters.utils.selector_ioloop_adapter.AbstractSelectorIOLoop`
     interface.

    c                     t                      | _        t          j                    | _        |                     | j        | j                  | _        d S r   )	rd   _timercollectionsdeque
_callbacks_get_poller_get_remaining_intervalr   _pollerr=   s    r   r'   zIOLoop.__init__p  sE    hh &+--''(D(,(=? ?r   c                     | j         ;| j                                         | j                                         g | _         dS dS )a  Release IOLoop's resources.

        `IOLoop.close` is intended to be called by the application or test code
        only after `IOLoop.start()` returns. After calling `close()`, no other
        interaction with the closed instance of `IOLoop` should be performed.

        N)r   r   rk   r   r=   s    r   rk   zIOLoop.closey  sI     ?&L   K !DOOO '&r   c                    d}t          | |          }t          t          d          r8t          rt          dk    r&t                              d           t          di |}|sMt          t          d          r8t          rt          dk    r&t                              d           t          di |}|snt          t          d          rYt          t          j                    d          r8t          rt          dk    r&t                              d	           t          di |}|s&t                              d
           t          di |}|S )a  Determine the best poller to use for this environment and instantiate
        it.

        :param get_wait_seconds: Function for getting the maximum number of
                                 seconds to wait for IO for use by the poller
        :param process_timeouts: Function for processing timeouts for use by the
                                 poller

        :returns: The instantiated poller instance supporting `_PollerBase` API
        :rtype: object
        N)get_wait_secondsr   epollzUsing EPollPollerkqueuezUsing KQueuePollerpollmodifyzUsing PollPollerzUsing SelectPollerr   )dicthasattrr   SELECT_TYPErr   rs   EPollPollerKQueuePollerr   
PollPollerSelectPoller)r   r   pollerkwargss       r   r   zIOLoop._get_poller  s\    --/ / / 67## 	/ /+"8"80111$..v.. 	0'&(33 	0 0+"9"91222%//// 	.76622 	.x00	. .+"7"7/000#--f-- 	,LL-...!++F++Fr   c                 8    | j                             ||          S )a  Add the callback to the IOLoop timer to be called after delay seconds
        from the time of call on best-effort basis. Returns a handle to the
        timeout.

        :param float delay: The number of seconds to wait to call callback
        :param callable callback: The callback method
        :returns: handle to the created timeout that may be passed to
            `remove_timeout()`
        :rtype: object

        )r   rv   )r(   rt   rI   s      r   rv   zIOLoop.call_later  s     {%%eX666r   c                 :    | j                             |           dS )zVRemove a timeout

        :param timeout_handle: Handle of timeout to remove

        N)r   ry   )r(   timeout_handles     r   ry   zIOLoop.remove_timeout  s      	"">22222r   c                     t          |          st          d|          | j                            |           | j                                         t                              d|           dS )a  Requests a call to the given function as soon as possible in the
        context of this IOLoop's thread.

        NOTE: This is the only thread-safe method in IOLoop. All other
        manipulations of IOLoop must be performed from the IOLoop's thread.

        For example, a thread may request a call to the `stop` method of an
        ioloop that is running in a different thread via
        `ioloop.add_callback_threadsafe(ioloop.stop)`

        :param callable callback: The callback method

        rK   z*add_callback_threadsafe: added callback=%rN)rL   rM   r   r   r   wake_threadsaferr   rs   )r(   rI   s     r   add_callback_threadsafezIOLoop.add_callback_threadsafe  s}     !! 	I)=EXGI I I 	x((( 	$$&&&A8LLLLLr   c                    t           j                            t          | j                            D ]@}| j                                        }t                              d|            |             A| j        	                                 dS )z|[Extension] Process pending callbacks and timeouts, invoking those
        whose time has come. Internal use only.

        z&process_timeouts: invoking callback=%rN)
rm   rn   xranger   r   popleftrr   rs   r   r   )r(   _rI   s      r   r   zIOLoop.process_timeouts  s|     ##C$8$899 	 	A..00HLLA8LLLHJJJJ$$&&&&&r   c                 F    | j         rdS | j                                        S )a  Get the remaining interval to the next callback or timeout
        expiration.

        :returns: non-negative number of seconds until next callback or timer
                  expiration; None if there are no callbacks and timers
        :rtype: float

        r   )r   r   r}   r=   s    r   r   zIOLoop._get_remaining_interval  s'     ? 	1{11333r   c                 >    | j                             |||           dS )a  Start watching the given file descriptor for events

        :param int fd: The file descriptor
        :param callable handler: When requested event(s) occur,
            `handler(fd, events)` will be called.
        :param int events: The event mask using READ, WRITE, ERROR.

        N)r   add_handler)r(   fdhandlereventss       r   r   zIOLoop.add_handler  s$     	  Wf55555r   c                 <    | j                             ||           dS )zChanges the events we watch for

        :param int fd: The file descriptor
        :param int events: The event mask using READ, WRITE, ERROR

        N)r   update_handler)r(   r   r   s      r   r   zIOLoop.update_handler  s"     	##B/////r   c                 :    | j                             |           dS )zhStop watching the given file descriptor for events

        :param int fd: The file descriptor

        N)r   remove_handler)r(   r   s     r   r   zIOLoop.remove_handler  s      	##B'''''r   c                 8    | j                                          dS )zl[API] Start the main poller loop. It will loop until requested to
        exit. See `IOLoop.stop`.

        N)r   startr=   s    r   r   zIOLoop.start  s    
 	r   c                 8    | j                                          dS )a6  [API] Request exit from the ioloop. The loop is NOT guaranteed to
        stop before this method returns.

        To invoke `stop()` safely from a thread other than this IOLoop's thread,
        call it via `add_callback_threadsafe`; e.g.,

            `ioloop.add_callback_threadsafe(ioloop.stop)`

        N)r   stopr=   s    r   r   zIOLoop.stop$  s     	r   c                 8    | j                                          dS )z)[Extension] Activate the poller

        N)r   activate_pollerr=   s    r   r   zIOLoop.activate_poller0  s     	$$&&&&&r   c                 8    | j                                          dS )z+[Extension] Deactivate the poller

        N)r   deactivate_pollerr=   s    r   r   zIOLoop.deactivate_poller6  s     	&&(((((r   c                 8    | j                                          dS )a	  [Extension] Wait for events of interest on registered file
        descriptors until an event of interest occurs or next timer deadline or
        `_PollerBase._MAX_POLL_TIMEOUT`, whichever is sooner, and dispatch the
        corresponding event handlers.

        N)r   r   r=   s    r   r   zIOLoop.poll<  s     	r   N)r?   r@   rA   rB   r   r   r   r   r'   rk   staticmethodr   rv   ry   r   add_callbackr   r   r   r   r   r   r   r   r   r   r   r   r   r%   r%   b  s>         ?DEE? ? ?! ! !  ' ' \'R7 7 73 3 3M M M6 +L' ' '4 4 4	6 	6 	60 0 0( ( (  
 
 
' ' ') ) )    r   r%   c                   D   e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zd Zed             Zd ZdS )_PollerBasez2Base class for select-based IOLoop implementations   rx   c                    || _         || _        t          j                    | _        t                      | _        t          j        t                      t          j
        t                      t          j        t                      i| _        i | _        d| _        d| _        |                                 \  | _        | _        |                     | j                                        | j        t          j                   dS )a  
        :param get_wait_seconds: Function for getting the maximum number of
                                 seconds to wait for IO for use by the poller
        :param process_timeouts: Function for processing timeouts for use by the
                                 poller

        FN)_get_wait_seconds_process_timeouts	threadingLock_waking_mutexr   _fd_handlersr   r   setr   r   
_fd_events_processing_fd_event_map_running	_stopping_get_interrupt_pair_r_interrupt_w_interruptr   fileno_read_interrupt)r(   r   r   s      r   r'   z_PollerBase.__init__Q  s     "2!1 '^-- !FF OSUUceecee
 )+%  04/G/G/I/I,4,*1133T5I#	* 	* 	* 	* 	*r   c                    | j         r
J d            | j        5  | j        l|                     | j                                                   | j                                         d| _        | j                                         d| _        ddd           n# 1 swxY w Y   |                                  d| _        d| _	        d| _
        dS )zRelease poller's resources.

        `close()` is intended to be called after the poller's `start()` method
        returns. After calling `close()`, no other interaction with the closed
        poller instance should be performed.

        z+Cannot call close() before start() unwinds.N)r   r   r   r   r   r   rk   r   r   r   r   r=   s    r   rk   z_PollerBase.closev  s    =OO"OOOO 	) 	) ,##D$5$<$<$>$>???!'')))$(!!'')))$(!	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	    (,%%%s   A4BB Bc                 h   | j         5  | j        	 ddd           dS 	 | j                            d           n`# t          j        j        $ r }|j        t          j        k    r Y d}~n1d}~wt          $ r!}t          
                    d|            d}~ww xY wddd           dS # 1 swxY w Y   dS )zlWake up the poller as soon as possible. As the name indicates, this
        method is thread-safe.

        N   Xz&Failed to send interrupt to poller: %s)r   r   sendrm   rn   SOCKET_ERRORr   EWOULDBLOCK	Exceptionrr   warning)r(   errs     r   r   z_PollerBase.wake_threadsafe  s>   
  	 	 (	 	 	 	 	 	 	 	 !&&t,,,,;+   9 111 21111    GMMM		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sC   	B':B'BA)$B')B6BBB''B+.B+c                 |    |                                  }|| j        }nt          || j                  }|| j        z  S )zGet the interval to the next timeout event, or a default interval

        :returns: maximum number of self.POLL_TIMEOUT_MULT-scaled time units
                  to wait for IO events
        :rtype: int

        )r   _MAX_POLL_TIMEOUTminPOLL_TIMEOUT_MULT)r(   rt   s     r   _get_max_waitz_PollerBase._get_max_wait  sD     &&((=*EEt566Et---r   c                 r    || j         |<   |                     ||           |                     ||           dS )zAdd a new fileno to the set to be monitored

        :param int fileno: The file descriptor
        :param callable handler: What is called when an event happens
        :param int events: The event mask using READ, WRITE, ERROR

        N)r   _set_handler_events_register_fd)r(   r   r   r   s       r   r   z_PollerBase.add_handler  sE     %,&!  000 	&&)))))r   c                 j    |                      ||          \  }}|                     ||||           dS )zSet the events to the current events

        :param int fileno: The file descriptor
        :param int events: The event mask using READ, WRITE, ERROR

        r   events_to_clearevents_to_setN)r   _modify_fd_events)r(   r   r   events_cleared
events_sets        r   r   z_PollerBase.update_handler  sS     &*%=%=ff%M%M"
 	*$	 	 	& 	& 	& 	& 	&r   c                     	 | j         |= n# t          $ r Y nw xY w|                     |d          \  }}| j        |= |                     ||           dS )z_Remove a file descriptor from the set

        :param int fileno: The file descriptor

        r   )r   N)r   KeyErrorr   r   _unregister_fd)r(   r   r   r   s       r   r   z_PollerBase.remove_handler  s}    	-f55 	 	 	D	 !44VQ??f% 	FNCCCCCs    
c                 :   d}d}t           j        t           j        t           j        fD ]p}||z  r5|| j        |         vr%| j        |                             |           ||z  }<|| j        |         v r%| j        |                             |           ||z  }q||fS )a  Set the handler's events to the given events; internal to
        `_PollerBase`.

        :param int fileno: The file descriptor
        :param int events: The event mask (READ, WRITE, ERROR)

        :returns: a 2-tuple (events_cleared, events_set)
        :rtype: tuple
        r   )r   r   r   r   r   adddiscard)r(   r   r   r   r   evts         r   r   z_PollerBase._set_handler_events  s     
OZ%5z7GH 	* 	*C| *!555OC(,,V444#%JT_S111OC(00888"c)Nz))r   c                 (   |                                   t          j        t                    }| j                                        D ]\  }}|D ]}||xx         |z  cc<   |                                D ]\  }}|                     ||           dS )zActivate the poller

        N)_init_pollerr   defaultdictintr   itemsr   )r(   fd_to_eventseventfile_descriptorsr   r   s         r   r   z_PollerBase.activate_poller   s    
 	".s33'+'<'<'>'> 	. 	.#E#* . .V$$$-$$$$. +0022 	. 	.NFFff----	. 	.r   c                 .    |                                   dS )zDeactivate the poller

        N)_uninit_pollerr=   s    r   r   z_PollerBase.deactivate_poller  s     	r   c                 V   | j         rt          d          t                              d           d| _         |                                  	 | j        s/|                                  |                                  | j        /	 t                              d           |                                  d| _        d| _         dS # d| _        d| _         w xY w# 	 t                              d           |                                  d| _        d| _         n# d| _        d| _         w xY ww xY w)zStart the main poller loop. It will loop until requested to exit.
        This method is not reentrant and will raise an error if called
        recursively (pika/pika#1095)

        :raises: RuntimeError

        z.IOLoop is not reentrant and is already runningzEntering IOLoopTzDeactivating pollerFN)	r   RuntimeErrorrr   rs   r   r   r   r   r   r=   s    r   r   z_PollerBase.start  sA    = 	QOPPP&'''	&n )		&&((( n )&2333&&(((!& % "' %%%%%&2333&&(((!& % "' %%%%%%%%%s0   6C .C CD(.DD(D$$D(c                 p    t                               d           d| _        |                                  dS )znRequest exit from the ioloop. The loop is NOT guaranteed to stop
        before this method returns.

        zStopping IOLoopTN)rr   rs   r   r   r=   s    r   r   z_PollerBase.stop0  s6    
 	&'''r   c                     t           )z7Wait for events on interested filedescriptors.
        NotImplementedErrorr=   s    r   r   z_PollerBase.poll9  s
     "!r   c                     t           )9Notify the implementation to allocate the poller resourcer  r=   s    r   r  z_PollerBase._init_poller?  
     "!r   c                     t           )8Notify the implementation to release the poller resourcer  r=   s    r   r
  z_PollerBase._uninit_pollerD  r  r   c                     t           )a<  The base class invokes this method to notify the implementation to
        register the file descriptor with the polling object. The request must
        be ignored if the poller is not activated.

        :param int fileno: The file descriptor
        :param int events: The event mask (READ, WRITE, ERROR)
        r  r(   r   r   s      r   r   z_PollerBase._register_fdI  
     "!r   c                     t           )  The base class invoikes this method to notify the implementation to
        modify an already registered file descriptor. The request must be
        ignored if the poller is not activated.

        :param int fileno: The file descriptor
        :param int events: absolute events (READ, WRITE, ERROR)
        :param int events_to_clear: The events to clear (READ, WRITE, ERROR)
        :param int events_to_set: The events to set (READ, WRITE, ERROR)
        r  r(   r   r   r   r   s        r   r   z_PollerBase._modify_fd_eventsT  s
     "!r   c                     t           )X  The base class invokes this method to notify the implementation to
        unregister the file descriptor being tracked by the polling object. The
        request must be ignored if the poller is not activated.

        :param int fileno: The file descriptor
        :param int events_to_clear: The events to clear (READ, WRITE, ERROR)
        r  r(   r   r   s      r   r   z_PollerBase._unregister_fda  r  r   c                 P   | j                                          || _         t          j                            |          D ]e}||vr||         }t
          j        t
          j        t
          j        fD ]}|| j	        |         vr|| z  }|r| j
        |         } |||           fdS )a   Helper to dispatch callbacks for file descriptors that received
        events.

        Before doing so we re-calculate the event mask based on what is
        currently set in case it has been changed under our feet by a
        previous callback. We also take a store a refernce to the
        fd_event_map so that we can detect removal of an
        fileno during processing of another callback and not generate
        spurious callbacks on it.

        :param dict fd_event_map: Map of fds to events received on them.
        N)r   clearrm   rn   dictkeysr   r   r   r   r   r   )r(   fd_event_mapr   r   r   r   s         r   _dispatch_fd_eventsz_PollerBase._dispatch_fd_eventsl  s     	%++---(4%k**<88 	( 	(F\))!&)F")9:;KL # #!555sdNF (+F3'''	( 	(r   c                  >    t           j                                        S )a&   Use a socketpair to be able to interrupt the ioloop if called
        from another thread. Socketpair() is not supported on some OS (Win)
        so use a pair of simple TCP sockets instead. The sockets will be
        closed and garbage collected by python when the ioloop itself is.
        )rm   rn   _nonblocking_socketpairr   r   r   r   z_PollerBase._get_interrupt_pair  s     {22444r   c                     	 | j                             d           dS # t          j        j        $ r!}|j        t
          j        k    r Y d}~dS d}~ww xY w)a   Read the interrupt byte(s). We ignore the event mask as we can ony
        get here if there's data to be read on our fd.

        :param int _interrupt_fd: (unused) The file descriptor to read from
        :param int _events: (unused) The events generated for this fd
        i   N)r   recvrm   rn   r   r   EAGAIN)r(   _interrupt_fd_eventsr   s       r   r   z_PollerBase._read_interrupt  sn    	""3'''''{' 	 	 	yEL(( )(((((	s    AAAN)r?   r@   rA   rB   r   r   r'   rk   r   r   r   r   r   r   r   r   r   r   abcabstractmethodr   r  r
  r   r   r   r#  r   r   r   r   r   r   r   r   F  s       <<
  #* #* #*J- - -6  ,. . . * * *& & &"D D D"* * *2. . .  & & &8   	" " "
 	" " " 	" " " 	" " " 	
" 
" 
" 	" " "( ( (B 5 5 \5    r   r   c                   :    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	S )
r   zDefault behavior is to use Select since it's the widest supported and has
    all of the methods we need for child classes as well. One should only need
    to override the update_handler and start methods for additional types.

    rx   c                    	 	 | j         t          j                 s.| j         t          j                 s| j         t          j                 rmt          j        | j         t          j                 | j         t          j                 | j         t          j                 |                                           \  }}}n,t          j        |                                            g g g }}}n'# t          $ r}t          |          rY d}~ d}~ww xY wt          j        t                    }t          |||ft          j        t          j        t          j        f          D ]\  }}|D ]}||xx         |z  cc<   |                     |           dS Wait for events of interest on registered file descriptors until an
        event of interest occurs or next timer deadline or _MAX_POLL_TIMEOUT,
        whichever is sooner, and dispatch the corresponding event handlers.

        TN)r   r   r   r   r   r   r   timesleep_SELECT_ERRORSr   r   r  r  zipr#  )r(   readwriteerrorr"  fd_setr   r   s           r   r   zSelectPoller.poll  s   	OJO4 4
(894
(894 *0
8
(89
(894;M;M;O;O*Q *Q&D% Jt1133444)+R%D!    '' HHHH	 #.s33ue$*"2J4DEG G 	, 	,KFC ! , ,V$$$+$$$$, 	  .....s   CC" "
D,D DDc                     dS r  Nr   r=   s    r   r  zSelectPoller._init_poller        r   c                     dS r  Nr   r=   s    r   r
  zSelectPoller._uninit_poller  r;  r   c                     dS @  The base class invokes this method to notify the implementation to
        register the file descriptor with the polling object. The request must
        be ignored if the poller is not activated.

        :param int fileno: The file descriptor
        :param int events: The event mask using READ, WRITE, ERROR
        Nr   r  s      r   r   zSelectPoller._register_fd  r;  r   c                     dS r  Nr   r  s        r   r   zSelectPoller._modify_fd_events  r;  r   c                     dS r  Nr   r  s      r   r   zSelectPoller._unregister_fd  r;  r   N)r?   r@   rA   rB   r   r   r  r
  r   r   r   r   r   r   r   r     s          $/ $/ $/LH H HG G G  	 	 	    r   r   c                   \     e Zd ZdZ fdZed             Zd Zd Zd Z	d Z
d Zd	 Z xZS )
r   zAKQueuePoller works on BSD based systems and is faster than selectc                 h    d| _         t          t          |                               ||           dS )z/Create an instance of the KQueuePoller
        N)_kqueuer&   r   r'   r(   r   r   r   s      r   r'   zKQueuePoller.__init__  s4     lD!!**+;=MNNNNNr   c                 P   d}| j         t          j        k    rt          j        }n| j         t          j        k    r0t          j        }| j        t          j        z  r|t          j	        z  }n<| j        t          j
        z  rt          j	        }nt                              d|            |S )zreturn the event type associated with a kevent object

        :param kevent kevent: a kevent object as returned by kqueue.control()

        r   zUnexpected kevent: %s)filterr   KQ_FILTER_READr   r   KQ_FILTER_WRITEr   flags	KQ_EV_EOFr   KQ_EV_ERRORrr   critical)keventmasks     r   
_map_eventzKQueuePoller._map_event  s     =F111?DD]f444#D|f.. ) 
((\F.. 	=#DDOO3V<<<r   c                 l   	 	 | j                             dd|                                           }n'# t          $ r}t	          |          rY d}~O d}~ww xY wt          j        t                    }|D ]*}||j        xx         | 	                    |          z  cc<   +| 
                    |           dS )r0  TN  )rG  controlr   r3  r   r   r  r  identrS  r#  )r(   keventsr7  r"  r  s        r   r   zKQueuePoller.poll   s    	,..tT4;M;M;O;OPP!    '' HHHH	 #.s33 	@ 	@E%%%)?)??%%%%  .....s   .2 
AAAAc                 H    | j         J t          j                    | _         dS r:  )rG  r   r   r=   s    r   r  zKQueuePoller._init_poller6  s!    |###}r   c                 X    | j         "| j                                          d| _         dS dS r=  )rG  rk   r=   s    r   r
  zKQueuePoller._uninit_poller<  s1    <#L   DLLL $#r   c                 8    |                      ||d|           dS )r@  r   r   Nr   r  s      r   r   zKQueuePoller._register_fdB  s9     	61F 	 	L 	L 	L 	L 	Lr   c                    | j         dS t                      }|t          j        z  r>|                    t          j        |t
          j        t
          j                             |t          j        z  r>|                    t          j        |t
          j        t
          j	                             |t          j
        z  r>|                    t          j        |t
          j        t
          j                             |t          j
        z  r>|                    t          j        |t
          j        t
          j	                             | j                             |d           dS )r  N)rJ  rM  r   )rG  listr   r   r   r   rQ  rK  KQ_EV_DELETE	KQ_EV_ADDr   rL  rV  )r(   r   r   r   r   rX  s         r   r   zKQueuePoller._modify_fd_eventsM  sb    <F&&Z_, 	0NN!0 -/ / /0 0 0
 :?* 	-NN!0 *, , ,- - -
 Z-- 	0NN!1 -/ / /0 0 0
 :++ 	-NN!1 *, , ,- - - 	Wa(((((r   c                 8    |                      |d|d           dS )r  r   r   Nr\  r  s      r   r   zKQueuePoller._unregister_fdw  s9     	1oQ 	 	P 	P 	P 	P 	Pr   )r?   r@   rA   rB   r'   r   rS  r   r  r
  r   r   r   rD   rE   s   @r   r   r     s         LKO O O O O   \./ / /,' ' '     	L 	L 	L() () ()T	P 	P 	P 	P 	P 	P 	Pr   r   c                   `     e Zd ZdZdZ fdZed             Zd Zd Z	d Z
d Zd	 Zd
 Z xZS )r   z|Poll works on Linux and can have better performance than EPoll in
    certain scenarios.  Both are faster than select.

    rU  c                 h    d| _         t          t          |                               ||           dS )z0Create an instance of the KQueuePoller

        N)_pollr&   r   r'   rH  s      r   r'   zPollPoller.__init__  s4     
j$(()9;KLLLLLr   c                  (    t          j                    S z'
        :rtype: `select.poll`
        )r   r   r   r   r   _create_pollerzPollPoller._create_poller  s    
 {}}r   c                    	 	 | j                             |                                           }n'# t          $ r}t	          |          rY d}~M d}~ww xY wt          j        t                    }|D ]D\  }}|t          j	        z  r t          j        j        r|t          j        z  }||xx         |z  cc<   E|                     |           dS r/  )rd  r   r   r3  r   r   r  r  r   POLLHUPrm   rn   ON_OSXr   r#  )r(   r   r7  r"  r   r  s         r   r   zPollPoller.poll  s    	););)=)=>>!    '' HHHH	 #.s33# 	* 	*MFE
 & (DK,> ('   E)      .....s   ,0 
AAAAc                 J    | j         J |                                 | _         dS r:  )rd  rg  r=   s    r   r  zPollPoller._init_poller  s'    z!!!((**


r   c                     | j         7t          | j         d          r| j                                          d| _         dS dS )r  Nrk   )rd  r   rk   r=   s    r   r
  zPollPoller._uninit_poller  sE    :!tz7++ #
  """DJJJ	 "!r   c                 N    | j         | j                             ||           dS dS r?  )rd  registerr  s      r   r   zPollPoller._register_fd  s2     :!J///// "!r   c                 N    | j         | j                             ||           dS dS rB  )rd  r   r  s        r   r   zPollPoller._modify_fd_events  s2     :!Jff----- "!r   c                 L    | j         | j                             |           dS dS rD  )rd  
unregisterr  s      r   r   zPollPoller._unregister_fd  s0     :!J!!&))))) "!r   )r?   r@   rA   rB   r   r'   r   rg  r   r  r
  r   r   r   rD   rE   s   @r   r   r     s          M M M M M   \/ / /:+ + +  	0 	0 	0. . .	* 	* 	* 	* 	* 	* 	*r   r   c                   ,    e Zd ZdZdZed             ZdS )r   z{EPoll works on Linux and can have better performance than Poll in
    certain scenarios. Both are faster than select.

    rx   c                  (    t          j                    S rf  )r   r   r   r   r   rg  zEPollPoller._create_poller  s    
 |~~r   N)r?   r@   rA   rB   r   r   rg  r   r   r   r   r     s?            \  r   r   )-rB   r+  r   r   rp   loggingr   r1  r   pika.compatrm   pika.adapters.utilsr   pika.adapters.base_connectionr   +pika.adapters.utils.selector_ioloop_adapterr   r   	getLoggerr?   rr   r   r   rn   PY3InterruptedErrorr7  IOErrorOSErrortuplekeysr3  r   r    objectrG   rd   r   r%   AbstractBaser   r   r   r   r   r   r   r   <module>r     sK    


                  . . . . . . 8 8 8 8 8 87 7 7 7 7 7 7 7 
	8	$	$ 
  ;? > 0>~+,'I'I v| $"B"B w "B"B w  -224455	 	 	R7 R7 R7 R7 R7~ R7 R7 R7j> > > > >v > > >BC2 C2 C2 C2 C2V C2 C2 C2L- - - - - - - -a a a a a# a a aH\ \ \ \ \$+* \ \ \~
T T T T T; T T TpCP CP CP CP CP; CP CP CPLa* a* a* a* a* a* a* a*H    *     r   