Your IP : 216.73.216.213


Current Path : /proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/test_buffer.cpython-34.pyo

�
g fDn�3@sddlZddlmZddlmZmZddlmZmZm	Z	ddl
mZddlZddl
Z
ddlZddlZddlmZddlmZyddlTWnek
r�dZYnXyddlZWnek
rdZYnXyddlZWnek
r,dZYnXy'ej��dd	lmZWdQXWnek
rndZYnXd
Zidd6dd6dd
6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6dd6Zer�ed=ed=nerLy0ejdddd�ded<ded<Wnejk
rHYnXniddfd6ddd>fd6dd >dd >fd
6ddd>fd6dd!>dd!>fd6ddd">fd6dd#>dd#>fd6ddd$>fd6dd#>dd#>fd6ddd$>fd6dd%>dd%>fd6ddd>fd6dd%>dd%>fd6dd&>dd&>fd6Z d'd(�Z!ied)6ed*6e d+6e d,6e d-6e d.6Z"er�x&e"d*D]Z#e!e#�e"d*e#<q�Wnej$�Z%ej$�Z&x%eD]Z'e'd/kr	e&e'=nq�Wej$�Z(x%eD]Z'e'd0kr=e(e'=nq We%e"d1<e%e"d2<e&e"d3<e(e"d
<e(e"d4<dZ)dZ*id)d*d+d,d-d.gd)d5d6d7gfd86d3gd)gfd96d)gd)gfd:6d2d1gd)gfd;6d4d
gd)gfd<6Z+d=d>�Z,d?d@�Z-dAdB�Z.dCdD�Z/d8dddEdF�Z0d8dGdH�Z1d8dIdJ�Z2dKdL�Z3dMdN�Z4dOdP�e"d*D�Z5dQdR�Z6dSdT�Z7dUdV�Z8dWdX�Z9dYdZ�Z:d[d\�Z;d]d^�Z<d_d`�Z=dadb�Z>dcdd�Z?dedf�Z@dgdh�ZAdidj�ZBdkdl�ZCdmdn�ZDdodp�ZEdqdr�ZFdsdt�ZGdudv�ZHdwdx�ZIdydz�ZJd{d|�ZKd}d"fd~d�ZLd�d��ZMd�d��ZNd}d"d�d��ZOd�d��ZPdd�d��ZQd�d��ZRdd�d��ZSd�d��ZTd�d��ZUd�d��ZVd�d�d��ZWd�d�d��ZXd}d�d��ZYdd�d��ZZd�d��Z[d�Z\d}Z]d�Z^er�d�Z\d�Z]d�Z^eXZUeYZVeZZnej_ed��ej_ed��Gd�d��d�ej`���Zaebd�krejc�ndS)��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)zReturn range of a native type.rr�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�lhZexp�r4�5/opt/alt/python34/lib64/python3.4/test/test_buffer.py�native_type_rangeVs 				
	/r6��@�<�>�=�!Z
bBhHiIlLfdZBbc�mz@m�az@b�1�2�3r�array�numpy�
memoryview�bytefmtcCs�tt||�}|dkrSt|g�}|dkrS|dkrSd}qSn|dkrnt|�}n|dks�|dkr�tj||�}tj||�d}n|S)	zWReturn random item for a type specified by a mode and a single
       format character.rrCssr
rrr)r�fmtdict�bytes�boolr/r0�unpack)�mode�char�obj�xr4r4r5�
randrange_fmt�srNcCsi|jd�\}}g}x'|D]}|jt|||��q"Wt|�dkr_|dSt|�S)zReturn single random item.�#rr)�split�appendrN�len�tuple)r2rLrJ�charsrMrr4r4r5�gen_item�s

rUcCsT|dkrt||�Sdg|}x't|�D]}t||�||<q3W|S)z,Return a list of random items (or a scalar).r)rU�range)rr2rL�lstrr4r4r5�	gen_items�s

rXcCs�tt|t�}|d}|jd�}tdd�}xlt|�D]^}ttt|��}tt|t�}||t	|r�|nd�7}|||7}qIWt
|||�}	t||�}
||	|
fS)NrO�ambr�
r)r�cap�MODE�striprrVrSrF�MULT�intrXrU)rrLrJZxfmtr2�nmemb�_rK�
multiplier�items�itemr4r4r5�struct_items�s
 recCs�|dkr#tt|t�}n|dkrHttt|��}ntt|t�}|d|t|ru|nd�}t|||�}t||�}|j	d�||}|||fS)z"Return random format, items, item.NrOrrY)
rr[r\rSrFr^r_rXrUr])rrLrJrKrbr2rcrdr4r4r5�	randitems�s$rfccsFx?t|tD]/}x&t|D]}t||||�Vq WqWdS)z1Iterate through supported mode/char combinations.N)r[r\rFrf)rrLrJrKr4r4r5�	iter_mode�srgccsFxt||�D]}|VqW|dkr4t�nt||�VdS)zwYield (format, items, item) for all possible modes and format
       characters plus one random compound format string.rN)rg�
StopIterationre)�nitemsZtestobj�tr4r4r5�iter_format�s
		rkcCs"d|kp!d|kp!d|kS)Nrrrr4)r2r4r4r5�is_byte_format�srlcCsFt|�}|dks4|dkoE|ddkoE||dtkS)zformat suitable for memoryviewrrrr8)rR�
MEMORYVIEW)r2rMr4r4r5�is_memoryview_format�s(rncCs"g|]}t|�s|�qSr4)rl)�.0rr4r4r5�
<listcomp>�s	rpcCst|t�S)z9Tuple items (representing structs) are regarded as atoms.)�
isinstance�list)rWr4r4r5�atomp�srscCs
t|t�S)N)rqrr)rWr4r4r5�listp�srtcCsIt|�dkrdS|d}x"|dd�D]}||9}q1W|S)zProduct of list elements.rrN)rR)rWrM�vr4r4r5�prod�s
rvcCs�|dkrfS|dkrst|dd��|g}x�t|ddd�D]}||||d9<qPWnM|gt|dd��}x-td|�D]}||||d9<q�W|S)	zOCalculate strides of a contiguous array. Layout is 'C' or
       'F' (Fortran).r�CrNr���rxrx)rrrV)�ndim�shape�itemsizeZlayout�stridesrr4r4r5�strides_from_shape�sr}cCs�t|�r|St|�dkr*|dSdg|d}|drYt|�|dnd}xMt|d�D];}||}t||||�|dd��||<qpW|S)zmConvert flat item list to the nested list representation of a
       multidimensional C array with shape 's'.rrN)rsrRrV�_ca)rc�srW�strider�startr4r4r5r~s$
