Your IP : 216.73.217.129


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_buffer.cpython-35.opt-2.pyc



���]P{�K@s�ddlZddlZddlmZddlmZmZddlmZm	Z	m
Z
ddlmZddl
Z
ddlZddlZddlZddlmZddlmZyddlTWnek
r�dZYnXyddlZWnek
r
dZYnXyddlZWnek
r8dZYnXy(e
j��dd	lmZWdQRXWnek
r{dZYnXd
Zddddd
dddddddddddddddddddddddddiZer�ed=ed=erDy0ejdddd�ded<ded<Wnej k
rCYnXdddfdddd>fd
dd >dd >fdddd>fddd!>dd!>fdddd">fddd#>dd#>fdddd$>fddd#>dd#>fdddd$>fddd%>dd%>fdddd>fddd%>dd%>fddd&>dd&>fiZ!d'd(�Z"d)ed*ed+e!d,e!d-e!d.e!iZ#er�x&e#d*D]Z$e"e$�e#d*e$<q�Wej%�Z&ej%�Z'x"eD]Z(e(d/kr�e'e(=q�Wej%�Z)x"eD]Z(e(d0kre)e(=q�We&e#d1<e&e#d2<e'e#d3<e)e#d
<e)e#d4<dZ*dZ+d5d)d*d+d,d-d.gd)d6d7d8gfd9d3gd)gfd:d)gd)gfd;d2d1gd)gfd<d4d
gd)gfiZ,d=d>�Z-d?d@�Z.dAdB�Z/dCdD�Z0d5dddEdF�Z1d5dGdH�Z2d5dIdJ�Z3dKdL�Z4dMdN�Z5dOdP�e#d*D�Z6dQdR�Z7dSdT�Z8dUdV�Z9dWdX�Z:dYdZ�Z;d[d\�Z<d]d^�Z=d_d`�Z>dadb�Z?dcdd�Z@dedf�ZAdgdh�ZBdidj�ZCdkdl�ZDdmdn�ZEdodp�ZFdqdr�ZGdsdt�ZHdudv�ZIdwdx�ZJdydz�ZKd{d|�ZLd}d"fd~d�ZMd�d��ZNd�d��ZOd}d"d�d��ZPd�d��ZQdd�d��ZRd�d��ZSdd�d��ZTd�d��ZUd�d��ZVd�d��ZWd�d�d��ZXd�d�d��ZYd}d�d��ZZdd�d��Z[d�d��Z\d�Z]d}Z^d�Z_er�d�Z]d�Z^d�Z_eYZVeZZWe[Zej`ed��ej`ed��Gd�d��d�eja���Zbecd�kr�ejd�dS)��N)�support)�permutations�product)�	randrange�sample�choice)�get_config_var)�Decimal)�Fraction)�*)�ndarrayT�?�c�b�B�h�H�i�I�l�L�n�N�f�d�P�Q��@��q������ �?i�cCs�|dkrd}n�|dkr*d}n�|dkr?d}n�|d	krTd}nuxCdD];}ytj|d|>d�PWq[tjk
r�Yq[Xq[W|d@r�d|>d|>fn
dd|>f}|S)Nrr�r
rrrr'ri���rr&r%r$r#r!r")rr()rrl����)r+r,lE����E)r-r.)
r)r*rr'r&r%r$r#r!r")�struct�pack�error)�fmt�lh�exp�r5�0/opt/alt/python35/lib64/python3.5/test_buffer.py�native_type_rangeWs 				
	/r7��@�<�>�=�!Z
bBhHiIlLfdZBbc�mz@m�az@br�1�2�3�array�numpy�
memoryview�bytefmtcCs�tt||�}|dkrMt|g�}|dkrM|dkrMd}|dkret|�}|dks}|dkr�tj||�}tj||�d}|S)	NrrDssr
rrr)r�fmtdict�bytes�boolr/r0�unpack)�mode�char�obj�xr5r5r6�
randrange_fmt�srOcCsi|jd�\}}g}x'|D]}|jt|||��q"Wt|�dkr_|dSt|�S)N�#rr)�split�appendrO�len�tuple)r2rMrK�charsrNrr5r5r6�gen_item�s

rVcCsT|dkrt||�Sdg|}x't|�D]}t||�||<q3W|S)Nr)rV�range)rr2rM�lstrr5r5r6�	gen_items�s

rYcCs�tt|t�}|d}|jd�}tdd�}xlt|�D]^}ttt|��}tt|t�}||t	|r�|nd�7}|||7}qIWt
|||�}	t||�}
||	|
fS)NrP�ambr�
r)r�cap�MODE�striprrWrTrG�MULT�intrYrV)rrMrKZxfmtr2�nmemb�_rL�
multiplier�items�itemr5r5r6�struct_items�s
 rfcCs�|dkr tt|t�}|dkrBttt|��}tt|t�}|d|t|ro|nd�}t|||�}t||�}|j	d�||}|||fS)NrPrrZ)
