a
    ze                    @   sD  d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
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mZ zd dlT W n ey   dZY n0 zd dlZW n ey   dZY n0 zd dlZW n ey   dZY n0 zfe He_e  d dlmZ W d   n1 s&0    Y  W d   n1 sF0    Y  W n eyj   dZY n0 zd dlZW n ey   dZY n0 d	Zd d d d d d d d d d d d d d d d
Z ere d= e d= er
z e!dd d e d< d e d< W n ej"y   Y n0 ddddddddddddddd>  dd> fdZ#dd Z$e e e#e#e#e#dZ%erze%d D ]Z&e$e&e%d e&< qbe ' Z(e ' Z)e D ]Z*e*d vre)e*= qe ' Z+e D ]Z*e*d!vre+e*= qe(e%d"< e(e%d#< e)e%d$< e+e%d%< e+e%d&< d Z,dZ-g dg d'fd$gd(gfd(gd(gfd#d"gd(gfd&d%gd(gfd)Z.d*d+ Z/d,d- Z0d.d/ Z1d0d1 Z2dd3d4Z3dd5d6Z4dd7d8Z5d9d: Z6d;d< Z7d=d> e%d D Z8d?d@ Z9dAdB Z:dCdD Z;dEdF Z<dGdH Z=dIdJ Z>dKdL Z?dMdN Z@dOdP ZAdQdR ZBdSdT ZCdUdV ZDdWdX ZEdYdZ ZFd[d\ ZGd]d^ ZHd_d` ZIdadb ZJdcdd ZKdedf ZLdgdh ZMdidj ZNddndoZOdpdq ZPdrds ZQddtduZRdvdw ZSddxdyZTdzd{ ZUdd|d}ZVd~d ZWdd ZXdd ZYdddZZdddZ[dddZ\dddZ]dd Z^dZ_dkZ`dZaerdZ_dZ`dZae[ZXe\ZYe]ZebedebedG dd dejcZdeedkr@ef  dS )    N)support)permutationsproduct)	randrangesamplechoice)Decimal)Fraction)*)ndarrayT)?cbBhHiIlLnNfdPr   r   Q    qr      r      )i   )i    )r   i   )           )r   l        l                     r                    )r   r   r   r   r   r   r   r   r   r   r   r   r   r   c              	   C   s   | dkrd}n| dkrd}n| dkr*d}n| dkrFdd	>  dd	> f}ndd
D ]8}zt | d|> d  W  qW qJ t jy   Y qJ0 qJ|d@ rd|>  d|> fn
dd|> f}|S )Nr   r    r   r   r   r&   r   r*   r+   )
r"      @   ?                      r   )structpackerror)fmtlhexp r;   &/usr/lib/python3.9/test/test_buffer.pynative_type_range^   s     
&r=   ) @<>=!r?   Z
bBhHiIlLfdZBbcm@mar   @b)r>   123r>   )r   arraynumpy
memoryviewbytefmtc                 C   sv   t t|  |  }|dkr6t|g}|dkr6|dkr6d}|dkrFt|}|dksV|dkrrt||}t||d }|S )	Nr   rL          r   r   r   r   )r   fmtdictbytesboolr5   r6   unpack)modecharobjxr;   r;   r<   randrange_fmt   s    
rY   c                 C   sJ   |  d\}}g }|D ]}|t||| qt|dkrB|d S t|S )N#r*   r   )splitappendrY   lentuple)r8   rW   rU   charsrX   r   r;   r;   r<   gen_item   s
    r`   c                 C   s<   | dkrt ||S dg|  }t| D ]}t ||||< q$|S Nr   )r`   range)r   r8   rW   lstr   r;   r;   r<   	gen_items   s    

rd   c                 C   s   t t| t }|d }|d}tdd}t|D ]H}t tt| }t t| t }||t	|rf|nd 7 }||| 7 }q4t
| ||}	t||}
||	|
fS )NrZ   ambr   
   r*   )r   capMODEstripr   rb   r^   rQ   MULTintrd   r`   )r   rW   rU   Zxfmtr8   nmemb_rV   
multiplieritemsitemr;   r;   r<   struct_items   s    


rq   r   c                 C   s   |d u rt t| t }|d u r0t tt| }t t| t }|d |t|rR|nd  }t| ||}t||}|	d| | }|||fS )NrZ   r*   re   )
r   rg   rh   r^   rQ   rj   rk   rd   r`   ri   )r   rW   rU   rV   rn   r8   ro   rp   r;   r;   r<   	randitems   s    
rr   c                 c   s4   t | t D ]"}t| D ]}t| |||V  qqd S N)rg   rh   rQ   rr   )r   rW   rU   rV   r;   r;   r<   	iter_mode   s    rt   c                 c   s2   t | |D ]
}|V  q
|dkr"d S t| |V  d S )Nr   )rt   rq   )nitemsZtestobjtr;   r;   r<   iter_format   s
    rw   c                 C   s   d| v pd| v pd| v S )Nr   r   r   r;   )r8   r;   r;   r<   is_byte_format   s    rx   c                 C   s4   t | }|dks$|dko2| d dko2| |d  tv S )Nr*   r   r   r?   )r]   
MEMORYVIEW)r8   rX   r;   r;   r<   is_memoryview_format   s    rz   c                 C   s   g | ]}t |s|qS r;   )rx   .0r   r;   r;   r<   
<listcomp>       r}   c                 C   s   t | t S rs   
isinstancelistrc   r;   r;   r<   atomp   s    r   c                 C   s
   t | tS rs   r   r   r;   r;   r<   listp   s    r   c                 C   s6   t | dkrdS | d }| dd  D ]}||9 }q$|S Nr   r*   )r]   )rc   rX   vr;   r;   r<   prod   s    
r   c                 C   s   | dkrdS |dkrZt |dd  |g }t| d ddD ]}||  ||d  9  < q:n>|gt |d d  }td| D ]}||  ||d  9  < qz|S )Nr   r;   Cr*   r   )r   rb   )ndimshapeitemsizeZlayoutstridesr   r;   r;   r<   strides_from_shape  s    r   c                 C   s   t | r| S t|dkr | d S dg|d  }|d rFt| |d  nd}t|d D ].}|| }t| |||  |dd  ||< qV|S r   )r   r]   rb   _ca)ro   src   strider   startr;   r;   r<   r     s    $r   c                 C   sl   t | r| S t|dkr | d S dg|d  }|d }t|d D ]$}t| |d | |dd  ||< qB|S r   )r   r]   rb   _fa)ro   r   rc   r   r   r;   r;   r<   r   $  s    "r   c                 C   s2   t | r(d|vr(t|t| kr(tdt| |S Nr   zprod(shape) != len(items))r   r   r]   
ValueErrorr   ro   r   r;   r;   r<   carray1  s     r   c                 C   s2   t | r(d|vr(t|t| kr(tdt| |S r   )r   r   r]   r   r   r   r;   r;   r<   farray6  s     r   c                 C   s   dd | D }t | S )Nc                 S   s   g | ]}t |qS r;   rb   r|   r   r;   r;   r<   r}   =  r~   zindices.<locals>.<listcomp>)r   )r   	iterablesr;   r;   r<   indices;  s    r   c                 C   s*   d}t | D ]}||| ||  7 }q|S ra   r   )r   indr   retr   r;   r;   r<   getindex@  s    r   c           	      C   s   |s| S t |}t||dd}t||d d d dd}dgt |  }t|D ]2}t|||}t||d d d |}| | ||< qL|S )Nr*   r   r   r   )r]   r   r   r   )	srcr   r   ZsstridesZdstridesdestr   frtor;   r;   r<   	transposeG  s    r   c                 C   s6   | g kr| S t | r| gS t| d t| dd   S r   r   _flattenr   r;   r;   r<   r   V  s
    r   c                 C   s   t | r| S t| S rs   r   r   r;   r;   r<   flatten^  s    r   c                 C   s4   t | rg S t| |d  gt| d |dd   S r   )r   r]   slice_shaperc   slicesr;   r;   r<   r   d  s    r   c                    s&   t | r| S  fdd|  d  D S )Nc                    s   g | ]}t | d d qS r*   N)
multislice)r|   Zsublstr   r;   r<   r}   o  r~   zmultislice.<locals>.<listcomp>r   )r   r   r;   r   r<   r   k  s    r   c                    sF   t |r|S  fddt|  d  |d  D }||  d < | S )Nc              	      s.   g | ]&\}}t || d d d d qS r   )m_assign)r|   r   rlslicesrslicesr;   r<   r}     s   zm_assign.<locals>.<listcomp>r   )r   zipllstrlstr   r   r;   r   r<   r   q  s    r   c                 C   sf   t | |}t ||}t|t|kr(dS tt|D ],}|| || krN dS || dkr4 dS q4dS )Nr   r   )r   r]   rb   )r   r   r   r   lshapershaper   r;   r;   r<   cmp_structure  s    

r   c                 C   s(   t | |||dk rtdt| |||S )Nr   z+lvalue and rvalue have different structures)r   r   r   r   r;   r;   r<   multislice_assign  s    r   c                    s   |  rdS |dk s |  | kr$dS t  fddD r>dS |dkrZ|dkoX oX S dv rfdS tfddt|D }tfddt|D }d|| ko||   | kS )NFr   c                 3   s   | ]}|  V  qd S rs   r;   r   r   r;   r<   	<genexpr>  r~   z#verify_structure.<locals>.<genexpr>Tc                 3   s.   | ]&}| d kr|  | d  V  qdS r   r*   Nr;   r|   jr   r   r;   r<   r     s   c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   )anysumrb   )memlenr   r   r   r   offsetiminimaxr;   )r   r   r   r<   verify_structure  s    r   c                 C   s   |D ]}| | } q| S rs   r;   )rc   r   r   r;   r;   r<   get_item  s    
r   c           
      C   s:   |\}}}}}}|}t |D ]}	|||	 | |	  7 }q|S rs   r   )
r   rv   r   r   r   r   r   r   pr   r;   r;   r<   memory_index  s
    r   c                 C   sR   | \}}}}}}d|> }t |D ],}t|| }	d|	> }
||
@ rD dS ||
O }q dS )Nr*   TF)r   r   )rv   r   r   r   r   r   r   Zvisitedr   r   bitr;   r;   r<   is_overlapping  s    

