o
    8ήc>                     @   s   d dl T 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
mZ d dlmZ d dlmZmZmZ d dlT d dlZG dd	 d	eZG d
d deeZedkrWe  dS dS )    )*N)compile_isolated)jit)typeserrors)ctypes_utils)MemoryLeakMixintagTestCasec                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestCTypesTypesc                 C   s   |t tj |ttj |ttj |ttj |t	tj
 |ttj |ttttj |ttttttj |d tj d S N)c_doubler   float64c_intintcc_uint16uint16c_size_tsize_t	c_ssize_tssize_tc_void_pvoidptrPOINTERc_floatCPointerfloat32void)selfcheck r    >/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/test_ctypes.py_conversion_tests   s   z!TestCTypesTypes._conversion_testsc                    s`    fdd}  |  t}tt W d   n1 s w   Y   dt|j dS )z@
        Test converting a ctypes type to a Numba type.
        c                    s   t | } || d S r   )r   from_ctypesassertEqualctytygotr   r    r!   r   %      
z/TestCTypesTypes.test_from_ctypes.<locals>.checkNzUnsupported ctypes type)	r"   assertRaises	TypeErrorr   r#   	c_wchar_passertInstr	exceptionr   r   raisesr    r)   r!   test_from_ctypes!   s   
z TestCTypesTypes.test_from_ctypesc                    sb    fdd}  |  t}ttj W d   n1 s!w   Y   dt|j	 dS )z@
        Test converting a Numba type to a ctypes type.
        c                    s   t |} ||  d S r   )r   	to_ctypesr$   r%   r)   r    r!   r   4   r*   z-TestCTypesTypes.test_to_ctypes.<locals>.checkNz.Cannot convert Numba type '...' to ctypes type)
r"   r+   r,   r   r4   r   ellipsisr.   r/   r0   r1   r    r)   r!   test_to_ctypes0   s   
zTestCTypesTypes.test_to_ctypesN)__name__
__module____qualname__r"   r3   r6   r    r    r    r!   r      s    r   c                   @   s   e Zd Zdd Zdd Ze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S )TestCTypesUseCasesc                 C   4   t }t|tjg}|j}d}| |||| d S NgQ	@)	use_c_sinr   r   doubleentry_pointr$   r   pyfunccrescfuncxr    r    r!   
test_c_sinC   
   zTestCTypesUseCases.test_c_sinc                 C   r;   r<   )use_two_funcsr   r   r>   r?   r$   r@   r    r    r!   test_two_funcsJ   s
   z!TestCTypesUseCases.test_two_funcszWindows-specific testc                 C   s    t ttjg}|j}|d d S )N   )r   use_c_sleepr   uintcr?   )r   rB   rC   r    r    r!   test_stdcallR   s   zTestCTypesUseCases.test_stdcallc                 C   r;   r<   )use_ctype_wrappingr   r   r>   r?   r$   r@   r    r    r!   test_ctype_wrappingY   rF   z&TestCTypesUseCases.test_ctype_wrappingc                 C   s4   t }t|tjg}|j}d}| |||d  d S )N{   rI   )use_c_pointerr   r   int32r?   r$   r@   r    r    r!   test_ctype_voidptr`   s
   z%TestCTypesUseCases.test_ctype_voidptrc                 C   s   t }tdd|}ttdfttdfttdfttdffD ]\}}}||||}||||}| || q| t|jd|j d S )NTnopythong      ?g      rI   )use_func_pointerr   c_sinc_cosr$   len	overloads)r   rA   rC   fafbrD   expectedr(   r    r    r!   test_function_pointerh   s   z(TestCTypesUseCases.test_function_pointerc                 C   sN   |  t}tttjg W d    n1 sw   Y  | dt|j d S )NzCctypes function '_numba_test_exp' doesn't define its argument types)	r+   r,   r   use_c_untypedr   r>   r.   r/   r0   )r   r2   r    r    r!   test_untyped_functionv   s   z(TestCTypesUseCases.test_untyped_functionc                    sf   ddi  fdd}t tt|fdd}tddd|}tjdgd	d
}| |||| d S )Nwhati c                    s    | d  d S )Nr   ascii)decodearr)mydctr    r!   call_me_maybe   s   z?TestCTypesUseCases.test_python_call_back.<locals>.call_me_maybec                    s    | }|S r   r    )ar`   )py_call_backr    r!   rA      s   z8TestCTypesUseCases.test_python_call_back.<locals>.pyfuncTrT   nogilS10dtype)	CFUNCTYPEr   	py_objectr   nparrayr$   )r   rf   rA   rC   rd   r    )re   rh   r!   test_python_call_back|   s   z(TestCTypesUseCases.test_python_call_backc                    s   dd }t ddd|tjdgdd d| }g   fd	d
 fddtdD }|D ]}|  q8|D ]}|  qAD ]}| || qJd S )Nc                 S   s"   d}t |D ]}|t| 7 }q|S Nr   )rangerh   )rg   repeatout_r    r    r!   rA      s   zATestCTypesUseCases.test_python_call_back_threaded.<locals>.pyfuncTri   r`   rk   rl   i  c                    s     | || d S r   )append)funcrd   ru   )outputsr    r!   run   s   z>TestCTypesUseCases.test_python_call_back_threaded.<locals>.runc                    s    g | ]}t j fd qS ))targetargs)	threadingThread).0rw   )rd   rC   ru   r{   r    r!   
<listcomp>   s    zETestCTypesUseCases.test_python_call_back_threaded.<locals>.<listcomp>
   )r   rp   rq   rt   startjoinr$   )r   rA   r\   threadsthr(   r    )rd   rC   rz   ru   r{   r!   test_python_call_back_threaded   s$   



z1TestCTypesUseCases.test_python_call_back_threadedc                 C   sB   dd }t ddd|}td}||}||}| || dS )zn
        Test the ".ctypes.data" attribute of an array can be passed
        as a "void *" parameter.
        c                 S   s   t | jjS r   )c_take_array_ptrctypesdatarc   r    r    r!   rA      s   zATestCTypesUseCases.test_passing_array_ctypes_data.<locals>.pyfuncTri      N)r   rp   aranger$   r   rA   rC   rd   r\   r(   r    r    r!   test_passing_array_ctypes_data   s   
z1TestCTypesUseCases.test_passing_array_ctypes_datac                 C   s<   t dd|}tddd}|d }||}| || |S )NTrS   r   r   r   g       @)r   rp   linspaceassertPreciseEqualr   r    r    r!   check_array_ctypes   s   z%TestCTypesUseCases.check_array_ctypesc                 C   s   |  t dS )zi
        Test the ".ctypes" attribute of an array can be passed
        as a "void *" parameter.
        N)r   use_c_vsquarer)   r    r    r!   !test_passing_array_ctypes_voidptr   s   z4TestCTypesUseCases.test_passing_array_ctypes_voidptrc                 C   s\   |  t}| tj}|tdg W d   n1 sw   Y  | dt|j	 dS )zz
        Test the ".ctypes" attribute of an array can be passed
        as a pointer parameter of the right type.
        g        Nz5No implementation of function ExternalFunctionPointer)
