o
    0 i3                     @   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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G d
d deZ$G dd deZ%eeG dd de%Z&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_py313_on_windowsz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   a/home/app/PaddleOCR-VL-test/.venv_paddleocr/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_worksZCONDA_BUILDZVSINSTALLDIR)numba.pycc.platformr   r   r   get
assertTrue)selfr   Z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 )NZ	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!   Znumba.pycc.decoratorsr$   )r   r$   r   r   r   tearDownC   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   Zextdirnamelibr   r   r   check_c_extN   s   zBasePYCCTest.check_c_extN)r   r   r   r#   r%   
contextlibcontextmanagerr/   r   r   r   r   r   9   s
    r   c                       sz   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  ZS )TestCCc                    s8   t t|   |   ddlm} || _t| j d S )Nr   )compile_with_pycc)	superr2   r#   skip_if_no_external_compilerZnumba.testsr3   _test_module	importlibreload)r   r3   	__class__r   r   r#   ]   s
   zTestCC.setUpc                 c   sN    | j |_|  | | j |j}|V  W d    d S 1 s w   Y  d S r&   )r   
output_dircompiler/   r-   )r   ccr.   r   r   r   check_cc_compiledd   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.   codeprologr   r   r   check_cc_compiled_in_subprocessm   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)r6   r=   assertEqualr-   r;   r   r   r(   isdirZoutput_fileZassertFalseexistsbasename
startswithr'   platformendswithr   rL   ZassertIn)r   r=   dfrL   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   )r>   r6   r=   multiassertPreciseEqualZmultfZsquareZassertIsZget_noneassertRaisesZeroDivisionErrordiv)r   r.   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 )NrW   rX   rY   rJ   )r6   r=   Z
target_cpur>   r]   r^   rM   r   )r   Zcpu_namer=   r.   rb   r   r   r   check_compile_for_cpu   s   "zTestCC.check_compile_for_cpuc                 C   s   |  t  d S r&   )rd   llZget_host_cpu_namer"   r   r   r   test_compile_for_cpu   s   zTestCC.test_compile_for_cpuc                 C      |  d d S )Nhost)rd   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)r>   r6   Zcc_helperlibpowerr^   npZ
complex128sqrtfloat64Znp_sqrtspacingZassertNotEqualrandomZRandomStateZrandom_samplesizerI   r   r.   rb   valrt   rG   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 )
Nr\   g        rs   )r   r   r   rn   g      >@)rp   g      @rq   g      @ro   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	   )r>   r6   cc_nrtr^   Zzero_scalarZzerosrM   listr	   Z
vector_dotrv   rx   Z
np_argsortZargsortdictrI   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"))
                )r>   r6   r   Zhash_literal_str_Ar^   hashZhash_strrI   )r   r.   rb   rG   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 )Nrl   )Zdtype)r>   r6   r   rv   ZarangeZintpZdict_usecaser^   )r   r.   Zarrgotexpectr   r   r   test_c_extension_usecase  s   
"zTestCC.test_c_extension_usecase)r   r   r   r#   r0   r1   r>   rI   rV   rc   rd   rf   ri   r~   r   r   r   __classcell__r   r   r9   r   r2   Y   s    
	  r2   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 )NZtest_pycc_distutilsZpycc_distutils_usecaseZwork)r4   r#   r5   r   r   r   r   r(   join	base_pathusecase_dirshutilcopytree)r   
source_dirr9   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rD   Popenr'   rF   r   PIPESTDOUTcommunicatewaitZfaildecodeargspout_rcr   r   r   r   
run_python&     

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]
            r@   
numbar   r(   abspathrA   rB   r   r   r   pathsepr   Zsetup_py_filer   Z
numba_pathr   rG   r   r   r   check_setup_py     
z#TestDistutilsSupport.check_setup_pyc                    r   )
Nr   r   r   c                    r   r   r   r   r   r   r   r   F  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]
            r@   r   r   r   r   r   check_setup_nested_py;  r   z*TestDistutilsSupport.check_setup_nested_pyc                 C   rg   )Nzsetup_distutils.pyr   r"   r   r   r   test_setup_py_distutils[     z,TestDistutilsSupport.test_setup_py_distutilsc                 C   rg   )Nzsetup_distutils_nested.pyr   r"   r   r   r   test_setup_py_distutils_nested^  r   z3TestDistutilsSupport.test_setup_py_distutils_nestedc                 C   rg   )Nzsetup_setuptools.pyr   r"   r   r   r   test_setup_py_setuptoolsa  r   z-TestDistutilsSupport.test_setup_py_setuptoolsc                 C   rg   )Nzsetup_setuptools_nested.pyr   r"   r   r   r   test_setup_py_setuptools_nestedd  r   z4TestDistutilsSupport.test_setup_py_setuptools_nested)r   r   r   r#   r   r   r   r   r   r   r   r   r   r9   r   r   
  s      r   __main__)*r0   r7   r   r   rD   r'   r    Zunittestr   ctypesnumpyrv   Zllvmlite.bindingZbindingre   Z
numba.corer   Znumba.tests.supportr   r   r   r   r	   r
   r   Z_skip_reasonZskipIfrR   rQ   r   r(   rA   r   rB   r   r   r   r   r2   r   r   mainr   r   r   r   <module>   sB    $  0\