r      r1   r;   c                    sZ   st |d }|dkrd|r,| | |dddfS t dd}||  }t ddkrP|  n|}|| |dd|fS d}	t d}
|
dkr|rd}	n|
dkrd}	dg|  t|D ]}t |	|d  |< qnt }d	}t d}
|
dkr|
d@ rd
nd}dg| | t | |d  |d < |s0|d  dkr0| |d < t|d ddD ]j}| |d  r` |d  nd9 }|r| t | |d  |< n"dt d |  t d|d  |< q@d }}d vrt fddt|D }t fddt|D }|| }|r | |  }||d |   }n*| | |  }t ddkrF| |  n|}|| | |fS )Nr*   r   r;      r   d   _   Z   r   TFr   r*   r   c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   z!rand_structure.<locals>.<genexpr>c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   )r   rb   r]   r   )r   Zvalidmaxdimmaxshaper   r   ru   r   r   minshaper   r   Z	maxstrideZzero_strider   r   r;   r   r<   rand_structure  s\    


"


r   c           
      C   sp   ||  }t |d }| r$|| |  nd}t d|d }|| |  }t|||}t||\}}}}	|	| krlt|S Nr*   )r   sliceslice_indicesRuntimeError)
slicelenZlistlenZmaxstartr   Zmaxstepstepstopr   rm   Zcontrolr;   r;   r<   randslice_from_slicelen&  s    r   c                 C   sp   dg|  }dg|  }t | D ]B}|| }|dkr>td|d nd}t||||< t||||< qt|t|fS r   )rb   r   r   r^   )r   r   r   r   r   r   r   r;   r;   r<   randslice_from_shape3  s    

r   c                 C   s  t d| d }d}t d}|dkr(d}n|dkr4d}t ddkrDdnd	}dg| }dg| }dg| }dg| }	t|D ]}t ||d }
t ||d }||
k r|
| }}
|rt |
 |
d }t |
 |
d }d
t d t d|
d  }t|||}t||
\}}}}n&|
dkr$t d|
d nd}t||
}t||}t ddkrp||
 ||< ||< || |	|< ||< qx|
| ||< ||< || |	|< ||< qx||t|t|	fS )Nr*   r   r   r   r   r   P   TFr   )r   rb   r   r   r   r^   )r   r   r   r   r   Z
all_randomr   r   r   r   Zsmallbigr   r   r   Zs_smallrm   r   Zs_bigr;   r;   r<   rand_aligned_slices?  s<    


r   c                 C   s$   |\}}}}}}t || d|  dS )NrZ   rL   )rd   )r8   rv   r   r   rm   r;   r;   r<   randitems_from_structurej  s    r   c           
      C   s(   |\}}}}}}	t | ||||	t|B dS )Nr   r   formatr   flags)r   ND_WRITABLE)
ro   r8   rv   r   r   r   r   r   r   r   r;   r;   r<   ndarray_from_structurep  s    
r   c                 C   sP   |\}}}}}}t |}	t| D ]\}
}t||	|
| | q t|	||||dS )N)bufferr   r   dtyper   )	bytearray	enumerater5   	pack_intonumpy_array)ro   r8   rv   r   r   r   r   r   r   bufr   r   r;   r;   r<   numpy_array_from_structurev  s    r   c                    s   | j }|r$t|  |krld |fS nH|g krH| jdks> |krld |fS n$t| \}}|g}|dkrld |fS |   fddtdt D }g }|D ]2}	t||	d }
|
|
krd|f  S |	|
 q|g kr||fS |d |fS )Nr   c                    s   g | ]}||   qS r;   r;   )r|   r   r   Zmemr;   r<   r}     r~   zcast_items.<locals>.<listcomp>nan)
nbytesr   r   divmodtobytesrb   r]   r5   rT   r\   )Zexporterr8   r   r   Zbytelenr   r   Z	byteitemsro   r   rp   r;   r   r<   
cast_items  s(    

 r   c                  #   s   t dD ]} | gV  qtdd}tddkr0dnd  fdd	t |D V  tdd}tddkrfdnd  fd
d	t |D V  d S )Nr/         r   r   r*   r   c                    s   g | ]}t  d qS r   r   r|   rm   r   r;   r<   r}     r~   z!gencastshapes.<locals>.<listcomp>c                    s   g | ]}t  d qS r   r   r   r   r;   r<   r}     r~   )rb   r   )r   r   r;   r   r<   gencastshapes  s    


r   c                 C   s0   t t|  | d t|  | d t|  | d S r   )r   rb   )r   r;   r;   r<   	genslices  s    r   c                    s    fddt | D }t| S )Nc                    s   g | ]}t  | qS r;   )r   r|   r   r   r;   r<   r}     r~   z"genslices_ndim.<locals>.<listcomp>)rb   r   )r   r   r   r;   r   r<   genslices_ndim  s    r  Fc                 C   s,   |s| dkrdnd}t || d }t|| S r   )r   r   )r   allow_emptyZminlenr   r;   r;   r<   rslice  s    r  c                 c   s   t dD ]}t| |V  qd S )Nr   )rb   r  )r   r  rm   r;   r;   r<   r     s    r   c                 #   sp   t |D ] }t fddt | D V  qt |D ] }t fddt | D V  q2tdd t | D V  d S )Nc                 3   s   | ]}t  | V  qd S rs   r  r   r   r;   r<   r     r~   zrslices_ndim.<locals>.<genexpr>c                 3   s   | ]}t  | d dV  qdS )T)r  Nr  r   r   r;   r<   r     r~   c                 s   s   | ]}t d dd V  qdS r   )r   r   r;   r;   r<   r     r~   )rb   r^   )r   r   Z
iterationsrm   r;   r   r<   rslices_ndim  s
    r  c                 c   s0   t | }|d u rt|n|}t t||V  d S rs   )r^   r]   r   )iterabler   poolr;   r;   r<   rpermutation  s    r  c              
   C   s   z|   }W n ttfy*   |  }Y n0 t| trD| j}| j}nd}d}td|| j	| j
| j|| j| j|f  tj  d S )Nunknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s))tolist	TypeErrorNotImplementedErrorr   r   r   r   r   printr   r   
suboffsetsr   r   sysstdoutflush)ndrX   r   r   r;   r;   r<   ndarray_print  s    

r  r   rf      r   z%struct module required for this test.z%ndarray object required for this testc                   @   s  e Zd Zdd ZdddddZdd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d  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Z d?d@ Z!dAdB Z"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)e*j+Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4e5j6dcdd Z7dedf Z8dgdh Z9didj Z:dkdl Z;dmdn Z<dodp Z=dqdr Z>dsdt Z?dudv Z@dwdx ZAdydz ZBd{d| ZCd}d~ ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRe*jSdd ZTdS )TestBufferProtocolc                 C   s   t  | _d S rs   )Zget_sizeof_void_psizeof_void_pselfr;   r;   r<   setUp  s    zTestBufferProtocol.setUpF)slicedcastc       
   %         s<  	rt 	 ns tndjrΈd d}tdD ]:}	| dkr\ q| dkrH||  	| d  7 }qH|gdd td D  j}d dk r| }|gtdd   j| 	j
 	j 	j j| 	j 	jt	 
r@sR	jt 	jt ttsztrr n }	| sd S |st }d }t	D ]}zNt|}t|}t|trtjg|R  }nt|}|| W n  ty0   d}Y  qBY n0 	|| q|s	 | t}d dd	 |D }	|! | rnd
}t"}dD ]}}|dkrt#drt#drt$|	}t|	|t%d}n0t#drt#dr|dkrt|	|d}t&t'| 	  | (t) | dkrPqt|}|rbdnt*}t+dt, 	t |   fddt|D }t|d dkrdd |D }t|	|d} 	t| t t }!	|!  tjd d}!	|!  tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkrdd |D }t|	|t%B d} 	t| t tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkr,dd |D }t#dr<t%nd}"t|	|"|B d} 	t| t tjdd}!	|!  qtr8zt}W n ty   Y d S 0 ttrȈjn|f	
fdd	}#|#| |- }$|#|$dd W d    n1 s&0    Y  |  d S )Nr;   r   r*   c                 S   s   g | ]}d qS )r   r;   r   r;   r;   r<   r}     r~   z-TestBufferProtocol.verify.<locals>.<listcomp>Tr>   c                 s   s   | ]}d | V  qdS )z%02xNr;   r{   r;   r;   r<   r   T  r~   z,TestBufferProtocol.verify.<locals>.<genexpr>r   r   FAr  r  r   r   r   r   r   r   c                    s   g | ]}t  | qS r;   r5   unpack_fromr   contigr8   r   r;   r<   r}     s   c                 S   s   g | ]}|d  qS r   r;   r   r;   r;   r<   r}     r~   r   r   r   orderc                    s   g | ]}t  | qS r;   r!  r   r#  r;   r<   r}     s   c                 S   s   g | ]}|d  qS r%  r;   r   r;   r;   r<   r}     r~   c                    s   g | ]}t  | qS r;   r!  r   r#  r;   r<   r}     s   c                 S   s   g | ]}|d  qS r%  r;   r   r;   r;   r<   r}     r~   c                    s    | j  | j | j | j | j| | j | jt	 	rns| j
t	
 | jt	 dkrdnt}t| | r n }| |  d S r   )assertIsrW   assertEqualr   r   r   readonlyr   r   r^   r   r  r]   r
  r   )rD   expected_readonlyr   rep)exexpected_lenr8   r   rc   r   resultr  r   r  r   r  r;   r<   check_memoryview  s    z3TestBufferProtocol.verify.<locals>.check_memoryview)r,  ).r   r]   r  ZassertGreaterrb   r  r   r)  rW   r*  r   r   r   r+  r   r   r^   r   r   r   rz   r
  r   r   r   get_pointerr   r5   r6   extendBufferErrorrM   joinhexr   is_contiguousr   
ND_FORTRANget_contiguous
PyBUF_READ
assertTrue
cmp_contigr   py_buffer_to_contiguousPyBUF_FULL_RO
toreadonly)%r  r0  rW   r   r8   r+  r   r   r   rc   r  r  Z
suboffset0r   Zstride0r-  r   Zbuf_errr   Zitem1Zitem2rX   rD   r   ZffZ	flattenedr(  expectedZtransrl   roZinitlstyZcontig_bytesr   r1  Zmmr;   )r$  r.  r/  r8   r   rc   r   r0  r  r   r  r   r  r<   verify  s   







&
,zTestBufferProtocol.verifyc                 C   sl  dd }|j r||ts|||tr(|jr|||tr8|jr|||trH|jr|||tsX|j	s|||t
sh|jr|||ts||tr| jtt||d d S t|tst|jr| }nt|td}| }||trdn|j }|j}	|j}
|j}||tsd}	| }||tsd}||tr$|jnd}||t
r:|jnd}t||d}| j|||
|	||||||d
 d S )	Nc                 S   s   | |@ |kS rs   r;   )reqflagr;   r;   r<   match  s    z/TestBufferProtocol.verify_getbuf.<locals>.matchgetbufFr>   r*   r;   )	rW   r   r8   r+  r   r   r   rc   r  )r+  PyBUF_WRITABLEPyBUF_C_CONTIGUOUSc_contiguousPyBUF_F_CONTIGUOUSf_contiguousPyBUF_ANY_CONTIGUOUS
contiguousPyBUF_INDIRECTr  PyBUF_STRIDESPyBUF_NDPyBUF_FORMATassertRaisesr4  r   r   rz   r   r
  r>  r   r   r   r   r   rC  )r  Zorig_exr.  rD  r  rF  rc   r  rA  r8   r   r   r   r   r;   r;   r<   verify_getbuf  sh    		
z TestBufferProtocol.verify_getbufc                    s  t ttttttttt	t
ttttf}dd tdD dfg ddfg ddfdd tdD d	ff}g g d
fg dg d
fdgg d
fdgdgdfdgdgd
fdgdgdfddgg d
fddgddgdfddgddgdfddgddgdff
}d
ttttB tttB f}d
ttttB f}|D ]\}}t| |D ]x\}}	}
 fdd|	D }	|
 9 }
|D ]J}|	rl|t@ rlqR|s|t@ rqR|r|n|d
 }t|||||	|
d}|r|d d d nd }t|}|rt|}|jd
ks|jdkr|r|	r| || |r.|jdkr.|r.|	r.| || |D ]h}|D ]\}| ||||B  | ||||B  |r:| j||||B dd | j||||B dd q:q2qRq(qg d}t|dgtd}| tt| tdgdg}t|td}| jtt|td | jtt|td | jtt|td | jtt|td | jtt|td | jtt|td t|td}g dg dfD ]}d
tfD ]p}t|||tB d}| t |d | t |d  |D ]2}t||d}| t |d | t |d  qq|qpd S )!Nc                 S   s   g | ]}|d  rdndqS )r   TFr;   r|   rX   r;   r;   r<   r}   
  r~   z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>   r   r*   r   r  r   r   r   r4   r3   	   rf      rW  r   r   c                 S   s$   g | ]}|d  rd| nd| qS )r   r%   r$   r;   rV  r;   r;   r<   r}     r~   r   r   )r*   r  r*   r   rZ  r   r   r  r   r*   r3   c                    s   g | ]}|  qS r;   r;   r   r   r;   r<   r}   (  r~   )r   r   r   r   r   T)r  r   r   rY  rG  )r*   rW  r*   )r4   r   r4   r  r   )!rP  rQ  rR  PyBUF_SIMPLErJ  rL  rN  
PyBUF_FULLr>  PyBUF_RECORDSPyBUF_RECORDS_ROPyBUF_STRIDEDPyBUF_STRIDED_ROPyBUF_CONTIGPyBUF_CONTIG_ROrb   r   r8  ND_PILrI  rS  r5   calcsizer   rM   r   r*  rU  ND_GETBUF_FAILrT  r4  r;  r7  )r  requestsZ	items_fmtZ	structureZndflagsZ
real_flagsro   r8   r   r   r   r   Z_itemsex1ex2m1m2rD  bitsr.  baser  r(  r;   r   r<   test_ndarray_getbuf  s    




$z&TestBufferProtocol.test_ndarray_getbufc           
   	   C   s  t dgdg}t dgdgtd}t |j|jfD ]H}| t|h d | t|g d | t|g d | jt|g ddhd	 | jt|g ddgdhd
 | jt|g ddgg d | jt|dgdgi d | jt|dgdgi d | jt|dgdgi d | jt|dgdgdgtd | jt|dgg td | jt|g dgd	 | jt|dgdgdd | jtj	|dgdgdd | jt|ddgdgdd | jt|g ddgdd t
d }| jt|dg| dg| d	 | jt|dgdgd	 | jt|g ddgd	 | jt|dgdd gd	 | jt|g dddgdd | jt|g ddgdgd
 | jt|dgdgdd gd
 | jt|ddgddgdgd
 | jt|g ddgdgdd | jt|g ddgdd | jt|g ddgddd | jt|g ddgd d | jtj	|dgdgd!d g d"}| jt||ddgd#d$gd%d& | jt|t d'd( | jt|dgdgtd | t|dg q.| jtt d)td* t dgdgtd}| jt|jdgdgtd t d)}| t|jdgdg | t|j t dgdg}|dgdg t|}| t|jdgdg | t|j |  |  | t|j ~| tti g d | ttd)i  t ttd+dgd+ d	}| tt|d%g t ttd,ddgd	}| tt|g d- | tt|ddg | tt|d#dg | tt|d.dg t g ddgdd}t |td*}| t|j t g ddgdd}t |}	t |	}| t|j t d/gdgd0d}| t|j t
}t tt|dg| d	}| t|j t dgdgd	}| ttddddd% | tt|d1d2 | tt|d.d2 | tt|td3 | tt|td4 | tt|td5 | tt|d6d7 t dgdgd	}| ttddddd% | tti | | tt|i  t dgdgd	}| ttddddd% | tti d7 | tt|d8 d S )9NrY  r*   r   >   r*   r   r  )r*   r   rJ   )r*   r   r  r   r*   r   r  r  r   r   r   r   r   r]  )r   rH  r   r   r   ZXXXr   i  r   r   r  r   ZQLr   rJ   r"   r*   r   r  r   r   rH   r*   r   r  r   r   r   r   r   )r   r   r   r>   z@#$)
r*   r   r  r   r   r   r4   r3   rY  rf   r[  r   )r   r   r   r   r      123rG  r   rW  )r   r  r   r)   )r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   ZLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLxyzr     u    Z   r     )r   ND_VAREXPORTpushrT  r  r8  rf  r   r5   r7   ZND_MAX_NDIMOverflowErrorr   r_  r4  rI  poprM   releaser2  r   rb   r^  r
  memoryview_from_bufferr9  r:  UnicodeEncodeErrorr<  r7  )
r  r  Zndmr   r   ro   rD   r.  rj  rk  r;   r;   r<   test_ndarray_exceptionsf  s    z*TestBufferProtocol.test_ndarray_exceptionsc                 C   s   t tdD ]}dgd }tg ddgtd}t||d< tddD ]$}|jg ddgd t|||< qDtdD ]}|||    qr| t|j	 ~qd S )Nr   r   rs  r  r]  r*   r   )
r   rb   r   r  rM   r  r  rT  r4  r  )r  permrD   r  r   r;   r;   r<   test_ndarray_linked_list  s    
z+TestBufferProtocol.test_ndarray_linked_listc                 C   sL   t dD ]>\}}}t|}t|d|d}| j|d ||dddd|d	 qd S )Nr   r;   r   TrW   r   r8   r+  r   r   r   rc   )rw   r5   rg  r   rC  )r  r8   Zscalarrm   r   r  r;   r;   r<   test_ndarray_format_scalar  s    
z-TestBufferProtocol.test_ndarray_format_scalarc                 C   sl   t dd}t|D ]T\}}}t|}dtfD ]6}t||g||d}| j|d ||dd|f|f|d	 q.qd S )Nr*   rf   r   r  Tr  )r   rw   r5   rg  rf  r   rC  )r  ru   r8   ro   rm   r   r   r  r;   r;   r<   test_ndarray_format_shape  s    


z,TestBufferProtocol.test_ndarray_format_shapec                 C   s   t dd}t|D ]\}}}t|}tddD ]}|dkr>q0t|d d | g}|| g}|dk rp||d  nd}	dtfD ]>}
t|||||	|
d}| j|d ||dd|||d d | d	 q|q0qd S )	Nr*      r   r   r   Tr  )	r   rw   r5   rg  rb   r]   rf  r   rC  )r  ru   r8   ro   rm   r   r   r   r   r   r   r  r;   r;   r<   test_ndarray_format_strides*  s$    


z.TestBufferProtocol.test_ndarray_format_stridesc                 C   s@   g d}t |ddd}t |ttB d}| | t|d d S )NrX  rr  )r*   r  r   rG  )r   rL  rS  r*  r
  r   )r  ro   r.  r  r;   r;   r<   test_ndarray_fortran?  s    z'TestBufferProtocol.test_ndarray_fortranc                 C   s^  t dD ]N}dd t |D }t|}t|D ]$}t|\}}}t|}dtfD ]}	|dkrl|	tkrlqVt||||	d}
t|||d}t	||}| j
|
d ||d||||d	 t|rt|||d	}t|ttB d
}
| |
jdk |
 }| j
|d ||d||||d	 t||||	tB d}
t|||d}t||}| j
|
d ||d||||d	 qVq0qd S )Nr   c                 S   s   g | ]}t d dqS )r   rf   r   r   r;   r;   r<   r}   G  r~   z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>r   r  r   Tr  r   rG  r;   r  )rb   r   r   rr   r5   rg  rf  r   r   r   rC  rz   re  rS  r;  r   r  r8  r   )r  r   shape_tru   r   r8   ro   rm   r   r   r  r   rc   r.  mvr;   r;   r<   test_ndarray_multidimE  sJ    



z(TestBufferProtocol.test_ndarray_multidimc                 C   s  t dgdgd}| t|jdd t|}| || | t|jdd t dgdgtd}| t|jd t|}| || | t|jd t dgdgtd}| t|j	d | t|jdd t|}| || | t
|j	d | t
|jdd g d}t |t|gdtd}| tj|jd	d
 | t|jdd t|}| || | t|jd	d
 | t|jdd g d}t |t|gdtd}| t|jd	d
 | tj|jdd d S )Nr*   r   r3   r]  r)   r*   r   r  r   r   r   r4   r3   r   r  r   i,  )r      )r*   r   rr  )r   r   ZLQ)s    1r  )r   rT  r  __setitem__rM   r*  r   __delitem__r  __getitem__
IndexErrorr]   r5   r7   r   )r  r  r  ro   r;   r;   r<   test_ndarray_index_invalidr  s:    z-TestBufferProtocol.test_ndarray_index_invalidc                 C   sJ  t ddtd}t|}| || |d }| |d |d }| | |  |d }| |d |d }| | |  | t|jd | t|jd | t|jdd | t|jdd | | d | | d d|d< | | d d|d< | | d d|d< | | d d|d< | | d d S )	Nr*   r;   r]  .r   r3   rY  r   )	r   r   rM   r*  r
  rT  r  r  r  )r  r  r  rX   r;   r;   r<   test_ndarray_index_scalar  s"    z,TestBufferProtocol.test_ndarray_index_scalarc                 C   sR   t ttdddgtd}t |td}| t|jd | t|jtddd d S )	Nr3   r   r   r]  rG  r*   r  r   )	r   r   rb   r   rd  rT  r4  r  r   )r  r.  r  r;   r;   r<   test_ndarray_index_null_strides  s    z2TestBufferProtocol.test_ndarray_index_null_stridesc                 C   s   t dD ]\}}}t|dg|d}tddD ]}| || ||  q,| t|jd | t|jd t|rt|}| || tddD ]}| || ||  q| t|jd | t|jd qt dD ]\}}}t|dgt	|d}t|t
tB d}tddD ]}| || ||  qt|r| }| ||t tddD ]}| || ||  qNqg d}t|dgd}t|td}tddD ]}| || ||  qg d}t|dgd}t|td}tddD ]}| || ||  qd S )	Nr   r   r  r&  rG  rw  r   )rw   r   rb   r*  rT  r  r  rz   rM   r   rd  rS  r  r)  __eq__NotImplementedre  r^  )r  r8   ro   rm   r  r   r  r.  r;   r;   r<   !test_ndarray_index_getitem_single  sB    z4TestBufferProtocol.test_ndarray_index_getitem_singlec                 C   s  t dD ]\}}}t|dg|td}tdD ]}|||< |||< q,| | | | t|jd| | t|jd| t	|sqt|dg|td}t
|}| || tdD ]}|||< |||< q| | | | t|jd| | t|jd| qt dD ]\}}}t|dg|td}tddD ]&}||d  ||< ||d  ||< q(| | | t	|snqt|dg|td}t
|}| || tddD ]&}||d  ||< ||d  ||< q| | | qd S )Nr   r  r  r  r   r*   )rw   r   r   rb   r*  r
  rT  r  r  rz   rM   )r  r8   ro   Zsingle_itemr  r   r  r;   r;   r<   !test_ndarray_index_setitem_single  sD    


z4TestBufferProtocol.test_ndarray_index_setitem_singlec              
   C   s  d}t |}t|D ]}t|\}}}dtfD ]}t||||d}t||}	t|d  |d D ]}
| |	|
 ||
   t|d  |d D ]b}| |	|
 | ||
 |   t|d  |d D ](}| |	|
 | | ||
 | |  qqqdt||||t	B d}t
||}	t|d  |d D ]}
| |	|
 ||
   t|d  |d D ]d}| |	|
 | ||
 |   t|d |d D ]*}| |	|
 | | ||
 | |  qq\q,q0qd S )Nr   r  r   r   r  r*   r   )r   r   rr   rf  r   r   rb   r*  r
  r8  r   )r  r  ru   r   r8   ro   rm   r   r  rc   r   r   kr;   r;   r<   #test_ndarray_index_getitem_multidim  s.    
 *
 z6TestBufferProtocol.test_ndarray_index_getitem_multidimc                 C   s   t ddd}| ttdt  t|}| || | ttdt  tdD ]\}}}t |dg|d}t|D ]&\}}| |||  | 	||v  qnt
|rLt|}t|D ]&\}}| |||  | 	||v  qqLd S )Nr*   r;   r   z1 in ndz1 in mvr   r   )r   rT  r  evallocalsrM   r*  rw   r   r;  rz   )r  r  r  r8   ro   rm   r   r   r;   r;   r<   test_ndarray_sequence9  s    z(TestBufferProtocol.test_ndarray_sequencec              	   C   s  g d}t |dgtd}t|}| t|jtddd| | t|jtddd| t |dgtd}t |dgtd}t |td}| t|jtddd| t |dgdtd}t|}| t	|j
tddd | t	|j
tddd t |d	d
gdtd}t|}| t	|j
tdddtdddf | t	|j
tdddtdddf | t|j
d | t|j
dtdddf | t|j
tdddi f | t|j
tdddtdddf | t|j
d t |dgdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |dgdtd}t |dgdd}	t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |d	d
gdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | g dg dg | t|jtddd|dd  t |dgdtd}t |dgdd}t|}t|}| t	|jtdd	d|dd  | | | | t	|jtdd	d|dd  | | | | tttddd	i  | ttdd | t	ttdddd
 t |dgdtd}
| t|
j t |dgdd}t |td}
| t|
j d S )Nr  r3   r]  r   r*   rG  r   r  r   r   z@%$r   r   r   r4   rx  )r   r   r4   r3   z###########)r   r   rM   rT  r  r  r   rR  r4  r   r  r  r*  r
  r   rf  add_suboffsetsr^  )r  ro   xlmlr.  xrr  r  mryrrX   r;   r;   r<   test_ndarray_slice_invalidL  s    


"""""
""z-TestBufferProtocol.test_ndarray_slice_invalidc                 C   s  g d}t |dgdtd}t |dgdd}|dd |dd< | | | t|}t|}| || | || |dd |dd< | | | t |ddgdtd}t |ddgdd}|d	d
ddf |d	d
d
d
f< | | t|ddg d S )NrX  rW  r   r  r   rY  r   r  r*   r   )r   r   r*  r
  rM   r   )r  ro   rX   rB  r  r  r;   r;   r<   test_ndarray_slice_zero_shape  s    $z0TestBufferProtocol.test_ndarray_slice_zero_shapec                 C   s  d}t |}t|}t|D ]}t|\}}}t|}dtfD ]}	t||||	d}
t||}t	||D ]}d }zt
||}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|
| }W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || qh| | | qhq@qd S Nr  r   r  )r]   r   r   rr   r5   rg  rf  r   r   r  r   	Exception	__class__r)  r*  r
  )r  r  r   ru   r   r8   ro   rm   r   r   r  rc   r   listerrr  enderrndslicedr;   r;   r<   test_ndarray_slice_multidim  s.    

z.TestBufferProtocol.test_ndarray_slice_multidimc                 C   s2  d}t |}t|}t|D ]}t|\}}}t|}t|||d}	|	  t|||d}
|
  t|
}t	||}t
||D ]}d }zt||}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|	| }W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || q| | | qqd S )N)r   r  r   r   r   )r]   r   r   rr   r5   rg  r   r  rM   r   r  r   r  r  r)  r*  r
  )r  r  r   ru   r   r8   ro   rm   r   r  r.  r  rc   r   r  r  r  r  r  r;   r;   r<   'test_ndarray_slice_redundant_suboffsets  s4    

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc                 C   s  t dD ]\}}}tdD ]}tdD ]}dtfD ]}|tB }t|dg||d}t|dg||d}	t|	}
d }d }|d d  }z0|| }|| }|| ||< t|t|k}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|| ||< W n, ty" } z|j}W Y d }~n
d }~0 0 |r8| 	|t
 n| | | | 	|| t|s`q8d }z|
