o
    Qœ_e  ã                   @   s  d 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
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 ddlmZ ddlmZ ddlZejr[dd	lmZmZmZmZ G d
d„ deƒZddd„Zeej dƒrej  !e¡dedefdd„ƒZ"dS dS )z1Bridges between the Twisted package and Tornado.
é    N)ÚDeferred©Úfailure)ÚFutureÚfuture_set_exc_info)Úutf8)Úgen)ÚResolver)Ú	GeneratorÚAnyÚListÚTuplec                
   @   s>   e Zd ZdZddd„Zej	ddeded	edd
fdd„ƒZ	dS )ÚTwistedResolvera‰  Twisted-based asynchronous resolver.

    This is a non-blocking and non-threaded resolver.  It is
    recommended only when threads cannot be used, since it has
    limitations compared to the standard ``getaddrinfo``-based
    `~tornado.netutil.Resolver` and
    `~tornado.netutil.DefaultExecutorResolver`.  Specifically, it returns at
    most one result, and arguments other than ``host`` and ``family``
    are ignored.  It may fail to resolve when ``family`` is not
    ``socket.AF_UNSPEC``.

    Requires Twisted 12.1 or newer.

    .. versionchanged:: 5.0
       The ``io_loop`` argument (deprecated since version 4.1) has been removed.
    ÚreturnNc                 C   s\   t jj ¡ | _t jj d¡}t jjj	| jd�}t jj
jd| jd�}t jj |||g¡| _d S )Nz
/etc/hosts)Úreactorz/etc/resolv.conf)ÚtwistedÚinternetÚasyncioreactorÚAsyncioSelectorReactorr   ÚnamesÚhostsr	   ÚcacheÚCacheResolverÚclientÚresolveÚResolverChainÚresolver)ÚselfÚhost_resolverÚcache_resolverÚreal_resolver© r!   ú:/usr/lib/python3/dist-packages/tornado/platform/twisted.pyÚ
initialize9   s   ÿ
ÿzTwistedResolver.initializer   ÚhostÚportÚfamilyz*Generator[Any, Any, List[Tuple[int, Any]]]c           
   
   c   s  � t jj |¡r|}tj}nZt jj |¡r|}tj}nM| j 	t
|ƒ¡}tƒ }| |j¡ |V }t|tjƒrOz| ¡  W n, t jjjyN } zt|ƒ‚d }~ww t jj |¡rZtj}nt jj |¡retj}ntj}|tjkry||krytd||f ƒ‚t t|¡||ffg}	|	S )Nz%Requested socket family %d but got %d)r   r   ÚabstractÚisIPAddressÚsocketÚAF_INETÚisIPv6AddressÚAF_INET6r   ÚgetHostByNamer   r   ÚaddBothÚ
set_resultÚ
isinstancer   ÚFailureÚraiseExceptionr   ÚerrorÚDomainErrorÚIOErrorÚ	AF_UNSPECÚ	ExceptionÚtypingÚcastÚint)
r   r$   r%   r&   ÚresolvedÚresolved_familyÚdeferredÚfutÚeÚresultr!   r!   r"   r   G   s:   €€ÿ
ÿzTwistedResolver.resolve©r   N)r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   Ú	coroutineÚstrr:   r   r!   r!   r!   r"   r   '   s    
ÿÿÿÿþr   r   c                  C   s   ddl m}  | ƒ  dS )a‹  Install ``AsyncioSelectorReactor`` as the default Twisted reactor.

    .. deprecated:: 5.1

       This function is provided for backwards compatibility; code
       that does not require compatibility with older versions of
       Tornado should use
       ``twisted.internet.asyncioreactor.install()`` directly.

    .. versionchanged:: 6.0.3

       In Tornado 5.x and before, this function installed a reactor
       based on the Tornado ``IOLoop``. When that reactor
       implementation was removed in Tornado 6.0.0, this function was
       removed as well. It was restored in Tornado 6.0.3 using the
       ``asyncio`` reactor instead.

    r   ©ÚinstallN)Útwisted.internet.asyncioreactorrI   rH   r!   r!   r"   rI   k   s   
rI   ÚregisterÚdc                    s0   t ƒ ‰ dtjdd f‡ fdd„}|  ˆ j|¡ ˆ S )Nr   r   c                    s,   z|   ¡  tdƒ‚   tˆ t ¡ ƒ Y d S )Nzerrback called without error)r2   r7   r   ÚsysÚexc_infor   ©Úfr!   r"   Úerrback‰   s
   z_.<locals>.errback)r   r   r1   ÚaddCallbacksr/   )rL   rQ   r!   rO   r"   Ú_…   s   rS   rA   )#rE   r)   rM   Útwisted.internet.abstractr   rJ   Útwisted.internet.deferr   Útwisted.pythonr   Útwisted.names.cacheÚtwisted.names.clientÚtwisted.names.hostsÚtwisted.names.resolveÚtornado.concurrentr   r   Útornado.escaper   Útornador   Útornado.netutilr	   r8   ÚTYPE_CHECKINGr
   r   r   r   r   rI   ÚhasattrÚconvert_yieldedrK   rS   r!   r!   r!   r"   Ú<module>   s0   
D
ý