/r~cCs�t|�r|St|�dkr*|dSdg|d}|d}xBt|d�D]0}t||d|�|dd��||<qVW|S)zsConvert flat item list to the nested list representation of a
       multidimensional Fortran array with shape 's'.rNr)rsrRrV�_fa)rcrrWr�rr4r4r5r�s
.r�cCsLt|�r?d|kr?t|�t|�kr?td��nt||�S)Nrzprod(shape) != len(items))rtrvrR�
ValueErrorr~)rcrzr4r4r5�carray)s0r�cCsLt|�r?d|kr?t|�t|�kr?td��nt||�S)Nrzprod(shape) != len(items))rtrvrRr�r�)rcrzr4r4r5�farray.s0r�cCsdd�|D�}t|�S)z(Generate all possible tuples of indices.cSsg|]}t|��qSr4)rV)rorur4r4r5rp5s	zindices.<locals>.<listcomp>)r)rz�	iterablesr4r4r5�indices3sr�cCs7d}x*t|�D]}|||||7}qW|S)zAConvert multi-dimensional index to the position in the flat list.r)rV)ry�indr|Zretrr4r4r5�getindex8sr�c	Cs�|s
|St|�}t||dd�}t||ddd�dd�}dgt|�}xSt|�D]E}t|||�}t||ddd�|�}||||<qmW|S)z�Transpose flat item list that is regarded as a multi-dimensional
       matrix defined by shape: dest...[k][j][i] = src[i][j][k]...  rrwNrrxrx)rRr}r�r�)	�srcrzryZsstridesZdstrides�destr�ZfrZtor4r4r5�	transpose?s"r�cCsE|gkr|St|�r#|gSt|d�t|dd��S)zflatten listrrN)rs�_flatten)rWr4r4r5r�Ns
r�cCst|�r|St|�S)zflatten list or return scalar)rsr�)rWr4r4r5�flattenVsr�cCs@t|�rgSt||d�gt|d|dd��S)zMGet the shape of lst after slicing: slices is a list of slice
       objects.rrN)rsrR�slice_shape)rW�slicesr4r4r5r�\sr�cs/t|�r|S�fdd�|�dD�S)z=Multi-dimensional slicing: slices is a list of slice objects.cs)g|]}t|�dd���qS)rN)�
multislice)roZsublst)r�r4r5rpgs	zmultislice.<locals>.<listcomp>r)rs)rWr�r4)r�r5r�csr�csWt|�r|S��fdd�t|�d|�d�D�}||�d<|S)a�Multi-dimensional slice assignment: llst and rlst are the operands,
       lslices and rslices are lists of slice objects. llst and rlst must
       have the same structure.

       For a two-dimensional example, this is not implemented in Python:

         llst[0:3:2, 0:3:2] = rlst[1:3:1, 1:3:1]

       Instead we write:

         lslices = [slice(0,3,2), slice(0,3,2)]
         rslices = [slice(1,3,1), slice(1,3,1)]
         multislice_assign(llst, rlst, lslices, rslices)
    c	s?g|]5\}}t||�dd��dd���qS)rN)�m_assign)ror�r)�lslices�rslicesr4r5rpzs	zm_assign.<locals>.<listcomp>r)rs�zip)�llst�rlstr�r�r4)r�r�r5r�is#r�cCs�t||�}t||�}t|�t|�kr:dSxFtt|��D]2}||||krkdS||dkrMdSqMWdS)z9Compare the structure of llst[lslices] and rlst[rslices].rrrxrx)r�rRrV)r�r�r�r��lshape�rshaperr4r4r5�
cmp_structuresr�cCs=t||||�dkr*td��nt||||�S)z:Return llst after assigning: llst[lslices] = rlst[rslices]rz+lvalue and rvalue have different structures)r�r�r�)r�r�r�r�r4r4r5�multislice_assign�sr�cs�|�rdS|dks*|�|kr.dSt�fdd��D��rQdS|dkru|dkot�ot�Sd�kr�dSt��fdd�t|�D��}t��fdd�t|�D��}d||ko�||�|kS)z�Verify that the parameters represent a valid array within
       the bounds of the allocated memory:
           char *mem: start of the physical memory block
           memlen: length of the physical memory block
           offset: (char *)buf - mem
    Frc3s|]}|�VqdS)Nr4)roru)r{r4r5�	<genexpr>�sz#verify_structure.<locals>.<genexpr>Tc3s5|]+}�|dkr�|�|dVqdS)rrNr4)ro�j)rzr|r4r5r��sc3s5|]+}�|dkr�|�|dVqdS)rrNr4)ror�)rzr|r4r5r��s)�any�sumrV)�memlenr{ryrzr|�offset�imin�imaxr4)r{rzr|r5�verify_structure�s
((r�cCsx|D]}||}qW|S)Nr4)rWr�rr4r4r5�get_item�s
r�c
CsO|\}}}}}}|}x*t|�D]}	|||	||	7}q+W|S)z-Location of an item in the underlying memory.)rV)
r�rjr�r{ryrzr|r��prr4r4r5�memory_index�s
r�cCsn|\}}}}}}d|>}xEt|�D]7}t||�}	d|	>}
||
@r\dS||
O}q/WdS)z�The structure 't' is overlapping if at least one memory location
       is visited twice while iterating through all possible tuples of
       indices.rTF)r�r�)rjr�r{ryrzr|r�Zvisitedr�rZbitr4r4r5�is_overlapping�s


r��cs�st|d�}|dkr�|r>|||ffdfStdd�}||}td�dkrp|n|}|||ff|fSnd}	td�}
|
dkr�|r�d}	n|
dkr�d}	ndg|�x:t|�D]}t|	|d��|<q�Wnt��}d}td�}
|
dkrI|
d@rId	nd
}
dg|�|t||d��|d<|
r��|ddkr�|�|d<nx�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��}n||}|r�||}||d|}n2|||}td�dkr||n|}|||��|fS)z�Return random structure:
           (memlen, itemsize, ndim, shape, strides, offset)
       If 'valid' is true, the returned structure is valid, otherwise invalid.
       If 'shape' is given, use that instead of creating a random shape.
    rrr$r�d�_�Zr�TFc3s5|]+}�|dkr�|�|dVqdS)rrNr4)ror�)rzr|r4r5r�sz!rand_structure.<locals>.<genexpr>c3s5|]+}�|dkr�|�|dVqdS)rrNr4)ror�)rzr|r4r5r�s�rxrxrx)rrx)rrVrRr�)r{Zvalid�maxdim�maxshaperzryrir�r��minshaperrZ	maxstrideZzero_strider�r�r4)rzr|r5�rand_structure�sT
		
"
 &-