| |
|< W n, ty } z|j}W Y d }~n
d }~0 0 |r| 	|t
 q8| |
 | | |
| | 	|| | j|
|	|j|d|j|j|j| d	 q8q*qqd S )Nr   r   r  Fr  )rw   r   rf  r   r   rM   r]   r  r  r)  r   r*  r
  rz   rC  r   r   r   r   )r  r8   ro   rm   lslicer  r   r   r  r.  r  ZlsterrZdiff_structurerc   ZlvalZrvalr  r  Zmverrr;   r;   r<    test_ndarray_slice_assign_single  sX    
z3TestBufferProtocol.test_ndarray_slice_assign_singlec                 C   s*  d}t |}t|}t|D ]}t|\}}}dtfD ]}ttD ]}t||\}	}
t||||t	B d}t
||}d }zt|||	|
}W n* ty } z|j}W Y d }~n
d }~0 0 d }z||
 ||	< W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || qD| | | qDq8qd S r  )r]   r   r   rr   rf  rb   
ITERATIONSr   r   r   r   r   r  r  r)  r*  r
  )r  r  r   ru   r   r8   ro   rm   r   r   r   r  rc   r  r0  r  r  r;   r;   r<   "test_ndarray_slice_assign_multidim2  s2    
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc                 C   s8  t tD ](}td D ]}t|}t|dttd}| t	|  t
||}t|||}| }t|}t|r| }	| |	| |d dkrt|||td}
|
 }| || t|
}t|r| ||
 | }	| |	| tr|d }d|v rqt|||}| j|d |j|d|j|j|j| d		 qqd S )
