o
    1&i,                     @   s   d dl Z d dl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	Z	d dl
mZ d dlmZ d dlmZ d dlmZmZ G dd deZG d	d
 d
eZG dd deZG dd deZdd Zdd Zdd Zdd Zedkrze  dS dS )    N)get_context)TypingError)TestCase)resolve_dispatcher_from_strdumpsloadsc                   @   s   e Zd Zdd Zej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dd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*TestDispatcherPicklingc                 O   s.   t tjd D ]}||g|R i | qd S )N   )rangepickleHIGHEST_PROTOCOL)selfmethargskwargsproto r   EC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_serialize.pyrun_with_protocols   s   z)TestDispatcherPickling.run_with_protocolsc                 c   s@    d}t |}|j}t|j||_z	d V  W ||_d S ||_w )Ncpu)r   Ztargetdescrtype)r   ZhwstrZdispatcher_clsZ	old_descrr   r   r   simulate_fresh_target   s   z,TestDispatcherPickling.simulate_fresh_targetc                    sd    fdd}|| t ||}  t |}|| W d    d S 1 s+w   Y  d S )Nc                    s@   t trttrj| g R   d S |    d S )N)
isinstancer   
issubclass	ExceptionassertRaisesZassertPreciseEqual)funcr   expected_resultr   r   r   check_result&   s
   
z7TestDispatcherPickling.check_call.<locals>.check_result)r   r   r   r   )r   r   r   r   r   r    picklednew_funcr   r   r   
check_call%   s   


"z!TestDispatcherPickling.check_callc                 C   s4   ddl m} | | j|dd | | j|dd d S )Nr
   )add_with_sig   r
      )333333?g@)serialize_usecasesr$   r   r#   )r   r$   r   r   r   test_call_with_sig4   s   z)TestDispatcherPickling.test_call_with_sigc                 C   sF   ddl m} | | j|dd | | j|dd | | j|dd d S )	Nr
   )add_without_sigr%   r&         @r(   g333333@abc)abc)r)   r+   r   r#   )r   r+   r   r   r   test_call_without_sig:   s   z,TestDispatcherPickling.test_call_without_sigc                 C   s<   ddl m} | | j|dd | | j|tt t f d S )Nr
   )add_nopythonr,   r-   )r)   r2   r   r#   r   object)r   r2   r   r   r   test_call_nopythonA   s   z)TestDispatcherPickling.test_call_nopythonc                 C   s"   ddl m} | | j|td d S )Nr
   )add_nopython_fail)r
      )r)   r5   r   r#   r   )r   r5   r   r   r   test_call_nopython_failG      z.TestDispatcherPickling.test_call_nopython_failc                 C   "   ddl m} | | j|dd d S )Nr
   )get_global_objmodeg      @)g      @)r)   r:   r   r#   )r   r:   r   r   r   test_call_objmode_with_globalL   s   z4TestDispatcherPickling.test_call_objmode_with_globalc                 C   s*   ddl m} |d}| | j|dd d S )Nr
   closure   )r6      )r)   r=   r   r#   )r   r=   innerr   r   r   test_call_closureP      z(TestDispatcherPickling.test_call_closurec                 K   s0   ddl m} |di |}| | j|dd d S )Nr
   )closure_with_globals      @g      @      @)rD   )r)   rC   r   r#   )r   Zjit_argsrC   r@   r   r   r   check_call_closure_with_globalsU   s   z6TestDispatcherPickling.check_call_closure_with_globalsc                 C      | j dd d S )NT)ZnopythonrG   r   r   r   r   'test_call_closure_with_globals_nopythonZ      z>TestDispatcherPickling.test_call_closure_with_globals_nopythonc                 C   rH   )NT)ZforceobjrI   rJ   r   r   r   &test_call_closure_with_globals_objmode]   rL   z=TestDispatcherPickling.test_call_closure_with_globals_objmodec                 C   *   ddl m} |d}| | j|dd d S )Nr
   )closure_calling_other_functionrD   g      &@)rF   g      @)r)   rO   r   r#   )r   rO   r@   r   r   r   (test_call_closure_calling_other_function`   rB   z?TestDispatcherPickling.test_call_closure_calling_other_functionc                 C   rN   )Nr
   closure_calling_other_closurerD   g       @rE   )r)   rR   r   r#   )r   rR   r@   r   r   r   'test_call_closure_calling_other_closuree   rB   z>TestDispatcherPickling.test_call_closure_calling_other_closurec                 C   r9   )Nr
   )dyn_func$   r>   )r)   rT   r   r#   )r   rT   r   r   r   test_call_dyn_funcj   r8   z)TestDispatcherPickling.test_call_dyn_funcc                 C   r9   )Nr
   )dyn_func_objmoderU   rV   )r)   rX   r   r#   )r   rX   r   r   r   test_call_dyn_func_objmodeo   r8   z1TestDispatcherPickling.test_call_dyn_func_objmodec                 C   s*   ddl m} |d}| | j||d d S )Nr
   )get_renamed_module        )r[   )r)   rZ   r   r#   )r   rZ   expectedr   r   r   test_renamed_modulet   s
   
z*TestDispatcherPickling.test_renamed_modulec                 C   sF   ddl m} |d}t|}djdi t }ttj	d|g dS )z}
        Check that reconstructing doesn't depend on resources already
        instantiated in the original process.
        r
   rQ   rD   zif 1:
            import pickle

            data = {pickled!r}
            func = pickle.loads(data)
            res = func(4.0)
            assert res == 8.0, res
            -cNr   )
