o
    Df                   	   @   sd  d 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
 ddlmZ ddlmZ ddlmZmZmZ zddlZW n eyI   ddlZY nw d	ZeeefZd
d
d
ddddd
dZdd ZejejfddZdd Z d+ddZ!dd Z"G dd deZ#dd Z$ejejfddZ%dd Z&dd  Zd!d" Zd+d#d$Z'd%d& Z(ej)e*fdddfd'd(Z+d)d* Z,dS ),z)Utilities for safely pickling exceptions.    N)	b64decode)	b64encode)partial)getmro)	takewhile)bytes_to_str	safe_reprstr_to_bytes)UnpickleableExceptionWrappersubclass_exceptionfind_pickleable_exceptioncreate_exception_clsget_pickleable_exceptionget_pickleable_etypeget_pickled_exception	strtoboolFT)falseno0trueyes1onoffc                 C   s   t | |fd|iS )zCreate new exception class.
__module__)type)nameparentmodule r   S/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/utils/serialization.pyr   !   s   r   c              	   C   sR   t | dg }t| jtD ]}z|| }||| W |  S  ty&   Y qw dS )a  Find first pickleable exception base class.

    With an exception instance, iterate over its super classes (by MRO)
    and find the first super exception that's 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.

    Arguments:
        exc (BaseException): An exception instance.
        loads: decoder to use.
        dumps: encoder to use

    Returns:
        Exception: Nearest pickleable parent exception class
            (except :exc:`Exception` and parents), or if the exception is
            pickleable it will return :const:`None`.
    argsN)getattritermro	__class__unwanted_base_classes	Exception)excloadsdumpsexc_argssuperclssuperexcr   r   r    r   &   s   r   c                    s   t  fddt| S )Nc                    s   |  vS Nr   )supstopr   r    <lambda>F   s    zitermro.<locals>.<lambda>)r   r   )clsr0   r   r/   r    r#   E   s   r#   c                 C   s   |st }t| ||S )z&Dynamically create an exception class.)r&   r   )r   r   r   r   r   r    r   I   s   r   c              	   C   sL   g }| D ]}z|| | | W q ty!   | t| Y qw t|S )a  Ensure items will serialize.

    For a given list of arbitrary objects, return the object
    or a string representation, safe for serialization.

    Arguments:
        items (Iterable[Any]): Objects to serialize.
        encoder (Callable): Callable function to serialize with.
    )appendr&   r   tuple)itemsencodersafe_exc_argsargr   r   r    ensure_serializableP   s   
r9   c                       sJ   e Zd ZdZdZdZdZd fdd	Zdd Zdd Z	e
d	d
 Z  ZS )r
   at  Wraps unpickleable exceptions.

    Arguments:
        exc_module (str): See :attr:`exc_module`.
        exc_cls_name (str): See :attr:`exc_cls_name`.
        exc_args (Tuple[Any, ...]): See :attr:`exc_args`.

    Example:
        >>> 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.
    Nc                    s<   t |dd }|| _|| _|| _|| _t |||| d S )Nc                 S   s   t t | S r-   )pickler(   r)   )vr   r   r    r1      s    z7UnpickleableExceptionWrapper.__init__.<locals>.<lambda>)r9   
exc_moduleexc_cls_namer*   textsuper__init__)selfr<   r=   r*   r>   r7   r$   r   r    r@      s   z%UnpickleableExceptionWrapper.__init__c                 C   s   t | j| j| j S r-   )r   r=   r<   r*   rA   r   r   r    restore   s
   z$UnpickleableExceptionWrapper.restorec                 C   s   | j S r-   )r>   rC   r   r   r    __str__   s   z$UnpickleableExceptionWrapper.__str__c                 C   s<   | |j j|j jt|dg t|}t|dr||j}|S )Nr!   __traceback__)r$   r   __name__r"   r   hasattrwith_tracebackrF   )r2   r'   resr   r   r    from_exception   s   

z+UnpickleableExceptionWrapper.from_exceptionr-   )rG   r   __qualname____doc__r<   r=   r*   r@   rD   rE   classmethodrK   __classcell__r   r   rB   r    r
   d   s    r
   c                 C   sD   zt t |  W | S  ty   Y nw t| }|r|S t| S )z"Make sure exception is pickleable.)r:   r(   r)   r&   r   r
   rK   )r'   nearestr   r   r    r      s   
r   c                 C   s*   z	|||  W | S  t y   t  Y S w )zGet pickleable exception type.)r&   )r2   r(   r)   r   r   r    r      s   r   c                 C   s   t | tr	|  S | S )z,Reverse of :meth:`get_pickleable_exception`.)
isinstancer
   rD   )r'   r   r   r    r      s   
r   c                 C   s   t tt| S r-   )r   base64encoder	   sr   r   r    r      s   r   c                 C   s   t t| S r-   )base64decoder	   rS   r   r   r    r      s   r   c                 C   sH   |du rt }t| tr"z||   W S  ty!   td| dw | S )z_Convert common terms for true/false to bool.

    Examples (true/false/yes/no/on/off/1/0).
    NzCannot coerce z to type bool)STRTOBOOL_DEFAULT_TABLErQ   strlowerKeyError	TypeError)termtabler   r   r    r      s   
r   c                 C   s   t | tjr(|  }| jr|d d |dd   }|dr&|d d d }|S t | tjr=|  }| jr;|d d }|S |  S )N      z+00:00iZ   )rQ   datetime	isoformatmicrosecondendswithtime)dtrr   r   r    _datetime_to_json   s   
rh   c                    s   ddl m} tt|||d t| |r| jdd} | du s#t| |r%| S t| ttfr5 fdd| D S t| trF fd	d
| 	 D S t| t
jt
jfrSt| S t| t
jr]t| S |du rqtdt| d| d| d|| S )z;Transform object making it suitable for json serialization.r   )Object)builtin_typeskey	keyfilterunknown_type_filterT)recurseNc                    s   g | ]} |qS r   r   ).0r;   )_jsonifyr   r    
<listcomp>   s    zjsonify.<locals>.<listcomp>c                    s0   i | ]\}}r|rn	 	 | ||dqS )   )rk   r   )ro   kr;   rp   rl   r   r    
<dictcomp>   s    zjsonify.<locals>.<dictcomp>zUnsupported type:  z
 (parent: ))kombu.abstractri   r   jsonifyrQ   as_dictr4   listdictr5   ra   datere   rh   	timedeltarW   
ValueErrorr   )objrj   rk   rl   rm   KombuDictTyper   rt   r    ry      s0   

ry   c                 C   s(   t  }|s| |d | u r | |d )Nrr   )sysexc_info)r'   r   r   r   r    raise_with_context  s   
r   r-   )-rM   ra   numbersr   base64r   rU   r   rR   	functoolsr   inspectr   	itertoolsr   kombu.utils.encodingr   r   r	   cPickler:   ImportError__all__r&   BaseExceptionobjectr%   rV   r   r(   r)   r   r#   r   r9   r
   r   r   r   r   rh   RealrW   ry   r   r   r   r   r    <module>   sR    


=



"