(+
#r�c
Cs�||}t|d�}|r.|||nd}td|d�}|||}t|||�}t||�\}}}}	|	|kr�t�n|S)z=Create a random slice of len slicelen that fits into listlen.r)r�slice�
slice_indices�RuntimeError)
�slicelenZlistlenZmaxstartr�Zmaxstep�step�stoprraZcontrolr4r4r5�randslice_from_slicelens
	r�cCs�dg|}dg|}xit|�D][}||}|dkrVtd|d�nd}t||�||<t||�||<q'Wt|�t|�fS)zxCreate two sets of slices for an array x with shape 'shape'
       such that shapeof(x[lslices]) == shapeof(x[rslices]).rr)rVrr�rS)ryrzr�r�rrr�r4r4r5�randslice_from_shape+s


%r�cCsAtd|d�}d}td�}|dkr:d}n|dkrOd}ntd�dkrgdnd	}dg|}dg|}dg|}dg|}	x�t|�D]s}t||d�}
t||d�}||
kr�|
|}}
n|rut|
|
d�}t|
|
d�}
dtd�td|
d�}t||
|�}t||
�\}}}}n4|
dkr�td|
d�nd}t||
�}t||�}td�dkr�||
||<||<|||	|<||<q�|
|||<||<|||	|<||<q�W||t|�t|	�fS)z�Create (lshape, rshape, tuple(lslices), tuple(rslices)) such that
       shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array
       with shape 'lshape' and y is an array with shape 'rshape'.rrr�r�rr��PTFrx)rrx)rrVr�r�r�rS)r�r�ryr�rZ
all_randomr�r�r�r�ZsmallZbigr�r�r�Zs_smallrar�Zs_bigr4r4r5�rand_aligned_slices7s@		



!%r�cCs0|\}}}}}}t||d|d�S)zMReturn a list of random items for structure 't' with format
       'fmtchar'.rOrC)rX)r2rjr�r{rar4r4r5�randitems_from_structurebsr�c

CsD|\}}}}}}	t|d|d|d|d|	dt|B�S)z:Return ndarray from the tuple returned by rand_structure()rzr|�formatr��flags)r�ND_WRITABLE)
rcr2rjr�r�r{ryrzr|r�r4r4r5�ndarray_from_structurehsr�cCs�|\}}}}}}t|�}	x4t|�D]&\}
}tj||	|
||�q1Wtd|	d|d|d|d|�S)z>Return numpy_array from the tuple returned by rand_structure()�bufferrzr|�dtyper�)�	bytearray�	enumerater/�	pack_into�numpy_array)rcr2rjr�r{ryrzr|r��bufr�rur4r4r5�numpy_array_from_structurens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)a�Interpret the raw memory of 'exporter' as a list of items with
       size 'itemsize'. If shape=None, the new structure is assumed to
       be 1-D with n * itemsize = bytelen. If shape is given, the usual
       constraint for contiguous arrays prod(shape) * itemsize = bytelen
       applies. On success, return (items, shape). If the constraints
       cannot be met, return (None, None). If a chunk of bytes is interpreted
       as NaN as a result of float conversion, return ('nan', None).Nrcs$g|]}�||���qSr4r4)ror)r{�memr4r5rp�s	zcast_items.<locals>.<listcomp>�nan)
�nbytesrvry�divmod�tobytesrVrRr/rIrQ)Zexporterr2r{rzZbytelenrr�Z	byteitemsrcrurdr4)r{r�r5�
cast_items|s(	

	
.

r�c#s�xtd�D]}|gVq
Wtdd�}td�dkrFdnd��fdd	�t|�D�Vtdd�}td�dkr�dnd��fd
d	�t|�D�VdS)z Generate shapes to test casting.r&��r�r�rrcsg|]}t�d��qS)r�)r)rora)r�r4r5rp�s	z!gencastshapes.<locals>.<listcomp>csg|]}t�d��qS)r�)r)rora)r�r4r5rp�s	N)rVr)rryr4)r�r5�
gencastshapes�sr�cCs:tt||d�t||d�t||d��S)z4Generate all possible slices for a single dimension.r)rrV)rr4r4r5�	genslices�sr�cs)�fdd�t|�D�}t|�S)z/Generate all possible slice tuples for 'shape'.cs g|]}t�|��qSr4)r�)ror)rzr4r5rp�s	z"genslices_ndim.<locals>.<listcomp>)rVr)ryrzr�r4)rzr5�genslices_ndim�sr�FcCs>|s|dkrdnd}t||d�}t||�S)z�Generate random slice for a single dimension of length n.
       If zero=True, the slices may be empty, otherwise they will
       be non-empty.rr)rr�)r�allow_emptyZminlenr�r4r4r5�rslice�sr�ccs)x"td�D]}t||�Vq