rr\r]rTrGr_r`rYrVr^)rrMrKrLrcr2rdrer5r5r6�	randitems�s$rgccsFx?t|tD]/}x&t|D]}t||||�Vq WqWdS)N)r\r]rGrg)rrMrKrLr5r5r6�	iter_mode�srhccsAxt||�D]}|VqW|dkr/dSt||�VdS)Nr)rhrf)�nitemsZtestobj�tr5r5r6�iter_format�s
	rkcCs"d|kp!d|kp!d|kS)Nrrrr5)r2r5r5r6�is_byte_format�srlcCsFt|�}|dks4|dkoE|ddkoE||dtkS)Nrrrr9)rS�
MEMORYVIEW)r2rNr5r5r6�is_memoryview_format�s(rncCs"g|]}t|�s|�qSr5)rl)�.0rr5r5r6�
<listcomp>�s	rpcCst|t�S)N)�
isinstance�list)rXr5r5r6�atomp�srscCs
t|t�S)N)rqrr)rXr5r5r6�listp�srtcCsIt|�dkrdS|d}x"|dd�D]}||9}q1W|S)Nrr)rS)rXrN�vr5r5r6�prod�s
rvcCs�|dkrfS|dkrst|dd��|g}x�t|ddd�D]}||||d9<qPWnM|gt|dd��}x-td|�D]}||||d9<q�W|S)Nr�Crr���rxrx)rrrW)�ndim�shape�itemsizeZlayout�stridesrr5r5r6�strides_from_shapesr}cCs�t|�r|St|�dkr*|dSdg|d}|drYt|�|dnd}xMt|d�D];}||}t||||�|dd��||<qpW|S)Nrr)rsrSrW�_ca)rd�srX�strider�startr5r5r6r~s$
/r~cCs�t|�r|St|�dkr*|dSdg|d}|d}xBt|d�D]0}t||d|�|dd��||<qVW|S)Nrr)rsrSrW�_fa)rdrrXr�rr5r5r6r�s
.r�cCsIt|�r<d|kr<t|�t|�kr<td��t||�S)Nrzprod(shape) != len(items))rtrvrS�
ValueErrorr~)rdrzr5r5r6�carray*s0r�cCsIt|�r<d|kr<t|�t|�kr<td��t||�S)Nrzprod(shape) != len(items))rtrvrSr�r�)rdrzr5r5r6�farray/s0r�cCsdd�|D�}t|�S)NcSsg|]}t|��qSr5)rW)rorur5r5r6rp6s	zindices.<locals>.<listcomp>)r)rz�	iterablesr5r5r6�indices4sr�cCs7d}x*t|�D]}|||||7}qW|S)Nr)rW)ry�indr|�retrr5r5r6�getindex9sr�c	Cs�|s
|St|�}t||dd�}t||ddd�dd�}dgt|�}xSt|�D]E}t|||�}t||ddd�|�}||||<qmW|S)Nrrwrrxrx)rSr}r�r�)	�srcrzryZsstridesZdstrides�destr��frZtor5r5r6�	transpose@s"r�cCsE|gkr|St|�r#|gSt|d�t|dd��S)Nrr)rs�_flatten)rXr5r5r6r�Os
r�cCst|�r|St|�S)N)rsr�)rXr5r5r6�flattenWsr�cCs@t|�rgSt||d�gt|d|dd��S)Nrr)rsrS�slice_shape)rX�slicesr5r5r6r�]sr�cs/t|�r|S�fdd�|�dD�S)Ncs)g|]}t|�dd���qS)rN)�
multislice)roZsublst)r�r5r6rphs	zmultislice.<locals>.<listcomp>r)rs)rXr�r5)r�r6r�dsr�csWt|�r|S��fdd�t|�d|�d�D�}||�d<|S)Nc	s?g|]5\}}t||�dd��dd���qS)rN)�m_assign)ror�r)�lslices�rslicesr5r6rp{s	zm_assign.<locals>.<listcomp>r)rs�zip)�llst�rlstr�r�r5)r�r�r6r�js#r�cCs�t||�}t||�}t|�t|�kr:dSxFtt|��D]2}||||krkdS||dkrMdSqMWdS)Nrrrxrx)r�rSrW)r�r�r�r��lshape�rshaperr5r5r6�
cmp_structure�sr�cCs:t||||�dkr'td��t||||�S)Nrz+lvalue and rvalue have different structures)r�r�r�)r�r�r�r�r5r5r6�multislice_assign�sr�cs�|�rdS|dks*|�|kr.dSt�fdd��D��rQdS|dkru|dkot�ot�Sd�kr�dSt��fdd�t|�D��}t��fdd�t|�D��}d||ko�||�|kS)NFrc3s|]}|�VqdS)Nr5)roru)r{r5r6�	<genexpr>�sz#verify_structure.<locals>.<genexpr>Tc3s5|]+}�|dkr�|�|dVqdS)rrNr5)ro�j)rzr|r5r6r��sc3s5|]+}�|dkr�|�|dVqdS)rrNr5)ror�)rzr|r5r6r��s)�any�sumrW)�memlenr{ryrzr|�offset�imin�imaxr5)r{rzr|r6�verify_structure�s
((r�cCsx|D]}||}qW|S)Nr5)rXr�rr5r5r6�get_item�s
r�c
CsO|\}}}}}}|}x*t|�D]}	|||	||	7}q+W|S)N)rW)
r�rjr�r{ryrzr|r��prr5r5r6�memory_index�s
r�cCsn|\}}}}}}d|>}xEt|�D]7}t||�}	d|	>}
||
@r\dS||
O}q/WdS)NrTF)r�r�)rjr�r{ryrzr|r�Zvisitedr�rZbitr5r5r6�is_overlapping�s


r��cs�s	t|d�}|dkr�|r>|||ffdfStdd�}||}td�dkrp|n|}|||ff|fSd}	td�}
|
dkr�|r�d}	n|
dkr�d}	dg|�x:t|�D]}t|	|d��|<q�Wnt��}d}td�}
|
dkrC|
d@rCd	nd
}
dg|�|t||d��|d<|
r��|ddkr�|�|d<x�t|ddd�D]z}|�|dr��|dnd9}|
r|t||d��|<q�dtd�|td|d��|<q�Wd}}d�kr�t��fdd�t|�D��}t��fd
d�t|�D��}||}|r�||}||d|}n2|||}td�dkr�||n|}|||��|fS)Nrrr$r�d�_�Zr�TFc3s5|]+}�|dkr�|�|dVqdS)rrNr5)ror�)rzr|r5r6r�sz!rand_structure.<locals>.<genexpr>c3s5|]+}�|dkr�|�|dVqdS)rrNr5)ror�)rzr|r5r6r�s�rxrxrx)rrx)rrWrSr�)r{Zvalid�maxdim�maxshaperzryrir�r��minshaperrZ	maxstrideZzero_strider�r�r5)rzr|r6�rand_structure�sT
	
"
 &-
((
#r�c
Cs�||}t|d�}|r.|||nd}td|d�}|||}t|||�}t||�\}}}}	|	|kr�t�|S)Nr)r�slice�
slice_indices�RuntimeError)
�slicelenZlistlenZmaxstartr�Zmaxstep�step�stoprrbZcontrolr5r5r6�randslice_from_slicelens
r�cCs�dg|}dg|}xit|�D][}||}|dkrVtd|d�nd}t||�||<t||�||<q'Wt|�t|�fS)Nrr)rWrr�rT)ryrzr�r�rrr�r5r5r6�randslice_from_shape,s


%r�cCs;td|d�}d}td�}|dkr:d}n|dkrLd}td�dkrddnd	}dg|}dg|}dg|}dg|}	x~t|�D]p}t||d�}
t||d�}||
kr�|
|}}
|rot|
|
d�}t|
|
d�}
dtd�td|
d�}t||
|�}t||
�\}}}}n4|
dkr�td|
d�nd}t||
�}t||�}td�dkr�||
||<||<|||	|<||<q�|
|||<||<|||	|<||<q�W||t|�t|	�fS)Nrrr�r�rr��PTFrx)rrx)rrWr�r�r�rT)r�r�ryr�rZ
all_randomr�r�r�r�Zsmall�bigr�r�r�Zs_smallrbr�Zs_bigr5r5r6�rand_aligned_slices8s@	




!%r�cCs0|\}}}}}}t||d|d�S)NrPrD)rY)r2rjr�r{rbr5r5r6�randitems_from_structurecsr�c

CsD|\}}}}}}	t|d|d|d|d|	dt|B�S)Nrzr|�formatr��flags)r�ND_WRITABLE)
rdr2rjr�r�r{ryrzr|r�r5r5r6�ndarray_from_structureisr�cCs�|\}}}}}}t|�}	x4t|�D]&\}
}tj||	|
||�q1Wtd|	d|d|d|d|�S)N�bufferrzr|�dtyper�)�	bytearray�	enumerater/�	pack_into�numpy_array)rdr2rjr�r{ryrzr|r��bufr�rur5r5r6�numpy_array_from_structureosr�csH|j}|r2t|��|kr�d|fSnh|gkrf|jdksY�|kr�d|fSn4t|��\}}|g}|dkr�d|fS|j����fdd�tdt����D�}g}xG|D]?}	tj||	�d}
|
|
krd|fS|j	|
�q�W|gkr:||fS|d|fS)Nrcs$g|]}�||���qSr5r5)ror)r{�memr5r6rp�s	zcast_items.<locals>.<listcomp>�nan)
�nbytesrvry�divmod�tobytesrWrSr/rJrR)Zexporterr2r{rzZbytelenrr�Z	byteitemsrdrurer5)r{r�r6�
cast_items}s(	

	
.

r�c#s�xtd�D]}|gVq
Wtdd�}td�dkrFdnd��fdd	�t|�D�Vtdd�}td�dkr�dnd��fd
d	�t|�D�VdS)Nr&��r�r�rrcsg|]}t�d��qS)r�)r)rorb)r�r5r6rp�s	z!gencastshapes.<locals>.<listcomp>csg|]}t�d��qS)r�)r)rorb)r�r5r6rp�s	)rWr)rryr5)r�r6�
gencastshapes�sr�cCs:tt||d�t||d�t||d��S)Nr)rrW)rr5r5r6�	genslices�sr�cs)�fdd�t|�D�}t|�S)Ncs g|]}t�|��qSr5)r�)ror)rzr5r6rp�s	z"genslices_ndim.<locals>.<listcomp>)rWr)ryrzr�r5)rzr6�genslices_ndim�sr�FcCs>|s|dkrdnd}t||d�}t||�S)Nrr)rr�)r�allow_emptyZminlenr�r5r5r6�rslice�sr�ccs)x"td�D]}t||�Vq
WdS)Nr�)rWr�)rr�rbr5r5r6r��sr�c#s�x8t|�D]*}t�fdd�t|�D��Vq
Wx8t|�D]*}t�fdd�t|�D��VqHWtdd�t|�D��VdS)Nc3s|]}t�|�VqdS)N)r�)ror)rzr5r6r��szrslices_ndim.<locals>.<genexpr>c3s%|]}t�|dd�VqdS)r�TN)r�)ror)rzr5r6r��scss!|]}tddd�VqdS)rrN)r�)rorbr5r5r6r��s)rWrT)ryrzZ
iterationsrbr5)rzr6�rslices_ndim�s
((r�ccsBt|�}|dkr$t|�n|}tt||��VdS)N)rTrSr)�iterabler��poolr5r5r6�rpermutation�sr�cCs�y|j�}Wn$ttfk
r6|j�}YnXt|t�r[|j}|j}nd}d}td||j	|j
|j||j|j
|f�tjj�dS)N�unknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s))�tolist�	TypeError�NotImplementedErrorr�rqrr�r��printrzr|�
suboffsetsr�r{�sys�stdout�flush)�ndrNr�r�r5r5r6�
ndarray_print�s	r�r�r[�r�z%struct module required for this test.z%ndarray object required for this testc@s�eZdZdd�Zd�dhd�dhdhd�d�d�dddd�
Zddd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zd=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~�ZDdd��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPd�d��ZQd�d��ZRd�S)��TestBufferProtocolcCst�|_dS)N)Zget_sizeof_void_p�
sizeof_void_p)�selfr5r5r6�setUp�szTestBufferProtocol.setUprFc%s[|rt|��}n�s.t|	�}n�}f}
|jr|j|d�d}xStd|�D]B}||dkr�P||dkri|||||d7}qiW|gdd�t|d�D�}
|j}|ddkr�|}|gt|dd��}|j|j|�|j	|j
|�|j	|j��|j	|j��|j	|j
|�|j	|j|�|j	|jt|��|
o�|
s�|j	|jt|��|j	|jt|
��t|t�s�t��r"�r|j�n	|j�}|j	||	��s,dS|s�t�}d}x�t|�D]�}yet||�}t|	|�}t|t�r�tj�|�}ntj�|�}|j|�Wntk
r�d}PYnX|j	||�qNW|s�|j	|j�|�t|�}dj dd�|D��}|j	|j!�|��rO�nd	}t"|	�}xed
ddgD]T}|}|dkr�t#|d�s�t#|d
�rt$||�}t|d
|d|dt%�}nCt#|d�st#|d�r|d
krt|d
|d|�}t&|t'|��|j	�j�|�|j(t)�|��|dkrhqqt|�}|r�dnt*} t+|d
t,��|j	t��|�����fdd�t|�D�}!t|!d�dkrdd�|!D�}!t|!d
|d| d��}"|j	t|"�t|��t+|dt,��|j	t��|�����fdd�t|�D�}!t|!d�dkr�dd�|!D�}!t|!d
|d| t%Bd��}"|j	t|"�t|��t+|dt,��|j	t��|�����fdd�t|�D�}!t|!d�dkrldd�|!D�}!t#|d�r�t%nd}#t|!d
|d|#| Bd��}"|j	t|"�t|��qqWt��rWyt|�}Wntk
r�dSYnXt|t�r|jn|}$|j|j|$�|j	|j
|�|j	|j��|j	|j��|j	|j
|�|j	|j|�|j	|jt|��|
o�|
s�|j	|jt|��|j	|jt|
��|dkr�dn	t|	�}|j	t|�|��r+|j�n	|j�}|j	||	�|j	||�dS)NrrcSsg|]}d�qS)rrxr5)rorur5r5r6rps	z-TestBufferProtocol.verify.<locals>.<listcomp>Tr8css|]}d|VqdS)z%02xNr5)rorr5r5r6r�Nsz,TestBufferProtocol.verify.<locals>.<genexpr>rrw�F�Arzr�r�cs)g|]}tj��|���qSr5)r/�unpack_from)ror)�contigr2r{r5r6rpys	cSsg|]}|d�qS)rr5)rorur5r5r6rp|s	cs)g|]}tj��|���qSr5)r/r�)ror)r�r2r{r5r6rp�s	cSsg|]}|d�qS)rr5)rorur5r5r6rp�s	cs)g|]}tj��|���qSr5)r/r�)ror)r�r2r{r5r6rp�s	cSsg|]}|d�qS)rr5)rorur5r5r6rp�s	)-rvrSr�Z
assertGreaterrWr�rr�assertIsrM�assertEqualr�r{r��readonlyryrzrTr|rqrrnr�r�r�r��get_pointerr�r/r0�extend�BufferErrorrE�join�hexr��
is_contiguousr��
ND_FORTRAN�get_contiguous�
PyBUF_READ�
assertTrue�
cmp_contigr��py_buffer_to_contiguous�
PyBUF_FULL_RO)%r��resultrMr{r2r�ryrzr|rX�sliced�castZexpected_lenr�Z
suboffset0rZstride0ZreprZbuf_errr�Zitem1Zitem2rNr>rZffZ	flattened�order�expectedZtransra�roZinitlst�yr�exr5)r�r2r{r6�verify�s�	$		
	" 
	zTestBufferProtocol.verifycCs9dd�}dd�}|jr0||t�s�||t�rI|js�||t�rb|js�||t�r{|js�||t�r�|j	s�||t
�r�|js�||t�r�||t�r�|j
tt|d|�dSt|t�st|j�r|j�}nt|dt�}|j�}||t�rJdn|j}	|j}
|j}|j}||t�s�d}
|j�}||t�s�d}||t�r�|jnf}
||t
�r�|jnf}t|d|�}|j|d	|d
|d|
d|	d
|d|
d|d|d|�	dS)NcSs|jdkp|jdkS)Nr8r)r�)r	r5r5r6�
simple_fmt�sz4TestBufferProtocol.verify_getbuf.<locals>.simple_fmtcSs||@|kS)Nr5)�req�flagr5r5r6�match�sz/TestBufferProtocol.verify_getbuf.<locals>.match�getbufrr8rrMr{r2r�ryrzr|rXr)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�assertRaisesr�rrqrnr�r�rr{ryr�rzr|r
)r�Zorig_exr	rrrrrXr�rr2r{ryrzr|r5r5r6�
verify_getbuf�s>			z TestBufferProtocol.verify_getbufcs�tttttttttt	t
ttt
tf}dd�td�D�dfddddd	d
ddd
dddgdfddddd	d
ddd
dddgdfdd�td�D�dff}ggdfdddggdfdggdfdgd gdfd
gdgdfd
gd!gdfddggdfddgd"d#gdfddgddgdfddgd$d%gdff
}dttttBtttBf}dttttBf}x|D]\}}tj|��x�|D]�\}}	}
�fdd�|	D�}	|
�9}
x�|D]�}|	rB|t@rBq)|rV|t@rVq)|rb|n|d}t|d|d|d|d|	d|
�}
|r�|
ddd&�nd}t|
�}|r�t|�}|
jdks�|
jdkr
|r
|	r
|j||
�|r>|jdkr>|r>|	r>|j||�x�|D]�}x||D]t}|j|
|
||B�|j|
|||B�|rR|j||||Bdd�|j||||Bdd�qRWqEWq)Wq�Wq�Wddddd	d
ddd
dddg}t|ddgdt�}|jtt|�td
gdg�}t|dt�}|jtt|dt�|jtt|dt�|jtt|dt�|jtt|dt�|jtt|dt�|jtt|dt�t|dt�}x�dddgdddgfD]�}x�dtfD]�}t|d|d|tB�}|jt |d��|jt |d��xL|D]D}t|d|�}|jt |d��|jt |d��q�Wq3Wq WdS)'NcSs&g|]}|drdnd�qS)rTFr5)rorNr5r5r6rp�s	z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>�r
rrr�r�r�r�r"r!�	r[�rrcSs.g|]$}|dr d|nd|�qS)rr%lli�r5)rorNr5r5r6rp�s	rrcsg|]}|��qSr5r5)roru)r{r5r6rps	r�r�rzr|r�rTrr�rwrx������rxr!rxr )!rrr�PyBUF_SIMPLErrr�
PyBUF_FULLr�
PyBUF_RECORDS�PyBUF_RECORDS_RO�
PyBUF_STRIDED�PyBUF_STRIDED_RO�PyBUF_CONTIG�PyBUF_CONTIG_ROrWr�r��ND_PILrrr/�calcsizerrEryr�r�ND_GETBUF_FAILrr�r�r�)r��requestsZ	items_fmtZ	structureZndflagsZ
real_flagsrdr2rzr|r�r�Z_items�ex1�ex2�m1�m2r�bitsr	�baser�rr5)r{r6�test_ndarray_getbuf�s�	--"	


*!

*%
z&TestBufferProtocol.test_ndarray_getbufc
CsU
tdgdg�}tdgdgdt�}xHt|j|jfD]1}|jt|dddh�|jt|dddg�|jt|ddd)g�|jt|dddgddh�|jt|dddgddgd	dh�|jt|dddgddgd
g�|jt|dgddgdi�|jt|dgddgdi�|jt|dgddgdi�|jt|dgddgd	dgdt�|jt|dgdgdt�|jt|gddg�|jt|d
gddgdd�|jtj	|dgddgdd�|jt|dd*gddgdd�|jt|dddgddgdd�t
d}|jt|dg|ddg|�|jt|dgdd+g�|jt|dddgddg�|jt|dgdd,g�|jt|dddddgdddgd
d�|jt|dddgddgd	dg�|jt|dgddgd	d-g�|jt|ddgdddgd	dg�|jt|ddddgddgd	dgdd�|jt|dddgddgd
d�|jt|dddgddgd
ddd�|jt|dddgddgdd�|jtj	|d.gddgdd�ddddddddddg
}|jt||dddgd	d/d0gd
d�|jt|t�dd�|jt|dgddgdt
�|jt|dg�qFW|jttddt�tdgdgdt�}|jt|jdgdgdt�td�}|jt|jdgdg�|jt|j�tdgdg�}|jdgdg�t|�}|jt|jdgdg�|jt|j�|j�|j�|jt|j�~|jttidddg�|jttdi�tttd��ddgd�}|jtt|dg�tttd��dddg�}|jtt|dddg�|jtt|ddg�|jtt|d1dg�|jtt|d2dg�tdddgddgdd�}t|dt�}|jt|j�tdddgddgdd�}t|�}	t|	�}|jt|j�td3d gddgddd �}|jt|j�t
}ttt|��ddg|�}|jt|j�tdgddg�}|jttddddd�|jtt|d!d"�|jtt|d4d"�|jtt|td#�|jtt|td$�|jtt|td%�|jtt|d&d'�tdgddg�}|jttddddd�|jtti|�|jtt|i�tdgddg�}|jttddddd�|jttid'�|jtt|d(�dS)5Nrrr�rr�rBr�rzr|r�r�rZXXXri�rZQLr)r�r@r8z@#$r�r"r!r[rs123r�rr���xyzrwi�u �Z�r���)r�r�)rr�rxl	l	)rrr����r r:l)rl)r�ND_VAREXPORT�pushrr�r�r*r�r/r1ZND_MAX_NDIM�
OverflowErrorr�r#r�r�poprE�releaser�rrrWr"r��memoryview_from_bufferr�r��UnicodeEncodeErrorr�r�)
r�r�Zndmrrrdr>r	r.r/r5r5r6�test_ndarray_exceptionsLs�%.+%%%'"%((+
'%4.
.0+*+!$%%

%$$$&%z*TestBufferProtocol.test_ndarray_exceptionscCs�x�ttd��D]�}dgd}tdddgddgdt�}t|�|d<xFtdd�D]5}|jdddgddg�t|�||<qjWx&td�D]}|||j�q�W|jt|j	�~qWdS)Nr�rrrr�rzr�)
rrWrr;rEr<r?rr�r>)r�Zpermr>r�rr5r5r6�test_ndarray_linked_list�s
$z+TestBufferProtocol.test_ndarray_linked_listcCs�x�td�D]s\}}}tj|�}t|dfd|�}|j|ddd|d|ddd	ddfd
fd|�q
WdS)Nrrzr�rMr{r2r�rryr|rX)rkr/r+rr
)r�r2Zscalarrbr{r�r5r5r6�test_ndarray_format_scalar�sz-TestBufferProtocol.test_ndarray_format_scalarcCs�tdd�}x�t|�D]�\}}}tj|�}xxdtfD]j}t|d|gd|d|�}|j|ddd|d	|d
dddd|fd|fd
|�qGWqWdS)Nrr[rrzr�r�rMr{r2r�ryr|rX)rrkr/r+r*rr
)r�rir2rdrbr{r�r�r5r5r6�test_ndarray_format_shapes!z,TestBufferProtocol.test_ndarray_format_shapecCs?tdd�}x)t|�D]\}}}tj|�}x�tdd�D]�}|dkr_qJt|dd|��g}||g}|dkr�||dnd}	x�dtfD]z}
t|d|d|d|d|	d	|
�}|j|d
dd|d|d
dddd|d|d|dd|��q�WqJWqWdS)Nr�r�rrzr|r�r�r�rMr{r2r�ryrX���)	rrkr/r+rWrSr*rr
)r�rir2rdrbr{r�rzr|r�r�r�r5r5r6�test_ndarray_format_stridess 
 z.TestBufferProtocol.test_ndarray_format_stridescCs{ddddddddd	d
ddg}t|d
ddd�}t|dttB�}|j|j�t|d��dS)Nrrr�r�r�r�r"r!rr[rrrzr|r)r�r�)rr�)r�r�)rrrr�r�r�)r�rdr	r�r5r5r6�test_ndarray_fortran%s*z'TestBufferProtocol.test_ndarray_fortrancCsHxAtd�D]3}dd�t|�D�}t|�}xt|�D]�}t|�\}}}tj|�}x�dtfD]�}	|dkr�|	tkr�q|t|d|d|d|	�}
t|||d�}t	||�}|j
|
d	dd
|d|dd
d|d|d|d|�t|�r�t|d|d|�}
t|
dtt
B�}
|j|
jfk�|
j�}|j
|d	dd
|d|dd
d|d|d|d|�t|d|d|d|	tB�}
t|||d�}t||�}|j
|
d	dd
|d|dd
d|d|d|d|�q|WqEWq
WdS)Nr�cSsg|]}tdd��qS)rr[)r)rorbr5r5r6rp-s	z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>rrzr�r�rwrMr{r2r�rryr|rXrr�)rWrvrrgr/r+r*rr}r�r
rnr)rr�r|r@r�r�)r�ry�shape_trirzr2rdrbr{r�r�r|rXr	�mvr5r5r6�test_ndarray_multidim+sB
z(TestBufferProtocol.test_ndarray_multidimcCs�tdgddg�}|jt|jdd�t|�}|j||�|jt|jdd�tdgddgdt�}|jt|jd�t|�}|j||�|jt|jd�tdgddgdt�}|jt|j	d�|jt|jdd�t|�}|j||�|jt
|j	d�|jt
|jdd�ddddd	d
ddg}t|dt|�gdd
dt�}|jtj
|jdd�|jt|jdd�t|�}|j||�|jt|jdd�|jt|jdd�dddg}t|dt|�gdddt�}|jt|jdd�|jtj
|jdd�dS)Nrrzr!r�rrr�r�r�r�r"r�ri,r�r5ZLQ�1llll)r�r5)r�r5)rr)r�r�)r�r�)rMr5)rrr��__setitem__rEr�r��__delitem__r=�__getitem__�
IndexErrorrSr/r1r�)r�r�rKrdr5r5r6�test_ndarray_index_invalidXs:''z-TestBufferProtocol.test_ndarray_index_invalidcCs�tddfdt�}t|�}|j||�|f}|j|d�|d}|j|j�|j��|f}|j|d�|d}|j|j�|j��|jt|jd�|jt|jd�|jt|jdd�|jt|jdd�|j|j�d�|j|j�d�d|f<|j|j�d�d|f<|j|j�d�d|d<|j|j�d�d|d<|j|j�d�dS)	Nrrzr�.rr!rr�)	rr�rEr�r�rr�rPrN)r�r�rKrNr5r5r6�test_ndarray_index_scalars2







z,TestBufferProtocol.test_ndarray_index_scalarcCsxtttd	��dddgdt�}t|dt�}|jt|jd�|jt|jtddd��dS)
Nrr�rzr�rrr�r�r!)	rrrrWr�r(rr�rPr�)r�r	r�r5r5r6�test_ndarray_index_null_strides�s*z2TestBufferProtocol.test_ndarray_index_null_stridesc	Cs�xtd�D]\}}}t|ddgd|�}x/tdd�D]}|j||||�qGW|jt|jd�|jt|jd�t|�r
t|�}|j||�x/td
d�D]}|j||||�q�W|jt|jd�|jt|jd�q
Wx�td�D]�\}}}t|ddgdt	d|�}t|dt
tB�}x/tdd�D]}|j||||�q�Wt|�r,|j�}|j
|j|�t�x/tdd�D]}|j||||�q�Wq,Wddd	d
dg}t|ddg�}t|dt�}x/tdd�D]}|j||||�qWWddd	d
dg}t|ddg�}t|dt�}x/tdd�D]}|j||||�q�WdS)Nr�rzr�r�r�rrrr�r�rGi����rGi����rGrGrGrG)rkrrWr�rrQrPrnrEr�r(rr@r��__eq__�NotImplementedr)r")r�r2rdrbr�rrKr	r5r5r6�!test_ndarray_index_getitem_single�sB! z4TestBufferProtocol.test_ndarray_index_getitem_singlec	Cs�xptd�D]b\}}}t|ddgd|dt�}x(td�D]}|||<|||<qJW|j|j�|�|jt|jd|�|jt|jd|�t	|�s�q
t|ddgd|dt�}t
|�}|j||�x(td�D]}|||<|||<q	W|j|j�|�|jt|jd	|�|jt|jd|�q
Wx2td�D]$\}}}t|ddgd|dt�}x;td
d�D]*}||d||<||d||<q�W|j|j�|�t	|�sq�t|ddgd|dt�}t
|�}|j||�x;tdd�D]*}||d||<||d||<q`W|j|j�|�q�WdS)Nr�rzr�r�r�r�ri����i����rGrG)rkrr�rWr�r�rrQrNrnrE)r�r2rdZsingle_itemr�rrKr5r5r6�!test_ndarray_index_setitem_single�sD!
!
!!z4TestBufferProtocol.test_ndarray_index_setitem_singlec
Cs`d	}t|�}xGt|�D]9}t|�\}}}xdtfD]
}t|d|d|d|�}t||�}	x�t|d|d�D]�}
|j|	|
||
j��x�t|d|d�D]w}|j|	|
|||
|j��xHt|d|d�D].}|j|	|
||||
||�qWq�Wq�Wt|d|d|d|t	B�}t
||�}	x�t|d|d�D]�}
|j|	|
||
j��x�t|d|d�D]v}|j|	|
|||
|j��xGt|d|d�D].}|j|	|
||||
||�qWq�Wq�WqGWqWdS)
Nrr�r�rrzr�r�r)rr�r�)rvrrgr*rr�rWr�r�r�r�)
r�rJrirzr2rdrbr�r�rXrr��kr5r5r6�#test_ndarray_index_getitem_multidims,&4
&z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCs?tddf�}|jttdt��t|�}|j||�|jttdt��x�td�D]�\}}}t|ddgd|�}xAt|�D]3\}}|j|||�|j	||k�q�Wt
|�rmt|�}xAt|�D]3\}}|j|||�|j	||k�qWqmWdS)Nrrzz1 in ndz1 in mvr�r�)rrr��eval�localsrEr�rkr�r�rn)r�r�rKr2rdrbrrur5r5r6�test_ndarray_sequencesz(TestBufferProtocol.test_ndarray_sequencecCs
ddddddddg}t|d	dgd
t�}t|�}|jt|jtddd�|�|jt|jtddd�|�t|d	dgd
t�}t|d	dgd
t�}t|dt�}|jt|jtddd�|�t|d	dgd
dd
t�}t|�}|jt	|j
tddd��|jt	|j
tddd��t|d	ddgd
dd
t�}t|�}|jt	|j
tddd�tddd�f�|jt	|j
tddd�tddd�f�|jt|j
d�|jt|j
dtddd�f�|jt|j
tddd�if�|jt|j
tddd�tddd�f�|jt|j
d�t|d	dgd
dd
t�}t|d	dgd
d�}t|�}t|�}|jt	|jtddd�|dd��|j|j
�|�|jt	|jtddd�|dd��|j|j
�|�t|d	dgd
dd
t�}t|d	dgd
d�}	t|�}t|�}|jt	|jtddd�|dd��|j|j
�|�|jt	|jtddd�|dd��|j|j
�|�t|d	ddgd
dd
t�}t|d	dgd
d�}t|�}t|�}|jt	|jtddd�|dd��|j|j
�ddddgddddgg�|jt|jtddd�|dd��t|d	dgd
dd
t�}t|d	dgd
d�}t|�}t|�}|jt	|jtddd�|dd��|j|j
�|�|jt	|jtddd�|dd��|j|j
�|�|jtttddd�i�|jttdd�|jt	ttddd�d�t|d	dgd
dd
t�}
|jt|
j�t|d	dgd
d�}t|dt�}
|jt|
j�dS)Nrrr�r�r�r�r"r!rzr�rrr�rz@%$rrz###########)rr�rErr�rNr�rr�r�rPr�r�r�r�r*�add_suboffsetsr")r�rd�xl�mlr	�xrr�rK�mr�yrrNr5r5r6�test_ndarray_slice_invalid2s�%%%!""$%%((%!//!//$/4!//""!z-TestBufferProtocol.test_ndarray_slice_invalidcCs�ddddddddd	d
ddg}t|d
dgdddt�}t|d
dgdd�}|d	d	�|dd�<|j|j�|�t|�}t|�}|j||�|j||�|d	d	�|dd�<|j|j�|�t|d
ddgdddt�}t|d
ddgdd�}|dd�dd�f|dd�dd�f<|j|j�t|ddg��dS)Nrrr�r�r�r�r"r!rr[rrrzr�rr�)rr�r�r�rEr�)r�rdrNrr`rbr5r5r6�test_ndarray_slice_zero_shape�s*!$2z0TestBufferProtocol.test_ndarray_slice_zero_shapec"Csd}t|�}t|�}xZt|�D]L}t|�\}}}tj|�}xdtfD]}	t|d|d|d|	�}
t||�}x�t	||�D]�}d}
yt
||�}Wn.tk
r�}z|j}
WYdd}~XnXd}y|
|}Wn.tk
r9}z|j}WYdd}~XnX|sF|
rY|j
||
�q�|j|j�|�q�WqbWq+WdS)	Nrr�r�rrzr�r�)rr�r�)rSrvrrgr/r+r*rr�r�r��	Exception�	__class__r�r�r�)r�rJryrirzr2rdrbr{r�r�rXr��listerrr�e�nderr�ndslicedr5r5r6�test_ndarray_slice_multidim�s.z.TestBufferProtocol.test_ndarray_slice_multidimc!Cs�d}t|�}t|�}xut|�D]g}t|�\}}}tj|�}t|d|d|�}	|	j�t|d|d|�}
|
j�t|
�}t	||�}x�t
||�D]�}
d}yt||
�}Wn.tk
r}z|j
}WYdd}~XnXd}y|	|
}Wn.tk
rX}z|j
}WYdd}~XnX|se|rx|j||�q�|j|j�|�q�Wq+WdS)Nrr�r�rzr�)rr�r�r)rSrvrrgr/r+rr^rEr�r�r�rfrgr�r�r�)r�rJryrirzr2rdrbr{r�r	rKrXr�rhrrirjrkr5r5r6�'test_ndarray_slice_redundant_suboffsets�s4

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc@Cs�x�td�D]�\}}}x�td�D]�}x�td�D]~}xudtfD]g}|tB}t|ddgd|d|�}t|ddgd|d|�}	t|	�}
d}d}|dd�}
y>|
|}|
|}|
||
|<t|�t|�k}Wn.tk
r7}z|j}WYdd}~XnXd}y||||<Wn.tk
r�}z|j}WYdd}~XnX|r�|j	|t
�n&|j|j�|
�|j	||�t
|�s�qOd}y|
||
|<Wn.tk
r}z|j}WYdd}~XnX|r1|j	|t
�qO|j|
j�|
�|j|
|�|j	||�|j|
d|	d|jd|d	dd
|jd|jd|jd|j��qOWq<Wq)Wq
WdS)
Nr�rrzr�r�rMr{r2r�ryr|rX)rkr�r*r�rrErSrfrgr�r�r�r�rnr
r{ryrzr|)r�r2rdrb�lslicer�r�rr�r	rKZlsterrZdiff_structurerXZlvalZrvalrirjZmverrr5r5r6� test_ndarray_slice_assign_single�sV
!!