Nr?   Tr   r   r   r   rq  r  Fr  )rb   r  rQ   r5   rg  r   MAXDIMMAXSHAPEr;  r   r   r   r
  rM   rz   r*  rf  r   r   rC  r   r   r   r   )r  rm   r8   r   rv   ro   rX   Zxlistr  ZmvlistrB  Zylistr   zr;   r;   r<   test_ndarray_randomS  sB    

z&TestBufferProtocol.test_ndarray_randomc                 C   s   t tD ]}td D ]}t|}t|dttd}| t	|  t
||}d}zt|||}W n* ty } z|j}W Y d }~n
d }~0 0 | | trd}	zt|||}
W q ty } z|j}	W Y d }~qd }~0 0 qqd S )Nr?   Fr  )rb   r  rQ   r5   rg  r   r  r  assertFalser   r   r   r  r  r;  r   r   )r  rm   r8   r   rv   ro   r  rX   r  Z	numpy_errrB  r;   r;   r<   test_ndarray_random_invalid|  s*    


z.TestBufferProtocol.test_ndarray_random_invalidc                 C   s2  t tD ]"}td D ]}t|}tttd\}}}}t|d|d}t|d|d}	| 	t
|  | 	t
|	  t||}
t||	}t|
||}t|||	}|| ||< | }| }t|}t|}| | | | | | |d dkr|	d dkrt|
||td}t|||	td}|| ||< | }| }| || | || t|}t|}| | | | | | trd|v sd|v rqt|
||}t|||	}|| ||< t|st|	s| j|d |j|d|j|j|j| d		 | j|d |j|d|j|j|j| d		 qqd S )
Nr?   r  Tr   r   r   rq  Fr  )rb   r  rQ   r5   rg  r   r  r  r   r;  r   r   r   r
  rM   r*  rf  r   r   r   rC  r   r   r   r   )r  rm   r8   r   r   r   r   r   tltrZlitemsZritemsr  r  ZxllistZxrlistr  r  Zylr  ZyllistZyrlistZzlZzrr;   r;   r<    test_ndarray_random_slice_assign  sb    






