U
    ,d=                     @   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	T d dl
Zd dlm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mZ d d	lmZmZmZmZmZmZ d dlZzd dl Z W n e!k
r   dZ Y nX d
Z"e#ej$%d e"Z&ej'(ej')e*Z+dd Z,G dd deZ-G dd deZ.eG dd de.Z/eG dd de.Z0eG dd deZ1e2dkre  dS )    N)skip)*)utils)main)clear_export_registry)find_shared_endingfind_pyext_ending)_external_compiler_ok)TestCasetagimport_dynamictemp_directoryhas_blasneeds_external_compilerszwindows onlywinc                   C   s   dt jkrt jd= dS )zVUnset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    MACOSX_DEPLOYMENT_TARGETN)osenviron r   r   9/tmp/pip-unpacked-wheel-eu7e0c37/numba/tests/test_pycc.pyunset_macosx_deployment_target'   s    
r   c                   @   s   e Zd Zedd ZdS )TestCompilerChecksc                 C   s6   t jdd d k	}|r2t jdd d k	r2| t d S )NZCONDA_BUILDZVSINSTALLDIR)r   r   get
assertTruer	   )selfZis_running_conda_buildr   r   r   test_windows_compiler_validity1   s    z1TestCompilerChecks.test_windows_compiler_validityN)__name__
__module____qualname___windows_onlyr   r   r   r   r   r   .   s   r   c                   @   s*   e Zd Zdd Zdd Zejdd ZdS )BasePYCCTestc                 C   s   t   td| _| jt_d S )NZ	test_pycc)r   r   tmpdirtempfiletempdirr   r   r   r   setUp>   s    
zBasePYCCTest.setUpc                 C   s   d t _t  d S N)r"   r#   r   r$   r   r   r   tearDownF   s    zBasePYCCTest.tearDownc              
   c   s@   t j| zt|}|V  W 5 t j| t j|d  X d S r&   )syspathappendremovemodulespopr   )r   Zextdirnamelibr   r   r   check_c_extM   s    
zBasePYCCTest.check_c_extN)r   r   r   r%   r'   
contextlibcontextmanagerr0   r   r   r   r   r    <   s   r    c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestLegacyAPIc                 C   s   t jtd}dt  }t j| j|}t j|r>t | tdd||gd t	|}t
ttttg|j_t|j_t
ttttg|j_t|j_t }|t|ddd | |jd	 t }|t|dd
d | |jd dS )zE
        Test creating a C shared library object using pycc.
        compile_with_pycc.pyZtest_dll_legacy--debug-oargsN{   A  ;    i )r   r)   join	base_pathr   r!   existsunlinkr   CDLLPOINTERc_doublec_void_pZmultargtypesc_intrestypec_floatmultfbyrefassertEqualvalue)r   sourceZcdll_modulenameZ	cdll_pathr/   resr   r   r   test_pycc_ctypes_lib[   s.    

  z"TestLegacyAPI.test_pycc_ctypes_libc              	   C   s   |  d tjtd}d}tj| j|t  }tj|rHt| t	ddd||gd | 
| j|6}|dd	}| |d
 |dd	}| |d W 5 Q R X dS )zF
        Test creating a CPython extension module using pycc.
        z0lack of environment can make the extension crashr4   Ztest_pyext_legacyr5   z--pythonr6   r7   r9   r:   r;   r<       lVAN)ZskipTestr   r)   r=   r>   r!   r   r?   r@   r   r0   multiassertPreciseEqualrI   )r   rM   
modulenameZout_modulenamer/   rN   r   r   r   test_pycc_pymodulew   s    


z TestLegacyAPI.test_pycc_pymodulec              	   C   s   t jtd}t j| jd}t j|r4t | tddd||d gd t|d}|	 }W 5 Q R X d	}d
}| 
|||f| dS )z?
        Test creating a LLVM bitcode file using pycc.
        compile_with_pyccztest_bitcode_legacy.bcr5   z--llvmr6   z.pyr7   rbs   s   BCN)r   r)   r=   r>   r!   r?   r@   r   openreadr   
startswith)r   rS   Zbitcode_modulenamefZbcZbitcode_wrapper_magicZbitcode_magicr   r   r   test_pycc_bitcode   s    