WdS)z.Generate random slices for a single dimension.r�N)rVr�)rr�rar4r4r5r��sr�c#s�x8t|�D]*}t�fdd�t|�D��Vq
Wx8t|�D]*}t�fdd�t|�D��VqHWtdd�t|�D��VdS)z)Generate random slice tuples for 'shape'.c3s|]}t�|�VqdS)N)r�)ror)rzr4r5r��szrslices_ndim.<locals>.<genexpr>c3s%|]}t�|dd�VqdS)r�TN)r�)ror)rzr4r5r��scss!|]}tddd�VqdS)rrN)r�)rorar4r4r5r��sN)rVrS)ryrzZ
iterationsrar4)rzr5�rslices_ndim�s
((r�ccsBt|�}|dkr$t|�n|}tt||��VdS)N)rSrRr)�iterabler�Zpoolr4r4r5�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)zPrint ndarray for debugging.�unknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s)N)�tolist�	TypeError�NotImplementedErrorr�rqrr�r��printrzr|�
suboffsetsr�r{�sys�stdout�flush)�ndrMr�r�r4r4r5�
ndarray_print�s	r�r�rZ�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�Z4dcdd�Z5dedf�Z6dgdh�Z7didj�Z8dkdl�Z9dmdn�Z:dodp�Z;dqdr�Z<dsdt�Z=dudv�Z>dwdx�Z?dydz�Z@d{d|�ZAd}d~�ZBdd��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�S)��TestBufferProtocolcCst�|_dS)N)Zget_sizeof_void_p�
sizeof_void_p)�selfr4r4r5�setUp�szTestBufferProtocol.setUprFc$sM|rt|��}n�s.t|	�}n�}f}
|jr|j|d�d}xYtd|�D]H}||dkr�Pn||dkri|||||d7}qiqiW|gdd�t|d�D�}
|j}|ddkr�|}n|gt|dd��}n|j|j|�|j	|j
|�|j	|j��|j	|j��|j	|j
|�|j	|j|�|j	|jt|��|
o�|
s�|j	|jt|��n|j	|jt|
��t|t�st��r4�r|j�n	|j�}|j	||	�n�s>dS|s�t�}d}x�t|�D]�}yet||�}t|	|�}t|t�r�tj�|�}ntj�|�}|j|�Wntk
r�d}PYnX|j	||�q`W|s�|j	|j�|��r#�nd}t|	�}xzddd	gD]f}|}|dkr�t |d	�s|t |d�r�t!||�}t|d
|d|dt"�}q�nFt |d	�s�t |d�r�|dkr�t|d
|d|�}nt#|t$|��|j	�j�|�|j%t&�|��|dkrEqEnt|�}|r]dnt'}t(|dt)��|j	t��|�����fd
d�t|�D�}t|d�dkr�dd�|D�}nt|d
|d|d��} |j	t*| �t*|��t(|dt)��|j	t��|�����fdd�t|�D�}t|d�dkr�dd�|D�}nt|d
|d|t"Bd��} |j	t*| �t*|��t(|d	t)��|j	t��|�����fdd�t|�D�}t|d�dkrRdd�|D�}nt |d�rgt"nd}!t|d
|d|!|Bd��} |j	t*| �t*|��qEWq�nt��rIyt*|�}"Wntk
r�dSYnXt|t*�r|jn|}#|j|"j|#�|j	|"j
|�|j	|"j��|j	|"j��|j	|"j
|�|j	|"j|�|j	|"jt|��|
o�|
s�|j	|"jt|��n|j	|"jt|
��|dkr�dn	t|	�}|j	t|"�|��r|j�n	|j�}|j	||	�|j	|"|�ndS)NrrcSsg|]}d�qS)rrxr4)rorur4r4r5rps	z-TestBufferProtocol.verify.<locals>.<listcomp>Trrw�F�Arzr�r�cs)g|]}tj��|���qSr4)r/�unpack_from)ror)�contigr2r{r4r5rpss	cSsg|]}|d�qS)rr4)rorur4r4r5rpvs	cs)g|]}tj��|���qSr4)r/r�)ror)r�r2r{r4r5rp~s	cSsg|]}|d�qS)rr4)rorur4r4r5rp�s	cs)g|]}tj��|���qSr4)r/r�)ror)r�r2r{r4r5rp�s	cSsg|]}|d�qS)rr4)rorur4r4r5rp�s	)+rvrRr�Z
assertGreaterrVr�rr�assertIsrL�assertEqualr�r{r��readonlyryrzrSr|rqrrnr�r�r�r��get_pointerr�r/r0�extend�BufferErrorr��
is_contiguousr��
ND_FORTRAN�get_contiguous�
PyBUF_READ�
assertTrue�
cmp_contigr��py_buffer_to_contiguous�
PyBUF_FULL_ROrD)$r��resultrLr{r2r�ryrzr|rW�sliced�castZexpected_lenr�Z
suboffset0rZstride0ZreprZbuf_errr�Zitem1Zitem2rMZffZ	flattened�order�expectedZtransr`�roZinitlst�yrr=�exr4)r�r2r{r5�verify�s�	"$	
 	
	"&
	zTestBufferProtocol.verifycCs?dd�}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�}n||t�s�d}n||t�r�|jnf}
||t
�r�|jnf}t|d|�}|j|d	|d
|d|
d|	d
|d|
d|d|d|�	dS)NcSs|jdkp|jdkS)Nr7r)r�)rr4r4r5�
simple_fmt�sz4TestBufferProtocol.verify_getbuf.<locals>.simple_fmtcSs||@|kS)Nr4)�reqZflagr4r4r5�match�sz/TestBufferProtocol.verify_getbuf.<locals>.match�getbufrr7rrLr{r2r�ryrzr|rWr�)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�r�r{ryr�rzr|r)r�Zorig_exrrr�rrrWr�rr2r{ryrzr|r4r4r5�
verify_getbuf�s>				z TestBufferProtocol.verify_getbufcstttttttttt	t
ttt
tf}dd�td�D�dfddddd	d
ddd
dddgdfddddd	d
ddd
dddgdfdd�td�D�dff}ggdfdggdfdgdgdfd
gdgdfd
gdgdfddggdfddgd d!gdfddgddgdfddgd"d#gdff	}dttttBtttBf}dttttBf}x"|D]\}}tj|��x�|D]�\}}	}
�fdd�|	D�}	|
�9}
x�|D]�}|	r0|t@r0qn|rG|t@rGqn|rS|n|d}t|d|d|d|d|	d|
�}
|r�|
ddd$�nd}t|
�}|r�t|�}n|
jdks�|
jdkr|r|	r|j||
�n|r8|jdkr8|r8|	r8|j||�nx�|D]�}x|D]w}|j|
|
||B�|j|
|||B�|rL|j||||Bdd�|j||||Bdd�qLqLWq?WqWq�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�}dS)%NcSs&g|]}|drdnd�qS)rTFr4)rorMr4r4r5rp�s	z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>�r
rrr�r�r�r�r"r!�	rZ�rrcSs.g|]$}|dr d|nd|�qS)rr%lli�r4)rorMr4r4r5rp�s	rrcsg|]}|��qSr4r4)roru)r{r4r5rps	r�r�rzr|r�r�Trrx������rxrrxr)rrr�PyBUF_SIMPLEr
rr�
PyBUF_FULLr��
PyBUF_RECORDS�PyBUF_RECORDS_RO�
PyBUF_STRIDED�PyBUF_STRIDED_RO�PyBUF_CONTIG�PyBUF_CONTIG_ROrVr�r��ND_PILr	rr/�calcsizerrDryr�r�ND_GETBUF_FAILrr�)r��requestsZ	items_fmtZ	structureZndflagsZ
real_flagsrcr2rzr|r�r�Z_items�ex1�ex2�m1�m2r�bitsr�baser�r4)r{r5�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�rAr�rzr|r�r�rZXXXri�rZQLr)r�r?r7z@#$r�r"r!rZrs123r�rr���xyzrwi�u �Z�r���)r�r�)rr�rxl	l	)rrr����rr3l)rl)r�ND_VAREXPORT�pushrr�r�r#r�r/r1ZND_MAX_NDIM�
OverflowErrorr�rr�r	�poprD�releaser�rrrVrr��memoryview_from_bufferr�r��UnicodeEncodeErrorr�r�)
r�r�Zndmrrrcr=rr'r(r4r4r5�test_ndarray_exceptions9s�%.+%%%'"%((+
'%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�)
rrVrr4rDr5r8rr�r7)r�Zpermr=r�rr4r4r5�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�rLr{r2r�rryr|rW)rkr/r$rr)r�r2Zscalarrar{r�r4r4r5�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)NrrZrrzr�r�rLr{r2r�ryr|rW)rrkr/r$r#rr)r�rir2rcrar{r�r�r4r4r5�test_ndarray_format_shape�s!z,TestBufferProtocol.test_ndarray_format_shapecCsBtdd�}x,t|�D]\}}}tj|�}x�tdd�D]�}|dkrbqJnt|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�rLr{r2r�ryrW���)	rrkr/r$rVrRr#rr)r�rir2rcrar{r�rzr|r�r�r�r4r4r5�test_ndarray_format_strides�s 
 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!rrZrrrzr|r)r�r�)rr�)r�r�)rrrr�r�r�)r�rcrr�r4r4r5�test_ndarray_fortrans*z'TestBufferProtocol.test_ndarray_fortrancCsNxGtd�D]9}dd�t|�D�}t|�}xt|�D]�}t|�\}}}tj|�}x�dtfD]�}	|dkr�|	tkr�q|nt|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|�nt|d|d|d|	tB�}