r   use_c_vcuber+   r   TypingErrorrp   r   r.   r/   r0   )r   rC   r2   r    r    r!   *test_passing_array_ctypes_voidptr_pass_ptr   s   
z=TestCTypesUseCases.test_passing_array_ctypes_voidptr_pass_ptrc           	         s   t t}|dd   fdd}t|tjd d d g}|j}tjdtjd}| }||}||}| 	|d | 
|| | 
|| d S )Nc                   S   s   dS )N   >[= r    r    r    r    r!   get_voidstar   s   zJTestCTypesUseCases.test_storing_voidptr_to_int_array.<locals>.get_voidstarc                    s     }|| d< |S rs   r    )rg   ptrr   r    r!   rA      s   zDTestCTypesUseCases.test_storing_voidptr_to_int_array.<locals>.pyfuncrI   rl   r   )rn   r   r   r   uintpr?   rp   zeroscopyr$   r   )	r   cprotorA   rB   rC   arr_got
arr_expectret_got
ret_expectr    r   r!   !test_storing_voidptr_to_int_array   s   
z4TestCTypesUseCases.test_storing_voidptr_to_int_arrayN)r7   r8   r9   rE   rH   unittest
skipUnless
is_windowsrL   rN   rR   r]   r_   rr   r   r   r   r   r   r   r    r    r    r!   r:   A   s     

'	r:   __main__)r   sysr~   numpyrp   numba.core.compilerr   numbar   
numba.corer   r   numba.core.typingr   numba.tests.supportr   r	   r
   numba.tests.ctypes_usecasesr   r   r:   r7   mainr    r    r    r!   <module>   s"    0 C