U
    hâËd€  ã                   @   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   úM/home/sam/Atlas/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_excZ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    r6   c                 C   s   t | j| jfƒ}t|ffS )zNstandard pickling for `_CustomPickled`.

    Uses `NumbaPickler` to dump.
    )r   r)   r*   r6   )Úcpr5   r   r   r   Ú_pickle__CustomPickledr   s    r8   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   r9   ’   s    r9   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    rC   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>   r@   r?   ÚsuperÚreducer_override)r+   r   ©Ú	__class__r   r   rE   Á   s    zNumbaPickler.reducer_override)r/   r0   r1   Úsetr@   rE   Ú__classcell__r   r   rF   r   r   ¼   s   r   c                 C   s   |   ¡ S r   )r.   )r7   r   r   r   Ú_custom_reduce__custompickledÈ   s    rJ   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   rL   )r   Úkwargsr   r   r   r   Ù   s    zReduceMixin._rebuildc                 C   s   | j S r   rF   r-   r   r   r   Ú_reduce_classß   s    zReduceMixin._reduce_classc                 C   s   t |  ¡ |  ¡ ƒS r   )r:   rP   rN   r-   r   r   r   Ú
__reduce__ä   s    zReduceMixin.__reduce__N)r/   r0   r1   r2   ÚabcÚabstractmethodrN   Úabstractclassmethodr   rP   rQ   r   r   r   r   rK   Ï   s   

rK   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   rV   )r+   r   rO   r   r   r   Ú__call__û   s    zPickleCallableByPath.__call__c                 C   s   t | ƒj| jj| jjffS r   )r>   r   rW   r0   r/   r-   r   r   r   rQ   þ   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)	r/   r0   r1   r2   r,   rY   rQ   r4   r   r   r   r   r   rU   è   s   rU   )r2   r[   rR   r   Úcopyregr;   Znumbar   Zllvmliter   r	   r
   r   r   r&   r   r'   r6   r8   r:   r9   r=   r?   rC   ZCloudPicklerr   rJ   Údispatch_tableÚABCrK   rU   r   r   r   r   Ú<module>   s6   	
	
