o
    1ήc0                     @   s  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m	Z	m
Z
 d dlmZ d dlmZmZmZmZ zd dlZW n eyC   dZY nMw dZdZee druz
e	dejjjZW n	 ey`   Y nw z
e	dejjjZW n	 eyt   Y nw edu re	d	ejjjZedu re	dejjjZejZejjedu d
dejje j dkddG dd dZ!G dd dZ"ejjedu ddG dd dZ#ejjedu ddG dd dZ$ejjedu ddG dd dZ%dS )    N)Path)	ndpointerload_libraryas_array)get_shared_lib_extension)assert_assert_array_equalassert_raisesassert_equalgettotalrefcount_multiarray_umath_d_multiarray_tests_multiarray_umathz#ctypes not available in this python)reasoncygwinzKnown to fail on cygwinc                   @   s   e Zd Zdd Zdd ZdS )TestLoadLibraryc                 C   st   t jjj}td|}ttd|}tdt|}td|}t|tjs%J ||  u r5|  u r5|u s8J  J d S )Nr   s   _multiarray_umath)	npcorer   __file__r   r   
isinstancectypesCDLL)selfloader_pathout1out2out3out4 r   A/tmp/pip-target-vg8gfxp4/lib/python/numpy/tests/test_ctypeslib.py
test_basic(   s   


*zTestLoadLibrary.test_basicc              
   C   s   z#zt dd}td| tjjj W W d S  ty#   td Y W d S w  ty@ } zdt| }t| W Y d }~d S d }~ww )NT)is_python_extz_multiarray_umath%sz&No distutils available, skipping test.zPctypes is not available on this python: skipping the test (import error was: %s))	r   r   r   r   r   r   ImportErrorprintstr)r   soemsgr   r   r   test_basic23   s   
zTestLoadLibrary.test_basic2N)__name__
__module____qualname__r    r(   r   r   r   r   r   #   s    r   c                   @   4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestNdpointerc              	   C   sT  t j}t|d}t|t dg| d}t|d}t|t dg| t d}t|d}|t dg| tt|jt dg|	d ddg}t jt j
g}||d}t |}t|d}t|t d	| t |}t|d}t|t d	| t j|d
d}|j|jkrtt|jt d	| d S t|t d	| d S )Ndtype   <i4z>i4swapxy)namesformats
   Talign)r   intcr   r   
from_paramarrayr/   r	   	TypeErrornewbyteorderfloat64zerositemsize)r   dtpdtnames	dtformatsdtdescrsamedtdt2r   r   r   
test_dtypeD   s2   








zTestNdpointer.test_dtypec                 C   s   t dd}t|td tt|jtdg t dd}tt|jtd t|tdg t dd}t|tdgg d S )Nr   ndimr0      r   r   r<   r   r=   r	   r>   r   rD   r   r   r   	test_ndim_   s   


zTestNdpointer.test_ndimc                 C   sb   t dd}t|tddgg tt|jtdgdgg t dd}t|td d S )N)r0   rM   shaper0   rM   r   rN   rO   r   r   r   
test_shapei   s
   

zTestNdpointer.test_shapec                 C   s   t jddgddggdd}tdd}t|| td	d}tt|j| t|jjd}t|| tt|jt ddgddgg d S )
Nr0   rM         F)orderFORTRAN)flags
CONTIGUOUS)	r   r=   r   r   r<   r	   r>   rY   num)r   r3   rD   r   r   r   
test_flagsp   s   

$zTestNdpointer.test_flagsc                 C   sh   t ttjdttjdu  t tddtddu  t tddtddu t tddtddu d S )Nr.   rM   rQ   rM   rK   )r   r   r   r@   r   r   r   r   
test_cachez   s   zTestNdpointer.test_cacheN)r)   r*   r+   rJ   rP   rS   r\   r_   r   r   r   r   r-   C   s    

r-   z0ctypes not available on this python installationc                   @   s\   e Zd Zdd Zejjdee	e
ddgddgddgd	d
gddgddd Zdd ZdS )TestNdpointerCFuncc                 C   s<   t jt_tddft_ttd tt j	ttd dS )z- Test that arguments are coerced from arrays rM   rK   rM   rT   )rM   rT   rU   N)
r   c_void_pc_forward_pointerrestyper   argtypesr   rA   r	   ArgumentErrorr^   r   r   r   test_arguments   s   z!TestNdpointerCFunc.test_argumentsrC   r1   abr   rM      )r6   r5   offsetsrB   floatzoverlapping-fields)idsc                 C   sh   t d|}t|j|jd}|t_|ft_t|}t|j|j t|j|j t|j	d |j	d  dS )z/ Test that return values are coerced to arrays ra   )rR   r/   dataN)
r   rA   r   rR   r/   rc   rd   re   r
   __array_interface__)r   rC   arrptr_typearr2r   r   r   test_return   s   zTestNdpointerCFunc.test_returnc                 C   s>   t d}t|jd}|t_|ft_t|}tt|| dS )zB Test that vague ndpointer return values do not promote to arrays ra   r.   N)	r   rA   r   r/   rc   rd   re   r   r   )r   rp   rq   retr   r   r   test_vague_return_value   s   
z*TestNdpointerCFunc.test_vague_return_valueN)r)   r*   r+   rg   pytestmarkparametrizerl   r   r/   dictrs   ru   r   r   r   r   r`      s$    
	