r)   rR   r   r   formatlocals
subprocessr#   sys
executable)r   rR   r   r!   coder   r   r   test_other_process|   s   
z)TestDispatcherPickling.test_other_processc           	      C   s  ddl m} |d}t|}|d}t|}t|}t|}t|}| || | |ddd |  | |ddd	 | || | |ddd	 |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%   r>   r6   r?   
   r'         *   +         N)r)   r=   r   r   r   assertIsassertEqualZdisable_compileassertIsNotgcZcollect)	r   r=   r   r!   Zfunc2Zpickled2fghr   r   r   
test_reuse   s2   








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
        r^   N)ra   r#   rb   rc   )r   rd   r   r   r   test_imp_deprecation   s   z+TestDispatcherPickling.test_imp_deprecationN)__name__
__module____qualname__r   
contextlibcontextmanagerr   r#   r*   r1   r4   r7   r;   rA   rG   rK   rM   rP   rS   rW   rY   r]   re   rt   ru   r   r   r   r   r	      s,    
&r	   c                   @   s   e Zd Zdd ZdS )TestSerializationMiscc                 C   s`   ddl m} t }t|}t|}|t|||}| || |t|||}| || d S )Nr   )_numba_unpickle)	Znumba.core.serializer|   r3   r   r   hashidro   rm   )r   r|   Z
random_objZbytebufhashedZgot1Zgot2r   r   r   test_numba_unpickle   s   
z)TestSerializationMisc.test_numba_unpickleN)rv   rw   rx   r   r   r   r   r   r{      s    r{   c                   @   sD   e Zd ZdZdZdd Zee dkddd Zd	d
 Z	dd Z
dS )TestCloudPickleIssueszOThis test case includes issues specific to the cloudpickle implementation.
    Fc                    s\   G dd d  fdd} fdd}t  }|  |  t| |  t| |  d S )Nc                   @      e Zd ZdZdS )zITestCloudPickleIssues.test_dynamic_class_reset_on_unpickle.<locals>.KlassNrv   rw   rx   classvarr   r   r   r   Klass       r   c                      s
   d _ d S Nd   )r   r   r   r   r   mutator   s   
zKTestCloudPickleIssues.test_dynamic_class_reset_on_unpickle.<locals>.mutatorc                      s     jd d S r   )rn   r   r   r   r   r   r   check   s   zITestCloudPickleIssues.test_dynamic_class_reset_on_unpickle.<locals>.checkr   )r   r   r   savedr   r   r   $test_dynamic_class_reset_on_unpickle   s   
z:TestCloudPickleIssues.test_dynamic_class_reset_on_unpickle__main__z*Test cannot run as when module is __main__c                 C   s:   t d}|jtd}|  |jdd | |jd d S )Nspawn)target<   timeoutr   )r   Process"check_main_class_reset_on_unpicklestartjoinrn   exitcode)r   mpprocr   r   r   !test_main_class_reset_on_unpickle   s
   z7TestCloudPickleIssues.test_main_class_reset_on_unpicklec                 C   sT   G dd d}t |}td}|jt|fd}|  |jdd | |jd d S )Nc                   @   r   )zRTestCloudPickleIssues.test_dynamic_class_reset_on_unpickle_new_proc.<locals>.KlassNr   r   r   r   r   r      r   r   r   )r   r   r   r   r   )r   r   r   !check_unpickle_dyn_class_new_procr   r   rn   r   )r   r   r   r   r   r   r   r   -test_dynamic_class_reset_on_unpickle_new_proc   s   zCTestCloudPickleIssues.test_dynamic_class_reset_on_unpickle_new_procc                 C   s   t t}| | d d S )N)r   r   )numbaZnjit
issue_7356rn   )r   Zcfuncr   r   r   test_dynamic_class_issue_7356  s   
z3TestCloudPickleIssues.test_dynamic_class_issue_7356N)rv   rw   rx   __doc__Z_numba_parallel_test_r   unittestZskipIfr   r   r   r   r   r   r   r      s    

r   c                   @   r   )DynClassN)rv   rw   rx   r/   r   r   r   r   r     s    r   c                  C   sx   t jdd dt_tj} W d    n1 sw   Y  t jdd tj}W d    | |fS 1 s3w   Y  | |fS )NZintp)beforer   )after)r   Zobjmoder   r/   )r   r   r   r   r   r     s   
r   c                  C   sN   t jddd} | d }|jdksJ |jdksJ t|}d|_t|| d S )Nz"numba.tests.cloudpickle_main_classr   )run_namer   r   )runpy
run_modulerw   r   r   _check_dyn_class)Zglbsr   r   r   r   r   r     s   r   c                 C   s*   t | }|jdksJ d|_t||  d S r   )r   r   r   )r   r   r   r   r   r   0  s   r   c                    sR    fdd}|  t |}| urtd|  t |}| ur$td|  d S )Nc                      s    j dkr	tdd S )Nr   zCheck failed. Klass reset.)r   AssertionErrorr   r   r   r   r   :  s   
z_check_dyn_class.<locals>.checkzExpected reuse)r   r   )r   r   r   Zloadedr   r   r   r   9  s   
r   r   )ry   rp   r   r   ra   rb   r   multiprocessingr   r   Znumba.core.errorsr   Znumba.tests.supportr   Znumba.core.target_extensionr   Znumba.cloudpickler   r   r	   r{   r   r3   r   r   r   r   r   rv   mainr   r   r   r   <module>   s2     :6		