
    Ef	                         d Z ddlmZmZmZmZ ddlZ ed ed            sd Z	e
Ze
Znd Z	eZeZd ZeZej$                  d	kD  r	 ed
       n ed       d Z G d de      Zd Zy)a  Miscellaneous utility functions and classes.

This module is used internally by Tornado.  It is not necessarily expected
that the functions and classes defined here will be useful to other
applications, but they are documented here in case they are.

The one public-facing part of this module is the `Configurable` class
and its `~Configurable.configure` method, which becomes a part of the
interface of its subclasses, including `.AsyncHTTPClient`, `.IOLoop`,
and `.Resolver`.
    )absolute_importdivisionprint_functionwith_statementN     c                     | S N ss    @/usr/lib/python3/dist-packages/zmq/eventloop/minitornado/util.pyur      s    r   c                 $    | j                  d      S )Nunicode_escape)decoder   s    r   r   r      s    xx())r   c                 f   t        | t              rt        t        ur| j                  d      } | j	                  d      dk(  rt        | dd      S | j                  d      }t        dj                  |dd       dd|d   gd      }	 t        ||d         S # t        $ r t        d|d   z        w xY w)a  Imports an object by name.

    import_object('x') is equivalent to 'import x'.
    import_object('x.y.z') is equivalent to 'from x.y import z'.

    >>> import tornado.escape
    >>> import_object('tornado.escape') is tornado.escape
    True
    >>> import_object('tornado.escape.utf8') is tornado.escape.utf8
    True
    >>> import_object('tornado') is tornado
    True
    >>> import_object('tornado.missing_module')
    Traceback (most recent call last):
        ...
    ImportError: No module named missing_module
    zutf-8.r   NzNo module named %s)
isinstanceunicode_typestrencodecount
__import__splitjoingetattrAttributeErrorImportError)namepartsobjs      r   import_objectr$   %   s    $ $%#\*A{{7#zz#!$d++JJsOE
SXXeCRj)4b	{A
FC<sE"I&& <.r:;;<s   B B0)   z
def raise_exc_info(exc_info):
    raise exc_info[1].with_traceback(exc_info[2])

def exec_in(code, glob, loc=None):
    if isinstance(code, str):
        code = compile(code, '<string>', 'exec', dont_inherit=True)
    exec(code, glob, loc)
ah  
def raise_exc_info(exc_info):
    raise exc_info[0], exc_info[1], exc_info[2]

def exec_in(code, glob, loc=None):
    if isinstance(code, basestring):
        # exec(string) inherits the caller's future imports; compile
        # the string first to prevent that.
        code = compile(code, '<string>', 'exec', dont_inherit=True)
    exec code in glob, loc
c                 j    t        | d      r| j                  S | j                  r| j                  d   S y)aL  Provides the errno from an Exception object.

    There are cases that the errno attribute was not set so we pull
    the errno out of the args but if someone instantiates an Exception
    without any args you will get a tuple error. So this function
    abstracts all that behavior to give you a safe way to get the
    errno.
    errnor   N)hasattrr'   args)es    r   errno_from_exceptionr+   a   s/     q'ww	
vvayr   c                        e Zd ZdZdZdZ fdZed        Zed        Z	d Z
ed        Zed        Zed	        Zed
        Z xZS )Configurablea  Base class for configurable interfaces.

    A configurable interface is an (abstract) class whose constructor
    acts as a factory function for one of its implementation subclasses.
    The implementation subclass as well as optional keyword arguments to
    its initializer can be set globally at runtime with `configure`.

    By using the constructor as the factory method, the interface
    looks like a normal class, `isinstance` works as usual, etc.  This
    pattern is most useful when the choice of implementation is likely
    to be a global decision (e.g. when `~select.epoll` is available,
    always use it instead of `~select.select`), or when a
    previously-monolithic class has been split into specialized
    subclasses.

    Configurable subclasses must define the class methods
    `configurable_base` and `configurable_default`, and use the instance
    method `initialize` instead of ``__init__``.
    Nc                    | j                         }i }| |u r8| j                         }|j                  r|j                  |j                         n| }|j                  |       t        t
        |   |      } |j                  |i | |S r
   )configurable_baseconfigured_class_Configurable__impl_kwargsupdatesuperr-   __new__
initialize)clsr)   kwargsbaseinit_kwargsimplinstance	__class__s          r   r4   zConfigurable.__new__   s    $$&$;'')D!!""4#5#56D6"s3D9 	T1[1r   c                     t               )zReturns the base class of a configurable hierarchy.

        This will normally return the class in which it is defined.
        (which is *not* necessarily the same as the cls classmethod parameter).
        NotImplementedErrorr6   s    r   r/   zConfigurable.configurable_base   s     "##r   c                     t               )zBReturns the implementation class to be used if none is configured.r>   r@   s    r   configurable_defaultz!Configurable.configurable_default   s     "##r   c                      y)zInitialize a `Configurable` subclass instance.

        Configurable classes should use `initialize` instead of ``__init__``.

        .. versionchanged:: 4.2
           Now accepts positional arguments in addition to keyword arguments.
        Nr   )selfs    r   r5   zConfigurable.initialize   s    r   c                     | j                         }t        |t        t        f      rt	        |      }|t        ||       st        d| z        ||_        ||_        y)zSets the class to use when the base class is instantiated.

        Keyword arguments will be saved and added to the arguments passed
        to the constructor.  This can be used to set global defaults for
        some parameters.
        NzInvalid subclass of %s)	r/   r   r   bytesr$   
issubclass
ValueError_Configurable__impl_classr1   )r6   r:   r7   r8   s       r   	configurezConfigurable.configure   s[     $$&d\512 &DJtS$95;<< #r   c                 |    | j                         }| j                  | j                         |_        |j                  S )z'Returns the currently configured class.)r/   rI   rB   r6   r8   s     r   r0   zConfigurable.configured_class   s;     $$&# # 8 8 :D   r   c                 R    | j                         }|j                  |j                  fS r
   r/   rI   r1   rL   s     r   _save_configurationz Configurable._save_configuration   s'    $$&!!4#5#566r   c                 L    | j                         }|d   |_        |d   |_        y )Nr      rN   )r6   savedr8   s      r   _restore_configurationz#Configurable._restore_configuration   s(    $$&!!H"1Xr   )__name__
__module____qualname____doc__rI   r1   r4   classmethodr/   rB   r5   rJ   r0   rO   rS   __classcell__)r<   s   @r   r-   r-   s   s    & LM" $ $ $ $ $ $ ! ! 7 7 & &r   r-   c                 x    | j                   | j                  | j                  dz  dz  z   dz  z   t        d      z  S )z<Equivalent to td.total_seconds() (introduced in python 2.7).   i  i@B )microsecondssecondsdaysfloat)tds    r   timedelta_to_secondsra      s6    OOrzzBGGbL4,??7JJeT[n\\r   )rW   
__future__r   r   r   r   sysr   typer   r   r   basestring_typeunicode
basestringr$   rF   
bytes_typeversion_infoexecr+   objectr-   ra   r   r   r   <module>rl      s   
 Q P 
 #tBx LO* L O<D 
d 
  	 

 
$`&6 `&F]r   