B
      ›\I  ã               @   sÐ   d Z ddlmZmZmZ ddlmZ edk r@ddlmZ	 e	ƒ Z
nddlmZ
 ddlZddlZdd	lmZmZ dd
lmZmZ dd„ ZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )z/
Serialization support for compiled functions.
é    )Úprint_functionÚdivisionÚabsolute_importé   )Ú	PYVERSION)é   r   )Ú	get_magic)ÚMAGIC_NUMBERN)ÚFunctionTypeÚ
ModuleType)ÚbytecodeÚcompilerc             G   s
   | j |Ž S )zM
    Global hook to rebuild a given class from its __reduce__ arguments.
    )Z_rebuild)ÚclsÚargs© r   ú.lib/python3.7/site-packages/numba/serialize.pyÚ_rebuild_reduction   s    r   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú
_ModuleRefc             C   s
   || _ d S )N)Úname)Úselfr   r   r   r   Ú__init__$   s    z_ModuleRef.__init__c             C   s   t | jffS )N)Ú_rebuild_moduler   )r   r   r   r   Ú
__reduce__'   s    z_ModuleRef.__reduce__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   "   s   r   c             C   s"   | d krt dƒ‚t| ƒ tj|  S )Nzcannot import None)ÚImportErrorÚ
__import__ÚsysÚmodules)r   r   r   r   r   +   s    r   c             C   sZ   t j | ¡}t  |¡}| ¡ }x,| ¡ D ] \}}t|tƒr(t|j	ƒ||< q(W | j
|d< |S )z]
    Analyse *func* and return a dictionary of global values suitable for
    reduction.
    r   )r   ZFunctionIdentityZfrom_functionZByteCodeZget_used_globalsÚitemsÚ
isinstancer   r   r   r   )ÚfuncZfunc_idZbcÚglobsÚkÚvr   r   r   Ú#_get_function_globals_for_reduction2   s    


r&   c             C   s0   | j rdd„ | j D ƒ}nd}t| jƒ|| j|fS )z¬
    Reduce a Python function and its globals to picklable components.
    If there are cell variables (i.e. references to a closure), their
    values will be frozen.
    c             S   s   g | ]
}|j ‘qS r   )Úcell_contents)Ú.0Zcellr   r   r   ú
<listcomp>J   s    z$_reduce_function.<locals>.<listcomp>N)Ú__closure__Ú_reduce_codeÚ__code__r   )r"   r#   Úcellsr   r   r   Ú_reduce_functionC   s    r.   c             C   s   t jtt  | ¡fS )z7
    Reduce a code object to picklable components.
    )ÚmarshalÚversionÚbc_magicÚdumps)Úcoder   r   r   r+   O   s    r+   c                s   ‡ fdd„S )z=
    A dummy function allowing us to build cell objects.
    c                  s   ˆ S )Nr   r   )Úxr   r   Ú<lambda>Y   s    z _dummy_closure.<locals>.<lambda>r   )r4   r   )r4   r   Ú_dummy_closureU   s    r6   c             C   sd   |rt dd„ |D ƒƒ}nd}t| Ž }|d }yt|ƒ W n tk
rR   |d= Y nX t|||d|ƒS )zA
    Rebuild a function from its _reduce_function() results.
    c             s   s   | ]}t |ƒjd  V  qdS )r   N)r6   r*   )r(   r%   r   r   r   ú	<genexpr>`   s    z$_rebuild_function.<locals>.<genexpr>r   r   )ÚtupleÚ_rebuild_coder   r   r
   )Zcode_reducedÚglobalsr   Zcell_valuesr-   r3   Úmodnamer   r   r   Ú_rebuild_function[   s    r<   c             C   s6   t j| krtdt j| f ƒ‚t|kr,tdƒ‚t  |¡S )z@
    Rebuild a code object from its _reduce_code() results.
    zHincompatible marshal version: interpreter has %r, marshalled code has %rzincompatible bytecode version)r/   r0   ÚRuntimeErrorr1   Úloads)Zmarshal_versionZbytecode_magicZ
marshalledr   r   r   r9   m   s    
r9   )Ú__doc__Z
__future__r   r   r   Zutilsr   Úimpr   Z
_get_magicr1   Úimportlib.utilr	   r/   r   Útypesr
   r   Ú r   r   r   Úobjectr   r   r&   r.   r+   r6   r<   r9   r   r   r   r   Ú<module>   s&   	