B
     \g                 @   s   d dl mZmZm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lmZ ddlmZmZ ddlT G d	d
 d
eZedkre  dS )    )print_functionabsolute_importdivisionN)unittest_support)TypingError)registry   )TestCasetag)*c               @   s   e Zd Zdd Zejdd Zdd Zeddd	 Z	edd
d Z
eddd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zedd&d' Zedd(d) Zd*d+ Zd,S )-TestDispatcherPicklingc             O   s,   x&t tjd D ]}||f|| qW d S )Nr   )rangepickleZHIGHEST_PROTOCOL)selfmethargskwargsproto r   9lib/python3.7/site-packages/numba/tests/test_serialize.pyrun_with_protocols   s    z)TestDispatcherPickling.run_with_protocolsc             c   s6   t jd }|j}t|j |_z
d V  W d ||_X d S )NZcpu)r   Zdispatcher_registryZtargetdescrtype)r   Zdispatcher_clsZ	old_descrr   r   r   simulate_fresh_target   s    

z,TestDispatcherPickling.simulate_fresh_targetc          	      sN    fdd}|| t ||}  t |}|| W d Q R X d S )Nc                s<   t tr(ttr(j| f   n|    d S )N)
isinstancer   
issubclass	ExceptionZassertRaisesZassertPreciseEqual)func)r   expected_resultr   r   r   check_result$   s    

z7TestDispatcherPickling.check_call.<locals>.check_result)r   dumpsr   loads)r   r   r   r   r   r   picklednew_funcr   )r   r   r   r   
check_call#   s    

z!TestDispatcherPickling.check_callZ	importantc             C   s(   |  | jtdd |  | jtdd d S )N   )r      )g333333?g@)r   r#   Zadd_with_sig)r   r   r   r   test_call_with_sig2   s    z)TestDispatcherPickling.test_call_with_sigc             C   s:   |  | jtdd |  | jtdd |  | jtdd d S )Nr$   )r   r%   g      @)g333333?g333333@abc)aZbc)r   r#   Zadd_without_sig)r   r   r   r   test_call_without_sig8   s    z,TestDispatcherPickling.test_call_without_sigc             C   s0   |  | jtdd |  | jttt t f d S )Ng      @)g333333?g333333@)r   r#   Zadd_nopythonr   object)r   r   r   r   test_call_nopython?   s    z)TestDispatcherPickling.test_call_nopythonc             C   s   |  | jttd d S )N)r      )r   r#   Zadd_nopython_failr   )r   r   r   r   test_call_nopython_failE   s    z.TestDispatcherPickling.test_call_nopython_failc             C   s   |  | jtdd d S )Ng      @)g      @)r   r#   Zget_global_objmode)r   r   r   r   test_call_objmode_with_globalI   s    z4TestDispatcherPickling.test_call_objmode_with_globalc             C   s   t d}| | j|dd d S )Nr      )r,      )closurer   r#   )r   innerr   r   r   test_call_closureL   s    z(TestDispatcherPickling.test_call_closurec             K   s    t d|}| | j|dd d S )N      @g      @)g      @)r4   )Zclosure_with_globalsr   r#   )r   Zjit_argsr2   r   r   r   check_call_closure_with_globalsP   s    
z6TestDispatcherPickling.check_call_closure_with_globalsc             C   s   | j dd d S )NT)Znopython)r5   )r   r   r   r   'test_call_closure_with_globals_nopythonT   s    z>TestDispatcherPickling.test_call_closure_with_globals_nopythonc             C   s   | j dd d S )NT)Zforceobj)r5   )r   r   r   r   &test_call_closure_with_globals_objmodeW   s    z=TestDispatcherPickling.test_call_closure_with_globals_objmodec             C   s   t d}| | j|dd d S )Ng      @g      &@)g      @g      @)Zclosure_calling_other_functionr   r#   )r   r2   r   r   r   (test_call_closure_calling_other_functionZ   s    z?TestDispatcherPickling.test_call_closure_calling_other_functionc             C   s   t d}| | j|dd d S )Ng      @g       @)g      @)closure_calling_other_closurer   r#   )r   r2   r   r   r   'test_call_closure_calling_other_closure^   s    z>TestDispatcherPickling.test_call_closure_calling_other_closurec             C   s   |  | jtdd d S )N$   )r/   )r   r#   Zdyn_func)r   r   r   r   test_call_dyn_funcb   s    z)TestDispatcherPickling.test_call_dyn_funcc             C   s   |  | jtdd d S )Nr;   )r/   )r   r#   Zdyn_func_objmode)r   r   r   r   test_call_dyn_func_objmodef   s    z1TestDispatcherPickling.test_call_dyn_func_objmodec             C   s   t d}| | jt |d d S )Ng        )g        )Zget_renamed_moduler   r#   )r   Zexpectedr   r   r   test_renamed_modulej   s    
z*TestDispatcherPickling.test_renamed_modulec             C   s(   |  | jtdd |  | jtdd d S )N.   )r   r,   y      @      ?)y              ?r,   )r   r#   Zgenerated_add)r   r   r   r   test_call_generatedq   s    

z*TestDispatcherPickling.test_call_generatedc             C   s6   t d}t|}djf t }ttjd|g dS )z}
        Check that reconstructing doesn't depend on resources already
        instantiated in the original process.
        g      @zif 1:
            import pickle

            data = {pickled!r}
            func = pickle.loads(data)
            res = func(4.0)
            assert res == 8.0, res
            z-cN)	r9   r   r   formatlocals
subprocessr#   sys
executable)r   r   r!   coder   r   r   test_other_processw   s    
z)TestDispatcherPickling.test_other_processc             C   s  t d}t|}t d}t|}t|}t|}t|}| || | |ddd |  | |ddd | || | |ddd t d}|d	d
 t|}~t	  t|}t|}| || | |ddd |  | |ddd dS )z
        Check that deserializing the same function multiple times re-uses
        the same dispatcher object.

        Note that "same function" is intentionally under-specified.
        r$   r/   r,   r0   
   r%         *   +         N)
r1   r   r   r    ZassertIsZassertEqualZdisable_compileZassertIsNotgcZcollect)r   r   r!   Zfunc2Zpickled2fghr   r   r   
test_reuse   s0    








z!TestDispatcherPickling.test_reusec             C   s   d}t tjd|g dS )zN
        The imp module was deprecated in v3.4 in favour of importlib
        a  if 1:
            import pickle
            import warnings
            with warnings.catch_warnings(record=True) as w:
                warnings.simplefilter('always', DeprecationWarning)
                from numba import njit
                @njit
                def foo(x):
                    return x + 1
                foo(1)
                serialized_foo = pickle.dumps(foo)
            for x in w:
                if 'serialize.py' in x.filename:
                    assert "the imp module is deprecated" not in x.msg
        z-cN)rC   r#   rD   rE   )r   rF   r   r   r   test_imp_deprecation   s    z+TestDispatcherPickling.test_imp_deprecationN)__name__
__module____qualname__r   
contextlibcontextmanagerr   r#   r
   r&   r)   r+   r-   r.   r3   r5   r6   r7   r8   r:   r<   r=   r>   r@   rG   rS   rT   r   r   r   r   r      s*   &r   __main__)Z
__future__r   r   r   rX   rO   r   rC   rD   Znumbar   ZunittestZnumba.errorsr   Znumba.targetsr   Zsupportr	   r
   Zserialize_usecasesr   rU   mainr   r   r   r   <module>   s    5