z3TestBufferProtocol.test_ndarray_slice_assign_singlec"Cs�d}t|�}t|�}xkt|�D]]}t|�\}}}x?dtfD]1}x(tt�D]}t||�\}	}
t|d|d|d|t	B�}t
||�}d}
yt|||	|
�}Wn.tk
r}z|j
}
WYdd}~XnXd}y||
||	<Wn.tk
rJ}z|j
}WYdd}~XnX|sW|
rj|j||
�qf|j|j�|�qfWqSWq+WdS)	Nrr�r�rrzr�r�)rr�r�)rSrvrrgr*rW�
ITERATIONSr�rr�r�r�rfrgr�r�r�)r�rJryrirzr2rdrbr�r�r�r�rXrhrrirjr5r5r6�"test_ndarray_slice_assign_multidims0
z5TestBufferProtocol.test_ndarray_slice_assign_multidimcCs�x�tt�D]�}x�tdD]�}tj|�}t|ddtdt�}|jt	|��t
||�}t|||�}|j�}t
|�}t|�r�|j�}	|j|	|�|ddkrJt|||dt�}
|
j�}|j||�t
|
�}t|�rJ|j||
�|j�}	|j|	|�tr|d}d|kriqt|||�}
|j|d	dd
|
jd|ddd
|
jd|
jd|
jd|
j��qWq
WdS)Nr9Tr�r�rrr�r�rMr{r2r�ryrzr|rX)rWrprGr/r+r��MAXDIM�MAXSHAPEr�r�r�r�r�rErnr�r*r�r�r
r{ryrzr|)r�rbr2r{rjrdrNZxlistrKZmvlistrZylistrz�zr5r5r6�test_ndarray_random9s>	
z&TestBufferProtocol.test_ndarray_randomc Cs)x"tt�D]}xtdD]�}tj|�}t|ddtdt�}|jt	|��t
||�}d}yt|||�}Wn.tk
r�}z|j
}WYdd}~XnX|j|�trd}	yt|||�}
Wqtk
r}z|j
}	WYdd}~XqXqWq
WdS)Nr9Fr�r�)rWrprGr/r+r�rrrs�assertFalser�r�r�rfrgr�r�r�)r�rbr2r{rjrdrjrNriZ	numpy_errrr5r5r6�test_ndarray_random_invalidbs(	
z.TestBufferProtocol.test_ndarray_random_invalidcCsAx:tt�D],}x#tdD]}tj|�}tdtdt�\}}}}t|dd|�}t|dd|�}	|j	t
|��|j	t
|	��t||�}
t||	�}t|
||�}t|||	�}
|
|||<|j
�}|
j
�}t|�}t|
�}|j|j
�|�|j|j
�|�|ddkr*|	ddkr*t|
||dt�}t|||	dt�}||||<|j
�}|j
�}|j||�|j||�t|�}t|�}|j|j
�|�|j|j
�|�trd|ksd|krKqt|
||�}t|||	�}||||<t|�r�t|	�r�|j|d	dd
|jd|ddd
|jd|jd|jd|j
��|j|
d	dd
|jd|ddd
|jd|jd|jd|j
��qWq
WdS)Nr9r�r�Trzrrr�rMr{r2r�ryr|rX)rWrprGr/r+r�rrrsr�r�r�r�r�r�rEr�r*r�r�r�r
r{ryrzr|)r�rbr2r{r�r�r�r��tl�trZlitemsZritemsr_raZxllistZxrlistr`rbZylrcZyllistZyrlistZzlZzrr5r5r6� test_ndarray_random_slice_assign~s\! z3TestBufferProtocol.test_ndarray_random_slice_assigncCs�ddddddddd	d
ddg}t|d
ddgdt�}t|�}|j|jt@�|j|j|�|j|jd�|j|j	�|j|j
�|j|j�dS)Nrrr�r�r�r�r"r!rr[rrrzr�rrx)rrx)rr*r�r�r�rMr�r�rvrrr)r�rdr�r	r5r5r6�test_ndarray_re_export�s*z)TestBufferProtocol.test_ndarray_re_exportcCstxmdtfD]_}tdddgddgd|�}t|�}|j||�|j|j�g�|j|j�g�tdddgddddgd|�}|j|j�g�tdddgddddgd|�}|j|j�gggg�tdddgddddgd|�}|j|j�ggggggggggggg�q
WdS)Nrrrr�rzr�)r*rrEr�r�)r�r�r�rKr5r5r6�test_ndarray_zero_shape�s$***z*TestBufferProtocol.test_ndarray_zero_shapec	Cs�x�dtfD]�}tdgddgddgd|�}t|�}|j||�|j|j�dddddg�|j|j�dddddg�q
WdS)Nrrrzr�r|r�)r*rrEr�r�)r�r�r�rKr5r5r6�test_ndarray_zero_strides�s'%z,TestBufferProtocol.test_ndarray_zero_stridescCs]tttd��ddgdd�}|j|jd�|j|j�dddg�dS)N�rzr�r�r"r!r)rrrrWr�r�r�)r�r�r5r5r6�test_ndarray_offset�s'z&TestBufferProtocol.test_ndarray_offsetcCs^xWdtfD]I}tttd��ddgd|�}|j�}|j||�q
WdS)Nrr�rzr�)r*rrrrWr@r�)r�r�r�r>r5r5r6�#test_ndarray_memoryview_from_buffer�s'z6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsyxrdtfD]d}tttd��ddgd|�}x4td�D]&}|j||t||g��qGWq
WdS)Nrr�rzr�)r*rrrrWr�r�)r�r�r�rr5r5r6�test_ndarray_get_pointer�s'z+TestBufferProtocol.test_ndarray_get_pointercCs�tttd��ddddg�}t|dttB�}|j|j�|j��t|�}|j|j�|j��dS)Nr~rzrr�r)rrrrWrrr�r�rE)r�r	r�r>r5r5r6� test_ndarray_tolist_null_strides�s
'z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs�|jtdd��tttd��dddg�}tttd��dddg�}|jt||��tdgddgdd	�}|jt|d
��|jtd
|��dS)Ns123s456rrzr�r�rr�rs)rvr�rrrrWr�)r�rNrr5r5r6�test_ndarray_cmp_contig�s$$z*TestBufferProtocol.test_ndarray_cmp_contigcCs�tjddddg�}t|�}|jtt|�tttd���}tttd��ddg�}|jt|�t|��tttd��dddg�}|jt|�t|��tttd��ddddg�}|jt|�t|��tt	ttd��dddg��}tttd��dddgdt
�}|jt|�t|��tt	ttd��ddddg��}tttd��ddddgdt
�}|jt|�t|��tttd���}tttd��ddddgdt�}|jt|�t|��tttd��ddddgd	d�}|jt|�t|j���dS)
Nrrrr�rrzr�r�r�)
rCrrr��hashrHrrrWr�r�r�r*r�)r�r?r�rr5r5r6�test_ndarray_hashs*!$'**----z$TestBufferProtocol.test_ndarray_hashc
Cse	tttttttttt	t
tf}|jt
tidt�tddfdddt�}xPdddgD]?}x6|D].}t|||�}|j||j��q~WqqWtd	gdd
gdddt�}xJdddgD]9}x0|D](}t|||�}|j|d�q�Wq�Wtttd��dd
d
dgdddt�}xJdddgD]9}x0|D](}t|||�}|j|d�quWqhWxd
tgD]}td	gdd	gddd|tB�}|j�}xJdddgD]9}x0|D](}t|||�}|j||�q	Wq�Wtd	d
dgddgddd|tB�}|j�}xJdddgD]9}x0|D](}t|||�}|j||�q�Wq�Wq�Wtd	d
dgdd
gdd
gdt�}|j�}xPdddgD]?}x6ttgD](}t|||�}|j||�q Wq
W|ddd �}|j�}xfdddgD]U}xL|D]D}yt|||�}Wntk
r�w�YnX|j||�q�WqWttd��}x�d
tgD]�}t|dddgd|tB�}trdtdt|�dddgddd|d
kr[dnd�}	|tkr�tt|ddg�dddgdt�}
|
j�}n|j�}x�|D]�}yt|d|�}Wntk
r�w�YnX|j||�tdd�|D�dddgdt�}|jt|�t|��tr�|j||	jdd��q�W|d
kr�tt|ddg�dddgdt�}
nt|dddgdt�}
|
j�}x�tttttgD]�}yt|d|�}Wntk
rw�YnX|j||�tdd�|D�dddgdttB�}|jt|�t|��tr�|j||	jdd��q�W|tkr�t|dddgdt�}
|
j�}n|j�}x�tttttgD]�}yt|d|�}Wntk
r/w�YnX|j||�tdd�|D�dddgd|tB�}|jt|�t|��tr�|j||	jdd��q�Wq�Wtttd��dddgdttB�}t|dt�}|j||j��tdd�|D�dddgdt�}|jt|�t|��t|dt�}tt|ddg�dddgdt�}
|j||
j��tdd�|D�dddgdttB�}|jt|�t|��t|dt�}|j||j��tdd�|D�dddgdt�}|jt|�t|��dS)!Nr�rrzr�rr�rwr�rr�r!rr"rr�rr|rr�r�r�rrcSsg|]}|�qSr5r5)rorur5r5r6rp�s	zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>cSsg|]}|�qSr5r5)rorur5r5r6rp�s	cSsg|]}|�qSr5r5)rorur5r5r6rp	s	cSsg|]}|�qSr5r5)rorur5r5r6rp	s	cSsg|]}|�qSr5r5)rorur5r5r6rp	s	cSsg|]}|�qSr5r5)rorur5r5r6rp	s	rx)rrrr"r#rr$r%r&r'r(r)rr�rrr�r�r�rrrWr�r�r�r�r�rE�tostringr*)
r�r-r�rZrequestrrZndbytesrXZnarNrrr5r5r6�test_py_buffer_to_contiguous0s�
$
*	
(
.
-

B"$	

+ $
/ 
/$.+-/+z/TestBufferProtocol.test_py_buffer_to_contiguouscCs�dgfdddgdgfttd��dddgfg}x/|D]'\}}t|d|�}t|�}|j|j�|j|j�t|�}t||dd�}t	||�}|j
|d|d	dd
dddd
|d|d|d|�t|�}	|j
|	d|d	dd
dddd
|d|d|d|�t|dttB�}
|j
|
jf�|
j�}|j
|ddd	dd
dddd
|d|d|d|�t|dt�}
|j
|
jd�|j
|
jf�|j
|
jf�|
j�}|dkr'|gn|}|j
|ddd	dd
dddd
dd|jgddd|�qIWx|D]\}}t|d|dt�}t|�}|j|j�|j|j�t|�}t||dd�}t||�}|j
|d|d	dd
dddd
|d|d|d|�t|�}	|j
|	d|d	dd
dddd
|d|d|d|�q{Wx�|dd�D]�\}}t|d|dt�}t|�}t|�}t	||�}|j
|d|d	dd
dddd
|d|d|jd|�t|�}	|j
|	d|d	dd
dddd
|d|d|jd|�q�W|jttdd�|jtti�tdddgddg�}t|dt�}
|jtt|
�t|dttB�}
|jtt|
�tdgdddgddd�}
|jtt|
�|jt|
j�|jtt|
td�|jtt|
td�|jtt|
ddd�td�dS)Nrrrr�r�rzrwrMr{r2rr�ryr|rXrr8rr�r��9rNr)r�rr�rF)rrx)rrrWrrEr�rrrSr}r�r
r)rr�r|r@r"r�rzr�r�rr�r*rr�r�r�r�r�)r�Zitems_shaperdrzr	r>ryr|rXr1r�r5r5r6�test_memoryview_construction 	s�B&z/TestBufferProtocol.test_memoryview_constructioncCs�dddg}xvdddgdddgdddgfD]J}t|d|�}|j|j�t|�}|jt|jd�q:Wxgtdd�D]V\}}}td�}|j|�}|j|j	�d�|j|j
�g�q�WdS)	Nrrr�rrzrrEr�)rr�rrErr�rrkr�r�r�)r�rdrzr	�msrcr2rbr>r5r5r6�test_memoryview_cast_zero_shape�	s1z2TestBufferProtocol.test_memoryview_cast_zero_shapecCs�|j}tj}d}d}ttd��}|td�||d|��t|dddgd	d
�}|t|�||d|��t|ddddgd	d
�}|t|�||d|��dS)NzPnin 2P2n2i5P PZ3nr!r�rrzrr�r�rr�)�check_sizeofrZcalcvobjsizerrrWrEr)r��checkZvsizeZbase_structZper_dimrdr?r5r5r6�test_memoryview_sizeof�	s		!!!z)TestBufferProtocol.test_memoryview_sizeofcCs}Gdd�dt�}Gdd�dt�}dd�}|d�|d�d#td
�dtdd
�dd
dgdddhddifd$dddtddtd�td�dddd|dd�g}x�tdd�D]�\}}}t|ddgd|d t�}t|ddgd|d t�}	t|�}
t	j
||	d!|�||
d!<|j|
d!|	d!�t	j|�}d"|kr�q�x�|D]�}d}
yt	j
||	||�Wnt	j
k
r�t	j
}
YnXd}y||
d<Wn4ttfk
r)}z|j}WYdd}~XnX|
s6|rY|j|
d�|j|d�q�|j|
d|	d�q�Wq�WdS)%Nc@s(eZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTcSs
||_dS)N)�val)r�r�r5r5r6�__init__�	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__cSs|jS)N)r�)r�r5r5r6�__int__�	szETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)�__name__�
__module__�__qualname__r�r�r5r5r5r6�INT�	sr�c@s(eZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXcSs
||_dS)N)r�)r�r�r5r5r6r��	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__cSs|jS)N)r�)r�r5r5r6�	__index__�	szGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r�r�r�r�r�r5r5r5r6�IDX�	sr�cSsdS)Nr"r5r5r5r5r6r�	sz;TestBufferProtocol.test_memoryview_struct_module.<locals>.frg������@y@z-21.1gffffff(@r�rrr�r�r�r"r!TF�asabcr?�abccSs|S)Nr5)rNr5r5r6�<lambda>�	szBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>r[rErzr�r�rry������@@)r)�objectr	r
rVr�rkrr�rEr/r�r�r+r1r�r�rgZassertIsNot)r�r�r�r�valuesr2rdrer	r�r>r{ruZ
struct_errZmv_errrir5r5r6�test_memoryview_struct_module�	sF'!!

z0TestBufferProtocol.test_memoryview_struct_modulecCs]tdddgddgddg�}|j|j�t|�}|jt|jd�dS)Nrrr�rzr|rr)rrvrrErr�r)r�r	r�r5r5r6�!test_memoryview_cast_zero_strides�	s'z4TestBufferProtocol.test_memoryview_cast_zero_stridescCsx�tD]�}td�r#d|n|}tj|�}x�tD]�}td�r[d|n|}tj|�}tttd��dd|gd|�}t|�}|jt	|j
|d|g�q?WqWx�td�D]�\}}	}
t|	ddgd|�}t|�}x�td�D]x\}}
}
t|�s]|jt
|j
|d|g�qt|�rt|�r|jt	|j
|d|g�qWq�Wtjd�}tjd�}tttd.|��ddd|gdd�}t|�}|jt	|j
ddd|gdd�tttd	��dddd
ddg�}t|�}
|jt	|
j
�|jt	|
j
ddd
�|jt	|
j
i�|jt
|
j
d
�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�|jt
|
j
d�td/d0gddgdd�}t|�}
|jt|
jd�|jt|
jdd�|jt|
j�tttd	��dddd
ddg�}t|�}
|jt	|
j
ddi�tttd	��dd3g�}t|�}
|jt|
j
ddd4g�|jt
|
j
ddd5g�|jt
|
j
dddd
ddddd6g�|jt
|
j
dddd
dddddg�|jt	|
j
dddd
dddddg�ttdd�td9�D��dd
ddd g�}t|�}
|jt	|
j
d!ddd
ddg�tttd"��dd"gdd!�}t|�}
|jt
|
j
d!dgd"�ttd#d�td<�D��dd?g�}t|�}
|jt	|
j
d!ddd
ddg�ttd$d�tdB�D��ddEg�}t|�}
|jt	|
j
dddd
ddg�tttd"��dd"gdd!�}t|�}tttd"��dd"gdd�}t|�}tjdGkr�|jt	|j
dddd&d'd(d)d*g�|jt
|j
ddHdIdJdKdLg�|jt
|j
d!dMdNdOdPdQg�no|jt	|j
ddd-g�|jt
|j
ddRdSdTdUdVg�|jt
|j
d!dWdXdYdZd[g�dS)\Nrr9r&rzr�rrr�xr�r�r��Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIZIIrr!rrr�r"rNcSsg|]}d�qS)rr5)rorbr5r5r6rpC
s	zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>rrr)cSsg|]}d�qS)rr5)rorbr5r5r6rpM
s	cSsg|]}d�qS)rr5)rorbr5r5r6rpR
s	r'�Ir*iQiAji��	r~r[i���r�)rr)r�r�r��r�lrxrxr#�ii�r#r�i�r#r�i�r#r�i�r#r�i�ll����iiiir!iiiiriir&r&riir&r!r)�NON_BYTE_FORMATrr/r+rrrrWrErr�rrkrnr�rlr�rPrNr�r=r��maxsize)r�ZsfmtZsformatZssizeZdfmtZdformatZdsizer	r�ZsitemsrbZsize_hZsize_dr>r�r0r1r5r5r6�test_memoryview_cast_invalid�	s�

+(1(-!-!1117(' .(.(''
z/TestBufferProtocol.test_memoryview_cast_invalidcsSddd�fddd�fddd�ff���fdd	�}tjd
�}tddgd
d
�}t|dd�\}}t|�}|jd�}�j|d|ddddddddd|ddd|dd�	tjd
�}tdg|d|gd
d�}t|d
|dg�\}}t|�}|jd
dg�}�j|d|d|dd
dddddfdfd|dd�	xQtdd�D]@\}	}
}tj|	|
�}t|�}||||
|	�q�Wx`tdd�D]O\}	}
}t|
ddgd
|	dt	�}t|�}||||
|	�q�WdS)NrcSst|j��S)N)rrr�)r	r5r5r6r�l
sz9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>rcSsdd�t|j��D�S)NcSs,g|]"}|dkr"|dn|�qS)r*r(r5)rorNr5r5r6rpm
s	zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rrr�)r	r5r5r6r�m
srcSsdd�t|j��D�S)NcSs%g|]}tt|�d��qS)zlatin-1)rH�chr)rorNr5r5r6rpn
s	zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rrr�)r	r5r5r6r�n
sc
stj|�}x��D]�\}}|j|�}||�}�j|d|ddd|dddddd	|gd
dd|dd
�	|j|�}	�j|	|�|j�}�j|	d|d|d|dddddd	gd
|fd|dd
�	qWdS)NrMr{rr2r�rryrzr%r|rXrT)r)r/r+rr
r�r�)
r	r>rdr2�srcsizerFZto_bytelistr1rX�m3)�bytespecr�r5r6�iter_roundtripq
s
z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtriprrrzr�rrMr{r2r�ryr|rXrTrr%rCrEr�)r)
r/r+rr�rErr
rkrCr�)r�r�r�r	Z	destitemsZ	destshaper>r1Zdestsizer2rdrbr5)r�r�r6�test_memoryview_castj
s<
"
!z'TestBufferProtocol.test_memoryview_castcCs0x�t�D]�}x�tdD]�}dtd�|}tj|�}t|�|}t|�rfdnd}x,t||�D]\}}}	tj|�}
|dkr�|gng}||
g}t|d|d|�}
t	|
�}t
|
||d|�\}}|dkr,|jt|j
||�q||d	kr;q|t|d|d|�}|j
|d|�}t|�}|j}|j�}|j|d
|
d|d|d
dd|d|d|d|dd�	|j
|�}|j
|d|�}t|�}|
j}|
j�}|j|d
|
d|
d|d
dd|d|d|d|dd�	|j|d
|
d|
d|d
dd|d|d|d|dd�	q|WqWq
Wtr,Gdd�dtj�}|dd�}t	|�}|j
d�}|j|j|�|j|jd�|j|jd�|j|jd�|j|j|jf�|j|jd�|j|jf�tjd�}t	|�}|j
d�}|j|j|�|j|jd�|j|jd�|j|jd�|j|j|jf�|j|jd�|j|jf�dS)Nr9r8rrErFrrzr�r�rMr{r2r�rryr|rXrTc@s.eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrNrN)r�r�r��ctypes�c_long�c_double�_fields_r5r5r5r6�BEPoint�
sr�r�g33333i@rg333333�?r)r8r9)r)r)r�rGrr/r+rvrlrkrrEr�rr�rrSr|r�r
r��BigEndianStructurer�rMr{r�ryrzr�r�r�)r�Z_tshaperLZtfmtZtsizerrMr2rdrb�sizerzZtshaper	r>Ztitemsr�r1ryr|rXr�Zm4r��pointr0rNr5r5r6�test_memoryview_cast_1D_ND�
s|
	
	
z-TestBufferProtocol.test_memoryview_cast_1D_NDcCs�tjdttdd���}t|�}|j||�|j|j�|j��|ddd�}|ddd�}|j||�|j|j�|j��tttd��ddddddgd	d
�}t|�}|j|j�|j��tddgddgd	d�}t|�}|jt|j�tdgdd
gd	d�}t|�}|jt|j�tddddddgdddgd	d�}t|�}|jt|j�dS)Nrr�rr�r�r"rrzr�rr3s12345rrr��b�c�d�e�fi����r�rF��i	)rr�)r"r)	rCrrrWrEr�r�rrr�)r�r?r>r	r5r5r6�test_memoryview_tolist�
s(!3!0z)TestBufferProtocol.test_memoryview_tolistcCsdttd��}|j�}|j|jd��|j�|j�}|j|jd��dS)Nrz<memoryz	<released)rEr��__repr__r��
startswithr?)r�r>r�r5r5r6�test_memoryview_reprs
z'TestBufferProtocol.test_memoryview_reprcCs�xqd	D]i}td�}tj|d|dg�}t|�}|jd|�|jd|�|jd|�qWtdgdd�}t|�}|jttdt��dS)
Nrrg�g�?g@g"@r�z9.0 in m)rr)	�floatrCrEZassertInrrr�r[r\)r�r2�infr	r>r5r5r6�test_memoryview_sequences
z+TestBufferProtocol.test_memoryview_sequenceccsB|jt��}dVWdQRX|jt|j�d|f�dS)Nz#index out of bounds on dimension %d)rrQr��str�	exception)r��dim�cmr5r5r6�assert_out_of_bounds_error+sz-TestBufferProtocol.assert_out_of_bounds_errorcCs�tddgdd�}t|�}|j|fd�|j|d|�|j|d|�|jt|jd�tddgdd
�}t|�}|jt|jf�tttd��ddgdt	�}t|�}|jt
|jd�|jt|jd�|jt|jd�|jt
|jd�|jt
|jd�tttd��dd	dgdt	�}t|�}|j|dd�|j|dd�|j|dd�|j|dd�|j|d!d�x6d#D].}|jdd��||dfWdQRXq�Wx6d%D].}|jdd��|d|fWdQRXq+W|jt
|jd'�|jt
|jd)�|jt|jd*�|jt|jd+�|jt|jf�|jt|jd�dS),Ng)@rzr�r.rrrr�Ziiir"r�rg@�r!rr�rr�r�)rrr�li����)rr)rr)rr�rxrx)rxrxr:r!)r:r!r!)r�r!rG)r�rG�)r�r�)rr�)rrr)r�r�)rrEr�rr�rPr�rrrWr�rQr�)r�r	r>�indexr5r5r6�test_memoryview_index2sF'*