z3TestBufferProtocol.test_ndarray_random_slice_assignc                 C   sv   g d}t |ddgtd}t |}| |jt@  | |j| | |jd | |j	 | |j
 | |j d S )NrX  r  r   r]  )r   r   )r   rf  r;  r   r)  rW   r*  r  r  rK  rM  rO  )r  ro   r  r.  r;   r;   r<   test_ndarray_re_export  s    z)TestBufferProtocol.test_ndarray_re_exportc              	   C   s   dt fD ]}tg ddg|d}t|}| || | | g  | | g  tg dg d|d}| | g  tg dg d|d}| | g g g g tg dg d|d}| | g g g gg g g gg g g gg qd S )Nr   rs  r]  r   r  r  r  r   r  )r  r  r   rf  r   rM   r*  r
  r  r   r  r  r;   r;   r<   test_ndarray_zero_shape  s    
z*TestBufferProtocol.test_ndarray_zero_shapec                 C   sd   dt fD ]V}tdgdgdg|d}t|}| || | | g d | | g d qd S )Nr   r*   r   ru  )r*   r*   r*   r*   r*   r  r  r;   r;   r<   test_ndarray_zero_strides  s    z,TestBufferProtocol.test_ndarray_zero_stridesc                 C   s>   t ttddgdd}| |jd | | g d d S )N   r  r4   rt  )r4   r3   rY  )r   r   rb   r*  r   r
  r  r  r;   r;   r<   test_ndarray_offset  s    z&TestBufferProtocol.test_ndarray_offsetc                 C   s>   dt fD ]0}tttddg|d}| }| || qd S Nr   r  r]  )rf  r   r   rb   r  r*  )r  r   r  rD   r;   r;   r<   #test_ndarray_memoryview_from_buffer  s    z6TestBufferProtocol.test_ndarray_memoryview_from_bufferc                 C   sP   dt fD ]B}tttddg|d}tdD ]}| || t||g q,qd S r  )rf  r   r   rb   r*  r2  )r  r   r  r   r;   r;   r<   test_ndarray_get_pointer
  s    z+TestBufferProtocol.test_ndarray_get_pointerc                 C   s\   t ttdg dd}t |ttB d}| | |  t|}| | |  d S )Nr  )r   r   r   r   rG  )r   r   rb   rR  rS  r*  r
  rM   )r  r.  r  rD   r;   r;   r<    test_ndarray_tolist_null_strides  s
    z3TestBufferProtocol.test_ndarray_tolist_null_stridesc                 C   s   |  tdd tttdddgd}tttdddgd}|  t|| tdgdgdd	}| t|d
 | td
| d S )Nr|  s   456rW  r  r   r   r*   r   r   rP   )r  r<  r   r   rb   r;  )r  rX   rB  r;   r;   r<   test_ndarray_cmp_contig  s    z*TestBufferProtocol.test_ndarray_cmp_contigc                 C   s  t  dg d}t|}| tt| tttd}tttddgd}| t|t| tttdddgd}| t|t| tttdg dd}| t|t| tt	ttdddgd}tttdddgt
d}| t|t| tt	ttdg d	d}tttdg d	t
d}| t|t| tttd}tttdg d
td}| t|t| tttdg d
dd}| t|t|  d S )Nr   rs  rW  r   r  r   )r  r   r   r]  r   r  r   r   r   r  r   )rK   r   rT  r   hashrR   r   rb   r*  r   r8  rf  r   )r  rF   r  r   r;   r;   r<   test_ndarray_hash%  s*    z$TestBufferProtocol.test_ndarray_hashc                 C   sT  t ttttttttt	t
tf}| tti dt tdddtd}dD ]*}|D ] }t|||}| ||  qJqBtdgdgdtd}dD ]&}|D ]}t|||}| |d	 qqtttd
g ddtd}dD ]&}|D ]}t|||}| |d	 qqdtfD ]}tdgdgd|tB d}| }dD ]*}|D ]}t|||}| || q.q&tg ddgd|tB d}| }dD ]*}|D ]}t|||}| || qqxqtg ddgdgtd}| }dD ].}ttfD ]}t|||}| || q֐q|d d d }| }dD ]J}|D ]>}zt|||}W n tyJ   Y qY n0 | || qqttd}dtfD ]}t|ddg|tB d}trtt|ddgd|dkrdndd}	|tkrtt|ddgddgtd}
|
 }n| }|D ]}zt|d|}W n ty&   Y qY n0 | || tdd |D ddgtd}| t|t| tr| ||	jdd q|dkrtt|ddgddgtd}
nt|ddgtd}
|
 }ttt ttfD ]}zt|d|}W n ty    Y qY n0 | || tdd |D ddgttB d}| t|t| tr| ||	jdd q|tkrt|ddgtd}
|
 }n| }ttt ttfD ]}zt|d|}W n ty   Y qY n0 | || tdd |D ddg|tB d}| t|t| tr| ||	jdd qqttttdddgttB d}t|dt}| ||  tdd |D ddgtd}| t|t| t|dt}tt|ddgddgtd}
| ||
  td d |D ddgttB d}| t|t| t|dt}| ||  td!d |D ddgtd}| t|t| d S )"Nr  rY  r;   r   r  r  r*   r   r~   r3   r   r   r4   r   rs  r  r   r   ru  r   rW  r   r]  r   r   )r   r   r   r(  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}     r~   zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>r'  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}   	  r~   r  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}   	  r~   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}   *	  r~   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}   1	  r~   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r}   7	  r~   )rP  rQ  rR  r^  r_  r>  r`  ra  rb  rc  rd  re  rT  r  r=  r   r   r*  r   r   rb   r8  r4  r   r   r   rM   Ztostringrf  )r  ri  r  r(  Zrequestr   r   Zndbytesrc   ZnarX   r@  rB  r;   r;   r<   test_py_buffer_to_contiguousJ  s    B


 

  z/TestBufferProtocol.test_py_buffer_to_contiguousc                 C   s|  dg fg ddgft tdg dfg}|D ]H\}}t||d}t|}| |j | |j t|}t||dd}t	||}| j
||dd	d
||||d	 t|}	| j
|	|dd	d
||||d	 t|ttB d}
| |
jd |
 }| j
|d dd	d
||||d	 t|td}
| |
jd | |
jd | |
jd |
 }|dkrR|gn|}| j
|d dd	d
d|jgd|d	 q,|D ]\}}t||td}t|}| |j | |j t|}t||dd}t||}| j
||dd	d
||||d	 t|}	| j
|	|dd	d
||||d	 q||dd  D ]v\}}t||td}t|}t|}t	||}| j
||dd	d
|||j|d	 t|}	| j
|	|dd	d
|||j|d	 q"| ttdd | tti  tg ddgd}t|td}
| tt|
 t|ttB d}
| tt|
 tdgd dgd dd}
| tt|
 | t|
j | tt|
td | tt|
td | tt|
d d d td d S )NrY  rs  r  r  r  r   r*   r   r   Tr  rG  r;   r>   r   r*   r]  r     9rX   r"   r   r   r   )r   rb   r   rM   r;  rK  rO  r]   r   r   rC  re  rS  r*  r   r  r^  r   r   r   r8  rM  r   rf  rT  r  r4  r   r9  r:  )r  Zitems_shapero   r   r.  rD   r   r   rc   rm  r  r;   r;   r<   test_memoryview_construction:	  s    (





z/TestBufferProtocol.test_memoryview_constructionc                 C   s   g d}g dg dg dfD ]4}t ||d}| |j t|}| t|jd qtddD ]<\}}}td}||}| |	 d | |
 g  q^d S )	Nrs  r  r  r   r   r*   rM   r~   )r   r;  rK  rM   rT  r  r  rw   r*  r   r
  )r  ro   r   r.  msrcr8   rm   rD   r;   r;   r<   test_memoryview_cast_zero_shape	  s    
z2TestBufferProtocol.test_memoryview_cast_zero_shapec                 C   s   | j }tj}d}d}ttd}|td||d|   t|ddgdd	}|t|||d|   t|g d
dd	}|t|||d|   d S )NzPnin 2P2n2i5P PZ3nr3   r~   r*   r   r   r   r   )r   r   r   r  )check_sizeofr   Zcalcvobjsizer   rb   rM   r   )r  checkZvsizeZbase_structZper_dimro   rF   r;   r;   r<   test_memoryview_sizeof	  s    z)TestBufferProtocol.test_memoryview_sizeofc                 C   s  G dd dt }G dd dt }dd }|d|ddtd	d
tddg dh dddiddddd tddtdtddddd|dd g}tddD ]6\}}}t|dg|td}t|dg|td}	t|}
t	
||	d| ||
d< | |
d |	d  t	|}d|v rq|D ]}d }zt	
||	|| W n t	jyH   t	j}Y n0 d }z||
d < W n0 ttfy } z|j}W Y d }~n
d }~0 0 |s|r| |d  | |d  n| |
d  |	d   qqd S )!Nc                   @   s   e Zd Zdd Zdd ZdS )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTc                 S   s
   || _ d S rs   valr  r  r;   r;   r<   __init__	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__c                 S   s   | j S rs   r  r  r;   r;   r<   __int__	  s    zETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)__name__
__module____qualname__r  r  r;   r;   r;   r<   INT	  s   r  c                   @   s   e Zd Zdd Zdd ZdS )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXc                 S   s
   || _ d S rs   r  r  r;   r;   r<   r  	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__c                 S   s   | j S rs   r  r  r;   r;   r<   	__index__	  s    zGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r  r  r  r  r  r;   r;   r;   r<   IDX	  s   r  c                   S   s   dS )Nr4   r;   r;   r;   r;   r<   r   	  r~   z;TestBufferProtocol.test_memoryview_struct_module.<locals>.frY  y@      @z-21.1gffffff(@r   r   rs  >   r   r   r   r4   r3   r;   rY  TF   as   abcrF   abcc                 S   s   | S rs   r;   )rX   r;   r;   r<   <lambda>	  r~   zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rf   rM   r  r   r   r*   )objectr   r	   Ellipsisr   rw   r   r   rM   r5   r   r*  rg  r7   r  r   r  ZassertIsNot)r  r  r  r   valuesr8   ro   rp   r.  r  rD   r   r   Z
