o
    wvXj¤#  ã                	   @   sò  d Z ddlmZm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 dd
lmZmZmZmZmZ ddlmZ zddlZW n eyc   ddlZY nw ej dkZ!dZ"ze#e$e%e&fZ'W n e(y�   e$e%e&fZ'Y nw dddddddddœZ)dd„ Z*ej+ej,fdd„Z-dd„ Z.d3dd„Z/dd„ Z0eG dd„ de$ƒƒZ1dd„ Z2ej+ej,fd d!„Z3d"d#„ Z4d$d%„ Z
d&d'„ Zd3d(d)„Z5d*d+„ Z6ej7efdddfd,d-„Z8e!ródd.l9m:Z: dZ;e:d/ƒ d0d1„ Z<dS d2d1„ Z<dS )4z)Utilities for safely pickling exceptions.é    )Úabsolute_importÚunicode_literalsN)Ú	b64decode)Ú	b64encode)Úpartial)Úgetmro)Ú	takewhile)Úbytes_to_strÚstr_to_bytes)Úbytes_if_py2ÚitemsÚpython_2_unicode_compatibleÚreraiseÚstring_té   )Ú	safe_repr)é   r   )Ú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 t| ƒ|fd|iƒS )zCreate new exception class.Ú
__module__)Útyper   )ÚnameÚparentÚmodule© r(   úW/var/www/html/myproject/venv/lib/python3.10/site-packages/celery/utils/serialization.pyr   0   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   5   s   ýÿür   c                    s   t ‡ fdd„t| ƒƒS )Nc                    s   | ˆ vS ©Nr(   )Úsup©Ústopr(   r)   Ú<lambda>U   s    zitermro.<locals>.<lambda>)r   r   )Úclsr9   r(   r8   r)   r,   T   s   r,   c                 C   s   |st }t| ||ƒS )z&Dynamically create an exception class.)r/   r   )r%   r'   r&   r(   r(   r)   r   X   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)r   ÚencoderÚsafe_exc_argsÚargr(   r(   r)   Úensure_serializable_   s   
ÿrA   c                   @   sB   e Zd ZdZdZdZdZddd„Zdd„ Zdd„ Z	e
d	d
„ ƒZd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                 C   s:   t |tjƒ}|| _|| _|| _|| _t | ||||¡ d S r6   )	rA   Úpickler2   Ú
exc_moduleÚexc_cls_namer3   Útextr/   Ú__init__)ÚselfrC   rD   r3   rE   r?   r(   r(   r)   rF   ’   s   z%UnpickleableExceptionWrapper.__init__c                 C   s   t | j| jƒ| jŽ S r6   )r   rD   rC   r3   ©rG   r(   r(   r)   Úrestoreš   s
   ÿÿz$UnpickleableExceptionWrapper.restorec                 C   s   | j S r6   )rE   rH   r(   r(   r)   Ú__str__ž   s   z$UnpickleableExceptionWrapper.__str__c                 C   s"   | |j j|j jt|dg ƒt|ƒƒS )Nr*   )r-   r#   Ú__name__r+   r   )r;   r0   r(   r(   r)   Úfrom_exception¡   s
   
ýz+UnpickleableExceptionWrapper.from_exceptionr6   )rK   r#   Ú__qualname__Ú__doc__rC   rD   r3   rF   rI   rJ   ÚclassmethodrL   r(   r(   r(   r)   r   s   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.)rB   r1   r2   r/   r   r   rL   )r0   Únearestr(   r(   r)   r   ©   s   ýÿ
r   c                 C   s*   z	||| ƒƒ W | S  t y   t  Y S w )zGet pickleable exception type.)r/   )r;   r1   r2   r(   r(   r)   r   ·   s   ýÿr   c                 C   s   t | tƒr	|  ¡ S | S )z,Reverse of :meth:`get_pickleable_exception`.)Ú
isinstancer   rI   )r0   r(   r(   r)   r   Á   s   
r   c                 C   s   t tt| ƒƒƒS r6   )r	   Úbase64encoder
   ©Úsr(   r(   r)   r   È   s   r   c                 C   s   t t| ƒƒS r6   )Úbase64decoder
   rS   r(   r(   r)   r   Ì   s   r   c                 C   sF   |du rt }t| tƒr!z||  ¡  W S  ty    td | ¡ƒ‚w | S )z_Convert common terms for true/false to bool.

    Examples (true/false/yes/no/on/off/1/0).
    Nz Cannot coerce {0!r} to type bool)ÚSTRTOBOOL_DEFAULT_TABLErQ   r   ÚlowerÚKeyErrorÚ	TypeErrorÚformat)Ú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
„t	| ƒD ƒS t| t
jt
jfƒrSt| ƒS t| t
jƒr]t| ƒS |du rltd t| ƒ| |¡ƒ‚|| ƒ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(   )Ú.0Úv)Ú_jsonifyr(   r)   Ú
<listcomp>  s    zjsonify.<locals>.<listcomp>c                    s0   i | ]\}}ˆrˆ|ƒrn	 	 |ˆ ||d�“qS )r   )rk   r(   )ro   Úkrp   ©rq   rl   r(   r)   Ú
<dictcomp>  s    þþþÿzjsonify.<locals>.<dictcomp>z+Unsupported type: {0!r} {1!r} (parent: {2}))Úkombu.abstractri   r   ÚjsonifyrQ   Úas_dictr=   ÚlistÚdictr   ra   Údatere   rh   Ú	timedeltaÚstrÚ
ValueErrorrZ   r$   )Úobjrj   rk   rl   rm   ÚKombuDictTyper(   rt   r)   rw   ñ   s4   þ

ÿ
ÿÿrw   )Úexec_z5def _raise_with_context(exc, ctx): raise exc from ctxc                 C   s0   t  ¡ }|s| ‚|d | u r‚ t| |d ƒ d S )Nr   )ÚsysÚexc_infoÚ_raise_with_context©r0   rƒ   r(   r(   r)   Úraise_with_context  s   r†   c                 C   s>   t  ¡ }|s| ‚|d | u r‚ |d rtt| ƒ| |d ƒ | ‚)Nr   é   )r‚   rƒ   r   r$   r…   r(   r(   r)   r†   "  s   r6   )=rN   Ú
__future__r   r   ra   Únumbersr‚   Úbase64r   rU   r   rR   Ú	functoolsr   Úinspectr   Ú	itertoolsr   Úkombu.utils.encodingr	   r
   Úcelery.fiver   r   r   r   r   Úencodingr   ÚcPicklerB   ÚImportErrorÚversion_infoÚPY33Ú__all__ÚStandardErrorr/   ÚBaseExceptionÚobjectr.   Ú	NameErrorrV   r   r1   r2   r   r,   r   rA   r   r   r   r   r   rh   ÚRealrw   Ú	vine.fiver�   r„   r†   r(   r(   r(   r)   Ú<module>   sn   ÿ
ÿþ
ÿ
5



ý$