r`   c                   @   r,   )TestAsArrayc                 C   s   ddl m} |d }t|dd}t|jd t|tddg t|d |dd|dd|dd	}t|jd
 t|tddgddgdd	gg d S )Nr   )c_intrM   r0   r]   rT   rU      rj   )rT   rM   )r   r{   r   r
   rR   r   r   r=   )r   r{   pair_trh   r   r   r   
test_array   s   &&zTestAsArray.test_arrayc                 C   s   ddl m}m}m} ||d td ||}t|dd}t|jd t|t	
d t|dd}t|jd t|t	
dd ttt| d S )Nr   )r{   castPOINTERr8   r7   rQ   )rM   r|   )r   r{   r   r   ranger   r
   rR   r   r   arangereshaper	   r>   )r   r{   r   r   rD   rh   r   r   r   test_pointer   s   zTestAsArray.test_pointerc                    s   ddl m m}m} G  fddd|}d| }d| ||dd|dd|dd||d	d|d
d|dd}tjg dg dgdtjfgdfdd}|t| |t||dd |t||d dd |t||d d dd d S )Nr   )c_int16	Structurepointerc                       s   e Zd Zd fgZdS )z5TestAsArray.test_struct_array_pointer.<locals>.Structrh   N)r)   r*   r+   _fields_r   )r   r   r   Struct   s    r   rT   rM   r0   )rh   rU   r|   rj   ))r0   r]   )rT   ))rU   )r|   )rj   rh   r.   c                    s   t | j j t |   d S )N)r
   r/   )r3   )expectedr   r   check   s   z4TestAsArray.test_struct_array_pointer.<locals>.checkr   rQ   r]   ra   )r   r   r   r   r   r=   int16r   )r   r   r   r   Struct3c_arrayr   r   )r   r   r   test_struct_array_pointer   s$   
 z%TestAsArray.test_struct_array_pointerc                 C   s   dd l }d}tj|tjd}tj|}tj  |||	|j
}tj||f}~~W d    d S 1 s9w   Y  d S )Nr   d   r.   )r   r   r   short	ctypeslib	as_ctypestestingassert_no_gc_cyclesr   r   c_shortr   )r   r   Nrh   pntnewpntri   r   r   r   test_reference_cycles   s   "z!TestAsArray.test_reference_cyclesc                 C   sD   t d}t j|}t|}~t| d u |d d d  d S )N)   r   rT   r   )r   rA   r   r   weakrefrefr   )r   rp   c_arrarr_refr   r   r   test_segmentation_fault
  s   

z#TestAsArray.test_segmentation_faultN)r)   r*   r+   r~   r   r   r   r   r   r   r   r   rz      s    rz   c                   @   sH   e Zd Z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 )TestAsCtypesTypez- Test conversion from dtypes to ctypes types c                 C   sn   t d}t j|}t|tjj t d}t j|}t|tjj t d}t j|}t|tj d S )Nz<u2z>u2u2)	r   r/   r   as_ctypes_typer
   r   c_uint16__ctype_le____ctype_be__r   rC   ctr   r   r   test_scalar  s   


zTestAsCtypesType.test_scalarc                 C   s4   t t jdf}t j|}t|ddtj   d S )Nra   rM   rT   )r   r/   int32r   r   r
   r   c_int32r   r   r   r   test_subarray(  s   zTestAsCtypesType.test_subarrayc                 C   sh   t dt jfdt jfg}t j|}tt|tj	 t
t||j t
|jdtjfdtjfg d S )Nrh   ri   )r   r/   uint16uint32r   r   r   
issubclassr   r   r
   sizeofrB   r   r   c_uint32r   r   r   r   test_structure-  s   
zTestAsCtypesType.test_structurec                 C   sx   t jdt jfdt jfgdd}t j|}tt|tj	 t
t||j t
|jdtjfdtjd fdtjfg d S )Nrh   ri   Tr9    rM   )r   r/   r   r   r   r   r   r   r   r   r
   r   rB   r   r   c_charr   r   r   r   r   test_structure_aligned;  s   
z'TestAsCtypesType.test_structure_alignedc                 C   sr   t tddgddgt jt jgd}t j|}tt|t	j
 tt	||j t|jdt	jfdt	jfg d S )Nrh   ri   r   r5   rk   r6   )r   r/   ry   r   r   r   r   r   r   r   Unionr
   r   rB   r   r   r   r   r   r   r   
test_unionJ  s   

zTestAsCtypesType.test_unionc                 C   s   t tddgddgt jt jgdd}t j|}tt|t	j
 tt	||j t|jdt	jfdt	jfdt	jd fg d S )Nrh   ri   r   r|   )r5   rk   r6   rB   r   )r   r/   ry   r   r   r   r   r   r   r   r   r
   r   rB   r   r   r   r   r   r   r   r   test_padded_unionY  s   

z"TestAsCtypesType.test_padded_unionc                 C   s8   t tddgddgt jt jgd}ttt jj| d S )Nrh   ri   r   rM   r   )r   r/   ry   r   r	   NotImplementedErrorr   r   )r   rC   r   r   r   test_overlappingj  s   
z!TestAsCtypesType.test_overlappingN)r)   r*   r+   __doc__r   r   r   r   r   r   r   r   r   r   r   r     s    r   )&sysrv   r   pathlibr   numpyr   numpy.ctypeslibr   r   r   numpy.distutils.misc_utilr   numpy.testingr   r   r	   r
   r   r"   cdll	test_cdllhasattrr   r   r   OSErrorr   forward_pointerrc   rw   skipifplatformr   r-   r`   rz   r   r   r   r   r   <module>   sf    
A6Y