z(TestBufferProtocol.test_memoryview_indexcCsctddgdddt�}t|�}d|f<|j|fd�d|d<|j|fd�|jt|jd	d
�tttd��ddg�}t|�}|jt|jdd
�tttd��ddgdt�}t|�}|jt	|jd2d�|jt|jdd
�|jt|jdd�|jt	|jd3d�|jt	|jdd�x�t
dD]�}|dks�|dkr�q�tdddgddgd|dt�}t|�}td4d�}d||<|j||d�|j||||�q�Wtdddgddgdddt�}t|�}d|d<|j|dd�tdd dgddgdddt�}t|�}d|d<|j|dd�td!gddgdddt�}t|�}|jt|jd	d"�tttd#��ddddd$d%gdt�}t|�}x�t
dj�D]�\}}|dkr|qa|dkr�qa|j
|�}|\}	}
|d&ks�|dkr�d8\}	}
|d(kr|jt|jd	|	d�|jt|jd	d)�|jt|jd	|
�qaW|j
d�}|jt|jd	d*�tttd��ddgdd+dt�}t|�}|jt|jd	d�td,gddgdd-dt�}t|�}|jt|jd	d�tttd.��ddd$gdt�}t|�}d/|d9<|j|d	dd/�d0|d<<|j|ddd0�x8d>D]0}|jd1d��d	||d	f<WdQRXqmWx8d@D]0}|jd1d��d	|d	|f<WdQRXq�W|jt	|jdBd	�|jt	|jdDd	�|jt|jdEd	�|jt|jdFd	�|jt|jd	ddg�dS)GNg)@rzr�rr�g�6@g�7@.rg33333�8@r"rr[rrg@�rr!r~�r9rr
rr��1�2�3r�TF�xr�r�r�r�rirr6s��ZxLs12345rr�*�+r�li����r:lE����E)r�r�)rrrxrx)rxrxr!)r�r!rG)r�rG�)r�r�)rr�)rrr)r�r�)rr�rEr�rr�rNrrrWrQrGrrdrr�r�r�)r�r	r>r2rr�r0Z_ranger1�lo�hir�r5r5r6�test_memoryview_assigngs�

