o
    i.:                     @   sV  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mZmZmZmZmZmZmZmZ d dlZdZeejd eZej !ej "e#Z$dd	 Z%eeG d
d deZ&eG dd deZ'eeeG dd de'Z(eeeeG dd deZ)e*dkre+  dS dS )    N)skip)*)utils)	TestCasetagimport_dynamictemp_directoryhas_blasneeds_setuptoolsskip_if_py313plus_on_windowsskip_if_linux_aarch64skip_if_freethreadingzwindows onlywinc                   C   s   dt jv rt jd= dS dS )zVUnset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    MACOSX_DEPLOYMENT_TARGETN)osenviron r   r   S/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/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   sL   ddl m} tjdd d u}|r"tjdd d ur$| |  d S d S d S )Nr   )external_compiler_worksCONDA_BUILDVSINSTALLDIR)numba.pycc.platformr   r   r   get
assertTrue)selfr   is_running_conda_buildr   r   r   test_windows_compiler_validity,   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 )N	test_pycc)r   r   tmpdirtempfiletempdirr   r   r   r   setUp>   s   
zBasePYCCTest.setUpc                 C   s   d t _ddlm} |  d S )Nr   )clear_export_registry)r&   r'   numba.pycc.decoratorsr*   )r   r*   r   r   r   tearDownF   s   
zBasePYCCTest.tearDownc              
   c   sZ    t j| zt|}|V  W t j| t j|d  d S t j| t j|d  w N)syspathappendr   removemodulespop)r   extdirnamelibr   r   r   check_c_extQ   s   zBasePYCCTest.check_c_extN)r   r    r!   r)   r,   
contextlibcontextmanagerr7   r   r   r   r   r#   ;   s
    r#   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dd Zdd Zdd Zdd Zdd Z  ZS )TestCCc                    s8   t t|   |   ddlm} || _t| j d S )Nr   )compile_with_pycc)	superr:   r)   skip_if_no_external_compilernumba.testsr;   _test_module	importlibreload)r   r;   	__class__r   r   r)   a   s
   zTestCC.setUpc                 c   sN    | j |_|  | | j |j}|V  W d    d S 1 s w   Y  d S r-   )r%   
output_dircompiler7   r5   )r   ccr6   r   r   r   check_cc_compiledh   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
            )r5   r/    -c)
r   r   r/   dirname__file__strip
subprocess
check_callr.   
executable)r   r6   codeprologr   r   r   check_cc_compiled_in_subprocessq   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rT| |d| ddlm} | | | d S d S )Npycc_test_simplezpycc_test_simple.linuxz.sor   )find_pyext_ending)r?   rF   assertEqualr5   rD   r   r   r/   isdiroutput_fileassertFalseexistsbasename
startswithr.   platformendswithr   rU   assertIn)r   rF   dfrU   r   r   r   test_cc_properties   s   

zTestCC.test_cc_propertiesc              	   C   s   |  | jjW}|dd}| |d |dd}| |d |d}| |d | | d  | 	t
 |dd	 W d    n1 sIw   Y  W d    d S W d    d S 1 saw   Y  d S )
N{   A  ;  i  g    lVA         r   )rG   r?   rF   multiassertPreciseEqualmultfsquareassertIsget_noneassertRaisesZeroDivisionErrordiv)r   r6   resr   r   r   test_compile   s   
"zTestCC.test_compilec                 C   sf   | j j}||_| |}|dd}| |d | |jjd W d    d S 1 s,w   Y  d S )Nrc   rd   re   rS   )r?   rF   
target_cpurG   ri   rj   rV   r    )r   cpu_namerF   r6   rr   r   r   r   check_compile_for_cpu   s   "zTestCC.check_compile_for_cpuc                 C   s   |  t  d S r-   )rv   llget_host_cpu_namer(   r   r   r   test_compile_for_cpu   s   zTestCC.test_compile_for_cpuc                 C      |  d d S )Nhost)rv   r(   r   r   r   test_compile_for_cpu_host   s   z TestCC.test_compile_for_cpu_hostc                 C   s2  |  | jj}|dd}| |d ddtdfD ]}||}| |d qddtdfD ]}|	|}| |d	 q2|
d
}| |d | |d|d |d}tjd }| || |tdgd }| |d dd|i }| || W d    d S 1 sw   Y  d S )N         y              y              ?         @       @      ?g      <*   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)rG   r?   cc_helperlibpowerrj   np
complex128sqrtfloat64np_sqrtspacingassertNotEqualrandomRandomStaterandom_samplesizerR   r   r6   rr   valr   rP   r   r   r   test_compile_helperlib   s,   



	"zTestCC.test_compile_helperlibc                 C   s   |  | jjR}| |dd |d}| t|g d tr-|	d}| |d t