zTestLegacyAPI.test_pycc_bitcodeN)r   r   r   rO   rT   r[   r   r   r   r   r3   X   s   r3   c                       s   e Zd Z f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eejdkohejdkddd Zdd Zdd Zdd Z  ZS )TestCCc                    s0   t t|   ddlm} || _t| j d S )Nr   )rU   )superr\   r%   Znumba.testsrU   _test_moduleimpreload)r   rU   	__class__r   r   r%      s    zTestCC.setUpc              	   c   s6   | j |_|  | | j |j}|V  W 5 Q R X d S r&   )r!   
output_dircompiler0   r.   )r   ccr/   r   r   r   check_cc_compiled   s    zTestCC.check_cc_compiledc                 C   s>   d|j tj|jd }|d| }ttj	d|g d S )Na  if 1:
            import sys
            import types
            # to disable numba package
            sys.modules['numba'] = types.ModuleType('numba')
            try:
                from numba import njit
            except ImportError:
                pass
            else:
                raise RuntimeError('cannot disable numba package')

            sys.path.insert(0, %(path)r)
            import %(name)s as lib
            )r.   r)    -c)
r   r   r)   dirname__file__strip
subprocess
check_callr(   
executable)r   r/   codeZprologr   r   r   check_cc_compiled_in_subprocess   s    z&TestCC.check_cc_compiled_in_subprocessc                 C   s   | j j}| |jd | j jj}| tj|| | j jj	}| 
tj|| | tj|d| tjdr| |d| | t | d S )Npycc_test_simplezpycc_test_simple.linuxz.so)r^   re   rK   r.   rc   r   r   r)   isdirZoutput_fileZassertFalser?   basenamerY   r(   platformendswithZassertInr   )r   re   drZ   r   r   r   test_cc_properties   s    

zTestCC.test_cc_propertiesc              
   C   s   |  | jj~}|dd}| |d |dd}| |d |d}| |d | | d  | 	t
 |dd	 W 5 Q R X W 5 Q R X d S )
Nr9   r:   r;   r<   rP            r   )rf   r^   re   rQ   rR   rI   ZsquareZassertIsZget_noneassertRaisesZeroDivisionErrordiv)r   r/   rN   r   r   r   test_compile   s    
zTestCC.test_compilec              	   C   sP   | j j}||_| |.}|dd}| |d | |jjd W 5 Q R X d S )Nr9   r:   r;   rq   )r^   re   Z
target_cpurf   rQ   rR   rK   r   )r   Zcpu_namere   r/   rN   r   r   r   check_compile_for_cpu   s    zTestCC.check_compile_for_cpuc                 C   s   |  t  d S r&   )r   llZget_host_cpu_namer$   r   r   r   test_compile_for_cpu   s    zTestCC.test_compile_for_cpuc                 C   s   |  d d S )Nhost)r   r$   r   r   r   test_compile_for_cpu_host   s    z TestCC.test_compile_for_cpu_hostdarwin))      )r      z4distutils incorrectly using gcc on python 3.8 buildsc              	   C   s  |  | jj}|dd}| |d ddtdfD ]}||}| |d q:ddtdfD ]}|	|}| |d	 qf|
d
}| |d | |d|d |d}tjd }| || |tdgd }| |d dd|i }| || W 5 Q R X d S )N   r      y              y              ?         @       @      ?g      <*   r   r   aR  if 1:
                from numpy.testing import assert_equal, assert_allclose
                res = lib.power(2, 7)
                assert res == 128
                res = lib.random(42)
                assert_allclose(res, %(expected)s)
                res = lib.spacing(1.0)
                assert_allclose(res, 2**-52)
                expected)rf   r^   Zcc_helperlibpowerrR   npZ
complex128sqrtfloat64Znp_sqrtspacingZassertNotEqualrandomZRandomStateZrandom_samplesizerp   r   r/   rN   valr   ro   r   r   r   test_compile_helperlib   s*    



	zTestCC.test_compile_helperlibc              	   C   s   |  | jj}| |dd |d}| t|dddg tr\|	d}| |d t
ddd	d
dg}||}t
|}| || dttd }| || W 5 Q R X d S )Nr{   g        r   r   r   g      >@r   g      @r   g      @r   aB  if 1:
                from numpy.testing import assert_equal
                from numpy import float64, argsort
                res = lib.zero_scalar(1)
                assert res == 0.0
                res = lib.zeros(3)
                assert list(res) == [0, 0, 0]
                if %(has_blas)s:
                    res = lib.vector_dot(4)
                    assert res == 30.0
                val = float64([2., 5., 1., 3., 4.])
                res = lib.np_argsort(val)
                expected = argsort(val)
                assert_equal(res, expected)
                )r   )rf   r^   cc_nrtrR   Zzero_scalarzerosrK   listr   Z
vector_dotr   r   Z
np_argsortZargsortdictrp   r   r   r   r   test_compile_nrt  s    



