o
    e,                     @   sB  d Z ddlmZ ddlmZ ddlmZ ddlmZ zddlZW n e	y/   ddl
ZdZY nw ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dgZdgZee Zeee eddZ eeB Z!ej"Z"ej#Z#e"e#B Z$ej%Z%e&edej'Z(G dd de(Z)e)Z*dd Z+dS )an  
This module provides :class:`GeventSelector`, a high-level IO
multiplexing mechanism. This is aliased to :class:`DefaultSelector`.

This module provides the same API as the selectors defined in :mod:`selectors`.

On Python 2, this module is only available if the `selectors2
<https://pypi.org/project/selectors2/>`_ backport is installed.

.. versionadded:: 20.6.0
    )absolute_import)division)print_function)defaultdictN
selectors2)_get_hub_noargs)sleep)	iteritems)
itervalues)copy_globals)Lazy)Event)_EV_READ)	_EV_WRITEDefaultSelectorGeventSelector)__all__)Znames_to_ignoreZdunder_names_to_keep_BaseSelectorImplc                       sZ   e Zd ZdZd fdd	Zdd Zedd Zdd	d
Zdd Z	dddZ
dd Z  ZS )r   a  
    A selector implementation using gevent primitives.

    This is a type of :class:`selectors.BaseSelector`, so the documentation
    for that class applies here.

    .. caution::
       As the base class indicates, it is critically important to
       unregister file objects before closing them. (Or close the selector
       they are registered with before closing them.) Failure to do so
       may crash the process or have other unintended results.
    Nc                    s>   |d ur|| _ i | _i | _tt| _t | _tt	| 
  d S N)hub_active_watchers_inactive_watchersr   int_accumulated_eventsr   _readysuperr   __init__)selfr   	__class__ @D:\Projects\ConvertPro\env\Lib\site-packages\gevent/selectors.pyr   n   s   
zGeventSelector.__init__c                 C   sJ   |dkr| j | }|t@ r|tO }|t@ r|tO }|| j |< | j  d S )Nr   )r   r   
EVENT_READr   EVENT_WRITEr   set)r   eventsfdZcur_event_for_fdr    r    r!   Z
__callbacky   s   

zGeventSelector.__callbackc                 C   s   t  S r   )get_hub)r   r    r    r!   r      s   zGeventSelector.hubc           
      C   sf   t | |||}|tkrt}n	|tkrt}nt}| jj}|j	}|j
}||j| | j|j< }	||	_|S r   )r   register_ALL_EVENTS	_POLL_ALLr"   r   r   r   loopioMAXPRIr&   r   priority)
r   fileobjr%   datakeyflagsr+   r,   r-   watcherr    r    r!   r(      s   zGeventSelector.registerc                 C   sZ   t | |}|j| jv r| j|j}n| j|j}|  |  | j|jd  |S r   )	r   
unregisterr&   r   popr   stopcloser   )r   r/   r1   r3   r    r    r!   r4      s   zGeventSelector.unregisterc                 C   s   |dur
|dk r
d}t | jD ]\}}|j| j|dd q| j| j | j  | j r1t	  | j
| | j  g }t | jD ])\}}| |}| j|}|  |rh||||j@ f || j|< qC|  qC| j  |S )z
        Poll for I/O.

        Note that, like the built-in selectors, this will block
        indefinitely if no timeout is given and no files have been
        registered.
        Nr   T)Zpass_events)r	   r   start_GeventSelector__callbackr   updateclearr   is_setr   waitr   _key_from_fdr5   r6   appendr%   r7   )r   timeoutr&   r3   resulteventr1   r    r    r!   select   s*   





zGeventSelector.selectc                 C   s`   | j | jfD ]}|d u rqt|D ]
}|  |  qqd  | _ | _d | _d | _t|  d S r   )r   r   r
   r6   r7   r   r   r   )r   dr3   r    r    r!   r7      s   
zGeventSelector.closer   )__name__
__module____qualname____doc__r   r9   r   r   r(   r4   rC   r7   __classcell__r    r    r   r!   r   E   s    (


>c           	         s   | j d }| j}|   dd l}d|jvr|jt |jd< | dd}|j|us*J t}|j	}t
|drO|j|j|fv rOddlm   fdd}||j	_d	|_|r\| |d
dd t|_t
|drst
|jdruddlm} ||j_d S d S d S )N
aggressiver   	selectorsrC   _selectrC   c                    s    |i |S r   r    )r   argskwargsrM   r    r!   rL     s   z(_gevent_do_monkey_patch.<locals>._selectTZEpollSelectorZKqueueSelectorZDevpollSelectorPollSelector_selector_cls)poll)Zpatch_kwargsZtarget_moduleZdefault_patch_itemssysmodulesrE   Zget_originalrC   __selectors__SelectSelectorhasattrrL   gevent.selectZ_gevent_monkeyZremove_itemr   rP   rR   rQ   )	Zpatch_requestrJ   Z
target_modrS   Zorig_select_selectrK   rV   rL   rR   r    rM   r!   _gevent_do_monkey_patch   s:   

rY   ),rH   
__future__r   r   r   collectionsr   rK   rU   ImportErrorr   Z
__target__Z
gevent.hubr   r'   Zgeventr   Zgevent._compatr	   r
   Zgevent._utilr   r   Zgevent.eventr   rX   r   r   Z__implements__Z	__extra__r   globalsZ__imports__r*   r"   r#   r)   SelectorKeygetattrBaseSelectorr   r   r   rY   r    r    r    r!   <module>   sX    2