U
    ø¾|e€  ã                   @   s  d Z ddl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„ Z
i Zdd„ Zd	d
„ Zdd„ ZejZG dd„ dƒZdd„ Zdd„ Ze ee¡ dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ dejƒZdd „ Zeeje< G d!d"„ d"ejƒZG d#d$„ d$ƒZdS )%z/
Serialization support for compiled functions.
é    N)Úcloudpickle)Úirc                 G   s
   | j |Ž S )zM
    Global hook to rebuild a given class from its __reduce__ arguments.
    )Ú_rebuild)ÚclsÚargs© r   úQ/var/www/website-v5/atlas_env/lib/python3.8/site-packages/numba/core/serialize.pyÚ_rebuild_reduction   s    r	   c                 C   s@   | |f}zt | }W n& tk
r:   t |¡ t |< }Y nX |S )zËUsed by `numba_unpickle` from _helperlib.c

    Parameters
    ----------
    address : int
    bytedata : bytes
    hashed : bytes

    Returns
    -------
    obj : object
        unpickled object
    )Ú_unpickled_memoÚKeyErrorr   Úloads)ÚaddressZbytedataÚhashedÚkeyÚobjr   r   r   Ú_numba_unpickle   s    r   c              	   C   s:   t }t ¡ �$}||dd�}| | ¡ | ¡ }W 5 Q R X |S )zISimilar to `pickle.dumps()`. Returns the serialized object in bytes.
    é   )Úprotocol)ÚNumbaPicklerÚioÚBytesIOÚdumpÚgetvalue)r   ÚpicklerÚbufÚpZpickledr   r   r   Údumps4   s    

r   c                 C   sZ   t  | ¡\}}}g }t|ƒ}|D ]*}t|tjƒr@| t|ƒ¡ q | |¡ q |t|ƒ|fS ©N)	r   r   ÚiterÚ
isinstancer   ÚValueÚappendÚnextÚtuple)Z
static_excÚexc_argsÚexcZstatic_argsZlocinfoZ	real_argsZexc_args_iterÚargr   r   r   Úruntime_build_excinfo_struct@   s    r'   c                   @   s0   e Zd ZdZdZdd„ Zdd„ Zedd„ ƒZd	S )
Ú_CustomPickleda€  A wrapper for objects that must be pickled with `NumbaPickler`.

    Standard `pickle` will pick up the implementation registered via `copyreg`.
    This will spawn a `NumbaPickler` instance to serialize the data.

    `NumbaPickler` overrides the handling of this type so as not to spawn a
    new pickler for the object when it is already being pickled by a
    `NumbaPickler`.
    ©ÚctorÚstatesc                 C   s   || _ || _d S r   r)   )Úselfr*   r+   r   r   r   Ú__init__]   s    z_CustomPickled.__init__c                 C   s   t j| j| jffS r   )r(   r   r*   r+   ©r,   r   r   r   Ú_reducea   s    z_CustomPickled._reducec                 C   s
   | ||ƒS r   r   )r   r*   r+   r   r   r   r   d   s    z_CustomPickled._rebuildN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r-   r/   Úclassmethodr   r   r   r   r   r(   P   s   
r(   c                 C   s   t | ƒ\}}t||ƒS )zPstandard unpickling for `_CustomPickled`.

    Uses `NumbaPickler` to load.
    )r   r(   )Ú