g d}||}t
|}| || dttd	 }| || W d    d S 1 s\w   Y  d S )
Nrh   g        r   )r   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	   )rG   r?   cc_nrtrj   zero_scalarzerosrV   listr	   
vector_dotr   r   
np_argsortargsortdictrR   r   r   r   r   test_compile_nrt   s    



"zTestCC.test_compile_nrtc                 C   sv   |  | jj*}| }| |td |d}| |td d}| || W d    d S 1 s4w   Y  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"))
                )rG   r?   r   hash_literal_str_Arj   hashhash_strrR   )r   r6   rr   rP   r   r   r   test_hashing   s   
"zTestCC.test_hashingc                 C   sb   |  | jj }tjdtjd}||}|| }| || W d    d S 1 s*w   Y  d S )Nr   dtype)rG   r?   r   r   arangeintpdict_usecaserj   )r   r6   arrgotexpectr   r   r   test_c_extension_usecase  s   
"zTestCC.test_c_extension_usecasec              	   C   s  |  | jj}tjdtjd}tjdtjd}||| | || tjdtjd}|	|| | || tjdtjd}tjdtjd}| 
t}||| W d   n1 s^w   Y  | d|j d|j t|j tjdtjd}tjdtjd}| 
t}|	|| W d   n1 sw   Y  | d|j d|j t|j W d   dS 1 sw   Y  dS )	zSee https://github.com/numba/numba/issues/9948

        Dynamic exception uses a symbol that in PYCC compilation must become
        linkonce_odr linkage to prevent symbol collision.
        )r}   r}   r   )r}   r   Nzcannot assign slice of shape z from input of shape )r      )r   rf   )rG   r?   	cc_dynexcr   r   r   onesdo_setitem1rj   do_setitem2ro   
ValueErrorr_   shapestr	exception)r   r6   abraisesr   r   r   test_dynamic_exc  s>   

"zTestCC.test_dynamic_exc)r   r    r!   r)   r8   r9   rG   rR   rb   rs   rv   ry   r|   r   r   r   r   r   __classcell__r   r   rB   r   r:   \   s    
	  r:   c                       sL   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )TestDistutilsSupportc                    sT   t    |   t  td| _tjt	d}tj| jd| _
t|| j
 d S )Ntest_pycc_distutilspycc_distutils_usecasework)r<   r)   r=   r   r   r%   r   r/   join	base_pathusecase_dirshutilcopytree)r   
source_dirrB   r   r   r)   :  s   

zTestDistutilsSupport.setUpc                       dd l }tjtjtj|j}ttj  ddr*|tj	  d   d< n| d<  fdd}||ddg d}|d	|g d S )
Nr   
PYTHONPATH c                    \   t jtjg|  jt jt j d}| \}}| }|dkr,	d|
dd  d S d S N)cwdstdoutstderrenvr   z+python failed with the following output:
%szutf-8ignorerM   Popenr.   rO   r   PIPESTDOUTcommunicatewaitfaildecodeargspout_rcr   r   r   r   
run_pythonR     

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]
            rI   
numbar   r/   abspathrJ   rK   r   r   r   pathsepr   setup_py_filer   
numba_pathr   rP   r   r   r   check_setup_pyG     
z#TestDistutilsSupport.check_setup_pyc                    r   )
Nr   r   r   c                    r   r   r   r   r   r   r   r   r  r   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]
            rI   r   r   r   r   r   check_setup_nested_pyg  r   z*TestDistutilsSupport.check_setup_nested_pyc                 C   rz   )Nzsetup_distutils.pyr   r(   r   r   r   test_setup_py_distutils     z,TestDistutilsSupport.test_setup_py_distutilsc                 C   rz   )Nzsetup_distutils_nested.pyr   r(   r   r   r   test_setup_py_distutils_nested  r   z3TestDistutilsSupport.test_setup_py_distutils_nestedc                 C   rz   )Nzsetup_setuptools.pyr   r(   r   r   r   test_setup_py_setuptools  r   z-TestDistutilsSupport.test_setup_py_setuptoolsc                 C   rz   )Nzsetup_setuptools_nested.pyr   r(   r   r   r   test_setup_py_setuptools_nested  r   z4TestDistutilsSupport.test_setup_py_setuptools_nested)r   r    r!   r)   r   r   r   r   r   r   r   r   r   rB   r   r   4  s      r   __main__),r8   r@   r   r   rM   r.   r&   unittestr   ctypesnumpyr   llvmlite.bindingbindingrw   
numba.corer   numba.tests.supportr   r   r   r   r	   r
   r   r   r   _skip_reasonskipIfr]   r\   r"   r/   rJ   r   rK   r   r   r   r#   r:   r   r   mainr   r   r   r   <module>   sL    ,  V\