U
    #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k
r   dZY nX dZdZee drze	dejjjZW n ek
r   Y nX ze	dejjjZW n ek
r   Y nX edkr
e	d	ejjjZedkr$e	dejjjZejZejjedk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kddG dd dZ#ejjedkddG dd dZ$ejjedk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gettotalrefcountZ_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   sr   t jjj}td|}ttd|}tdt|}td|}t|tjsJt	||  krh|  krh|ksnn t	d S )Nr   s   _multiarray_umath)
npcorer   __file__r   r   
isinstancectypesCDLLAssertionError)selfZloader_pathZout1Zout2Zout3Zout4 r   >/tmp/pip-unpacked-wheel-b2rbor69/numpy/tests/test_ctypeslib.py
test_basic(   s    


zTestLoadLibrary.test_basicc              
   C   s   zDz"t dd}td| tjjj W n tk
r@   td Y nX W n6 tk
rz } zdt| }t| W 5 d }~X Y nX d S )NT)Z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                   @   s4   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r:tt|jt d	| nt|t d	| d S )Ndtype   <i4z>i4Zswapxy)namesformats
   TZalign)r   Zintcr   r   
from_paramarrayr(   r	   	TypeErrorZnewbyteorderfloat64zerositemsize)r   dtpZdtnamesZ	dtformatsZdtdescrZsamedtZ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   ndimr)      r   r   r2   r   r3   r	   r4   r   r9   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)r)   r=   shaper)   r=   r   r>   r?   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 )
Nr)   r=         F)orderZFORTRAN)flagsZ
CONTIGUOUS)	r   r3   r   r   r2   r	   r4   rH   num)r   r+   r9   r   r   r   
test_flagsp   s    

zTestNdpointer.test_flagsc                 C   sh   t ttjdttjdk t tddtddk t tddtddk	 t tddtddk	 d S )Nr'   r=   rA   r=   r;   )r   r   r   r5   r   r   r   r   
test_cachez   s    zTestNdpointer.test_cacheN)r#   r$   r%   r:   r@   rC   rJ   rM   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 r=   r;   r=   rD   )r=   rD   rE   N)
r   c_void_pc_forward_pointerrestyper   argtypesr   r6   r	   ArgumentErrorrL   r   r   r   test_arguments   s      z!TestNdpointerCFunc.test_argumentsr8   r*   abr   r=      )r.   r-   offsetsr7   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 rO   )rB   r(   dataN)
r   r6   r   rB   r(   rQ   rR   rS   r
   Z__array_interface__)r   r8   arrptr_typeZ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 rO   r'   N)	r   r6   r   r(   rQ   rR   rS   r   r   )r   r]   r^   retr   r   r   test_vague_return_value   s    
z*TestNdpointerCFunc.test_vague_return_valueN)r#   r$   r%   rU   pytestmarkZparametrizerZ   r   r(   dictr_   ra   r   r   r   r   rN      s"   
	
rN   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )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_intr=   r)   rK   rD   rE      rX   )rD   r=   )r   rf   r   r
   rB   r   r   r3   )r   rf   Zpair_trV   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   )rf   castPOINTERr0   r/   rA   )r=   rg   )r   rf   ri   rj   ranger   r
   rB   r   r   arangeZreshaper	   r4   )r   rf   ri   rj   r9   rV   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dddgdddg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>.StructrV   N)r#   r$   r%   _fields_r   )rn   r   r   Struct   s   rr   rD   r=   r)   )rV   rE   rg   rX   )r)   rK   )rD   )rE   )rg   )rX   rV   r'   c                    s   t | j j t |   d S )N)r
   r(   )r+   )expectedr   r   check   s    z4TestAsArray.test_struct_array_pointer.<locals>.checkr   rA   rO   )r   rn   ro   rp   r   r3   Zint16r   )r   ro   rp   rr   ZStruct3Zc_arrayrt   r   )rn   rs   r   test_struct_array_pointer   s$    
z%TestAsArray.test_struct_array_pointerc              	   C   sj   dd l }d}tj|tjd}tj|}tj . |||	|j
}tj||f}~~W 5 Q R X d S )Nr   d   r'   )r   r   rl   short	ctypeslib	as_ctypesZtestingZassert_no_gc_cyclesri   rj   c_shortr   )r   r   NrV   ZpntZnewpntrW   r   r   r   test_reference_cycles   s    z!TestAsArray.test_reference_cyclesc                 C   sD   t d}t j|}t|}~t| d k	 |d d d  d S )N)   r}   rD   r   )r   r6   rx   ry   weakrefrefr   )r   r]   Zc_arrZarr_refr   r   r   test_segmentation_fault
  s    

z#TestAsArray.test_segmentation_faultN)r#   r$   r%   rh   rm   ru   r|   r   r   r   r   r   re      s
   re   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(   rx   as_ctypes_typer
   r   c_uint16__ctype_le____ctype_be__r   r8   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 )NrO   r=   rD   )r   r(   Zint32rx   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 )NrV   rW   )r   r(   uint16uint32rx   r   r   
issubclassr   ro   r
   sizeofr7   rq   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 )NrV   rW   Tr1    r=   )r   r(   r   r   rx   r   r   r   r   ro   r
   r   r7   rq   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 )NrV   rW   r   r-   rY   r.   )r   r(   rd   r   r   rx   r   r   r   r   Unionr
   r   r7   rq   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 )NrV   rW   r   rg   )r-   rY   r.   r7   r   )r   r(   rd   r   r   rx   r   r   r   r   r   r
   r   r7   rq   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 )NrV   rW   r   r=   r   )r   r(   rd   r   r	   NotImplementedErrorrx   r   )r   r8   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   )&sysrb   r~   pathlibr   Znumpyr   Znumpy.ctypeslibr   r   r   Znumpy.distutils.misc_utilr   Znumpy.testingr   r   r	   r
   r   r   cdllZ	test_cdllhasattrr   r   r   OSErrorr   Zforward_pointerrQ   rc   Zskipifplatformr   r&   rN   re   r   r   r   r   r   <module>   s^   



A6Y