o
    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   Q/home/ubuntu/veenaModal/venv/lib/python3.10/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)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   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)
static_excexc_argsexcstatic_argslocinfo	real_args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   r0   )selfr1   r2   r   r   r   __init__]   s   
z_CustomPickled.__init__c                 C   s   t j| j| jffS r   )r/   r   r1   r2   r3   r   r   r   _reducea      z_CustomPickled._reducec                 C   s
   | ||S r   r   )r   r1   r2   r   r   r   r   d   s   
z_CustomPickled._rebuildN)	__name__
__module____qualname____doc__	__slots__r4   r6   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/   )
serializedr1   r2   r   r   r   _unpickle__CustomPickledi   s   
r?   c                 C   s   t | j| jf}t|ffS )zNstandard pickling for `_CustomPickled`.

    Uses `NumbaPickler` to dump.
    )r   r1   r2   r?   )cpr>   r   r   r   _pickle__CustomPickledr   s   
rA   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   r2   r   r   r   custom_reduce   s   rC   c                 C   s   | j | j}}|jdi |S )ziCustomized object deserialization.

    This function is referenced internally by `custom_reduce()`.
    Nr   )r1   r2   r   )custom_pickledr   r2   r   r   r   rB      s   rB   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   
$rH   c                 C   s   t dt|  d)NzPickling of z is unsupported)rE   rF   type)r   r   r   r   
_no_pickle   s   rJ   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   rN   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   )rI   rK   rJ   superreducer_override)r3   r   	__class__r   r   rP      s   zNumbaPickler.reducer_override)r8   r9   r:   setrK   rP   __classcell__r   r   rQ   r   r      s    r   c                 C   s   |   S r   )r6   )r@   r   r   r   _custom_reduce__custompickled   s   rU   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   NotImplementedErrorr5   r   r   r   _reduce_states      zReduceMixin._reduce_statesc                 K   rW   r   rX   )r   kwargsr   r   r   r      r[   zReduceMixin._rebuildc                 C   s   | j S r   rQ   r5   r   r   r   _reduce_class   s   zReduceMixin._reduce_classc                 C   s   t |  |  S r   )rC   r]   rZ   r5   r   r   r   
__reduce__   r7   zReduceMixin.__reduce__N)r8   r9   r:   r;   abcabstractmethodrZ   abstractclassmethodr   r]   r^   r   r   r   r   rV      s    

rV   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)r3   fnr   r   r   r4      s   
zPickleCallableByPath.__init__c                 O   s   | j |i |S r   rc   )r3   r   r\   r   r   r   __call__   s   zPickleCallableByPath.__call__c                 C   s   t | j| jj| jjffS r   )rI   r   rd   r9   r8   r5   r   r   r   r^      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)	r8   r9   r:   r;   r4   rf   r^   r=   r   r   r   r   r   rb      s    rb   )r;   rh   r_   r   copyregrE   numbar   llvmliter   r	   r
   r   r   r.   r   r/   r?   rA   rC   rB   rH   rJ   rN   CloudPicklerr   rU   dispatch_tableABCrV   rb   r   r   r   r   <module>   s8    	
	