t|||d�}t||�}|j
|
d	dd
|d|dd
d|d|d|d|�q|WqEWq
WdS)Nr�cSsg|]}tdd��qS)rrZ)r)rorar4r4r5rps	z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>rrzr�r�rwrLr{r2r�rryr|rWrr�)rVrvrrfr/r$r#rr}r�rrnr"rr�r|r9r�r�)r�ry�shape_trirzr2rcrar{r�r�r|rWr�mvr4r4r5�test_ndarray_multidimsB

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�r.ZLQ�1llll)r�r.)r�r.)rr)r�r�)r�r�)rFr.)rrr��__setitem__rDr�r��__delitem__r6�__getitem__�
IndexErrorrRr/r1r�)r�r�rDrcr4r4r5�test_ndarray_index_invalidEs:''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�rDr�r�rr�rIrG)r�r�rDrMr4r4r5�test_ndarray_index_scalarls2







z,TestBufferProtocol.test_ndarray_index_scalarcCsxtttd	��dddgdt�}t|dt�}|jt|jd�|jt|jtddd��dS)
Nrr�rzr�rrr�r�r!)	rrrrVr�r!rr�rIr�)r�rr�r4r4r5�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
q
Wx�td�D]�\}}}t|ddgdt	d|�}t|dt
tB�}x/tdd�D]}|j||||�q�Wt|�r/|j�}|j
|j|�t�x2tdd�D]}|j||||�q�Wq/q/Wddd	d
dg}t|ddg�}t|dt�}x/tdd�D]}|j||||�q]Wddd	d
dg}t|ddg�}t|dt�}x/tdd�D]}|j||||�q�WdS)Nr�rzr�r�r�rrrr�r�r@i����r@i����r@r@r@r@)rkrrVr�rrJrIrnrDr�r!rr9r��__eq__�NotImplementedr"r)r�r2rcrar�rrDrr4r4r5�!test_ndarray_index_getitem_single�sB!#z4TestBufferProtocol.test_ndarray_index_getitem_singlec	Cs�xstd�D]e\}}}t|ddgd|dt�}x(td�D]}|||<|||<qJW|j|j�|�|jt|jd|�|jt|jd|�t	|�s�q
nt|ddgd|dt�}t
|�}|j||�x(td�D]}|||<|||<qW|j|j�|�|jt|jd	|�|jt|jd|�q
Wx5td�D]'\}}}t|ddgd|dt�}x;td
d�D]*}||d||<||d||<q�W|j|j�|�t	|�sq�nt|ddgd|dt�}t
|�}|j||�x;tdd�D]*}||d||<||d||<qfW|j|j�|�q�WdS)Nr�rzr�r�r�r�ri����i����r@r@)rkrr�rVr�r�rrJrGrnrD)r�r2rcZsingle_itemr�rrDr4r4r5�!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�)rvrrfr#rr�rVr�r�r�r�)
r�rCrirzr2rcrar�r�rWrr��kr4r4r5�#test_ndarray_index_getitem_multidim�s,&4
&z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCsBtddf�}|jttdt��t|�}|j||�|jttdt��x�td�D]�\}}}t|ddgd|�}xAt|�D]3\}}|j|||�|j	||k�q�Wt
|�rmt|�}xDt|�D]3\}}|j|||�|j	||k�qWqmqmWdS)Nrrzz1 in ndz1 in mvr�r�)rrr��eval�localsrDr�rkr�r�rn)r�r�rDr2rcrarrur4r4r5�test_ndarray_sequences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�rDrr�rGr�rr�r�rIr�r�r�r�r#�add_suboffsetsr)r�rc�xl�mlr�xrr�rD�mr�yrrMr4r4r5�test_ndarray_slice_invalids�%%%!""$%%((%!//!//$/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!rrZrrrzr�rr�)rr�r�r�rDr�)r�rcrMrrYr[r4r4r5�test_ndarray_slice_zero_shapexs*!$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�)rRrvrrfr/r$r#rr�r�r��	Exception�	__class__r�r�r�)r�rCryrirzr2rcrar{r�r�rWr��listerrr��e�nderr�ndslicedr4r4r5�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)rRrvrrfr/r$rrWrDr�r�r�r_r`r�r�r�)r�rCryrirzr2rcrar{r�rrDrWr�rar�rbrcrdr4r4r5�'test_ndarray_slice_redundant_suboffsets�s4

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc@Cs�x�td�D]�\}}}x�td�D]�}x�td�D]�}xxdtfD]j}|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�qOnd}y|
||
|<Wn.tk
r}z|j}WYdd}~XnX|r4|j	|t
�qO|j|
j�|
�|j|
|�|j	||�|j|
d|	d|jd|d	dd
|jd|jd|jd|j��qOWq<Wq)Wq
WdS)
Nr�rrzr�r�rLr{r2r�ryr|rW)rkr�r#r�rrDrRr_r`r�r�r�r�rnrr{ryrzr|)r�r2rcra�lslicer�r�rr�rrDZlsterrZdiff_structurerWZlvalZrvalrbrcZmverrr4r4r5� 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�)rRrvrrfr#rV�
ITERATIONSr�rr�r�r�r_r`r�r�r�)r�rCryrirzr2rcrar�r�r�r�rWrar�rbrcr4r4r5�"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|	|�n|ddkrSt|||dt�}
|
j�}|j||�t
|
�}t|�rS|j||
�|j�}	|j|	|�qSntr|d}d|kruqnt|||�}
|j|d	dd
|
jd|ddd
|
jd|
jd|
jd|
j��qqWq
WdS)Nr8Tr�r�rrr�r�rLr{r2r�ryrzr|rW)rVrirFr/r$r��MAXDIM�MAXSHAPEr�r�r�r�r�rDrnr�r#r�r�rr{ryrzr|)r�rar2r{rjrcrMZxlistrDZmvlistrZylistrz�zr4r4r5�test_ndarray_random&s>	
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|||�}
Wq tk
r}z|j
}	WYdd}~Xq XqqWq
WdS)Nr8Fr�r�)rVrirFr/r$r�rkrl�assertFalser�r�r�r_r`r�r�r�)r�rar2r{rjrcrcrMrbZ	numpy_errrr4r4r5�test_ndarray_random_invalidOs(	
z.TestBufferProtocol.test_ndarray_random_invalidcCsMxFtt�D]8}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
�|�ntrd|ksd|krQqnt|
||�}t|||	�}||||<t|�r�t|	�r�|j|d	dd
|jd|ddd
|jd|jd|jd|j
��n|j|
d	dd
|jd|ddd
|jd|jd|jd|j
��qqWq
WdS)Nr8r�r�Trzrrr�rLr{r2r�ryr|rW)rVrirFr/r$r�rkrlr�r�r�r�r�r�rDr�r#r�r�r�rr{ryrzr|)r�rar2r{r�r�r�r�ZtlZtrZlitemsZritemsrXrZZxllistZxrlistrYr[Zylr\ZyllistZyrlistZzlZzrr4r4r5� test_ndarray_random_slice_assignks\! 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!rrZrrrzr�rrx)rrx)rr#r�r�r�rLr�r�rorr
r)r�rcr�rr4r4r5�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rDr�r�)r�r�r�rDr4r4r5�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rDr�r�)r�r�r�rDr4r4r5�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rrrVr�r�r�)r�r�r4r4r5�test_ndarray_offset�s'z&TestBufferProtocol.test_ndarray_offsetcCs^xWdtfD]I}tttd��ddgd|�}|j�}|j||�q
WdS)Nrr�rzr�)r#rrrrVr9r�)r�r�r�r=r4r4r5�#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rrrVr�r�)r�r�r�rr4r4r5�test_ndarray_get_pointer�s'z+TestBufferProtocol.test_ndarray_get_pointercCs�tttd��ddddg�}t|dttB�}|j|j�|j��t|�}|j|j�|j��dS)Nrurzrr�r)rrrrVrrr�r�rD)r�rr�r=r4r4r5� 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)ror�rrrrVr�)r�rMrr4r4r5�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�)
rBrrr��hashrGrrrVr�r�r�r#r�)r�r>r�rr4r4r5�test_ndarray_hash�s*!$'**----z$TestBufferProtocol.test_ndarray_hashc
Csq	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�}trgtdt|�dddgddd|d
kr[dnd�}	n|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�q�W|d
kr�tt|ddg�dddgdt�}
nt|dddgdt�}
|
j�}x�tttttgD]�}yt|d|�}Wntk
r%w�YnX|j||�tdd�|D�dddgdttB�}|jt|�t|��tr�|j||	jdd��q�q�W|tkr�t|dddgdt�}
|
j�}n|j�}x�tttttgD]�}yt|d|�}Wntk
r8wYnX|j||�tdd�|D�dddgd|tB�}|jt|�t|��tr|j||	jdd��qqWq�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�rr�cSsg|]}|�qSr4r4)rorur4r4r5rp�s	zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>cSsg|]}|�qSr4r4)rorur4r4r5rp�s	cSsg|]}|�qSr4r4)rorur4r4r5rp�s	cSsg|]}|�qSr4r4)rorur4r4r5rp�s	cSsg|]}|�qSr4r4)rorur4r4r5rp	s	cSsg|]}|�qSr4r4)rorur4r4r5rp
	s	rx)rrrrrr�rrrr r!r"rr�r�rr�r�r�rrrVr�r�r�r�r�rDZtostringr#)
