o
    1&i                     @   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   ?C:\wamp64\www\opt\env\Lib\site-packages\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)addressZ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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      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di |S )ziCustomized object deserialization.

    This function is referenced internally by `custom_reduce()`.
    Nr   )r)   r*   r   )Zcustom_pickledr   r*   r   r   r   r:      s   r:   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   Zfoutr   r   r   r   is_serialiable   s   
$r>   c                 C   s   t dt|  d)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v 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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   s   zReduceMixin._reduce_statesc                 K   rM   r   rN   )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;   rR   rP   r-   r   r   r   
__reduce__   r/   zReduceMixin.__reduce__N)r0   r1   r2   r3   abcabstractmethodrP   r5   r   rR   rS   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 |i |S r   rW   )r+   r   rQ   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   rS      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   rS   r5   r   r   r   r   r   rV      s    rV   )r3   r\   rT   r   copyregr<   Znumbar   Zllvmliter   r	   r
   r   r   r&   r   r'   r7   r9   r;   r:   r>   r@   rD   ZCloudPicklerr   rK   dispatch_tableABCrL   rV   r   r   r   r   <module>   s8    	
	