!'*
!	
!	
$3-$*



z)TestBufferProtocol.test_memoryview_assigncCs�tttd��ddgdt�}t|�}|jt|jtddd��|jt|j	tddd�t
ddg��|jt|jf�tttd��ddgdt�}t|�}|jt|jtddd�tddd�f�|jt|j	tddd�tddd�ft
ddg��|jt|jtddd�if�|jt|j	tddd�ift
ddg��|jt|j	tddd�dg�xdt
fD]�}tttd��ddgddgdd	dt|B�}tttd
��ddgddgd|�}t|�}t|�}|dd�|dd�<|dd�|dd�<|j||�|j||�|dd�ddd�|dd�ddd�<|dd�ddd�|dd�ddd�<|j||�|j||�|dd
d�ddd�|d
dd�ddd�<|dd
d�ddd�|d
dd�ddd�<|j||�|j||�q�WdS)Nrrzr�rrrr|r�rr�r�r�r�rxrxrxr rxr rx)rrrrWr�rErr�rPr�rNr�r�r�r*r�)r�r	r>r�r.r/r0r1r5r5r6�test_memoryview_slice�sD'"'%4($(-
044::z(TestBufferProtocol.test_memoryview_slicec&Csdd�}xltdd�D][}xRt|d�D]A\}}}x/t|�D]!}xt|�D]
}tj||�}tj||�}	t|	�}
|j|
|�|j|
j�|j��|j|
j�|j��|jt|
�t|��||||	|
|�d}d}y>||}
||}||||<t|
�t|�k}Wn.t	k
r�}z|j
}WYdd}~XnXd}y|
||
|<Wn.t	k
r�}z|j
}WYdd}~XnX|r�|j|t�qa|s�|r
|j||�qa|j|
|�|j|
j�|j��|j|
j�|j��||||	|
|�qaWqNWq2WqWdS)Nc	Ss�x�t|�D]�\}}||}||}|j||�|||<||krv|j||�|j||�n |j||�|j||�|||<|j||�|j||�|||<|||<q
WdS)N)r�r��assertNotEqual)	Ztestcaser?rr>�
singleitemrrbZai�mir5r5r6�cmptests