r�r&r�r�ZrequestrrZndbytesrWZnarMrrr4r4r5�test_py_buffer_to_contiguouss�
$
*	
(
.
-

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�rzrwrLr{r2rr�ryr|rWrr7rr�r��9rMr)r�rr�r?)rrx)rrrVrrDr�rrrRr}r�rr"rr�r|r9rr�rzr�r�r
r�r#rr�r�r�r�r�)r�Zitems_shapercrzrr=ryr|rWr*r�r4r4r5�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�rrzrrDr})rr�rrDrr�r�rkr�r�r�)r�rcrzr�msrcr2rar=r4r4r5�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 3cPZ3nr!r}rrzrr�r�rr�)�check_sizeofrZcalcvobjsizerrrVrDr)r�ZcheckZvsizeZbase_structZper_dimrcr>r4r4r5�test_memoryview_sizeof�	s		!!!z)TestBufferProtocol.test_memoryview_sizeofcCs�Gdd�dt�}Gdd�dt�}dd�}|d�|d�d#td
�dtdd
�dd
dgdddhidd6fd$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�nx�|D]�}d}
yt	j
||	||�Wnt	j
k
r�t	j
}
YnXd}y||
d<Wn4ttfk
r-}z|j}WYdd}~XnX|
s:|r]|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�r4r4r5�__init__�	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__cSs|jS)N)r�)r�r4r4r5�__int__�	szETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)�__name__�
__module__�__qualname__r�r�r4r4r4r5�INT�	sr�c@s(eZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXcSs
||_dS)N)r�)r�r�r4r4r5r��	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__cSs|jS)N)r�)r�r4r4r5�	__index__�	szGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r�r�r�r�r�r4r4r4r5�IDX�	sr�cSsdS)Nr"r4r4r4r4r5r�	sz;TestBufferProtocol.test_memoryview_struct_module.<locals>.frg������@y@z-21.1gffffff(@r�rrr�r�r�r!r"TF�asabcr>�abccSs|S)Nr4)rMr4r4r5�<lambda>�	szBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rZrDrzr�r�rry������@@)r)�objectr	r
rOr�rkrr�rDr/r�r�r$r1r�r�r`ZassertIsNot)r�r�r�r�valuesr2rcrdrr�r=r{ruZ
struct_errZmv_errrbr4r4r5�test_memoryview_struct_module�	sF(!!

z0TestBufferProtocol.test_memoryview_struct_modulecCs]tdddgddgddg�}|j|j�t|�}|jt|jd�dS)Nrrr�rzr|rr)rrorrDrr�r�)r�rr�r4r4r5�!test_memoryview_cast_zero_strides�	s'z4TestBufferProtocol.test_memoryview_cast_zero_stridescCs$x�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]�\}}
}
t|�sHt|�rk|jt
|j
|d|g�qt|�rt|�r|jt	|j
|d|g�qqWq�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)\Nrr8r&rzr�rrr�xr�r�r��Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIZIIrr!rrr�r"rMcSsg|]}d�qS)rr4)rorar4r4r5rp1
s	zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>rrr)cSsg|]}d�qS)rr4)rorar4r4r5rp;
s	cSsg|]}d�qS)rr4)rorar4r4r5rp@
s	r'�Ir*iQiAji��	rurZi���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rrrVrDrr�r�rkrnr�rlr�rIrGr�r6r��maxsize)r�ZsfmtZsformatZssizeZdfmtZdformatZdsizerr�ZsitemsraZsize_hZsize_dr=r�r)r*r4r4r5�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�)rr4r4r5r�Z
sz9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>rcSsdd�t|j��D�S)NcSs,g|]"}|dkr"|dn|�qS)r*r(r4)rorMr4r4r5rp[
s	zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rrr�)rr4r4r5r�[
srcSsdd�t|j��D�S)NcSs%g|]}tt|�d��qS)zlatin-1)rG�chr)rorMr4r4r5rp\
s	zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rrr�)rr4r4r5r�\
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)NrLr{rr2r�rryrzr%r|rWr�T)r)r/r$r�rr�r�)
rr=rcr2�srcsizerEZto_bytelistr*rW�m3)�bytespecr�r4r5�iter_roundtrip_
s
z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtriprrrzr�rrLr{r2r�ryr|rWr�Trr%rBrDr�)r)
r/r$rr�rDr�rrkrBr�)r�r�r�rZ	destitemsZ	destshaper=r*Zdestsizer2rcrar4)r�r�r5�test_memoryview_castX
s<
"
!z'TestBufferProtocol.test_memoryview_castcCs�x�t�D]�}x�tdD]�}dtd�|}tj|�}t|�|}t|�rfdnd}x2t||�D]!\}}}	tj|�}
|dkr�|gng}||
g}t|d|d|�}
t	|
�}t
|
||d|�\}}|dkr/|jt|j
||�q|n|d	krAq|nt|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
WdS)Nr8r7rrDrErrzr�r�rLr{r2r�rryr|rWr�T)r7r8)r�rFrr/r$rvrlrkrrDr�rr�r�rRr|r�r)r�Z_tshaperKZtfmtZtsizerrLr2rcra�sizerzZtshaperr=Ztitemsr�r*ryr|rWr�Zm4r4r4r5�test_memoryview_cast_1D_ND�
sP
	
	
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�r?��i	)rr�)r"r)	rBrrrVrDr�r�rrr�)r�r>r=rr4r4r5�test_memoryview_tolist�
s(!3!0z)TestBufferProtocol.test_memoryview_tolistcCsdttd��}|j�}|j|jd��|j�|j�}|j|jd��dS)Nrz<memoryz	<released)rDr��__repr__r��
startswithr8)r�r=r�r4r4r5�test_memoryview_repr�
s
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)	�floatrBrDZassertInrrr�rTrU)r�r2�infrr=r4r4r5�test_memoryview_sequence�
s
z+TestBufferProtocol.test_memoryview_sequencecCs�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|�}|jt|jd�|jt|jdd�|jt|jd�dS)Ng)@rzr�r.rrrr�Ziiir"r�rg@gr!rr�r)rrr�li����)rrDr�rr�rIr�rrrVr�rJrG)r�rr=r4r4r5�test_memoryview_index�
s*'*z(TestBufferProtocol.test_memoryview_indexc	CsTtddgdddt�}t|�}d|f<|j|fd�d|d<|j|fd�|jt|jd	d
�tttd��ddg�}t|�}|jt|jdd
�tttd��ddgdt�}t|�}|jt	|jd/d�|jt|jdd
�|jt|jdd�|jt	|jd0d�|jt	|jdd�x�t
dD]�}|dks�|dkr�q�ntdddgddgd|dt�}t|�}td1d�}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�qdn|dkr�qdn|j
|�}|\}	}
|d&ks�|dkr�d5\}	}
n|d(kr|jt|jd	|	d�|jt|jd	d)�n|jt|jd	|
�qdW|j
d�}|jt|jd	d*�tttd��ddgdd+dt�}t|�}|jt|jd	d�td,gddgdd-dt�}t|�}|jt|jd	d�tttd.��ddd$gdt�}t|�}|jt|jd	ddg�dS)6Ng)@rzr�rr�g�6@g�7@.rg33333�8@r"rrZrrg@grr!ru�r8rr
rr��1�2�3rTF�xr�r�r�r�rirr/s��ZxLs12345rrli����r3lE����E)r�r�)rr�rDr�rr�rGrrrVrJrFrrcr�r�r�)r�rr=r2rr�r)Z_ranger*�lo�hir4r4r5�test_memoryview_assign!s�

!'*
!	
!	
$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�rxrxrxrrxrrx)rrrrVr�rDrr�rIr�rGr�r�r�r#r�)r�rr=r�r'r(r)r*r4r4r5�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=�
singleitemrraZaiZmir4r4r5�cmptest�s




z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestrr�rB)
rVrkr�rBrDr�r�r�rRr_r`r�r�)r�r�rr2rcr�rgr�r>rr=Z	array_errZhave_resizeZal�arrbZm_errr4r4r5�test_memoryview_array�sF

