U
    9%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   S/var/www/html/Darija-Ai-API/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 )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   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 )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    
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   	
	