zTestCC.test_compile_nrtc              	   C   s`   |  | jjH}| }| |td |d}| |td d}| || W 5 Q R X d S )NAzif 1:
                from numpy.testing import assert_equal
                res = lib.hash_literal_str_A()
                assert_equal(res, hash("A"))
                res = lib.hash_str("A")
                assert_equal(res, hash("A"))
                )rf   r^   r   Zhash_literal_str_ArR   hashZhash_strrp   )r   r/   rN   ro   r   r   r   test_hashing7  s    
zTestCC.test_hashingc              	   C   sL   |  | jj4}tjdtjd}||}|| }| || W 5 Q R X d S )Nr   )Zdtype)rf   r^   r   r   ZarangeZintpZdict_usecaserR   )r   r/   Zarrgotexpectr   r   r   test_c_extension_usecaseG  s
    
zTestCC.test_c_extension_usecase)r   r   r   r%   r1   r2   rf   rp   rx   r   r   r   r   unittestskipIfr(   ru   r   Z	PYVERSIONr   r   r   r   __classcell__r   r   ra   r   r\      s"   
	
  r\   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze	e
dkddd Ze	e
dkddd ZdS )TestDistutilsSupportc                 C   sB   t   td| _tjtd}tj| jd| _t	|| j d S )NZtest_pycc_distutilsZpycc_distutils_usecaseZwork)
r   r   r!   r   r)   r=   r>   usecase_dirshutilcopytree)r   
source_dirr   r   r   r%   S  s
    
zTestDistutilsSupport.setUpc                    s   dd l }tjtjtj|j}ttj  ddrT|tj	  d   d< n| d<  fdd}||ddg d}|d	|g d S )
Nr   
PYTHONPATH c                    sX   t jtjg|  jt jt j d}| \}}| }|dkrT	d|
dd  d S N)cwdstdoutstderrenvr   z+python failed with the following output:
%szutf-8ignorerl   Popenr(   rn   r   PIPESTDOUTcommunicatewaitZfaildecoder8   pout_rcr   r   r   r   
run_pythonh  s    
z7TestDistutilsSupport.check_setup_py.<locals>.run_python	build_ext	--inplacezif 1:
            import pycc_compiled_module as lib
            assert lib.get_const() == 42
            res = lib.ones(3)
            assert list(res) == [1.0, 1.0, 1.0]
            rh   
numbar   r)   abspathri   rj   r   r   r   pathsepr   Zsetup_py_filer   Z
numba_pathr   ro   r   r   r   check_setup_py]  s    
z#TestDistutilsSupport.check_setup_pyc                    s   dd l }tjtjtj|j}ttj  ddrT|tj	  d   d< n| d<  fdd}||ddg d}|d	|g d S )
Nr   r   r   c                    sX   t jtjg|  jt jt j d}| \}}| }|dkrT	d|
dd  d S r   r   r   r   r   r   r     s    
z>TestDistutilsSupport.check_setup_nested_py.<locals>.run_pythonr   r   zif 1:
            import nested.pycc_compiled_module as lib
            assert lib.get_const() == 42
            res = lib.ones(3)
            assert list(res) == [1.0, 1.0, 1.0]
            rh   r   r   r   r   r   check_setup_nested_py}  s    
z*TestDistutilsSupport.check_setup_nested_pyc                 C   s   |  d d S )Nzsetup_distutils.pyr   r$   r   r   r   test_setup_py_distutils  s    z,TestDistutilsSupport.test_setup_py_distutilsc                 C   s   |  d d S )Nzsetup_distutils_nested.pyr   r$   r   r   r   test_setup_py_distutils_nested  s    z3TestDistutilsSupport.test_setup_py_distutils_nestedNztest needs setuptoolsc                 C   s   |  d d S )Nzsetup_setuptools.pyr   r$   r   r   r   test_setup_py_setuptools  s    z-TestDistutilsSupport.test_setup_py_setuptoolsc                 C   s   |  d d S )Nzsetup_setuptools_nested.pyr   r$   r   r   r   test_setup_py_setuptools_nested  s    z4TestDistutilsSupport.test_setup_py_setuptools_nested)r   r   r   r%   r   r   r   r   r   r   
setuptoolsr   r   r   r   r   r   r   P  s   
  
r   __main__)3r1   r_   r   r   rl   r(   r"   r   r   ctypesZnumpyr   Zllvmlite.bindingZbindingr   Z
numba.corer   Z
numba.pyccr   Znumba.pycc.decoratorsr   Znumba.pycc.platformr   r   r	   Znumba.tests.supportr
   r   r   r   r   r   r   ImportErrorZ_skip_reasonr   ru   rY   r   r)   ri   r   rj   r>   r   r   r    r3   r\   r   r   r   r   r   r   <module>   sL    
H /[