z(TestBufferProtocol.test_memoryview_arrayc	Cshtjddddg�}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||�trdGdd�dtj�}|dd�}t|�}t|�}|j||�|j||�|j||�|j
t|j�ndS)Nrrrr�r"�__lt__�__le__�__gt__�__ge__rrzr�zl x d xr�r��ur/c@s.eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrMrN)r�r�r��ctypes�c_long�_fields_r4r4r4r5�BEPointsr�r�r.)z__lt__z__le__z__gt__z__ge__)rrrx)rBrDr��getattrrOr8r�r�rr�r�r��BigEndianStructurerr�r�)	r�r>rru�w�attrr�r��pointr4r4r5�%test_memoryview_compare_special_cases�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�rDr�ro�__ne__r�rNrrrVr#)r��nd1�nd2rur�r�rr=r4r4r5�!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'���rxri��i����i'���i��i����i'���rxri��i����i'���i��i����i'���rxri��i����i'���i��i����i'���rxr)rrDr�r�r#)r�r�r�rur�r4r4r5� test_memoryview_compare_ndim_oneg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rDr�)r�r�r�rur�r4r4r5�"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rDr�)r�r�r�rur�r4r4r5�$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)
NrZz@mrDr8rrzr�r�r�r�r3r3)rFrfr#rrDr�rV)
r�rrKr2rcr�r�r�r=rar4r4r5�&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@hrr?rz=f q xxLg333333�?z< f 2Qr�r"z! b B xLz
= Qx l xxLri�)rrr�333333�)r�rr)rrr)rrr)rrrrVrDr�r�)r�r�r�rur�r4r4r5�"test_memoryview_compare_multidim_c�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�rr?rrr�z=Qqz=qQrZ0llz@bi�l�����rx)r�rxrxl�����)rxr�i�i�i�i�)rrrrVr�rDr�r�)r�r�r�rur�r4r4r5�(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�r?g������@zd b czd h c�(r!z@Irrr%rz=iiz>iirr�123Zb3sZi3si��ffffff
����)r�r�r�皙��������)r�r�r�rxrrl��l��)r�r�rxrl��l��)r�r�rrrrr)rr�r)rr�r)rrrVr�rr�rDr�r�)	r��lst1�lst2r�r�rur�r'r(r4r4r5�&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)
Nr?rzrr�rr�rr�z@i)rrrrVrDr�r�)r�r�r�rur�r4r4r5�+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|rrrrrZz=lQz<lQ)rr)rr)rrDr�r�)r�r�r�rur�r4r4r5�-test_memoryview_compare_multidim_zero_stridess&(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)NNr�rzr�r!r�z@Ir�rrrr�rz=Qqr"z>Qqr?rZBbr�hellor}�Z5s0sP�sushir#z@lr�sashimi�sliced������4@�ramen�spicy�fffff�"@rz
< 10p 9p dz
> 10p 9p drxrrl�����rx)r�rxr)rrrxrl�����rx)r�rxr)rrrrrr(r1rx)r1rxrr(r1rx)r1rxrrr)r�r}r)r�r}rr)r�r}r)r�r}rri�)r�r�r�)r�r�r�)
rrrrVr#rDr�r�r�r�r�)	r�r'r�r(r�rur�r�r�r4r4r5�+test_memoryview_compare_multidim_suboffsetss�*"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;r9r:r<rr'r�rzr�r�r�rr�rr%r#ZQLHll����)rrr����)rrr)rrr)rr�r�rDr�r�)r��	byteorderrMrr>rr4r4r5�!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 mrrrLr�r�r{r�ryrzr|r�rr
r)zobjznbyteszreadonlyzitemsizezformatzndimzshapezstridesz
suboffsetszc_contiguouszf_contiguousz
contiguous)rBrDr8rr�r�rr�r�rTrUrIrG�__getattribute__r�r�)r�r>r=r�rr)r*r4r4r5�test_memoryview_check_released�s.


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|��ndS)Ni�@iq�i�rzr�r�z@hrz>hQiLlcsg|]}��qSr4r4)rora)rjr4r5rp�s	z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rrr�z=hQiLlcsg|]}��qSr4r4)rora)rjr4r5rp�s	r�z<hQiLlc@s.eZdZdejfdejfgZdS)z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrMrN)r�r�r�r�r�r�r4r4r4r5r��sr�r�r.�������'���)r
rrrr)rrDr�r�rVr�r�rG)r�r�r=r�r�r>r4)rjr5�test_memoryview_tobytes�s.: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�rZrr!r"rrrurr�r�rx)rr�r�r�r�ZPyBUF_WRITErr�r�rJrIr�rrrVrGr�r#r�r)r�r�r�r=r4r4r5�test_memoryview_get_contiguouss�'