z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestrr�rC)
rWrkr�rCrEr�r�r�rSrfrgr�r�)r�r�rr2rdr�rnr�r?rr>Z	array_errZhave_resizeZal�arriZm_errr5r5r6�test_memoryview_arraysF

z(TestBufferProtocol.test_memoryview_arrayc	Csetjddddg�}tjddddg�}t|�}t|�}xLdD]D}|jt||�|�t�|jt||�|�t�qUWt|�}|j�|j||�|j||�|j||�t|�}t|�}|j�|j||�|j||�t|�}|j|dddg�tdgddgdd
dt	�}dt
d�f|d
<|jt|�|�tjdd�}t|�}|j||�|j||�traGdd�dtj�}|dd�}t|�}t|�}|j||�|j||�|j||�|j
t|j�dS)Nrrrr�r"�__lt__�__le__�__gt__�__ge__rrzr�zl x d xr�r��ur6c@s.eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrNrN)r�r�r�r�r�r�r5r5r5r6r�usr�r�r5)r�r�r�r�)rrrx)rCrEr��getattrrVr?r�r�rr�r�r�r�rr�r�)	r�r?rru�w�attrr�r�r�r5r5r6�%test_memoryview_compare_special_casesJsF
#

$z8TestBufferProtocol.test_memoryview_compare_special_casescCs|tddgdd�}tddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�|j||�|j|j|��|j|j|��d|f<|j||�|j||�|j||�|j||�|j||�|j||�|j|j|��|j|j|��ttt	d��ddgdtt
B�}ttt	d��ddgdtt
B�}t|�}|j||�d	|d
<|j||�tddgdd
�}tddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�|j||�tddgdd
�}tddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�|j||�|j||�|j||�dS)N�rzr�z@Lrr�i�rr�r�333333�?�12345ZLf5sZhf5s)r�r�r�)r�r�r�)r�r�r��?���)r�r�r�)rr�rEr�rv�__ne__r�rUrrrWr*)r��nd1�nd2rur�r�r	r>r5r5r6�!test_memoryview_compare_ndim_zerosf
++
		z4TestBufferProtocol.test_memoryview_compare_ndim_zerocCs$tdddddgddgdd	�}tdddddgddgdd	�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdddddgddgdd
�}tdddddgddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tddd gddgdd	�}td!dd"dd#gddgdd	�}t|�}t|�}|j||ddd
��|j|ddd
�|�|j||ddd
��|j|ddd$�|ddd%��td&d'd(gddgdd�}td)dd*dd+gddgdd�}t|�}t|�}|j||ddd
��|j|ddd
�|�|j||ddd
��|j|ddd,�|ddd-��td.d/d0gddgdd	�}td1dd2dd3gddgdd	dt�}t|�}t|�}|j||ddd
��|j|ddd
�|�|j||ddd
��|j|ddd4�|ddd5��td6d7d8gddgdd�}td9dd:dd;gddgdddt�}t|�}t|�}|j||ddd
��|j|ddd
�|�|j||ddd
��|j|ddd<�|ddd=��dS)>Nii@iqi�i�rzr�r�z@hz<iz>hr�rrz!hz<lr�zh  0cz>  hi��i����i'���i��i����i��i����i'���i��i����i��i����i'���i��i����i'���rxr i��i����i'���i��i����i'���rxr i��i����i'���i��i����i'���rxr i��i����i'���i��i����i'���rxr )rrEr�r�r*)r�r�r�rur�r5r5r6� test_memoryview_compare_ndim_one�sh****$**$**$'	*$'	z3TestBufferProtocol.test_memoryview_compare_ndim_onecCsXtddgddgdd�}td	d
gddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tddgddgdd�}tddgddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)
Ni�i�rzrr�z@hz= h0cz@   ii|���i?���i|���i?���)rrEr�)r�r�r�rur�r5r5r6�"test_memoryview_compare_zero_shape
s$!!!!z5TestBufferProtocol.test_memoryview_compare_zero_shapecCsktddddgddgdd�}tdgddgddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdgdddgdd	�}tdgddgddgdd
�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)
N�rzr�r�z@Lr|rrz@ Liz!L  h)r�r�)r�r�)rrEr�)r�r�r�rur�r5r5r6�$test_memoryview_compare_zero_strides 
s$''"'z7TestBufferProtocol.test_memoryview_compare_zero_stridesc

Csud}x�tdD]�}t|dd|�\}}}x�dtfD]r}t|d|gd|d|�}t|�}|j||�|ddd�}t|�}|j||�qBWqWd}x�td
�D]�}	t|�\}}}x�dtfD]r}t|d|gd|d|�}t|�}|j||�|ddd�}t|�}|j||�q�Wq�WdS)
Nr[z@mrEr9rrzr�r�r�r�r:r:)rGrgr*rrEr�rW)
r�rrLr2rdr�r�r�r>rbr5r5r6�&test_memoryview_compare_random_formats:
s(!!z9TestBufferProtocol.test_memoryview_compare_random_formatscCs�tttdd��ddddgdd�}tttdd	��ddddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdgd	ddddgdd�}tdgd	ddddgdd
�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd	��ddddgdd�}tttd	��ddddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdgddddgdd�}tdgddddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd	��ddddgdd�}tttd	��ddddgdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)Nr#rzr�rr�r�z@hrrFrz=f q xxLg333333�?z< f 2Qr�r"z! b B xLz
= Qx l xxLri�)rrr�333333�)r�rr)rrr)rrr)rrrrWrEr�r�)r�r�r�rur�r5r5r6�"test_memoryview_compare_multidim_cV
sZ00((--%%--z5TestBufferProtocol.test_memoryview_compare_multidim_ccCstttdd��ddddgdddt�}tttd	d
��ddddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdgd
dddgdddt�}tdgd
dddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttdd��ddddgdddt�}tttdd��ddddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttdd��ddddgdddt�}tttdd��ddddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd
��ddddgdddt�}tttd
��ddddgdddt�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS) Nr#rzr�rr�r�z@hr�rrFrrr�z=Qqz=qQrZ0llz@bi�l�����rx)r�rxrxl�����)rxr�i�i�i�i�)rrrrWr�rEr�r�)r�r�r�rur�r5r5r6�(test_memoryview_compare_multidim_fortran�
sn-	-	"	"	-	-	-	-	*	*	z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc	Cslttdd��}t|dddg�}t|ddddgdd�}t|ddddgddd	t�}t|�}t|�}|j||�|j||�|j||�d"gd
}d%|d<t|dddg�}t|ddddgdd�}t|ddddgddd	t�}t|�}t|�}|j||�|j||�|j||�tttd��dddgdd�}|ddd&�ddd'�f}tttd��dddgdd�}|ddd�ddd(�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�td-gddddgdd�}|ddd.�ddd/�f}td4gddddgdd�}|ddd�ddd5�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd
��ddddgdd�}|dd�ddd6�f}tttd
��ddddgdd�}|dd�ddd7�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd
��ddddgdd�}|dd�ddd8�f}tttd
��ddddgdd�}|dd�ddd9�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�td:gd
ddddgdd�}|dd�ddd;�f}td<gd
ddddgdd�}|dd�ddd=�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)>Nr#r�rr�rzr�z@lrr�gffffff
@�r�rFg������@zd b czd h c�(r!z@Irrr%rz=iiz>iirr�123Zb3sZi3si��ffffff
����)rrr�皙��������)rrr�rxr r l��l��)rrrxr l��l��)r	r
r r r r r )rrr )rrr )rrrWr�rr�rEr�r�)	r��lst1�lst2r�r�rur�r.r/r5r5r6�&test_memoryview_compare_multidim_mixed�
s�!'

!'*"*"%"%"----((z9TestBufferProtocol.test_memoryview_compare_multidim_mixedcCs�tttd��ddddgdd�}tttd��ddddgdd	�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd��ddddgdd�}tttd��ddddgdd	�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)
NrFrzrr�rr�rr�z@i)rrrrWrEr�r�)r�r�r�rur�r5r5r6�+test_memoryview_compare_multidim_zero_shape=s$----z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapecCs�tdgdddddgdd�}tdgddddgdd	d	d	gdd
�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�|j|j�|j��tdgd
dddgdd�}tdgdddgdd	d	gdd�}t|�}t|�}|j||�|j||�|j||�|j||�|j||�dS)Ni�r�rzr�r�r�z@Lr|rrrrr[z=lQz<lQ)rr)rr)rrEr�r�)r�r�r�rur�r5r5r6�-test_memoryview_compare_multidim_zero_stridesWs&(3%-z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc		Cs�tttd��dddgdd�}|ddd%�ddd&�f}tttd��dddgdd
dt�}|ddd�ddd'�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�td+gddddgdd
dt�}d-|d	d<|ddd.�ddd/�f}td3gddddgdddttB�}d5|d	d<|ddd�ddd6�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd��dd	ddgdddt�}|dd�ddd7�f}tttd��ddd	dgdd�}|dd�ddd8�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�td<gddd	ddgdddttB�}|dd	�ddd=�f}tdAgdddd	dgdd�}|dd	�dddB�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tttd��dddd	gdddt�}|dd�dddC�f}tttd��dddd	gdddt�}|dd�dddD�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdEgdddddgdddttB�}dF|dd	d	<|dd�dddG�f}tdHgdddddgdddttB�}dI|dd	d	<|dd�dddJ�f}t|�}t|�}|j||�|j||�|j||�|j||�|j||�ttdKd��}t|dd	dg�}t|ddd	dgdddt�}t|ddd	dgdddt	tB�}t|�}t|�}|j||�|j||�|j||�dLgd}dM|d"<t|dd	dg�}t|ddd	dgdd#dt�}t|ddd	dgdd$dt	tB�}t|�}t|�}|j||�|j||�|j||�dS)NNrrzr�r!r�z@Ir�rrrr�rz=Qqr"z>QqrFrZBbr�hellor��Z5s0sP�sushir#z@lr�sashimi�sliced������4@�ramen�spicy�fffff�"@rz
< 10p 9p dz
> 10p 9p drxr r l�����rx)rrxr )rr rxr l�����rx)rrxr )rr r r r r(r8rx)r8rxr r(r8rx)r8rxr r r )rr�r)rr�rr )rr�r)rr�rr i�)rrr)rrr)
rrrrWr*rEr�r�r�r�r�)	r�r.r�r/r�rur�rrr5r5r6�+test_memoryview_compare_multidim_suboffsetsrs�*"0""	""
"*	-%
(33%
%
'+

'
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetsc
Csx�ddddgD]�}tdgddd	d
dddgd|d�}tdgddd	d
dddgd|dd
ttB�}d|dd	ddd<t|�}t|�}|j||�|j||�|j||�|j||�|j||�tdgddd	d
dddgd|d�}tdgddd	d
dddgd|dd
ttB�}d|dd	ddd<t|�}t|�}|j||�|j||�|j||�|j||�|j||�qWdS)Nr<r:r;r=rr'r�rzr�r�r�rr�rr%r#ZQLHll����)rrr����)rr r!)rrr)rr�r�rEr�r�)r��	byteorderrNrr?rr5r5r6�!test_memoryview_compare_not_equal�s02/
%
%z4TestBufferProtocol.test_memoryview_compare_not_equalcCs�tjddddg�}t|�}|j�|jtt|�|jt|jd�|jtt|�|jt|j�|jt|j�|jtt	dt
��|jt|jd�|jt|jdd�x$dD]}|jt|j
|�q�Wtjddddg�}t|�}t|�}|j||�|j�|j||�|j||�|j||�dS)Nrg�������?g������@gffffff
@rz1.0 in mrrrMr�r�r{r�ryrzr|r�rrr)rMr�r�r{r�ryrzr|r�rrr)rCrEr?rr�rrr�r�r[r\rPrN�__getattribute__r�r�)r�r?r>r�rr0r1r5r5r6�test_memoryview_check_releaseds.