struct_errZmv_errr  r;   r;   r<   test_memoryview_struct_module	  sH    

z0TestBufferProtocol.test_memoryview_struct_modulec                 C   s>   t g ddgdgd}| |j t|}| t|jd d S )Nrs  r  r   r   r   )r   r  rK  rM   rT  r  r  )r  r.  r  r;   r;   r<   !test_memoryview_cast_zero_strides
  s    z4TestBufferProtocol.test_memoryview_cast_zero_stridesc              	   C   s0  t D ]}tdrd| n|}t|}t D ]^}tdr>d| n|}t|}tttdd| g|d}t|}| t	|j
|d| g q*qtdD ]\}}	}
t|	dg|d}t|}tdD ]T\}}
}
t|s| t|j
|d| g qt|st|s| t	|j
|d| g qqtd}td}tttd| dd|gdd}t|}| jt	|j
dd|gdd tttd	g d
d}t|}| t	|j
 | t	|j
ddd | t	|j
i  | 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ddgdgdd}t|}| t|jd | t|jdd | t|j tttd	g d
d}t|}| jt	|j
di d tttd	d	gd}t|}| jt|j
ddgd | jt|j
ddgd | jt|j
dg dd | jt|j
dg dd | jt	|j
dg d d ttd!d" td#D g d$d}t|}| jt	|j
d%g d&d tttd'd'gd%d}t|}| t|j
d%dgd'  ttd(d" td#D d#gd}t|}| jt	|j
d%g d&d ttd)d" td#D d#gd}t|}| jt	|j
dg d&d tttd'd'gd%d}t|}tttd'd'gdd}t|}tjd*kr| t	|j
dg d+ | t|j
dg d, | t|j
d%g d- nB| t	|j
ddd.g | t|j
dg d/ | t|j
d%g d0 d S )1Nr   r?   r/   r   r*   r   r   r   x   rw  r   r  Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIr  rr  ZIIr   r3   r   r)   r   )r   r  r   r   r   r4   r   )r   r  r   r   r   r4   r   )r   r  r   r   r   r4   rX   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r}   ]
  r~   zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i  )r  r   r4   rZ  r   )r   r  r   r   r"   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r}   g
  r~   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r}   l
  r~   l    )r4   r4   I   r,   iQ  iAj i	 )   r     r  r3   )r  r  r  r  r   )r  r  r/   r/   r   )r  r  r/   r3   r   )NON_BYTE_FORMATr   r5   rg  r   r   rb   rM   rT  r  r  rw   rz   r   rx   r  r  r  r
  r  r  maxsize)r  ZsfmtZsformatZssizeZdfmtZdformatZdsizer.  r  Zsitemsrm   Zsize_hZsize_drD   r  rl  rm  r;   r;   r<   test_memoryview_cast_invalid
  s    



 "  z/TestBufferProtocol.test_memoryview_cast_invalidc                    sl  ddd fddd fddd ff  fdd	}t d
}tdg d
d}t|dd\}}t|}|d}j||dddd|d|dd
 t d
}tdg| |gdd}t|d
|g d\}}t|}|jd
g d}j|||d
dddd|dd
 tddD ],\}	}
}t|	|
}t|}||||
|	 qtddD ]4\}	}
}t|
dg|	t	d}t|}||||
|	 q2d S )Nr   c                 S   s   t |  S rs   r   r   r.  r;   r;   r<   r  
  r~   z9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>r   c                 S   s   dd t |  D S )Nc                 S   s    g | ]}|d kr|d n|qS )r,   r!   r;   rV  r;   r;   r<   r}   
  r~   MTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>r  r  r;   r;   r<   r  
  r~   r   c                 S   s   dd t |  D S )Nc                 S   s   g | ]}t t|d qS )zlatin-1)rR   chrrV  r;   r;   r<   r}   
  r~   r  r  r  r;   r;   r<   r  
  r~   c           
         s   t |} D ]~\}}||}|| }j|| d|ddd| gd|dd
 ||}	|	|  |  }j|	| ||dddg|f|dd
 qd S )Nr*   Fr0   r  T	rW   r   r8   r+  r   r   r   rc   r  )r5   rg  r  rC  r*  r
  )
r.  rD   ro   r8   srcsizerN   Zto_bytelistrm  rc   m3Zbytespecr  r;   r<   iter_roundtrip
  s"    



z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtripr   rY  r   r*   Tr  r  r   r   r;   r0   rK   rM   r  )
r5   rg  r   r   rM   r  rC  rw   rK   r   )r  r  r	  r.  Z	destitemsZ	destshaperD   rm  Zdestsizer8   ro   rm   r;   r  r<   test_memoryview_cast
  sB    





z'TestBufferProtocol.test_memoryview_castc                 C   s  t  D ]}td D ]}|dkr$qdtd | }t|}t|| }t|rVdnd}t||D ]:\}}}	t|}
|dkr|gng }||
g }t|||d}t	|}t
||||d	\}}|d u r| t|j|| qd|d
krqdt|||d}|j||d	}t|}|j}| }| j||||d||||dd
 ||}|j||d	}t|}|j}| }| j|||
|d||||dd
 | j|||
|d||||dd
 qdqqtrG dd dtj}|dd}t	|}|d}| |j| | |jd | |jd | |jd | |j|jf | |jd | |jd td}t	|}|d}| |j| | |jd | |jd | |jd | |j|jf | |jd | |jd d S )Nr?   r   )r>   r?   r   rM   rN   r   r   r   r   Tr  c                   @   s    e Zd ZdejfdejfgZdS )z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrX   rB  N)r  r  r  ctypesc_longc_double_fields_r;   r;   r;   r<   BEPoint
  s   r  r   g33333i@r   r*   Fr  r;   333333?r   )r   rQ   r   r5   rg  r   rx   rw   r   rM   r   rT  r  r  r]   r   r
  rC  r  BigEndianStructurer*  rW   r   r)  r+  r   r   r   r  r  )r  Z_tshaperV   ZtfmtZtsizer   rW   r8   ro   rm   sizer   Ztshaper.  rD   Ztitemsr  rm  r   r   rc   r
  Zm4r  pointrl  rX   r;   r;   r<   test_memoryview_cast_1D_ND
  s    







z-TestBufferProtocol.test_memoryview_cast_1D_NDc                 C   s2  t  dttdd}t|}| || | | |  |dd d }|dd d }| || | | |  tttdg ddd	}t|}| | |  td
dgdgdd	}t|}| t|j tdgdgdd	}t|}| t|j tg dddgdd	}t|}| t|j d S )Nr   r  r   r   r  i	  )rZ  r   r4   r  r   r   r   )r   r   )r4   rZ  r9      12345r*   r   )r     b   c   d   e   f)	rK   r   rb   rM   r*  r
  r   rT  r  )r  rF   rD   r.  r;   r;   r<   test_memoryview_tolist  s(    z)TestBufferProtocol.test_memoryview_tolistc                 C   sH   t td}| }| |d |  | }| |d d S )NrY  z<memoryz	<released)rM   r   __repr__r;  
startswithr  )r  rD   r   r;   r;   r<   test_memoryview_repr2  s    z'TestBufferProtocol.test_memoryview_reprc                 C   s|   dD ]J}t d}t|d|dg}t|}| d| | d| | d| qtdg dd}t|}| ttdt  d S )	N)r   r   g            ?g      @g      "@r   r{  z9.0 in m)	floatrK   rM   ZassertInr   rT  r  r  r  )r  r8   infr.  rD   r;   r;   r<   test_memoryview_sequence;  s    z+TestBufferProtocol.test_memoryview_sequencec                 c   sL   |  t}d V  W d    n1 s&0    Y  | t|jd|f  d S )Nz#index out of bounds on dimension %d)rT  r  r*  str	exception)r  dimcmr;   r;   r<   assert_out_of_bounds_errorI  s
    $z-TestBufferProtocol.assert_out_of_bounds_errorc              	   C   sN  t dg dd}t|}| |d d | |d | | |d | | t|jd t dg dd}t|}| t|jd t ttd	d	gt	d
}t|}| t
|jd | t|jd | t|jd | t
|jd | t
|jd t ttdddgt	d
}t|}| |d d | |d d | |d d | |d d | |d d dD ]@}| jdd ||df  W d    n1 s0    Y  qbdD ]@}| jdd |d|f  W d    n1 s0    Y  q| t
|jd | t
|jd | t|jd  | t|jd! | t|jd | t|jd d S )"N      )@r   r   r;   .r   rs  Ziiir4   r]  r)          @        r3   rW  r  r   r   r   )r   r   rv  rZ  r   r   )rz  r\  r  r\  r*   r(  r   r  r   r)   r   r(   r   r   r   r-  r-  )r   rM   r*  rT  r  r  r  r   rb   r   r  r*  )r  r.  rD   indexr;   r;   r<   test_memoryview_indexP  sF    00z(TestBufferProtocol.test_memoryview_indexc              	   C   s  t dg dtd}t|}d|d< | |d d d|d< | |d d | t|jdd	 t ttd
d
gd}t|}| t|jdd t ttd
d
gtd}t|}| t	|jdd | t|jdd | t|jdd | t	|jdd | t	|jdd t
d D ]r}|dks|dkr(qt g ddg|td}t|}tdd}d||< | || d | || ||  qt g ddgdtd}t|}d|d< | |d d t g d dgdtd}t|}d!|d"< | |d" d! t d#gd"gdtd}t|}| t|jdd$ t ttd%g d&td}t|}t
d  D ]\}}|dkr^qH|dkrlqH||}|\}	}
|d'ks|dkrdd(  dd(  }	}
|d)kr| t|jd|	d"  | t|jdd* | t|jd|
 qH|d}| t|jdd+ t ttd"d"gd,td}t|}| t|jdd" t d-gd"gd.td}t|}| t|jdd" t ttd/dd0gtd}t|}d1|d2< | |d d" d1 d3|d4< | |d d d3 d5D ]@}| jd"d6 d||df< W d    n1 s0    Y  qd7D ]@}| jdd6 d|d|f< W d    n1 sD0    Y  q| t	|jd8d | t	|jd9d | t|jd:d | t|jd;d | t|jdddg d S )<Nr+  r   r  g     6@r;   g     7@.r   g333338@r4   r   r   rf   r]  r)   rY  r,  r-  rZ  r.  r  r3      r?   r   r   rs  r  rz  )   1   2   3r  )TFTTr*      xr   r  rw  r   r  r   r}  s   ZxLr  r   rW  r   *   )r   r*   +   r0  r1  r2  r3  r4  r(   r5  r6  )r   r   rM   r*  rT  r  r  r   rb   r  rQ   r   r)  ro   r  r   r  r*  )r  r.  rD   r8   r   r  rl  Z_rangerm  lohir7  r;   r;   r<   test_memoryview_assign  s    





