U
    ,dD                     @   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 Zi 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)cloudpicklec                 G   s
   | j | S )zM
    Global hook to rebuild a given class from its __reduce__ arguments.
    )_rebuild)clsargs r   8/tmp/pip-unpacked-wheel-eu7e0c37/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 )zUsed 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   dumps3   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 Nr   )selfr   r   r   r   r   __init__P   s    z_CustomPickled.__init__c                 C   s   t j| j| jffS r    )r   r   r   r   r!   r   r   r   _reduceT   s    z_CustomPickled._reducec                 C   s
   | ||S r    r   )r   r   r   r   r   r   r   W   s    z_CustomPickled._rebuildN)	__name__
__module____qualname____doc__	__slots__r"   r$   classmethodr   r   r   r   r   r   C   s   
r   c                 C   s   t | \}}t||S )zPstandard unpickling for `_CustomPickled`.

    Uses `NumbaPickler` to load.
    )r   r   )
serializedr   r   r   r   r   _unpickle__CustomPickled\   s    r,   c                 C   s   t | j| jf}t|ffS )zNstandard pickling for `_CustomPickled`.

    Uses `NumbaPickler` to dump.
    )r   r   r   r,   )cpr+   r   r   r   _pickle__CustomPicklede   s    r.   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_reducer   s    r0   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 )zCheck 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    
r3   c                 C   s   t dt|  dd S )NzPickling of z is unsupported)r1   r2   type)r   r   r   r   
_no_pickle   s    r5   c                 C   s   t j|  tt j| < | S )z1This is called on a type to disable pickling
    )r   disabled_typesaddr5   dispatch_table)typr   r   r   disable_pickling   s    
r:   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    )r4   r6   r5   superreducer_override)r!   r   	__class__r   r   r<      s    zNumbaPickler.reducer_override)r%   r&   r'   setr6   r<   __classcell__r   r   r=   r   r      s   r   c                 C   s   |   S r    )r$   )r-   r   r   r   _custom_reduce__custompickled   s    rA   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    rC   )r   kwargsr   r   r   r      s    zReduceMixin._rebuildc                 C   s   | j S r    r=   r#   r   r   r   _reduce_class   s    zReduceMixin._reduce_classc                 C   s   t |  |  S r    )r0   rG   rE   r#   r   r   r   
__reduce__   s    zReduceMixin.__reduce__N)r%   r&   r'   r(   abcabstractmethodrE   abstractclassmethodr   rG   rH   r   r   r   r   rB      s   

rB   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    rM   )r!   r   rF   r   r   r   __call__   s    zPickleCallableByPath.__call__c                 C   s   t | j| jj| jjffS r    )r4   r   rN   r&   r%   r#   r   r   r   rH      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%   r&   r'   r(   r"   rP   rH   r*   r   r   r   r   r   rL      s   rL   )r(   rR   rI   r   copyregr1   Znumbar   r   r	   r   r   r   r   r,   r.   r0   r/   r3   r5   r:   ZCloudPicklerr   rA   r8   ABCrB   rL   r   r   r   r   <module>   s2   	
	