z1TestBufferProtocol.test_memoryview_check_releasedcs�d�t�ddgdd	�}t|�}|j||�|j|j�|j��t�gdd
gdd�}t|�}|j||�|j|j�|j��t�fdd
�td�D�ddddgdd�}t|�}|j||�|j|j�|j��t�fdd
�td�D�ddddddgdd�}t|�}|j||�|j|j�|j��tr�Gdd�dtj�}|dd�}t|�}|j|j�t|��dS)Ni�@iq�i�rzr�r�z@hrz>hQiLlcsg|]}��qSr5r5)rorb)rjr5r6rpGs	z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rrr�z=hQiLlcsg|]}��qSr5r5)rorb)rjr5r6rpLs	r�z<hQiLlc@s.eZdZdejfdejfgZdS)z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrNrN)r�r�r�r�r�r�r5r5r5r6r�Usr�r�r5�������'���)r(r&r)r'r*)rrEr�r�rWr�r�rH)r�r�r>r�r�r?r5)rjr6�test_memoryview_tobytes8s.:7	z*TestBufferProtocol.test_memoryview_tobytescCs�|jttitd�|jttdtd�tdddgddgddg�}|jtt|td	�td
dfdd�}xMddd	gD]<}t|t|�}|j||�|j|fd
�q�Wtd
dfddd
t�}xMddd	gD]<}t|t|�}|j||�|j|fd
�qWx�ddd	gD]x}d
|f<t|t|�}|j||�|j|fd
�d|f<|j|fd�|j|fd�qXWtdgddgddd
t�}xeddd	gD]T}t|t|�}|jt	|j
d�|j||�|j|j�g�qWttt
d��ddddgddd
t�}xKddd	gD]:}t|t|�}|jt|�j�ggg�q�Wtdgddgddd
t�}xUddd	gD]D}t|t|�}|j||�|j|j�|j��qWtdddgddgddd
t�}xUddd	gD]D}t|t|�}|j||�|j|j�|j��q�Wtdddgddgddgd
t�}x�ddd	gD]�}t|t|�}|j||�|j|j�|j��|jt|jdd�|j|dd�|j|dd�qW|ddd�}x�ddd	gD]�}t|t|�}|j||�|j|j�|j��|jt|jdd�|j|dd�|j|dd�q�Wttt
d��dddgd
t�}xHdd	gD]:}t|t|�}|jt|�j�|j��q�W|jtt|td�t|t|�}|jt|�j�|j��ttt
d��dddgd
ttB�}xHdd	gD]:}t|t|�}|jt|�j�|j��qNW|jtt|td�t|t|�}|jt|�j�|j��ttt
d��dddgd
ttB�}xdddd	gD]S}|jtt|t|�t|t|�}|jt|�j�|j��qWtdddddgddgddg�}t|td�}|j|j�dS)Nr�r�rwrrr�rzr|r�rr�rr�r[rr!r"rrr~rr�r�rx)rr�r�r�r�ZPyBUF_WRITErr�r�rQrPr�rrrWrNr�r*r�r)r�r�rr>r5r5r6�test_memoryview_get_contiguous[s�'

$*	&$ * -*&"!
&".&-z1TestBufferProtocol.test_memoryview_get_contiguouscCstjd�}tjddddddg�}t|�}tj|�}td|�}|j|�|j|j	�|�tjd�}t
ttd��d	dddgd
d�}t|�}tj|�}td|�}|j|�|j|j	�|�dS)
Nrrrr�r�r�rrrzr�r�r)
r/r+rCrE�io�BytesIOr��readintor�r�rrrrW)r�r�r?r>r�rr�r5r5r6�test_memoryview_serializing�s!
-
z.TestBufferProtocol.test_memoryview_serializingc	Cs�tttd���}t|�}|jt|�t|��|jddddg�}|jt|�t|��|ddd�}tttd��ddd��}|jt|�t|��tttd��ddddgd	t�}t|�}|jt|�t|��tttd��ddddg�}|ddd�dd�ddd�f}t|�}|jt|�t|��tttd��ddddgd	t	�}|ddd�dd�ddd�f}t|�}|jt|�t|��tttd��ddgdd�}t|�}tttd��ddgdd
�}t|�}|j||�|jt|�t|��tttd��ddddgdd�}t|�}|j
t|j�tttdd��ddddgdd�}t|�}|j
t|j�tttd��ddddgdd�}t|�}|j
t|j�tttdd��ddddgdd�}t|�}|j
t|j�dS)Nrrrzr�r�rrFr�r�rr�rrrr�rz= Lz< hr r rxrxi����i����)
rHrrrWrEr�r�rrr�r*rr��__hash__)	r�rr>ZmcZmxr�rNr?rr5r5r6�test_memoryview_hash�sJ%-'+-+''-0-0z'TestBufferProtocol.test_memoryview_hashc=Cs{tdddg�}t|�}t|�}|jt|j�~|j�tdddg�}t|�}t|dtdt�}t|dtdt�}|j|j	|�|jt|j�~~|j�tdddg�}t|�}t|�}t|�}|j�|jt|j�~|j�tdddg�}t|�}t|�}t|dtdt�}t|dtdt�}|j|j	|�|j�|jt|j�~~|j�tdddgddgdt
�}t|�}|jddd	d
dgddg�t|�}t|�}|j|j
�|j
��t|�}	|j|	j
�|j
��|j|	j
�|j
��|j�|	j�|j�|j|j
�|j
��~|j�|j�dd
�}
td�}t|��+}|
|�|j|dtd��WdQRXtttd��ddddgdd�}t|dtdt�}	t|	dtdt�}|j|j	|�t|��L}|
|�|j|dd�j
�dddgdddggg�WdQRXxdtfD]�}td�}t|���}~t|dtd|�}	t|	���}~	t|dtd|�}t|��y}
~|
|
�|
|�|
|�|j|dtd��|j|dtd��|j|
dtd��~
WdQRX~WdQRX~WdQRXtd�}t|���}~t|dtd|�}	t|	���}~	t|dtd|�}t|��}
~|
|�|
|�|
|
�|j|dtd��|j|dtd��|j|
dtd��~~~
WdQRXWdQRXWdQRXqWtd�}|jt��;t|��'}t|�}|dtd�kWdQRXWdQRXdS)Nrrr�rr�rzr�r�r�r"r!c	Sst|��}WdQRXdS)N)rE)rr1r5r5r6�catch22lsz;TestBufferProtocol.test_memoryview_release.<locals>.catch22s123rr�rr�rr�r�)r�rErrr�r?r�ND_REDIRECTr�rMr;r<r�r�r>�ordrrrW)r�r?r>r�r�r�r0r1rNrr3rtr�r�r	r5r5r6�test_memoryview_release*s�





$%





!-
B








"z*TestBufferProtocol.test_memoryview_releasecCs�tdd�td�D�ddgdd�}tjddd�td�D��}xO||fD]A}t|dtd	t�}t|dtd	t�}t|�}|j|j|�|j|j|�|j|j|�|j||�|j||�|j||�|j|d
d�|d
d��|j|d
d�|d
d��|j|d
d�|d
d��~~|j|d
d�|d
d��q`WdS)NcSsg|]}d|�qS)g�?r5)rorNr5r5r6rp�s	z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rrzr�rcSsg|]}d|�qS)g�?r5)rorNr5r5r6rp�s	rr�rr�)	rrWrCrr4rEr�rMr�)r�r�r?rNrrtr>r5r5r6�test_memoryview_redirect�s".%$$$z+TestBufferProtocol.test_memoryview_redirectcCs�d}dddddddd	d
ddd
g}|jttddd�t�}t|�}|j|d|ddd|ddddddgddgd|�x(td�D]}|j|||�q�W~~t�}t|�}~~t�}t|dt�}t|dt�}t|�}|j	|j
|�|j	|j
|�|j|d|ddd|ddddddgddgd|�~~~~t�}t|dtdt�}t|dtdt�}t|�}|j	|j
|�|j	|j
|�|j	|j
|�|j|d|ddd|ddddddgddgd|�~~~~tdd�}t|�}|j|ddddd|ddddddgddgd|�x(td�D]}|j|||�q�W~~tdd�}t|�}~~tdd�}t|dt�}t|dt�}t|�}|j	|j
d�|j	|j
|�|j|d|ddd|ddddddgddgd|�~~~~tdd�}t|dtdt�}t|dtdt�}t|�}|j	|j
d�|j	|j
|�|j	|j
|�|j|d|ddd|ddddddgddgd|�~~~~dS)Nrrrrr�r�r�r�r"r!rr[rrMr{r2r�ryrzrr|rXrr�Zlegacy_modeT)rr�ZstaticarrayrEr
rWr�rrr�rMr4)r�r2rXrNrrrtr>r5r5r6�$test_memoryview_from_static_exporter�s�*				z7TestBufferProtocol.test_memoryview_from_static_exportercCs<tdddgdgdttB�}|jtt|�dS)Nrrr�r�)rr,ZND_GETBUF_UNDEFINEDrr�rE)r�r�r5r5r6�#test_memoryview_getbuffer_undefined s%z6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs;tdddgddgdt�}|jtt|�dS)Nrrr�rzr�)rr,rr�rE)r�rNr5r5r6�test_issue_7385&s$z"TestBufferProtocol.test_issue_7385Nrxrxrxrxrx)Sr�r�r�r�r
rr4rBrCrDrErHrIrLrRrSrTrWrXrZr]rdrerlrmrorqrurwrzr{r|r}rr�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r��
contextlib�contextmanagerr�r�r�r�r�r�r�r�r�r�r�r�r�r
rrrr#r%r+r,r0r2r6r7r8r9r:r5r5r5r6r��s��2i�
-'	0.Y #6!)>
		%�u	
8	s<P	5m8>5?H>Ha�(#u<�^r��__main__)er;ZunittestZtestr�	itertoolsrr�randomrrr�	sysconfigr�warningsr�rCr-�decimalr	Z	fractionsr
Z_testbuffer�ImportErrorrr/r��catch_warningsrDr�Z
SHORT_TESTZNATIVEr0r1ZSTANDARDr7rGr2�copyrmZARRAYrYZBYTEFMTr]r_r\rOrVrYrfrgrhrkrlrnr�rsrtrvr}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�rprrrsZ
skipUnlessZTestCaser�r��mainr5r5r5r6�<module>s8$




	
%%%%%0








*	

	

?
+!��������������C