σ
P'^c           @@  sD  d  Z  d d l m Z d d l m Z d d l m Z y d d l Z Wn e	 k
 re d d l Z n Xd d l
 m Z d d	 d
 d d d d g Z y e e e e f Z Wn  e k
 rΙ e e e f Z n Xd   Z e j e j d  Z e Z d   Z d d  Z d e f d     YZ d   Z e j e j d  Z d   Z d S(   so   
    celery.utils.serialization
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    Utilities for safely pickling exceptions.

i    (   t   absolute_import(   t   getmro(   t	   takewhileNi   (   t	   safe_reprt   UnpickleableExceptionWrappert   subclass_exceptiont   find_pickleable_exceptiont   create_exception_clst   get_pickleable_exceptiont   get_pickleable_etypet   get_pickled_exceptionc         C@  s   t  |  | f i | d 6 S(   Nt
   __module__(   t   type(   t   namet   parentt   module(    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR   !   s    c         C@  sa   t  |  d g   } xH t |  j t  D]4 } y  | |   } | | |   Wn q% X| Sq% Wd S(   s?  With an exception instance, iterate over its super classes (by mro)
    and find the first super exception that is pickleable.  It does
    not go below :exc:`Exception` (i.e. it skips :exc:`Exception`,
    :class:`BaseException` and :class:`object`).  If that happens
    you should use :exc:`UnpickleableException` instead.

    :param exc: An exception instance.

    Will return the nearest pickleable parent exception class
    (except :exc:`Exception` and parents), or if the exception is
    pickleable it will return :const:`None`.

    :rtype :exc:`Exception`:

    t   argsN(   t   getattrt   itermrot	   __class__t   unwanted_base_classes(   t   exct   loadst   dumpst   exc_argst   superclst   superexc(    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR   %   s    c         @  s   t    f d   t |    S(   Nc         @  s
   |    k S(   N(    (   t   sup(   t   stop(    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyt   <lambda>C   t    (   R   R   (   t   clsR   (    (   R   s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR   B   s    c         C@  s   | s t  } n  t |  | |  S(   s&   Dynamically create an exception class.(   t	   ExceptionR   (   R   R   R   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR   F   s    	c           B@  sM   e  Z d  Z d Z d Z d Z d d  Z d   Z d   Z	 e
 d    Z RS(   sp  Wraps unpickleable exceptions.

    :param exc_module: see :attr:`exc_module`.
    :param exc_cls_name: see :attr:`exc_cls_name`.
    :param exc_args: see :attr:`exc_args`

    **Example**

    .. code-block:: python

        >>> def pickle_it(raising_function):
        ...     try:
        ...         raising_function()
        ...     except Exception as e:
        ...         exc = UnpickleableExceptionWrapper(
        ...             e.__class__.__module__,
        ...             e.__class__.__name__,
        ...             e.args,
        ...         )
        ...         pickle.dumps(exc)  # Works fine.

    c         C@  s   g  } xS | D]K } y t  j |  | j |  Wq t k
 rW | j t |   q Xq W| |  _ | |  _ | |  _ | |  _ t j	 |  | | | |  d  S(   N(
   t   pickleR   t   appendR    R   t
   exc_modulet   exc_cls_nameR   t   textt   __init__(   t   selfR#   R$   R   R%   t   safe_exc_argst   arg(    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR&   n   s    				c         C@  s   t  |  j |  j  |  j   S(   N(   R   R$   R#   R   (   R'   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyt   restore|   s    	c         C@  s   |  j  S(   N(   R%   (   R'   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyt   __str__   s    c         C@  s1   |  | j  j | j  j t | d g   t |   S(   NR   (   R   R   t   __name__R   R   (   R   R   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyt   from_exception   s    	N(   R,   R   t   __doc__t   NoneR#   R$   R   R&   R*   R+   t   classmethodR-   (    (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR   M   s   		c         C@  sU   y t  j t  j |    Wn t k
 r- n X|  St |   } | rH | St j |   S(   s"   Make sure exception is pickleable.(   R!   R   R   R    R   R   R-   (   R   t   nearest(    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR      s    c         C@  s'   y | | |    Wn t  SX|  Sd  S(   N(   R    (   R   R   R   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR	      s
    c         C@  s   t  |  t  r |  j   S|  S(   sY   Get original exception from exception pickled using
    :meth:`get_pickleable_exception`.(   t
   isinstanceR   R*   (   R   (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyR
   ’   s    
(   R.   t
   __future__R    t   inspectR   t	   itertoolsR   t   cPickleR!   t   ImportErrort   encodingR   t   __all__t   StandardErrorR    t   BaseExceptiont   objectR   t	   NameErrorR   R   R   R   t!   find_nearest_pickleable_exceptionR   R/   R   R   R   R	   R
   (    (    (    s<   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/serialization.pyt   <module>   s4   			>		