00z)TestBufferProtocol.test_memoryview_assignc              	   C   s  t ttddgtd}t|}| t|jtddd | t|j	tdddt
ddg | t|jd t ttddgtd}t|}| t|jtdddtdddf | t|j	tdddtdddft
ddg | t|jtdddi f | t|j	tdddi ft
ddg | t|j	tddddg dtfD ]l}t ttddgdgdt|B d	}t ttd
dgdg|d}t|}t|}|dd |dd< |dd |dd< | || | || |dd d d d |dd d d d< |dd d d d |dd d d d< | || | || |ddd d d d |ddd d d d< |ddd d d d |ddd d d d< | || | || qBd S )NrW  r]  r   r   r*   r;   r   rZ  )r   r   r   r      ru  r   r  r   r[  )r   r   rb   r   rM   rT  r   r  r   r  r   r  r  rf  r*  )r  r.  rD   r   rj  rk  rl  rm  r;   r;   r<   test_memoryview_slice  sN    


 
((,,z(TestBufferProtocol.test_memoryview_slicec                 C   s  dd }t ddD ]}t|dD ]\}}}t|D ]}t|D ]}t||}t||}	t|	}
| |
| | |
 |  | |
 |  | t|
t| || ||	|
| d }d }z0|| }|| }|| ||< t|t|k}W n, t	y& } z|j
}W Y d }~n
d }~0 0 d }z|
| |
|< W n, t	yh } z|j
}W Y d }~n
d }~0 0 |r~| |t qD|s|r| || qD| |
| | |
 |  | |
 |  || ||	|
| qDq6q"qd S )Nc           	      S   s   t |D ]\}}|| }|| }| || |||< ||krV| || | || n| || | || |||< | || | || |||< |||< qd S rs   )r   r*  assertNotEqual)	ZtestcaserF   r   rD   
singleitemr   rm   Zaimir;   r;   r<   cmptest,  s    z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr*   r   rK   )rb   rw   r   rK   rM   r*  r
  r   r]   r  r  r)  r   )r  rH  r   r8   ro   rF  r  r  rF   r   rD   Z	array_errZhave_resizeZalarr  Zm_errr;   r;   r<   test_memoryview_array*  sF    z(TestBufferProtocol.test_memoryview_arrayc           	      C   s  t  dg d}t  dg d}t|}t|}dD ]0}| t|||t | t|||t q4t|}|  | || | || | || t|}t|}|  | || | || t|}| |g d tdgdgdt	d}d	t
d
f|d< | t|| t  dd}t|}| || | || trG dd dtj}|dd}t|}t|}| || | || | || | t|j d S )Nr   rs  )r*   r   r4   )__lt____le____gt____ge__r/  r*   zl x d xr  r   r   r   ur}  c                   @   s    e Zd ZdejfdejfgZdS )zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrX   rB  Nr  r  r  r  r  r  r;   r;   r;   r<   r    s   r  r   r  )rK   rM   r)  getattrr  r  r*  rE  r   r   r#  r  r  rT  r  r
  )	r  rF   r   r   wattrr  r  r  r;   r;   r<   %test_memoryview_compare_special_casesh  sF    
z8TestBufferProtocol.test_memoryview_compare_special_casesc                 C   sn  t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d|d< | || | || | || | || | || | || | || | || t tt	ddgtt
B d	}t tt	ddgtt
B d	}t|}| || d
|d< | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d S )N  @Lr   r   r  i  r;   rW  r]  r   rY  )rU  r  r  ZLf5sZhf5s)i?r  r  )r   r   rM   r*  r  __ne__rE  r  r   rb   rf  )r  nd1nd2r   rR  r  r.  rD   r;   r;   r<   !test_memoryview_compare_ndim_zero  sj    z4TestBufferProtocol.test_memoryview_compare_ndim_zeroc                 C   sX  t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  d S )N@    'r   @hr   )r\  r]  r^  r_  i  z<iz>h)r\  r^  r`  r  r   r   r[  z!hz<lr  zh  0cz>  h)r   rM   r*  rE  rf  r  rX  rY  r   rR  r;   r;   r<    test_memoryview_compare_ndim_one  sl       z3TestBufferProtocol.test_memoryview_compare_ndim_onec                 C   s   t ddgdgdd}t ddgdgdd}t|}t|}| || | || | || | || | || t ddgdgdd}t ddgdgd	d}t|}t|}| || | || | || | || | || d S )
N  r~  r   ra  r   i|i?z= h0cz@   ir   rM   r*  rb  r;   r;   r<   "test_memoryview_compare_zero_shape$  s$    z5TestBufferProtocol.test_memoryview_compare_zero_shapec                 C   s   t g ddgdd}t dgdgdgdd}t|}t|}| || | || | || | || | || t d	gd dgd
d}t d	gdgdgdd}t|}t|}| || | || | || | || | || d S )N)rd  rd  rd  rd  r   rV  r   rd  r   r   ry  )rd  rd  z@ Liz!L  hre  rb  r;   r;   r<   $test_memoryview_compare_zero_strides>  s$    z7TestBufferProtocol.test_memoryview_compare_zero_stridesc           
      C   s   d}t d D ]n}t|dd|\}}}dtfD ]L}t||g||d}t|}| || |d d d }t|}| || q,qd}tdD ]h}	t|\}}}dtfD ]L}t||g||d}t|}| || |d d d }t|}| || qqd S )	Nrf   rE   rM   r?   r   r  rz  r   )rQ   rr   rf  r   rM   r*  rb   )
r  r   rV   r8   ro   rF  r   r  rD   rm   r;   r;   r<   &test_memoryview_compare_random_formatsX  s(    z9TestBufferProtocol.test_memoryview_compare_random_formatsc                 C   s  t ttddg ddd}t ttddg ddd}t|}t|}| || | || | || | || | || t dgd g dd	d}t d
gd g ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t dgd ddgdd}t dgd ddgdd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr2   r  r   r   ra  r   r   r  r   r*   r   z=f q xxL)g333333r*   r   z< f 2Qr  r      r  r4   z! b B xLz
= Qx l xxLr   r   r   rb   rM   r*  rE  rb  r;   r;   r<   "test_memoryview_compare_multidim_ct  sZ    z5TestBufferProtocol.test_memoryview_compare_multidim_cc                 C   s  t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t dgd	 d
dgdtd}t dgd	 d
dgdtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttdg ddtd}t ttdg ddtd}t|}t|}| || | || | || | || | || d S )Nri  r2   )r   r   r  ra  r  r   r  r   r   r   r   r  =Qq)r   r   z=qQr  r   rj  Z0llrG   )r   r   rb   r8  rM   r*  rE  rb  r;   r;   r<   (test_memoryview_compare_multidim_fortran  s    z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc           	      C   sP  t tdd}t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || dgd	 }d
|d< t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || tt tdddgdd}|dddd d df }tt tdddgdd}|dddd d df }t|}t|}| || | || | || | || | || tdgd ddgdd}|dddd d df }tdgd ddgdd}|dddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tdgd	 g d d!d}|ddd d df }tdgd	 g d d"d}|ddd d df }t|}t|}| || | || | || | || | || d S )#Nri  r2   rj  @lr   r   r  )gffffff
r=  r  )grs  r=  r   zd b czd h c(   r3   @Ir  r*   r   r[  r   )r   r$      rZ  r   z=iiz>iir  r   r   )r   r|  r   r  r   Zb3sZi3s)r   rb   r   r   r8  rM   r*  rE  )	r  lst1lst2rX  rY  r   rR  rj  rk  r;   r;   r<   &test_memoryview_compare_multidim_mixed  s    
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedc                 C   s  t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr  )r   r  r   r   r   )r   r   r   z@irm  rb  r;   r;   r<   +test_memoryview_compare_multidim_zero_shape[  s$    z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapec                 C   s  t dgd g ddd}t dgg dg ddd}t|}t|}| || | || | || | || | || | | |  t d	gd
 ddgdd}t d	gddgddgdd}t|}t|}| || | || | || | || | || d S )Nrd  r   )r   r   r   rV  r   r5  r   ry  r  rf   r   r   z=lQr   z<lQ)r   rM   r*  r
  rb  r;   r;   r<   -test_memoryview_compare_multidim_zero_stridesu  s&    z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc           	      C   s^  t ttdddgdd}|dddd d d	f }t ttdddgd
td}|dddd d d	f }t|}t|}| || | || | || | || | || t dgd ddgdtd}d|d d< |dddd d d	f }t dgd ddgdttB d}d|d d< |dddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}|ddd d d	f }t dgd g ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddtd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}d|d d d< |ddd d d	f }t dgd g ddttB d}d|d d d< |ddd d d	f }t|}t|}| || | || | || | || | || ttdd }t|g d}t |g dd!td}t |g dd"t	tB d}t|}t|}| || | || | || d#gd }d$|d%< t|g d}t |g dd&td}t |g dd't	tB d}t|}t|}| || | || | || d S )(Nrt  r   r3   ru  r   r  r*   r   r[  r   r  ro  rp  )r*   r[  r   r4   z>Qqr  r  r   rj  )r  r   ZBbrw  r   )s   hellor~   r*      )r  r  r  Z5s0sP)s   sushir~   r*   ri  r2   rr  r   )s   sashimis   slicedg4@)s   ramens   spicygfffff"@rZ  z
< 10p 9p dz
> 10p 9p d)
r   r   rb   rf  rM   r*  rE  r   r   r8  )	r  rj  rX  rk  rY  r   rR  rx  ry  r;   r;   r<   +test_memoryview_compare_multidim_suboffsets  s    
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetsc                 C   sT  dD ]H}t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || qd S )N)rB   r@   rA   rC   r'   r  )r  r   r   r   r   r   r   r  r*   r   r  )r'   r%   r#   ZQLH)r*   r*   r*   )r   r   r8  rM   r*  rE  )r  	byteorderrX   rB  rF   r   r;   r;   r<   !test_memoryview_compare_not_equal  s6    
z4TestBufferProtocol.test_memoryview_compare_not_equalc                 C   s  t  dg d}t|}|  | tt| | t|jd | tt| | t|j | t|j | tt	dt
  | t|jd | t|jdd dD ]}| t|j| qt  dg d}t|}t|}| || |  | || | || | || d S )Nr   )g?g@gffffff