serializedr*   r+   r   r   r   Ú_unpickle__CustomPickledi   s    r7   c                 C   s   t | j| jfƒ}t|ffS )zNstandard pickling for `_CustomPickled`.

    Uses `NumbaPickler` to dump.
    )r   r*   r+   r7   )Úcpr6   r   r   r   Ú_pickle__CustomPickledr   s    r9   c                 C   s   t t| |ƒffS )a€  For customizing object serialization in `__reduce__`.

    Object states provided here are used as keyword arguments to the
    `._rebuild()` class method.

    Parameters
    ----------
    states : dict
        Dictionary of object states to be serialized.

    Returns
    -------
    result : tuple
        This tuple conforms to the return type requirement for `__reduce__`.
    )Úcustom_rebuildr(   )r   r+   r   r   r   Úcustom_reduce   s    r;   c                 C   s   | j | j }}|jf |ŽS )ziCustomized object deserialization.

    This function is referenced internally by `custom_reduce()`.
    )r*   r+   r   )Zcustom_pickledr   r+   r   r   r   r:   ’   s    r:   c              	   C   s`   t  ¡ �N}t|ƒ}z| | ¡ W n" tjk
rB   Y W 5 Q R £ dS X W 5 Q R £ dS W 5 Q R X dS )z‰Check if *obj* can be serialized.

    Parameters
    ----------
    obj : object

    Returns
    --------
    can_serialize : bool
    FTN)r   r   r   r   ÚpickleÚPicklingError)r   Zfoutr   r   r   r   Úis_serialiable›   s    
r>   c                 C   s   t  dt| ƒ› d�¡‚d S )NzPickling of z is unsupported)r<   r=   Útype)r   r   r   r   Ú
_no_pickle°   s    r@   c                 C   s   t j | ¡ | S )z1This is called on a type to disable pickling
    )r   Údisabled_typesÚadd)Útypr   r   r   Údisable_pickling´   s    rD   c                       s"   e Zd Zeƒ Z‡ fdd„Z‡  ZS )r   c                    s"   t |ƒ| jkrt|ƒ tƒ  |¡S r   )r?   rA   r@   ÚsuperÚreducer_override)r,   r   ©Ú	__class__r   r   rF   Á   s    zNumbaPickler.reducer_override)r0   r1   r2   ÚsetrA   rF   Ú__classcell__r   r   rG   r   r   ¼   s   r   c                 C   s   |   ¡ S r   )r/   )r8   r   r   r   Ú_custom_reduce__custompickledÈ   s    rK   c                   @   s<   e Zd ZdZejdd„ ƒZejdd„ ƒZdd„ Z	dd	„ Z
d
S )ÚReduceMixinznA mixin class for objects that should be reduced by the NumbaPickler
    instead of the standard pickler.
    c                 C   s   t ‚d S r   ©ÚNotImplementedErrorr.   r   r   r   Ú_reduce_statesÕ   s    zReduceMixin._reduce_statesc                 K   s   t ‚d S r   rM   )r   Úkwargsr   r   r   r   Ù   s    zReduceMixin._rebuildc                 C   s   | j S r   rG   r.   r   r   r   Ú_reduce_classß   s    zReduceMixin._reduce_classc                 C   s   t |  ¡ |  ¡ ƒS r   )r;   rQ   rO   r.   r   r   r   Ú
__reduce__ä   s    zReduceMixin.__reduce__N)r0   r1   r2   r3   ÚabcÚabstractmethodrO   Úabstractclassmethodr   rQ   rR   r   r   r   r   rL   Ï   s   

rL   c                   @   s4   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZd
S )ÚPickleCallableByPathaã  Wrap a callable object to be pickled by path to workaround limitation
    in pickling due to non-pickleable objects in function non-locals.

    Note:
    - Do not use this as a decorator.
    - Wrapped object must be a global that exist in its parent module and it
      can be imported by `from the_module import the_object`.

    Usage:

    >>> def my_fn(x):
    >>>     ...
    >>> wrapped_fn = PickleCallableByPath(my_fn)
    >>> # refer to `wrapped_fn` instead of `my_fn`
    c                 C   s
   || _ d S r   ©Ú_fn)r,   Úfnr   r   r   r-   ø   s    zPickleCallableByPath.__init__c                 O   s   | j ||ŽS r   rW   )r,   r   rP   r   r   r   Ú__call__û   s    zPickleCallableByPath.__call__c                 C   s   t | ƒj| jj| jjffS r   )r?   r   rX   r1   r0   r.   r   r   r   rR   þ   s    zPickleCallableByPath.__reduce__c                 C   s   | t tj| |ƒƒS r   )ÚgetattrÚsysÚmodules)r   ÚmodnameZfn_pathr   r   r   r     s    zPickleCallableByPath._rebuildN)	r0   r1   r2   r3   r-   rZ   rR   r5   r   r   r   r   r   rV   è   s   rV   )r3   r\   rS   r   Úcopyregr<   Únumbar   Úllvmliter   r	   r
   r   r   r'   r   r(   r7   r9   r;   r:   r>   r@   rD   ÚCloudPicklerr   rK   Údispatch_tableÚABCrL   rV   r   r   r   r   Ú<module>   s6   	
	
