o
    8ήcD                     @   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   ;/tmp/pip-target-vg8gfxp4/lib/python/numba/core/serialize.py_rebuild_reduction   s   
r   c                 C   s>   | |f}zt | }W |S  ty   t| t |< }Y |S w )zUsed by `numba_unpickle` from _helperlib.c

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

    Returns
    -------
    obj : object
        unpickled object
    )_unpickled_memoKeyErrorr   loads)addressbytedatahashedkeyobjr   r   r   _numba_unpickle   s   
r   c                 C   sP   t }t }||dd}||  | }W d   |S 1 s!w   Y  |S )zISimilar to `pickle.dumps()`. Returns the serialized object in bytes.
       )protocolN)NumbaPicklerioBytesIOdumpgetvalue)r   picklerbufp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      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   
r1   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   r3   c                 C   s   | j | j}}|jdi |S )ziCustomized object deserialization.

    This function is referenced internally by `custom_reduce()`.
    Nr   )r    r!   r   )custom_pickledr   r!   r   r   r   r2      s   r2   c              	   C   sj   t  '}t|}z||  W n tjy!   Y W d   dS w 	 W d   dS 1 s.w   Y  dS )zCheck if *obj* can be serialized.

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

    Returns
    --------
    can_serialize : bool
    NFT)r   r   r   r   picklePicklingError)r   foutr   r   r   r   is_serialiable   s   
$r8   c                 C   s   t dt|  d)NzPickling of z is unsupported)r5   r6   type)r   r   r   r   
_no_pickle   s   r:   c                 C   s   t j|  tt j| < | S )z1This is called on a type to disable pickling
    )r   disabled_typesaddr:   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v rt| t |S r"   )r9   r;   r:   superreducer_override)r#   r   	__class__r   r   rA      s   zNumbaPickler.reducer_override)r(   r)   r*   setr;   rA   __classcell__r   r   rB   r   r      s    r   c                 C   s   |   S r"   )r&   )r0   r   r   r   _custom_reduce__custompickled   s   rF   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      t r"   NotImplementedErrorr%   r   r   r   _reduce_states      zReduceMixin._reduce_statesc                 K   rH   r"   rI   )r   kwargsr   r   r   r      rL   zReduceMixin._rebuildc                 C   s   | j S r"   rB   r%   r   r   r   _reduce_class   s   zReduceMixin._reduce_classc                 C   s   t |  |  S r"   )r3   rN   rK   r%   r   r   r   
__reduce__   r'   zReduceMixin.__reduce__N)r(   r)   r*   r+   abcabstractmethodrK   abstractclassmethodr   rN   rO   r   r   r   r   rG      s    

rG   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 |i |S r"   rT   )r#   r   rM   r   r   r   __call__   s   zPickleCallableByPath.__call__c                 C   s   t | j| jj| jjffS r"   )r9   r   rU   r)   r(   r%   r   r   r   rO      s   zPickleCallableByPath.__reduce__c                 C   s   | t tj| |S r"   )getattrsysmodules)r   modnamefn_pathr   r   r   r      s   zPickleCallableByPath._rebuildN)	r(   r)   r*   r+   r$   rW   rO   r-   r   r   r   r   r   rS      s    rS   )r+   rY   rP   r   copyregr5   numbar   r   r	   r   r   r   r   r/   r1   r3   r2   r8   r:   r?   CloudPicklerr   rF   r=   ABCrG   rS   r   r   r   r   <module>   s4    	
	