@r   z1.0 in mr   r*   )rW   r   r+  r   r   r   r   r   r  rK  rM  rO  )rK   rM   r  rT  r   r  r   r
  r   r  r  r  r  __getattribute__r*  rE  )r  rF   rD   rS  r   rl  rm  r;   r;   r<   test_memoryview_check_released.  s*    z1TestBufferProtocol.test_memoryview_check_releasedc                    sP  d t  dgdd}t|}| || | | |  t  gdgdd}t|}| || | | |  t  fddtd	D g d
dd}t|}| || | | |  t  fddtdD g ddd}t|}| || | | |  trLG dd dtj}|dd}t|}| | t| d S )Nr[  r   ra  r   r*   z>hQiLlc                    s   g | ]} qS r;   r;   r   rv   r;   r<   r}   e  r~   z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rW  r  z=hQiLlc                    s   g | ]} qS r;   r;   r   r  r;   r<   r}   j  r~   r  )r   r   r   r  r   z<hQiLlc                   @   s    e Zd ZdejfdejfgZdS )z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrX   rB  NrP  r;   r;   r;   r<   r  s  s   r  r   r  )r   rM   r*  r   rb   r  r  rR   )r  r  rD   r  r  rF   r;   r  r<   test_memoryview_tobytesV  s0    $
z*TestBufferProtocol.test_memoryview_tobytesc                 C   s  |  tti td |  ttdtd tg ddgdgd}|  tt|td tdd	d
d}dD ],}t|t|}| || | |d	 d q^tdd	d
td}dD ],}t|t|}| || | |d	 d qdD ]\}d|d	< t|t|}| || | |d	 d d|d	< | |d	 d | |d	 d qtdgdgd
td}dD ]>}t|t|}|  t	|j
d | || | | g  qHtttdg dd
td}dD ]*}t|t|}| t| g g g qtdgdgdtd}dD ]2}t|t|}| || | | |  qtg ddgdtd}dD ]2}t|t|}| || | | |  q:tg ddgdgtd}dD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d q|d d d }dD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d qtttdddgtd}dD ]*}t|t|}| t| |  q|  tt|td t|t|}| t| |  tttdddgttB d}dD ]*}t|t|}| t| |  q
|  tt|td t|t|}| t| |  tttdddgttB d}dD ]<}|  tt|t| t|t|}| t| |  qtg ddgdgd}t|td}| |j d S )Nr  r=  r   rs  r   r   r  rY  r;   r   r   r  r  rf   r*   r   r3   r  r   r  r   ru  r  r   rW  r   r]  )r   r  )r  r  rw  )rT  r  r9  r:  r4  ZPyBUF_WRITEr   r*  r   r  r  r
  r   rb   r  r8  rf  r;  rK  )r  r  r(  rD   r;   r;   r<   test_memoryview_get_contiguousy  s    z1TestBufferProtocol.test_memoryview_get_contiguousc                 C   s   t d}tdg d}t|}t|}td| }|| | |	 | t d}t
ttdg ddd}t|}t|}td| }|| | |	 | d S )Nr   rw  r   r   rW  r  r   )r5   rg  rK   rM   ioBytesIOr   readintor*  r   r   r   rb   )r  r  rF   rD   r   r   r  r;   r;   r<   test_memoryview_serializing  s    





z.TestBufferProtocol.test_memoryview_serializingc           	      C   s  t ttd}t|}| t|t| |jdddgd}| t|t| |d d d }t ttdd d d }| t|t| tttdg dtd	}t|}| t|t| tttdg dd}|d d d
d d d d df }t|}| t|t| tttdg dt	d	}|d d d
d d d d df }t|}| t|t| tttddgdd}t|}tttddgdd}t|}| || | t|t| tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j d S )NrW  r   r  r   r   r[  r  rj  r]  r   r   )r   r   r  r   r   r   r  r   r  r   r   z= Lz< h)rR   r   rb   rM   r*  r  r  r   r8  rf  rT  r   __hash__)	r  r   rD   ZmcZmxr  rX   rF   rB  r;   r;   r<   test_memoryview_hash  sJ    z'TestBufferProtocol.test_memoryview_hashc                 C   s|  t g d}t|}t|}| t|j ~|  t g d}t|}t|ttd}t|ttd}| |j	| | t|j ~~|  t g d}t|}t|}t|}|  | t|j ~|  t g d}t|}t|}t|ttd}t|ttd}| |j	| |  | t|j ~~|  tg ddgt
d}t|}|jg ddgd t|}t|}| | |  t|}	| |	 |  | |	 |  |  |	  |  | | |  ~|  |  dd	 }
t d
}t|,}|
| | |d td W d    n1 sF0    Y  tttdg ddd}t|ttd}	t|	ttd}| |j	| t|>}|
| | |dd  g dg dgg W d    n1 s0    Y  dtfD ]
}t d
}t|}~t|t|d}	t|	}~	t|t|d}t|h}~|
| |
| |
| | |d td | |d td | |d td ~W d    n1 s0    Y  ~W d    n1 s0    Y  ~W d    n1 s0    Y  t d
}t|}~t|t|d}	t|	}~	t|t|d}t|l}~|
| |
| |
| | |d td | |d td | |d td ~~~W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  qt d
}| tR t|(}t|}|d tdk W d    n1 sN0    Y  W d    n1 sn0    Y  d S )Nrs  rH  r   r  r]  )r   r   r   r4   r3   r   r   c                 S   s,   t | }W d    n1 s0    Y  d S rs   )rM   )r   rm  r;   r;   r<   catch22  s    
z;TestBufferProtocol.test_memoryview_release.<locals>.catch22r|  r   r:  rW  r  r   r   r*   rk  )r  r   r   r;  r   r<  )r   rM   r   rT  r4  r  r>  ND_REDIRECTr)  rW   r  r  r*  r
  r  ordr   rb   )r  rF   rD   r  rX  rY  rl  rm  rX   rB  r  r  r   r
  r.  r;   r;   r<   test_memoryview_releaseH  s    
4
F


"""


j
z*TestBufferProtocol.test_memoryview_releasec                 C   s0  t dd tdD dgdd}tddd tdD }||fD ]}t |ttd}t |ttd}t|}| |j| | |j| | |j| | || | || | || | |dd	 |dd	  | |dd	 |dd	  | |dd	 |dd	  ~~| |dd	 |dd	  q@d S )
Nc                 S   s   g | ]}d | qS r"  r;   rV  r;   r;   r<   r}     r~   z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rW  r   r   c                 S   s   g | ]}d | qS r  r;   rV  r;   r;   r<   r}     r~   r  r*   r  )	r   rb   rK   r>  r  rM   r)  rW   r*  )r  r  rF   rX   rB  r  rD   r;   r;   r<   test_memoryview_redirect  s"    z+TestBufferProtocol.test_memoryview_redirectc                 C   s  d}g d}|  ttddd t }t|}| j||d|dddgdg|d	 tdD ]}| || | qT~~t }t|}~~t }t|td	}t|td	}t|}| 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~t }t|ttd
}t|ttd
}t|}| 	|j
| | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|}| j|d d|dddgdg|d	 tdD ]}| || | q~~tdd}t|}~~tdd}t|td	}t|td	}t|}| 	|j
d  | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|ttd
}t|ttd
}t|}| 	|j
d  | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~d S )Nr   )r   r*   r   r  r   r   r   r4   r3   rY  rf   rZ  r*   r   r  TrW  r  rG  r  )Zlegacy_mode)rT  r  ZstaticarrayrM   rC  rb   r*  r   r>  r)  rW   r  )r  r8   rc   rX   rB  r   r  rD   r;   r;   r<   $test_memoryview_from_static_exporter  s    









z7TestBufferProtocol.test_memoryview_from_static_exporterc                 C   s*   t g ddgttB d}| tt| d S )Nrs  r  rq  )r   rh  ZND_GETBUF_UNDEFINEDrT  r4  rM   r  r;   r;   r<   #test_memoryview_getbuffer_undefined>  s    z6TestBufferProtocol.test_memoryview_getbuffer_undefinedc                 C   s&   t g ddgtd}| tt| d S )Nrs  r  r]  )r   rh  rT  r4  rM   )r  rX   r;   r;   r<   test_issue_7385D  s    z"TestBufferProtocol.test_issue_7385c                 C   s&   dD ]}|  t|t| qd S )N)r>   iiZ3s)r*  	_testcapiZPyBuffer_SizeFromFormatr5   rg  )r  r   r;   r;   r<   test_pybuffer_size_from_formatH  s    z1TestBufferProtocol.test_pybuffer_size_from_formatN)F)Ur  r  r  r  rC  rU  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r!  r%  
contextlibcontextmanagerr*  r8  rB  rD  rJ  rT  rZ  rc  rf  rg  rh  rn  rq  rz  r{  r|  r~  r  r  r  r  r  r  r  r  r  r  r  Zcpython_onlyr  r;   r;   r;   r<   r    s    K
0i 
-'	0.Y #6!)>		% qu8	s<T	
5m8>5?H>Ha (#u< ^r  __main__)r   NN)r   )r   )r   r1   r;   )r   r1   )r   )N)F)F)r   )N)gr  Zunittesttestr   	itertoolsr   r   randomr   r   r   warningsr  rK   r  osZdecimalr   Z	fractionsr	   Z_testbufferImportErrorr   r5   r  ZEnvironmentVarGuardenvironcatch_warningsrL   r   r  Z
SHORT_TESTZNATIVEr6   r7   ZSTANDARDr=   rQ   r8   copyry   ZARRAYr  ZBYTEFMTrh   rj   rg   rY   r`   rd   rq   rr   rt   rw   rx   rz   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r  r  r  r  Z
skipUnlessZTestCaser  r  mainr;   r;   r;   r<   <module>   sf   


P

	
	





	



	
?
+

!





                            o