$*	&$ * -*&"!
&".&-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$rBrD�io�BytesIOr��readintor�r�rrrrV)r�r�r>r=r�rr�r4r4r5�test_memoryview_serializingys!
-
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�rr?r�r�rr�rrrr�rz= Lz< hrrrxrxi����i����)
rGrrrVrDr�r{r�rr�r#rr��__hash__)	r�rr=ZmcZmxr�rMr>rr4r4r5�test_memoryview_hash�sJ%-'+-+''-0-0z'TestBufferProtocol.test_memoryview_hashc8Csqtdddg�}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��WdQXtttd��ddddgdd�}t|dtdt�}	t|	dtdt�}|j|j	|�t|��L}|
|�|j|dd�j
�dddgdddggg�WdQXxdtfD]�}td�}t|���}~t|dtd|�}	t|	���}~	t|dtd|�}t|��y}
~|
|
�|
|�|
|�|j|dtd��|j|dtd��|j|
dtd��~
WdQX~WdQX~WdQXtd�}t|���}~t|dtd|�}	t|	���}~	t|dtd|�}t|��}
~|
|�|
|�|
|
�|j|dtd��|j|dtd��|j|
dtd��~~~
WdQXWdQXWdQXqWtd�}|jt��:t|��'}t|�}|dtd�kWdQXWdQXdS)Nrrr�rr�rzr�r�r�r"r!cSst|��}WdQXdS)N)rD)rr*r4r4r5�catch22sz;TestBufferProtocol.test_memoryview_release.<locals>.catch22s123rr�rr�rr�r�)r�rDrrr�r8r��ND_REDIRECTr�rLr4r5r�r�r7�ordrrrV)r�r>r=r�r�r�r)r*rMrrrmr�r�rr4r4r5�test_memoryview_release�s�





$%





 -
A


			


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�?r4)rorMr4r4r5rpUs	z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rrzr�rcSsg|]}d|�qS)g�?r4)rorMr4r4r5rpVs	rr�rr�)	rrVrBr�rrDr�rLr�)r�r�r>rMrrmr=r4r4r5�test_memoryview_redirectSs".%$$$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!rrZrrLr{r2r�ryrzrr|rWrr�Zlegacy_modeT)rr�ZstaticarrayrDrrVr�rr�r�rLr)r�r2rWrMrrrmr=r4r4r5�$test_memoryview_from_static_exporterks�*				z7TestBufferProtocol.test_memoryview_from_static_exportercCs<tdddgdgdttB�}|jtt|�dS)Nrrr�r�)rr%ZND_GETBUF_UNDEFINEDrr�rD)r�r�r4r4r5�#test_memoryview_getbuffer_undefined�s%z6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs;tdddgddgdt�}|jtt|�dS)Nrrr�rzr�)rr%rr�rD)r�rMr4r4r5�test_issue_7385�s$z"TestBufferProtocol.test_issue_7385Nrxrxrxrxrx)Pr�r�r�r�rrr-r;r<r=r>rArBrErKrLrMrPrQrSrVr]r^rerfrhrjrnrprqrrrsrtrvrwrxryrzr|r~r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr
rrrrrrrrrr4r4r4r5r��s��2\�
-'	0.Y #6!)>
		%�u	
8	t<6	"\8>5?H>Ha�(#u<�^r��__main__)dZunittestZtestr�	itertoolsrrZrandomrrr�	sysconfigr�warningsr�rBr�decimalr	Z	fractionsr
Z_testbuffer�ImportErrorrr/r��catch_warningsrCr�Z
SHORT_TESTZNATIVEr0r1ZSTANDARDr6rFr2�copyrmZARRAYrRZBYTEFMTr\r^r[rNrUrXrerfrgrkrlrnr�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�rirkrlZ
skipUnlessZTestCaser�r��mainr4r4r4r5�<module>s<$






'''''/









+	

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