HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/local/lib/python3.7/test/__pycache__/test_buffer.cpython-37.pyc
B

��gN|�@sddlZddlZddlmZddlmZmZddlmZm	Z	m
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlmZyddlTWnek
r�dZYnXyddlZWnek
r�dZYnXyddlZWnek
r�dZYnXy:e���(e_e���ddlmZWdQRXWdQRXWnek
�rFdZYnXd	Zdddddddddddddddd
�Ze�r�ed=ed=e�r�y e� d
d�ded<ded
<Wnej!k
�r�YnXddddddddddddddd>dd>fd�Z"dd�Z#eee"e"e"e"d�Z$e�r6x"e$dD]Z%e#e%�e$de%<�qWe�&�Z'e�&�Z(xeD]Z)e)d k�rLe(e)=�qLWe�&�Z*xeD]Z)e)d!k�rte*e)=�qtWe'e$d"<e'e$d#<e(e$d$<e*e$d%<e*e$d&<dZ+dZ,d'dd(d)d*d+gd'd,d-d.gfd$gd'gfd'gd'gfd#d"gd'gfd&d%gd'gfd/�Z-d0d1�Z.d2d3�Z/d4d5�Z0d6d7�Z1d�d9d:�Z2d�d;d<�Z3d�d=d>�Z4d?d@�Z5dAdB�Z6dCdD�e$dD�Z7dEdF�Z8dGdH�Z9dIdJ�Z:dKdL�Z;dMdN�Z<dOdP�Z=dQdR�Z>dSdT�Z?dUdV�Z@dWdX�ZAdYdZ�ZBd[d\�ZCd]d^�ZDd_d`�ZEdadb�ZFdcdd�ZGdedf�ZHdgdh�ZIdidj�ZJdkdl�ZKdmdn�ZLdodp�ZMd�dtdu�ZNdvdw�ZOdxdy�ZPd�dzd{�ZQd|d}�ZRd�d~d�ZSd�d��ZTd�d�d��ZUd�d��ZVd�d��ZWd�d��ZXd�d�d��ZYd�d�d��ZZd�d�d��Z[d�d�d��Z\d�d��Z]d�Z^dqZ_d�Z`e�r�d�Z^d�Z_d�Z`eZZWe[ZXe\Ze�aed��e�aed��Gd�d��d�ejb���Zcedd�k�re�e�dS)��N)�support)�permutations�product)�	randrange�sample�choice)�Decimal)�Fraction)�*)�ndarrayT)�?�c�b�B�h�H�i�I�l�L�n�N�f�d�Prr�Ql�����q)r�)r�)i�����)i���i�)ri)i�l)rl)l����l)rl�i�)rr
rrrrrrrrrrrrc	Cs�|dkrd}n�|dkrd}n�|dkr*d}n�|dkrFdd	>dd	>f}nfx>d
D]6}yt�|d|>d�PWqLtjk
r�YqLXqLW|d@r�d|>d|>fn
dd|>f}|S)zReturn range of a native type.r
)rrr)rrr)l����lrr i�)
r��@�?� �����r)�struct�pack�error)�fmt�lhZexp�r/�,/usr/local/lib/python3.7/test/test_buffer.py�native_type_rangeYs 

&r1)��@�<�>�=�!r3Z
bBhHiIlLfdZBbc�mz@m�arz@br2r4r5r6r7�1�2�3)r�array�numpy�
memoryview�bytefmtcCsvtt||�}|dkr6t|g�}|dkr6|dkr6d}|dkrFt|�}|dksV|dkrrt�||�}t�||�d}|S)	zWReturn random item for a type specified by a mode and a single
       format character.r
r>��rrrr)r�fmtdict�bytes�boolr*r+�unpack)�mode�char�obj�xr/r/r0�
randrange_fmt�s
rKcCsN|�d�\}}g}x|D]}|�t|||��qWt|�dkrF|dSt|�S)zReturn single random item.�#r r)�split�appendrK�len�tuple)r-rIrG�charsrJr
r/r/r0�gen_item�s

rRcCs@|dkrt||�Sdg|}xt|�D]}t||�||<q&W|S)z,Return a list of random items (or a scalar).r)rR�range)rr-rI�lstrr/r/r0�	gen_items�s

rUcCs�tt|t�}|d}|�d�}tdd�}xTt|�D]H}ttt|��}tt|t�}||t	|rh|nd�7}|||7}q6Wt
|||�}	t||�}
||	|
fS)NrL�ambr�
r )r�cap�MODE�striprrSrPrC�MULT�intrUrR)rrIrGZxfmtr-�nmemb�_rH�
multiplier�items�itemr/r/r0�struct_items�s


rbrcCs�|dkrtt|t�}|dkr0ttt|��}tt|t�}|d|t|rR|nd�}t|||�}t||�}|�	d�||}|||fS)z"Return random format, items, item.NrLr rV)
rrXrYrPrCr[r\rUrRrZ)rrIrGrHr_r-r`rar/r/r0�	randitems�s
rcccs<x6t|tD]&}x t|D]}t||||�VqWqWdS)z1Iterate through supported mode/char combinations.N)rXrYrCrc)rrIrGrHr/r/r0�	iter_mode�srdccs6xt||�D]
}|VqW|dkr&dSt||�VdS)zwYield (format, items, item) for all possible modes and format
       characters plus one random compound format string.rN)rdrb)�nitemsZtestobj�tr/r/r0�iter_format�s

rgcCsd|kpd|kpd|kS)Nr
rrr/)r-r/r/r0�is_byte_format�srhcCs4t|�}|dks$|dko2|ddko2||dtkS)zformat suitable for memoryviewr rrr3)rO�
MEMORYVIEW)r-rJr/r/r0�is_memoryview_format�srjcCsg|]}t|�s|�qSr/)rh)�.0r
r/r/r0�
<listcomp>�srlcCst|t�S)z9Tuple items (representing structs) are regarded as atoms.)�
isinstance�list)rTr/r/r0�atomp�srocCs
t|t�S)N)rmrn)rTr/r/r0�listp�srpcCs:t|�dkrdS|d}x|dd�D]}||9}q&W|S)zProduct of list elements.rr N)rO)rTrJ�vr/r/r0�prod�srrcCs�|dkrdS|dkr^t|dd��|g}xtt|ddd�D]}||||d9<q<WnB|gt|dd��}x*td|�D]}||||d9<q�W|S)zOCalculate strides of a contiguous array. Layout is 'C' or
       'F' (Fortran).rr/�Cr Nr���)rnrS)�ndim�shape�itemsizeZlayout�stridesrr/r/r0�strides_from_shapesrycCs�t|�r|St|�dkr |dSdg|d}|drFt|�|dnd}x>t|d�D].}||}t||||�|dd��||<qXW|S)zmConvert flat item list to the nested list representation of a
       multidimensional C array with shape 's'.rr N)rorOrS�_ca)r`�srT�strider�startr/r/r0rzs&rzcCspt|�r|St|�dkr |dSdg|d}|d}x4t|d�D]$}t||d|�|dd��||<qDW|S)zsConvert flat item list to the nested list representation of a
       multidimensional Fortran array with shape 's'.rNr )rorOrS�_fa)r`r{rTr|rr/r/r0r~s$r~cCs2t|�r(d|kr(t|�t|�kr(td��t||�S)Nrzprod(shape) != len(items))rprrrO�
ValueErrorrz)r`rvr/r/r0�carray,s r�cCs2t|�r(d|kr(t|�t|�kr(td��t||�S)Nrzprod(shape) != len(items))rprrrOrr~)r`rvr/r/r0�farray1s r�cCsdd�|D�}t|�S)z(Generate all possible tuples of indices.cSsg|]}t|��qSr/)rS)rkrqr/r/r0rl8szindices.<locals>.<listcomp>)r)rv�	iterablesr/r/r0�indices6sr�cCs.d}x$t|�D]}|||||7}qW|S)zAConvert multi-dimensional index to the position in the flat list.r)rS)ru�indrx�retrr/r/r0�getindex;sr�c	Cs�|s|St|�}t||dd�}t||ddd�dd�}dgt|�}x>t|�D]2}t|||�}t||ddd�|�}||||<qNW|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]...  r rsNrtr)rOryr�r�)	�srcrvruZsstridesZdstrides�destr��fr�tor/r/r0�	transposeBsr�cCs6|gkr|St|�r|gSt|d�t|dd��S)zflatten listrr N)ro�_flatten)rTr/r/r0r�Qs
r�cCst|�r|St|�S)zflatten list or return scalar)ror�)rTr/r/r0�flattenYsr�cCs4t|�rgSt||d�gt|d|dd��S)zMGet the shape of lst after slicing: slices is a list of slice
       objects.rr N)rorO�slice_shape)rT�slicesr/r/r0r�_sr�cs&t|�r|S�fdd�|�dD�S)z=Multi-dimensional slicing: slices is a list of slice objects.csg|]}t|�dd���qS)r N)�
multislice)rkZsublst)r�r/r0rljszmultislice.<locals>.<listcomp>r)ro)rTr�r/)r�r0r�fsr�csFt|�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|]&\}}t||�dd��dd���qS)r N)�m_assign)rkr�r)�lslices�rslicesr/r0rl}szm_assign.<locals>.<listcomp>r)ro�zip)�llst�rlstr�r�r/)r�r�r0r�lsr�cCsft||�}t||�}t|�t|�kr(dSx8tt|��D](}||||krNdS||dkr6dSq6WdS)z9Compare the structure of llst[lslices] and rlst[rslices].rtr)r�rOrS)r�r�r�r��lshape�rshaperr/r/r0�
cmp_structure�s

r�cCs(t||||�dkrtd��t||||�S)z:Return llst after assigning: llst[lslices] = rlst[rslices]rz+lvalue and rvalue have different structures)r�rr�)r�r�r�r�r/r/r0�multislice_assign�sr�cs�|�rdS|dks |�|kr$dSt�fdd��D��r>dS|dkrZ|dkoX�oX�Sd�krfdSt��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)Nr/)rkrq)rwr/r0�	<genexpr>�sz#verify_structure.<locals>.<genexpr>Tc3s.|]&}�|dkr�|�|dVqdS)rr Nr/)rk�j)rvrxr/r0r��sc3s.|]&}�|dkr�|�|dVqdS)rr Nr/)rkr�)rvrxr/r0r��s)�any�sumrS)�memlenrwrurvrx�offset�imin�imaxr/)rwrvrxr0�verify_structure�sr�cCsx|D]}||}qW|S)Nr/)rTr�rr/r/r0�get_item�s
r�c
Cs>|\}}}}}}|}x$t|�D]}	|||	||	7}qW|S)z-Location of an item in the underlying memory.)rS)
r�rfr�rwrurvrxr��prr/r/r0�memory_index�s
r�cCsT|\}}}}}}d|>}x6t|�D]*}t||�}	d|	>}
||
@rDdS||
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.r TF)r�r�)rfr�rwrurvrxr�Zvisitedr�r�bitr/r/r0�is_overlapping�s
r��r&r/csb�s�t|d�}|dkrd|r,|||dddfStdd�}||}td�dkrP|n|}|||dd|fSd}	td�}
|
dkr�|r�d}	n|
dkr�d}	dg|�x,t|�D]}t|	|d��|<q�Wnt��}d	}td�}
|
dkr�|
d@r�d
nd}
dg|�|t||d��|d<|
�s4�|ddk�r4|�|d<x~t|ddd�D]j}|�|d�rf�|dnd9}|
�r�|t||d��|<n"d
td�|td|d��|<�qFWd}}d�k�r�t��fdd�t|�D��}t��fdd�t|�D��}||}|�r(||}||d|}n*|||}td�dk�rN||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.
    r rr/�r�d�_�Zr�TFrt)r rtc3s.|]&}�|dkr�|�|dVqdS)rr Nr/)rkr�)rvrxr/r0r�sz!rand_structure.<locals>.<genexpr>c3s.|]&}�|dkr�|�|dVqdS)rr Nr/)rkr�)rvrxr/r0r�s)rrSrOr�)rwZvalid�maxdim�maxshapervrurer�r��minshaperrZ	maxstrideZzero_strider�r�r/)rvrxr0�rand_structure�sT


"(

r�c
Csp||}t|d�}|r$|||nd}td|d�}|||}t|||�}t||�\}}}}	|	|krlt�|S)z=Create a random slice of len slicelen that fits into listlen.r )r�slice�
slice_indices�RuntimeError)
�slicelenZlistlenZmaxstartr}Zmaxstep�step�stopr{r^Zcontrolr/r/r0�randslice_from_slicelen!sr�cCstdg|}dg|}xNt|�D]B}||}|dkr@td|d�nd}t||�||<t||�||<qWt|�t|�fS)zxCreate two sets of slices for an array x with shape 'shape'
       such that shapeof(x[lslices]) == shapeof(x[rslices]).rr )rSrr�rP)rurvr�r�rrr�r/r/r0�randslice_from_shape.s

r�cCs�td|d�}d}td�}|dkr(d}n|dkr4d}td�dkrDdnd	}dg|}dg|}dg|}dg|}	�x(t|�D�]}t||d�}
t||d�}||
kr�|
|}}
|�rt|
|
d�}t|
|
d�}
d
td�td|
d�}t||
|�}t||
�\}}}}n&|
dk�r(td|
d�nd}t||
�}t||�}td�dk�rt||
||<||<|||	|<||<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'.r rr�r�rr��PTF)r rt)rrSr�r�r�rP)r�r�rur�rZ
all_randomr�r�r�r�ZsmallZbigr}r�r�Zs_smallr^r�Zs_bigr/r/r0�rand_aligned_slices:s@






r�cCs$|\}}}}}}t||d|d�S)zMReturn a list of random items for structure 't' with format
       'fmtchar'.rLr>)rU)r-rfr�rwr^r/r/r0�randitems_from_structureesr�c
Cs(|\}}}}}}	t|||||	t|Bd�S)z:Return ndarray from the tuple returned by rand_structure())rvrx�formatr��flags)r�ND_WRITABLE)
r`r-rfr�r�rwrurvrxr�r/r/r0�ndarray_from_structureks
r�cCsT|\}}}}}}t|�}	x(t|�D]\}
}t�||	|
||�q"Wt|	||||d�S)z>Return numpy_array from the tuple returned by rand_structure())�bufferrvrx�dtyper�)�	bytearray�	enumerater*�	pack_into�numpy_array)r`r-rfr�rwrurvrxr��bufr�rqr/r/r0�numpy_array_from_structureqsr�cs�|j}|r$t|��|krld|fSnH|gkrH|jdks>�|krld|fSn$t|��\}}|g}|dkrld|fS|�����fdd�tdt����D�}g}x6|D].}	t�||	�d}
|
|
kr�d|fS|�	|
�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).Nrcsg|]}�||���qSr/r/)rkr)rw�memr/r0rl�szcast_items.<locals>.<listcomp>�nan)
�nbytesrrru�divmod�tobytesrSrOr*rFrN)Zexporterr-rwrvZbytelenrr�Z	byteitemsr`rqrar/)rwr�r0�
cast_itemss(

 
r�c#s�xtd�D]}|gVq
Wtdd�}td�dkr4dnd��fdd	�t|�D�Vtdd�}td�dkrjdnd��fd
d	�t|�D�VdS)z Generate shapes to test casting.r$��r�r�r rcsg|]}t�d��qS)r�)r)rkr^)r�r/r0rl�sz!gencastshapes.<locals>.<listcomp>csg|]}t�d��qS)r�)r)rkr^)r�r/r0rl�sN)rSr)rrur/)r�r0�
gencastshapes�s

r�cCs0tt||d�t||d�t||d��S)z4Generate all possible slices for a single dimension.r )rrS)rr/r/r0�	genslices�sr�cs�fdd�t|�D�}t|�S)z/Generate all possible slice tuples for 'shape'.csg|]}t�|��qSr/)r�)rkr)rvr/r0rl�sz"genslices_ndim.<locals>.<listcomp>)rSr)rurvr�r/)rvr0�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�r/r/r0�rslice�sr�ccs"xtd�D]}t||�Vq
WdS)z.Generate random slices for a single dimension.r�N)rSr�)rr�r^r/r/r0r��sr�c#sxx,t|�D] }t�fdd�t|�D��Vq
Wx,t|�D] }t�fdd�t|�D��Vq8Wtdd�t|�D��VdS)z)Generate random slice tuples for 'shape'.c3s|]}t�|�VqdS)N)r�)rkr)rvr/r0r��szrslices_ndim.<locals>.<genexpr>c3s|]}t�|dd�VqdS)T)r�N)r�)rkr)rvr/r0r��scss|]}tddd�VqdS)rr N)r�)rkr^r/r/r0r��sN)rSrP)rurvZ
iterationsr^r/)rvr0�rslices_ndim�s
  r�ccs0t|�}|dkrt|�n|}tt||��VdS)N)rPrOr)�iterabler�Zpoolr/r/r0�rpermutation�sr�c
Cs�y|��}Wn ttfk
r,|��}YnXt|t�rF|j}|j}nd}d}td||j	|j
|j||j|j
|f�tj��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�rmrr�r��printrvrx�
suboffsetsr�rw�sys�stdout�flush)�ndrJr�r�r/r/r0�
ndarray_print�s
r�r�rW�r�z%struct module required for this test.z%ndarray object required for this testc
@s�eZdZdd�Zddhddhdhdddddf
dd�Zd�dd	�Zd
d�Zdd
�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Zd>d?�Z d@dA�Z!dBdC�Z"dDdE�Z#dFdG�Z$dHdI�Z%dJdK�Z&dLdM�Z'dNdO�Z(dPdQ�Z)e*j+Z+dRdS�Z,dTdU�Z-dVdW�Z.dXdY�Z/dZd[�Z0d\d]�Z1d^d_�Z2d`da�Z3dbdc�Z4e5j6ddde��Z7dfdg�Z8dhdi�Z9djdk�Z:dldm�Z;dndo�Z<dpdq�Z=drds�Z>dtdu�Z?dvdw�Z@dxdy�ZAdzd{�ZBd|d}�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�d��ZPd�d��ZQd�d��ZRd�S)��TestBufferProtocolcCst�|_dS)N)Zget_sizeof_void_p�
sizeof_void_p)�selfr/r/r0�setUp�szTestBufferProtocol.setUprtr Fc%	s8|rt|��}n�s t|	�}n�}d}
|jr�|�|d�d}xFtd|�D]8}||dkr\P||dkrJ|||||d7}qJW|gdd�t|d�D�}
|j}|ddkr�|}|gt|dd��}|�|j|�|�	|j
|�|�	|j��|�	|j��|�	|j
|�|�	|j|�|�	|jt|��|
�rB|
�sT|�	|jt|��|�	|jt|
��t|t��s|t���r���r�|��n|��}|�	||	���s�dS|�st�}d}x�t|�D]|}yLt||�}t|	|�}t|t��r�tj�f|��}nt��|�}|�|�Wntk
�r.d}PYnX|�	||��q�W|�s|�	|��|�t|�}d� dd	�|D��}|�	|�!�|���r��nd
}t"|	�}�xhdD�]^}|}|dk�r�t#|d
��r�t#|d��rt$||�}t|||t%d�}n0t#|d
��rt#|d��r|dk�rt|||d�}t&|t'|��|�	���|�|�(t)�|��|dk�rT�q�t|�}|�rfdnt*} t+|dt,��|�	t��|�����fdd�t|�D�}!t|!d�dk�r�dd�|!D�}!t|!|| �d�}"|�	t|"�t|��t+|dt,��|�	t��|�����fdd�t|�D�}!t|!d�dk�rBdd�|!D�}!t|!|| t%B�d�}"|�	t|"�t|��t+|d
t,��|�	t��|�����fdd�t|�D�}!t|!d�dk�r�dd�|!D�}!t#|d��r�t%nd}#t|!||#| B�d�}"|�	t|"�t|���q�Wt���r4yt|�}Wntk
�r2dSXt|t��rF|jn|}$|�|j|$�|�	|j
|�|�	|j��|�	|j��|�	|j
|�|�	|j|�|�	|jt|��|
�r�|
�s�|�	|jt|��|�	|jt|
��|dk�r�dnt|	�}|�	t|�|���r|��n|��}|�	||	�|�	||�dS)Nr/rr cSsg|]}d�qS)rtr/)rkrqr/r/r0rlsz-TestBufferProtocol.verify.<locals>.<listcomp>Tr2css|]}d|VqdS)z%02xNr/)rkr
r/r/r0r�Psz,TestBufferProtocol.verify.<locals>.<genexpr>r)rs�F�Ar�r�rs)rvr�r�)rvr�csg|]}t���|���qSr/)r*�unpack_from)rkr)�contigr-rwr/r0rl{scSsg|]}|d�qS)rr/)rkrqr/r/r0rl~s)rvr�r�csg|]}t���|���qSr/)r*r�)rkr)r�r-rwr/r0rl�scSsg|]}|d�qS)rr/)rkrqr/r/r0rl�scsg|]}t���|���qSr/)r*r�)rkr)r�r-rwr/r0rl�scSsg|]}|d�qS)rr/)rkrqr/r/r0rl�s)-rrrOr�Z
assertGreaterrSr�rn�assertIsrI�assertEqualr�rwr��readonlyrurvrPrxrmrrjr�r�r�r��get_pointerr�r*r+�extend�BufferErrorr?�join�hexr��
is_contiguousr��
ND_FORTRAN�get_contiguous�
PyBUF_READ�
assertTrue�
cmp_contigr��py_buffer_to_contiguous�
PyBUF_FULL_RO)%r��resultrIrwr-r�rurvrxrT�sliced�castZexpected_lenr�Z
suboffset0rZstride0ZreprZbuf_errr�Zitem1Zitem2rJr8rZffZ	flattened�order�expectedZtransr]�roZinitlst�yr�exr/)r�r-rwr0�verify�s�







zTestBufferProtocol.verifycCstdd�}dd�}|jr ||t�s�||t�r0|jr�||t�r@|jr�||t�rP|jr�||t�s`|j	s�||t
�sp|jr�||t�s�||t�r�|j
tt||d�dSt|t�s�t|j�r�|��}nt|td�}|��}||t�r�dn|j}	|j}
|j}|j}||t��s
d}
|��}||t��sd}||t��r,|jnd	}
||t
��rB|jnd	}t||d�}|j||||
|	||
|||d
�
dS)NcSs|jdkp|jdkS)Nr2r)r�)rr/r/r0�
simple_fmt�sz4TestBufferProtocol.verify_getbuf.<locals>.simple_fmtcSs||@|kS)Nr/)�req�flagr/r/r0�match�sz/TestBufferProtocol.verify_getbuf.<locals>.match)�getbufrr2r r/)	rIrwr-r�rurvrxrTr�)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�rrmrjr�r�r�rwrur�rvrxr)r�Zorig_exrrr�rr
rTr�rr-rwrurvrxr/r/r0�
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}ggdfdddggdfdggdfdgdgdfd
gdgdfd
gdgdfddggdfddgddgdfddgddgdfddgddgdff
}dttttBtttBf}dttttBf}�x�|D�]�\}}t�|���x�|D�]�\}}	}
�fdd�|	D�}	|
�9}
�x\|D�]R}|	�r�|t@�r��q�|�s�|t@�r��q�|�r�|n|d}t|||||	|
d�}
|�r�|
ddd�nd}t|
�}|�r
t|�}|
jdk�s.|
jdk�r:|�r:|	�r:|�||
�|�rd|jdk�rd|�rd|	�rd|�||�xt|D]l}xd|D]\}|�|
|
||B�|�|
|||B�|�rt|j||||Bdd�|j||||Bdd��qtW�qjW�q�W�qZW�q>Wddddd	d
ddd
dddg}t|dgtd�}|�tt|�td
gdg�}t|td�}|jtt|td�|jtt|td�|jtt|td�|jtt|td�|jtt|td�|jtt|td�t|td�}x�dddgdddgfD]�}x�dtfD]t}t|||tBd�}|�t |d��|�t |d��x:|D]2}t||d�}|�t |d��|�t |d���qW�q�W�q�WdS) NcSsg|]}|drdnd�qS)rTFr/)rkrJr/r/r0rl�sz:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>�rr rr�r�r�r�r)r(�	rW�rrcSs$g|]}|drd|nd|�qS)rli�r/)rkrJr/r/r0rl�srrrt������csg|]}|��qSr/r/)rkrq)rwr/r0rls)r�r�rvrxr�T)r�)rvr�)rr�rs)!rrr�PyBUF_SIMPLEr
rr�
PyBUF_FULLr��
PyBUF_RECORDS�PyBUF_RECORDS_RO�
PyBUF_STRIDED�PyBUF_STRIDED_RO�PyBUF_CONTIG�PyBUF_CONTIG_ROrSr�r��ND_PILrrr*�calcsizerr?rur�r�ND_GETBUF_FAILrr�r�r�)r��requestsZ	items_fmtZ	structureZndflagsZ
real_flagsr`r-rvrxr�r�Z_items�ex1�ex2�m1�m2r�bitsr�baser�rr/)rwr0�test_ndarray_getbuf�s�


$

&
z&TestBufferProtocol.test_ndarray_getbufc
Cstdgdg�}tdgdgtd�}�x�t|j|jfD�]z}|�t|dddh�|�t|dddg�|�t|dddg�|jt|dddgdhd�|jt|dddgdgdhd	�|jt|dddgdggd
�|jt|dgdgid�|jt|dgdgid�|jt|dgdgid
�|jt|dgdgdgtd�|jt|dggtd�|jt|gdgd�|jt|dgdgdd�|jtj	|dgdgdd�|jt|ddgdgdd�|jt|dddgdgdd�t
d}|jt|dg|dg|d�|jt|dgdgd�|jt|dddgdgd�|jt|dgddgd�|jt|dddddgddgdd
�|jt|dddgdgdgd	�|jt|dgdgddgd	�|jt|ddgddgdgd	�|jt|ddddgdgdgdd�|jt|dddgdgdd
�|jt|dddgdgddd�|jt|dddgdgdd�|jtj	|dgdgdd�ddddddd d!dd"g
}|jt||ddgd#d$gdd%�|jt|t�d&d'�|jt|dgdgt
d
�|�t|dg�q2W|jttd(td)�tdgdgtd�}|jt|jdgdgtd�td(�}|�t|jdgdg�|�t|j�tdgdg�}|�dgdg�t|�}|�t|jdgdg�|�t|j�|��|��|�t|j�~|�ttidddg�|�ttd(i�tttd*��dgd*d�}|�tt|dg�tttd+��ddgd�}|�tt|dddg�|�tt|ddg�|�tt|d#dg�|�tt|d,dg�tdddgdgdd�}t|td)�}|�t|j�tdddgdgdd�}t|�}	t|	�}|�t|j�td-gdgd.d�}|�t|j�t
}ttt|��dg|d�}|�t|j�tdgdgd�}|�ttddddd�|�tt|d/d0�|�tt|d,d0�|�tt|td1�|�tt|td2�|�tt|td3�|�tt|d4d5�tdgdgd�}|�ttddddd�|�tti|�|�tt|i�tdgdgd�}|�ttddddd�|�ttid5�|�tt|d6�dS)7Nrr )r�rr�r<)r�r�)rv)rvrx)rvr�)rvr�)rvr�)rvr)rvrxr�ZXXXri�r)rr�ZQLrtrr�r�r:)rvrxr�)rvr�r�r2)r rr�z@#$r�r)r(rW���r)rvrxr�r)r�s123)rr�rl)�r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r Z�LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL�xyzrsi�u �Z�r���)r�ND_VAREXPORT�pushrr�r�r&rr*r,ZND_MAX_NDIM�
OverflowErrorr�rr�r�popr?�releaser�rnrSrr��memoryview_from_bufferr�r��UnicodeEncodeErrorr�r�)
r�r�Zndmr
rr`r8rr*r+r/r/r0�test_ndarray_exceptionsNs�"z*TestBufferProtocol.test_ndarray_exceptionscCs�x�ttd��D]�}dgd}tdddgdgtd�}t|�|d<x4tdd�D]&}|jdddgdgd�t|�||<qJWx td�D]}|||��q~W|�t|j	�~qWdS)Nr�rr rr�)rvr�)rv)
rrSrr6r?r7r:rr�r9)r�Zpermr8r�rr/r/r0�test_ndarray_linked_list�s
z+TestBufferProtocol.test_ndarray_linked_listcCsPxJtd�D]>\}}}t�|�}t|d|d�}|j|d||dddd|d�	q
WdS)Nrr/)rvr�r )rIrwr-r�rurvrxrT)rgr*r'rr)r�r-Zscalarr^rwr�r/r/r0�test_ndarray_format_scalar�s
z-TestBufferProtocol.test_ndarray_format_scalarc
Csttdd�}xdt|�D]X\}}}t�|�}xBdtfD]6}t||g||d�}|j|d||dd|f|f|d�	q2WqWdS)Nr rWr)rvr�r�)rIrwr-r�rurvrxrT)rrgr*r'r&rr)r�rer-r`r^rwr�r�r/r/r0�test_ndarray_format_shapes


z,TestBufferProtocol.test_ndarray_format_shapecCs�tdd�}x�t|�D]�\}}}t�|�}x�tdd�D]�}|dkrBq4t|dd|��g}||g}|dkrt||dnd}	xJdtfD]>}
t|||||	|
d�}|j|d||dd|||dd|�d�	q�Wq4WqWdS)Nr ����r�r)rvrxr�r�r�)rIrwr-r�rurvrxrT)	rrgr*r'rSrOr&rr)r�rer-r`r^rwr�rvrxr�r�r�r/r/r0�test_ndarray_format_stridess 


z.TestBufferProtocol.test_ndarray_format_stridescCsTddddddddd	d
ddg}t|d
dd�}t|ttBd�}|�|��t|d
��dS)Nr rr�r�r�r�r)r(rrWrr)r�r�)r r�)rvrx)r)rrrr�r�r�)r�r`rr�r/r/r0�test_ndarray_fortran'sz'TestBufferProtocol.test_ndarray_fortrancCsp�xhtd�D�]Z}dd�t|�D�}t|�}�x8t|�D�]*}t|�\}}}t�|�}�x
dtfD]�}	|dkrx|	tkrxqbt||||	d�}
t|||d�}t	||�}|j
|
d||d||||d�	t|��rt|||d	�}
t|
tt
Bd
�}
|�|
jdk�|
��}|j
|d||d||||d�	t||||	tBd�}
t|||d�}t||�}|j
|
d||d||||d�	qbWq8WqWdS)
Nr�cSsg|]}tdd��qS)rrW)r)rkr^r/r/r0rl/sz<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>r)rvr�r�rsr )rIrwr-r�rurvrxrT)rvr�)rr/r�)rSrrrrcr*r'r&rryr�rrjr%rr�rxr;r�r�)r�ru�shape_trervr-r`r^rwr�r�rxrTr�mvr/r/r0�test_ndarray_multidim-sB



z(TestBufferProtocol.test_ndarray_multidimcCs�tdgdgd�}|�t|jdd�t|�}|�||�|�t|jdd�tdgdgtd�}|�t|jd�t|�}|�||�|�t|jd�tdgdgtd�}|�t|j	d�|�t|jdd�t|�}|�||�|�t
|j	d�|�t
|jdd�ddddd	d
ddg}t|t|�gdtd
�}|�tj
|jdd�|�t|jdd�t|�}|�||�|�t|jdd�|�t|jdd�dddg}t|t|�gdtd
�}|�t|jdd�|�tj
|jdd�dS)Nr )rvr()rvr�lrr�r�r�r�r)r)rvr�r�i,)r���)r r)r�r�)r�r�ZLQ)s1rH)rrr��__setitem__r?r�r��__delitem__r8�__getitem__�
IndexErrorrOr*r,r)r�r�rFr`r/r/r0�test_ndarray_index_invalidZs:
z-TestBufferProtocol.test_ndarray_index_invalidcCsJtddtd�}t|�}|�||�|d}|�|d�|d}|�|��|���|d}|�|d�|d}|�|��|���|�t|jd�|�t|jd�|�t|jdd�|�t|jdd�|�|��d�|�|��d�d|d<|�|��d�d|d<|�|��d�d|d<|�|��d�d|d<|�|��d�dS)	Nr r/)rvr�.rr(rr�)	rr�r?r�r�rr�rKrI)r�r�rFrJr/r/r0�test_ndarray_index_scalar�s2z,TestBufferProtocol.test_ndarray_index_scalarcCsRtttd��ddgtd�}t|td�}|�t|jd�|�t|jtddd��dS)	Nr(rr�)rvr�)rr r�r�)	rrnrSr�r$rr�rKr�)r�rr�r/r/r0�test_ndarray_index_null_strides�sz2TestBufferProtocol.test_ndarray_index_null_stridescCs,x�td�D]�\}}}t|dg|d�}x&tdd�D]}|�||||�q0W|�t|jd�|�t|jd�t|�r
t|�}|�||�x&tdd�D]}|�||||�q�W|�t|jd�|�t|jd�q
Wx�td�D]�\}}}t|dgt	|d�}t|t
tBd�}x(tdd�D]}|�||||��qWt|�r�|��}|�
|�|�t�x(tdd�D]}|�||||��qbWq�Wddd	d
dg}t|dgd�}t|td�}x(tdd�D]}|�||||��q�Wddd	d
dg}t|dgd�}t|td�}x(tdd�D]}|�||||��q
WdS)Nr�)rvr�rBi����)rvr�r�)rr rr�r�)rv)rgrrSr�rrLrKrjr?r�r$rr;r��__eq__�NotImplementedr%r)r�r-r`r^r�rrFrr/r/r0�!test_ndarray_index_getitem_single�sBz4TestBufferProtocol.test_ndarray_index_getitem_singlecCs��xtd�D]�\}}}t|dg|td�}x td�D]}|||<|||<q2W|�|��|�|�t|jd|�|�t|jd|�t	|�s�qt|dg|td�}t
|�}|�||�x td�D]}|||<|||<q�W|�|��|�|�t|jd|�|�t|jd|�qWx�td�D]�\}}}t|dg|td�}x4tdd�D]&}||d||<||d||<�q:W|�|��|�t	|��s��qt|dg|td�}t
|�}|�||�x4tdd�D]&}||d||<||d||<�q�W|�|��|��qWdS)Nr�)rvr�r�i����rBr�r )rgrr�rSr�r�rrLrIrjr?)r�r-r`Zsingle_itemr�rrFr/r/r0�!test_ndarray_index_setitem_single�sD
z4TestBufferProtocol.test_ndarray_index_setitem_singlec

Cs�d}t|�}�x�t|�D�]�}t|�\}}}�x�dtfD�]�}t||||d�}t||�}	x�t|d|d�D]�}
|�|	|
||
���x~t|d|d�D]f}|�|	|
|||
|���x@t|d|d�D](}|�|	|
||||
||�q�Wq�WqnWt||||t	Bd�}t
||�}	x�t|d|d�D]�}
|�|	|
||
���x�t|d|d�D]h}|�|	|
|||
|���x@t|d|d�D]*}|�|	|
||||
||��q�W�qtW�qBWq8WqWdS)N)rr�r�r)rvr�r�r r)rrrrcr&rr�rSr�r�r�r�)
r�rErervr-r`r^r�r�rTrr��kr/r/r0�#test_ndarray_index_getitem_multidims,
 0
 z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCs�tddd�}|�ttdt��t|�}|�||�|�ttdt��x�td�D]�\}}}t|dg|d�}x2t|�D]&\}}|�|||�|�	||k�qrWt
|�rNt|�}x2t|�D]&\}}|�|||�|�	||k�q�WqNWdS)Nr r/)rvz1 in ndz1 in mvr�)rvr�)rrr��eval�localsr?r�rgr�r�rj)r�r�rFr-r`r^rrqr/r/r0�test_ndarray_sequence!sz(TestBufferProtocol.test_ndarray_sequencec	Cs�ddddddddg}t|dgtd	�}t|�}|�t|jtd
dd�|�|�t|jtd
dd�|�t|dgtd	�}t|dgtd	�}t|td�}|�t|jtd
dd�|�t|dgdtd
�}t|�}|�t	|j
td
dd
��|�t	|j
td
dd
��t|ddgdtd
�}t|�}|�t	|j
td
dd�td
dd
�f�|�t	|j
td
dd
�td
dd�f�|�t|j
d�|�t|j
dtd
dd�f�|�t|j
td
dd�if�|�t|j
td
dd�td
dd
�f�|�t|j
d�t|dgdtd
�}t|dgdd�}t|�}t|�}|�t	|jtd
dd�|dd��|�|�
�|�|�t	|jtd
dd�|dd��|�|�
�|�t|dgdtd
�}t|dgdd�}	t|�}t|�}|�t	|jtd
dd�|dd��|�|�
�|�|�t	|jtd
dd�|dd��|�|�
�|�t|ddgdtd
�}t|dgdd�}t|�}t|�}|�t	|jtd
dd�|dd��|�|�
�ddddgddddgg�|�t|jtd
dd�|dd��t|dgdtd
�}t|dgdd�}t|�}t|�}|�t	|jtd
dd�|dd��|�|�
�|�|�t	|jtd
dd�|dd��|�|�
�|�|�tttd
dd�i�|�ttdd�|�t	ttd
dd
�d�t|dgdtd
�}
|�t|
j�t|dgdd�}t|td�}
|�t|
j�dS)Nr rr�r�r�r�r)r()rvr�r)rr)rvr�r�z@%$rr)rvr�z###########)rr�r?rr�rIr�rr�rrKr�r�r�r�r&�add_suboffsetsr)r�r`�xl�mlr�xrr�rF�mr�yrrJr/r/r0�test_ndarray_slice_invalid4s�


"""""$""z-TestBufferProtocol.test_ndarray_slice_invalidcCsddddddddd	d
ddg}t|dgd
td�}t|dgd
d�}|d	d	�|dd�<|�|��|�t|�}t|�}|�||�|�||�|d	d	�|dd�<|�|��|�t|ddgd
td�}t|ddgd
d�}|dd�dd�f|dd�dd�f<|�|��t|ddg��dS)Nr rr�r�r�r�r)r(rrWrrr)rvr�r�)rvr�)rr�r�r�r?r�)r�r`rJrr[r]r/r/r0�test_ndarray_slice_zero_shape�s$z0TestBufferProtocol.test_ndarray_slice_zero_shapec
Cs"d}t|�}t|�}�xt|�D]�}t|�\}}}t�|�}x�dtfD]�}	t||||	d�}
t||�}x�t	||�D]�}d}
yt
||�}Wn(tk
r�}z
|j}
Wdd}~XYnXd}y|
|}Wn(tk
r�}z
|j}Wdd}~XYnX|s�|
�r|�
||
�qp|�|��|�qpWqFWq WdS)N)rr�r�r)rvr�r�)rOrrrrcr*r'r&rr�r�r��	Exception�	__class__r�r�r�)r�rErurervr-r`r^rwr�r�rTr��listerrr��e�nderr�ndslicedr/r/r0�test_ndarray_slice_multidim�s.


z.TestBufferProtocol.test_ndarray_slice_multidimcCs:d}t|�}t|�}�xt|�D�]}t|�\}}}t�|�}t|||d�}	|	��t|||d�}
|
��t|
�}t	||�}x�t
||�D]�}
d}yt||
�}Wn(tk
r�}z
|j
}Wdd}~XYnXd}y|	|
}Wn*tk
�r}z
|j
}Wdd}~XYnX|�s|�r|�||�q�|�|��|�q�Wq WdS)N)rr�r�r)rvr�)rOrrrrcr*r'rrYr?r�r�r�rarbr�r�r�)r�rErurervr-r`r^rwr�rrFrTr�rcr�rdrerfr/r/r0�'test_ndarray_slice_redundant_suboffsets�s4

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetscCs$�xtd�D�]\}}}�xtd�D�]�}�x�td�D�]�}�x�dtfD�]�}|tB}t|dg||d�}t|dg||d�}	t|	�}
d}d}|dd�}
y0|
|}|
|}|
||
|<t|�t|�k}Wn(tk
r�}z
|j}Wdd}~XYnXd}y||||<Wn*tk
�r.}z
|j}Wdd}~XYnX|�rD|�	|t
�n|�|��|
�|�	||�t
|��slqHd}y|
||
|<Wn*tk
�r�}z
|j}Wdd}~XYnX|�r�|�	|t
�qH|�|
��|
�|�|
|�|�	||�|j|
|	|j|d|j|j|j|��d�	qHWq6Wq$WqWdS)Nr�r)rvr�r�)rIrwr-r�rurvrxrT)rgr�r&r�rr?rOrarbr�rr�r�rjrrwrurvrx)r�r-r`r^�lslicer�r�rr�rrFZlsterrZdiff_structurerTZlvalZrvalrdreZmverrr/r/r0� test_ndarray_slice_assign_single�sV
z3TestBufferProtocol.test_ndarray_slice_assign_singlec
Cs4d}t|�}t|�}�xt|�D�]
}t|�\}}}x�dtfD]�}x�tt�D]�}t||�\}	}
t||||t	Bd�}t
||�}d}
yt|||	|
�}Wn(tk
r�}z
|j
}
Wdd}~XYnXd}y||
||	<Wn(tk
r�}z
|j
}Wdd}~XYnX|�s|
�r|�||
�qL|�|��|�qLWq>Wq WdS)N)rr�r�r)rvr�r�)rOrrrrcr&rS�
ITERATIONSr�rr�r�r�rarbr�r�r�)r�rErurervr-r`r^r�r�r�r�rTrcr�rdrer/r/r0�"test_ndarray_slice_assign_multidims0
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc
CsF�x>tt�D�]0}�x(tdD�]}t�|�}t|dttd�}|�t	|��t
||�}t|||�}|��}t
|�}t|�r�|��}	|�|	|�|ddkr�t|||td�}
|
��}|�||�t
|
�}t|�r�|�||
�|��}	|�|	|�tr|d}d|k�rqt|||�}
|j|d|
j|d|
j|
j|
j|
��d�	qWqWdS)	Nr3T)r�r�rr)r�r�)rIrwr-r�rurvrxrT)rSrkrCr*r'r��MAXDIM�MAXSHAPEr�r�r�r�r�r?rjr�r&r�r�rrwrurvrx)r�r^r-rwrfr`rJZxlistrFZmvlistrZylistrv�zr/r/r0�test_ndarray_random;s>


z&TestBufferProtocol.test_ndarray_randomcCs�x�tt�D]�}x�tdD]�}t�|�}t|dttd�}|�t	|��t
||�}d}yt|||�}Wn(tk
r�}z
|j
}Wdd}~XYnX|�|�trd}	yt|||�}
Wqtk
r�}z
|j
}	Wdd}~XYqXqWq
WdS)Nr3F)r�r�)rSrkrCr*r'r�rmrn�assertFalser�r�r�rarbr�r�r�)r�r^r-rwrfr`rerJrdZ	numpy_errrr/r/r0�test_ndarray_random_invalidds(


z.TestBufferProtocol.test_ndarray_random_invalidc
Cs>�x6tt�D�](}�x tdD�]}t�|�}tttd�\}}}}t|d|d�}t|d|d�}	|�	t
|��|�	t
|	��t||�}
t||	�}t|
||�}t|||	�}
|
|||<|�
�}|
�
�}t|�}t|
�}|�|�
�|�|�|�
�|�|ddk�r�|	ddk�r�t|
||td�}t|||	td�}||||<|�
�}|�
�}|�||�|�||�t|�}t|�}|�|�
�|�|�|�
�|�trd|ksd|k�r�qt|
||�}t|||	�}||||<t|��s
t|	��s
|j|d|j|d|j|j|j|�
�d�	|j|
d|j|d|j|j|j|�
�d�	qWqWdS)	Nr3)r�r�T)rvrr)r�)rIrwr-r�rurvrxrT)rSrkrCr*r'r�rmrnr�r�r�r�r�r�r?r�r&r�r�r�rrwrurvrx)r�r^r-rwr�r�r�r��tl�trZlitemsZritemsrZr\ZxllistZxrlistr[r]Zylr^ZyllistZyrlistZzlZzrr/r/r0� test_ndarray_random_slice_assign�s\


z3TestBufferProtocol.test_ndarray_random_slice_assigncCs�ddddddddd	d
ddg}t|ddgtd
�}t|�}|�|jt@�|�|j|�|�|jd�|�|j	�|�|j
�|�|j�dS)Nr rr�r�r�r�r)r(rrWrr)rvr�)rrt)rr&r�r�r�rIr�r�rqrrr)r�r`r�rr/r/r0�test_ndarray_re_export�sz)TestBufferProtocol.test_ndarray_re_exportc	Cs�x�dtfD]�}tdddgdg|d�}t|�}|�||�|�|��g�|�|��g�tdddgdddg|d�}|�|��g�tdddgdddg|d�}|�|��gggg�tdddgdddg|d�}|�|��ggggggggggggg�q
WdS)Nrr rr�)rvr�)r&rr?r�r�)r�r�r�rFr/r/r0�test_ndarray_zero_shape�s
z*TestBufferProtocol.test_ndarray_zero_shapec	CstxndtfD]b}tdgdgdg|d�}t|�}|�||�|�|��dddddg�|�|��dddddg�q
WdS)Nrr r�)rvrxr�)r&rr?r�r�)r�r�r�rFr/r/r0�test_ndarray_zero_strides�sz,TestBufferProtocol.test_ndarray_zero_stridescCs@tttd��dgdd�}|�|jd�|�|��dddg�dS)N�r�r))rvr�r(r)rrnrSr�r�r�)r�r�r/r/r0�test_ndarray_offset�sz&TestBufferProtocol.test_ndarray_offsetcCsBx<dtfD]0}tttd��dg|d�}|��}|�||�q
WdS)Nrr�)rvr�)r&rrnrSr;r�)r�r�r�r8r/r/r0�#test_ndarray_memoryview_from_buffer�sz6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsXxRdtfD]F}tttd��dg|d�}x(td�D]}|�||t||g��q0Wq
WdS)Nrr�)rvr�)r&rrnrSr�r�)r�r�r�rr/r/r0�test_ndarray_get_pointer�sz+TestBufferProtocol.test_ndarray_get_pointercCs^tttd��dddgd�}t|ttBd�}|�|��|���t|�}|�|��|���dS)Nryrr�)rv)r)rrnrSrrr�r�r?)r�rr�r8r/r/r0� test_ndarray_tolist_null_strides�s
z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs�|�tdd��tttd��ddgd�}tttd��ddgd�}|�t||��tdgdgdd	�}|�t|d
��|�td
|��dS)Ns123s456rr�r�)rvr r)rvr�rB)rqr�rrnrSr�)r�rJrr/r/r0�test_ndarray_cmp_contigsz*TestBufferProtocol.test_ndarray_cmp_contigcCs�t�ddddg�}t|�}|�tt|�tttd���}tttd��dgd�}|�t|�t|��tttd��ddgd�}|�t|�t|��tttd��dddgd�}|�t|�t|��tt	ttd��ddgd��}tttd��ddgt
d�}|�t|�t|��tt	ttd��dddgd��}tttd��dddgt
d�}|�t|�t|��tttd���}tttd��dddgtd�}|�t|�t|��tttd��dddgdd	�}|�t|�t|����dS)
Nrr rr�r)rvr�)rvr�)rvr�)
r=rrr�hashrDrnrSr�r�r�r&r�)r�r9r�rr/r/r0�test_ndarray_hash
s*z$TestBufferProtocol.test_ndarray_hashc
Cs�tttttttttt	t
tf}|�t
tidt�tdddtd�}x6dD].}x(|D] }t|||�}|�||���qNWqDWtdgdgdtd�}x2dD]*}x$|D]}t|||�}|�|d	�q�Wq�Wtttd
��dddgdtd�}x2dD]*}x$|D]}t|||�}|�|d	�q�Wq�Wx�dtgD]�}tdgdgd
|tBd�}|��}x6dD].}x&|D]}t|||�}|�||��qNW�qDWtdddgdgd|tBd�}|��}x6dD].}x&|D]}t|||�}|�||��q�W�q�W�qWtdddgdgdgtd�}|��}x:dD]2}x*ttgD]}t|||�}|�||��qW�qW|ddd�}|��}xVdD]N}xF|D]>}yt|||�}Wntk
�r��w\YnX|�||��q\W�qRWttd��}�x�dtgD�]�}t|ddg|tBd�}t�rtt|�ddgd|dk�r�dndd�}	|tk�r4tt|ddg�ddgtd�}
|
��}n|��}x�|D]�}yt|d|�}Wntk
�rp�wBYnX|�||�tdd�|D�ddgtd�}|�t|�t|��t�rB|�||	jdd���qBW|dk�r�tt|ddg�ddgtd�}
nt|ddgtd�}
|
��}x�tttttgD]�}yt|d|�}Wntk
�rN�w YnX|�||�tdd�|D�ddgttBd�}|�t|�t|��t�r |�||	jdd���q W|tk�r�t|ddgtd�}
|
��}n|��}x�tttttgD]�}yt|d|�}Wntk
�r�w�YnX|�||�tdd�|D�ddg|tBd�}|�t|�t|��t�r�|�||	jdd���q�W�q�Wtttd��ddgttBd�}t|dt�}|�||���tdd�|D�ddgtd�}|�t|�t|��t|dt�}tt|ddg�ddgtd�}
|�||
���tdd�|D�ddgttBd�}|�t|�t|��t|dt�}|�||���td d�|D�ddgtd�}|�t|�t|��dS)!Nr�rr/r)rvr�r�)rsr�r�r r�r(rr)rr�r)rvrxr�rtrr�)rvr�rrs)r�rvr�rcSsg|]}|�qSr/r/)rkrqr/r/r0rl�szCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>)rcSsg|]}|�qSr/r/)rkrqr/r/r0rl�sr�cSsg|]}|�qSr/r/)rkrqr/r/r0rl	scSsg|]}|�qSr/r/)rkrqr/r/r0rl	scSsg|]}|�qSr/r/)rkrqr/r/r0rl	scSsg|]}|�qSr/r/)rkrqr/r/r0rl	s)rrrrrr�r r!r"r#r$r%rr�r�rr�r�r�rnrSr�r�r�r�r�r?Ztostringr&)
r�r)r�rZrequestrrZndbytesrTZnarJrrr/r/r0�test_py_buffer_to_contiguous2s�













B






 


   z/TestBufferProtocol.test_py_buffer_to_contiguouscCs�dgfdddgdgfttd��dddgfg}�xR|D�]H\}}t||d�}t|�}|�|j�|�|j�t|�}t||dd�}t	||�}|j
||dd	d||||d
�	t|�}	|j
|	|dd	d||||d
�	t|ttBd�}
|�
|
jd�|
��}|j
|ddd	d||||d
�	t|td�}
|�
|
jd
�|�
|
jd�|�
|
jd�|
��}|dk�rZ|gn|}|j
|ddd	dd|jgd|d
�	q4Wx�|D]�\}}t||td�}t|�}|�|j�|�|j�t|�}t||dd�}t||�}|j
||dd	d||||d
�	t|�}	|j
|	|dd	d||||d
�	�q�Wx�|dd�D]v\}}t||td�}t|�}t|�}t	||�}|j
||dd	d|||j|d
�	t|�}	|j
|	|dd	d|||j|d
�	�q2W|�ttdd�|�tti�tdddgdgd�}t|td�}
|�tt|
�t|ttBd�}
|�tt|
�tdgddgddd�}
|�tt|
�|�t|
j�|�tt|
td�|�tt|
td�|�tt|
ddd�td�dS)Nrr rr�rAr�)rvrsr)rIrwr-r�rurvrxrT)rr/r2r)r )rvr�r��9rJrr)rvr�rt)rnrSrr?r�rrrOryr�rr%rr�rxr;rr�rvr�r�rr�r&rr�r�rr�r�)r�Zitems_shaper`rvrr8rurxrTr-r�r/r/r0�test_memoryview_construction"	s�,



z/TestBufferProtocol.test_memoryview_constructioncCs�dddg}xTdddgdddgdddgfD]4}t||d�}|�|j�t|�}|�t|jd�q(WxJtdd�D]<\}}}td�}|�|�}|�|�	�d�|�|�
�g�qlWdS)	Nr rr�r)rvr
r?r�)rr�rr?rr�rrgr�r�r�)r�r`rvr�msrcr-r^r8r/r/r0�test_memoryview_cast_zero_shape�	s
"
z2TestBufferProtocol.test_memoryview_cast_zero_shapecCs�|j}tj}d}d}ttd��}|td�||d|��t|ddgdd	�}|t|�||d|��t|dddgdd	�}|t|�||d
|��dS)NzPnin 2P2n2i5P PZ3nr(r�r rr�r)rvr�r�)�check_sizeofrZcalcvobjsizernrSr?r)r�ZcheckZvsizeZbase_structZper_dimr`r9r/r/r0�test_memoryview_sizeof�	sz)TestBufferProtocol.test_memoryview_sizeofcCs�Gdd�dt�}Gdd�dt�}dd�}|d�|d�dtd	�d
tdd�d
ddgdddhddidddddtddtd�td�dddd|dd�g}�xJtdd�D�]:\}}}t|dg|td�}t|dg|td�}	t|�}
t	�
||	d |�||
d <|�|
d |	d �t	�|�}d!|k�rq�x�|D]�}d}
yt	�
||	||�Wnt	j
k
�rTt	j
}
YnXd}y||
d
<Wn.ttfk
�r�}z
|j}Wdd}~XYnX|
�s�|�r�|�|
d�|�|d�n|�|
d
|	d
��qWq�WdS)"Nc@seZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTcSs
||_dS)N)�val)r�r�r/r/r0�__init__�	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__cSs|jS)N)r�)r�r/r/r0�__int__�	szETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)�__name__�
__module__�__qualname__r�r�r/r/r/r0�INT�	sr�c@seZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXcSs
||_dS)N)r�)r�r�r/r/r0r��	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__cSs|jS)N)r�)r�r/r/r0�	__index__�	szGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r�r�r�r�r�r/r/r/r0�IDX�	sr�cSsdS)Nr)r/r/r/r/r0r�	r�z;TestBufferProtocol.test_memoryview_struct_module.<locals>.fry������@@z-21.1gffffff(@r�rr r�r�r�r)r(r/)rTF�asabcr9�abccSs|S)Nr/)rJr/r/r0�<lambda>�	r�zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rWr?)rvr�r�rr)�objectrr	rQr�rgrr�r?r*r�r�r'r,r�rrbZassertIsNot)r�r�r�r�valuesr-r`rarr�r8rwrqZ
struct_errZmv_errrdr/r/r0�test_memoryview_struct_module�	sF


z0TestBufferProtocol.test_memoryview_struct_modulecCs@tdddgdgdgd�}|�|j�t|�}|�t|jd�dS)Nr rr�r)rvrxr
)rrqrr?rr�r)r�rr�r/r/r0�!test_memoryview_cast_zero_strides�	sz4TestBufferProtocol.test_memoryview_cast_zero_stridescCs�x�tD]�}td�rd|n|}t�|�}xftD]^}td�rBd|n|}t�|�}tttd��d|g|d�}t|�}|�t	|j
|d|g�q.WqWx�td�D]�\}}	}
t|	dg|d�}t|�}x`td�D]T\}}
}
t|�s�|�t
|j
|d|g�q�t|�s�t|�s�|�t	|j
|d|g�q�Wq�Wt�d�}t�d�}tttd|��dd|gdd�}t|�}|jt	|j
dd|gdd�tttd	��ddd
ddgd�}t|�}
|�t	|
j
�|�t	|
j
ddd
�|�t	|
j
i�|�t
|
j
d
�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�|�t
|
j
d�tddgdgdd�}t|�}
|�t|
jd�|�t|
jdd�|�t|
j�tttd	��ddd
ddgd�}t|�}
|jt	|
j
did�tttd	��d	gd�}t|�}
|jt|
j
ddgd�|jt
|
j
ddgd�|jt
|
j
ddd
dddddgd�|jt
|
j
ddd
dddddgd�|jt	|
j
ddd
ddddd gd�ttd!d"�td#�D��d
ddd$gd�}t|�}
|jt	|
j
d%dd
ddgd�tttd&��d&gd%d�}t|�}
|�t
|
j
d%dgd&�ttd'd"�td#�D��d#gd�}t|�}
|jt	|
j
d%dd
ddgd�ttd(d"�td#�D��d#gd�}t|�}
|jt	|
j
ddd
ddgd�tttd&��d&gd%d�}t|�}tttd&��d&gdd�}t|�}tjd)k�rJ|�t	|j
dddd*d+d,d-d.g�|�t
|j
dd/d/d0d0dg�|�t
|j
d%d/d/d0d0dg�nN|�t	|j
ddd1g�|�t
|j
dd0d0dddg�|�t
|j
d%d0d0dddg�dS)2Nrr3r$)rvr�r rrr��xr�r�)rv�Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBI)r r)r�r�ZIIrr(rlrtr�r)rJcSsg|]}d�qS)rr/)rkr^r/r/r0rlE
szCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i�rrrcSsg|]}d�qS)rr/)rkr^r/r/r0rlO
scSsg|]}d�qS)rr/)rkr^r/r/r0rlT
sl�����Ir!iQiAji��	iii���)�NON_BYTE_FORMATrr*r'rrnrSr?rr�rrgrjrrhr�rKrIr�r8r��maxsize)r�ZsfmtZsformatZssizeZdfmtZdformatZdsizerr�Zsitemsr^Zsize_hZsize_dr8r�r,r-r/r/r0�test_memoryview_cast_invalid�	s�



 

 """&  
z/TestBufferProtocol.test_memoryview_cast_invalidcstddd�fddd�fddd�ff���fdd	�}t�d
�}tdgd
d�}t|dd
�\}}t|�}|�d�}�j||d
dd
d
|d|dd�
t�d
�}tdg||gdd�}t|d
|gd�\}}t|�}|jd
gd�}�j|||d
d
ddd|dd�
x:tdd�D],\}	}
}t�|	|
�}t|�}||||
|	�q�WxBtdd�D]4\}	}
}t|
dg|	t	d�}t|�}||||
|	��q8WdS)NrcSst|���S)N)rnr�)rr/r/r0r�n
r�z9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>rcSsdd�t|���D�S)NcSs g|]}|dkr|dn|�qS)r!rr/)rkrJr/r/r0rlo
szMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rnr�)rr/r/r0r�o
r�r
cSsdd�t|���D�S)NcSsg|]}tt|�d��qS)zlatin-1)rD�chr)rkrJr/r/r0rlp
szMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rnr�)rr/r/r0r�p
r�c

s�t�|�}x��D]~\}}|�|�}||�}�j||d|ddd|gd|dd�
|�|�}	��|	|�|��}�j|	|||dddg|f|dd�
qWdS)Nr rr%)r T)	rIrwr-r�rurvrxrTr)r*r'rrr�r�)
rr8r`r-�srcsizer@Zto_bytelistr-rT�m3)�bytespecr�r/r0�iter_roundtrips
s




z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtriprr)rvr�r )r T)	rIrwr-r�rurvrxrTr)rvrr/r%r=r?)rvr�r�)
r*r'rr�r?rrrgr=r�)r�r�r�rZ	destitemsZ	destshaper8r-Zdestsizer-r`r^r/)r�r�r0�test_memoryview_castl
s<






z'TestBufferProtocol.test_memoryview_castcCs��x�t�D�]�}�x�tdD�]�}|dkr,qdtd�|}t�|�}t|�|}t|�r^dnd}�xJt||�D�]:\}}}	t�|�}
|dkr�|gng}||
g}t|||d�}
t	|
�}t
|
|||d	�\}}|dkr�|�t|j
||�qp|d
kr�qpt|||d�}|j
||d	�}t|�}|j}|��}|j||
||d||||dd
�
|�
|�}|j
||d	�}t|�}|
j}|
��}|j||
|
|d||||dd
�
|j||
|
|d||||dd
�
qpWqWq
Wt�r�Gdd�dtj�}|dd�}t	|�}|�
d�}|�|j|�|�|jd�|�|jd�|�|jd�|�|j|jf�|�|jd�|�|jd�t�d�}t	|�}|�
d�}|�|j|�|�|jd�|�|jd�|�|jd�|�|j|jf�|�|jd�|�|jd�dS)Nr3r)r2r3rr?r@r)rvr�)rvr�r T)	rIrwr-r�rurvrxrTrc@s eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrJrN)r�r�r��ctypes�c_long�c_double�_fields_r/r/r/r0�BEPoint�
sr�r�g33333i@r)r r/g333333�?r
)r�rCrr*r'rrrhrgrr?r�rr�rrOrxr�rr��BigEndianStructurer�rIrwr�rurvr�r�r�)r�Z_tshaperHZtfmtZtsizerrIr-r`r^�sizervZtshaperr8Ztitemsr�r-rurxrTr�Zm4r��pointr,rJr/r/r0�test_memoryview_cast_1D_ND�
s�









z-TestBufferProtocol.test_memoryview_cast_1D_NDcCs@t�dttdd���}t|�}|�||�|�|��|���|ddd�}|ddd�}|�||�|�|��|���tttd��ddddd	gd
d�}t|�}|�|��|���tdd
gdgdd�}t|�}|�t|j�tdgdgdd�}t|�}|�t|j�tddddddgddgdd�}t|�}|�t|j�dS)Nri����r�rr�i	rr)r�r)rvr�)rr�)r)rr.s12345r r{r��b�c�d�e�f)	r=rnrSr?r�r�rrr�)r�r9r8rr/r/r0�test_memoryview_tolist�
s( z)TestBufferProtocol.test_memoryview_tolistcCsHttd��}|��}|�|�d��|��|��}|�|�d��dS)Nrz<memoryz	<released)r?r��__repr__r��
startswithr:)r�r8r�r/r/r0�test_memoryview_reprsz'TestBufferProtocol.test_memoryview_reprcCs�xRdD]J}td�}t�|d|dg�}t|�}|�d|�|�d|�|�d|�qWtdgdd�}t|�}|�ttdt��dS)	N)rrg�g�?g@g"@r)r�z9.0 in m)	�floatr=r?ZassertInrrr�rVrW)r�r-�infrr8r/r/r0�test_memoryview_sequence#s
z+TestBufferProtocol.test_memoryview_sequencec	cs8|�t��}dVWdQRX|�t|j�d|f�dS)Nz#index out of bounds on dimension %d)rrLr��strZ	exception)r��dim�cmr/r/r0�assert_out_of_bounds_error1sz-TestBufferProtocol.assert_out_of_bounds_errorc
Cs*tdgdd�}t|�}|�|dd�|�|d|�|�|d|�|�t|jd�tdgdd�}t|�}|�t|jd�tttd	��d	gt	d
�}t|�}|�t
|jd�|�t|jd�|�t|jd
�|�t
|jd�|�t
|jd�tttd��ddgt	d
�}t|�}|�|dd�|�|dd�|�|dd�|�|dd�|�|dd�x2dD]*}|jdd��||dfWdQRX�qdWx2dD]*}|jdd��|d|fWdQRX�q�W|�t
|jd�|�t
|jd�|�t|jd �|�t|jd!�|�t|jd�|�t|jd�dS)"Ng)@r)rvr�r/.r)r rr�Ziiir))rvr�lg@gi����r(rr�r�)rr)rr)rr�r)rtrt)r1r)r�rr )r�)r�rBr)lr)rl)rrr)gg)rr?r�rr�rKr�rnrSr�rLr�)r�rr8�indexr/r/r0�test_memoryview_index8sF

z(TestBufferProtocol.test_memoryview_indexc
Cs�tdgdtd�}t|�}d|d<|�|dd�d|d<|�|dd�|�t|jdd	�tttd
��d
gd�}t|�}|�t|jdd
�tttd
��d
gtd�}t|�}|�t	|jdd�|�t|jdd
�|�t|jdd�|�t	|jdd�|�t	|jdd�x�t
dD]t}|dk�s|dk�r*�qtdddgdg|td�}t|�}tdd�}d||<|�||d�|�||||��qWtddd gdgdtd�}t|�}d!|d<|�|dd!�td"d#d"gdgdtd�}t|�}d"|d<|�|dd"�td$gdgdtd�}t|�}|�t|jdd%�tttd&��dddd'd(gtd�}t|�}x�t
d��D]�\}}|dk�rp�qZ|dk�r~�qZ|�
|�}|\}	}
|d)k�s�|dk�r�dd*dd*}	}
|d+k�r�|�t|jd|	d�|�t|jdd,�|�t|jd|
��qZW|�
d�}|�t|jdd-�tttd��dgd.td�}t|�}|�t|jdd�td/gdgd0td�}t|�}|�t|jdd�tttd1��dd'gtd�}t|�}d2|d3<|�|ddd2�d4|d5<|�|ddd4�x2d6D]*}|jdd7��d||df<WdQRX�q�Wx2d8D]*}|jdd7��d|d|f<WdQRX�qW|�t	|jd9d�|�t	|jd:d�|�t|jd;d�|�t|jd<d�|�t|jdddg�dS)=Ng)@r)rvr�r�g�6@r/g�7@.rg33333�8@r))rvrrW)rvr�lrg@gri����ryr(�r3r
rr r�r1�1�2�3r�TF�xr�r�r�r�rirr2s��ZxLs12345r{r�*)rr �+)rtrt)r�r)r�)r�rB)lr)rl)rrr)gg)rr�r?r�rr�rIrnrSrLrCrr`rrr�r�)r�rr8r-rr�r,Z_ranger-�lo�hir�r/r/r0�test_memoryview_assignms�
 






z)TestBufferProtocol.test_memoryview_assignc	Cs�tttd��dgtd�}t|�}|�t|jtddd��|�t|j	tddd�t
ddg��|�t|jd�tttd��dgtd�}t|�}|�t|jtddd�tddd�f�|�t|j	tddd�tddd�ft
ddg��|�t|jtddd�if�|�t|j	tddd�ift
ddg��|�t|j	tddd�dg��xzdt
fD�]l}tttd��dgdgdt|Bd	�}tttd
��dgdg|d�}t|�}t|�}|dd�|dd�<|dd�|dd�<|�||�|�||�|dd�ddd�|dd
�ddd�<|dd�ddd�|dd
�ddd�<|�||�|�||�|ddd�ddd�|ddd�ddd�<|ddd�ddd�|ddd�ddd�<|�||�|�||��qFWdS)Nr)rvr�rrr r/rtr)rvrxr�r��)rvrxr�r�r�r�r)rrnrSr�r?rrrKr�rIr�r�r�r&r�)r�rr8r�r*r+r,r-r/r/r0�test_memoryview_slice�sD

$((,,z(TestBufferProtocol.test_memoryview_slicecCs�dd�}�x�tdd�D�]�}�x�t|d�D�]�\}}}�x�t|�D�]�}�x�t|�D�]�}t�||�}t�||�}	t|	�}
|�|
|�|�|
��|���|�|
��|���|�t|
�t|��||||	|
|�d}d}y0||}
||}||||<t|
�t|�k}Wn*t	k
�r4}z
|j
}Wdd}~XYnXd}y|
||
|<Wn*t	k
�rt}z
|j
}Wdd}~XYnX|�r�|�|t�qT|�s�|�r�|�||�qT|�|
|�|�|
��|���|�|
��|���||||	|
|�qTWqBWq*WqWdS)Nc	Ss�x�t|�D]�\}}||}||}|�||�|||<||krX|�||�|�||�n|�||�|�||�|||<|�||�|�||�|||<|||<q
WdS)N)r�r��assertNotEqual)	Ztestcaser9rr8�
singleitemrr^Zai�mir/r/r0�cmptestsz9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr r�r=)
rSrgr�r=r?r�r�r�rOrarbr�r)r�r�rr-r`r�rir�r9rr8Z	array_errZhave_resizeZal�arrdZm_errr/r/r0�test_memoryview_arraysFz(TestBufferProtocol.test_memoryview_arrayc	Cs�t�ddddg�}t�ddddg�}t|�}t|�}x8dD]0}|�t||�|�t�|�t||�|�t�q:Wt|�}|��|�||�|�||�|�||�t|�}t|�}|��|�||�|�||�t|�}|�|dddg�tdgdgdt	d	�}d
t
d�f|d<|�t|�|�t�d
d�}t|�}|�||�|�||�t�r�Gdd�dtj�}|dd�}t|�}t|�}|�||�|�||�|�||�|�
t|j�dS)Nrr rr�r))�__lt__�__le__�__gt__�__ge__)rrzl x d x)rvr�r�rtr�r�ur2c@s eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrJrN)r�r�r�r�r�r�r/r/r/r0r�{sr�r�rH)r=r?r��getattrrQr:r�r�rr�r�r�r�rr�r�)	r�r9rrq�w�attrr�r�r�r/r/r0�%test_memoryview_compare_special_casesPsF

z8TestBufferProtocol.test_memoryview_compare_special_casescCsntdgdd�}tdgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�|�||�|�|�|��|�|�|��d|d<|�||�|�||�|�||�|�||�|�||�|�||�|�|�|��|�|�|��ttt	d��dgtt
Bd	�}ttt	d��dgtt
Bd	�}t|�}|�||�d
|d<|�||�tdgd
d�}tdgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�|�||�tdgd
d�}tdgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�|�||�|�||�|�||�dS)Ni�z@L)rvr�r)rvr�r�i�r/r)rvr�r�r)i�g333333�?s12345ZLf5sZhf5s)i?���g333333�?s12345)rr�r?r�rq�__ne__r�rPrnrSr&)r��nd1�nd2rqr�r�rr8r/r/r0�!test_memoryview_compare_ndim_zero�sfz4TestBufferProtocol.test_memoryview_compare_ndim_zerocCs�tdddddgdgdd�}tddddd	gdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdddddgdgd
d�}tddddd	gdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdddgdgdd�}tdddddgdgdd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tdddgdgdd�}tdddddgdgdd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tdddgdgdd�}tdddddgdgdtd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tdddgdgdd�}tdddddgdgdtd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��dS)Ni��i@i����i�i'���r�z@h)rvr�i�z<iz>hr�rrtrz!hz<l)rvr�r�zh  0cz>  h)rr?r�r�r&)r�r�r�rqr�r/r/r0� test_memoryview_compare_ndim_one�sh   z3TestBufferProtocol.test_memoryview_compare_ndim_onecCs�tddgdgdd�}tddgdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tddgdgdd�}tddgdgd	d�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)
Ni�i�rz@h)rvr�i|���i?���z= h0cz@   i)rr?r�)r�r�r�rqr�r/r/r0�"test_memoryview_compare_zero_shape
s$z5TestBufferProtocol.test_memoryview_compare_zero_shapecCs�tddddgdgdd�}tdgdgdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgddgd	d�}tdgdgdgd
d�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Ni�r�z@L)rvr�rr)rvrxr�)i�i�z@ Liz!L  h)rr?r�)r�r�r�rqr�r/r/r0�$test_memoryview_compare_zero_strides&
s$z7TestBufferProtocol.test_memoryview_compare_zero_stridesc
Csd}x~tdD]r}t|dd|�\}}}xXdtfD]L}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�q0WqWd}xxtd�D]l}	t|�\}}}xXdtfD]L}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�q�Wq�WdS)	NrWz@mr?r3r)rvr�r�r1r�)rCrcr&rr?r�rS)
r�rrHr-r`r�r�r�r8r^r/r/r0�&test_memoryview_compare_random_formats@
s(z9TestBufferProtocol.test_memoryview_compare_random_formatscCs�tttdd��dddgdd�}tttdd	��dddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�td
gd	dddgdd�}tdgd	dddgd
d�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd	��dddgdd�}tttd	��dddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�td
gdddgdd�}td
gdddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd	��dddgdd�}tttd	��dddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Ni�r'r�rr�z@h)rvr�rrA)rr rz=f q xxL)g333333�r rz< f 2Qr�r)z! b B xLz
= Qx l xxLr)rrnrSr?r�r�)r�r�r�rqr�r/r/r0�"test_memoryview_compare_multidim_c\
sZz5TestBufferProtocol.test_memoryview_compare_multidim_ccCs�tttdd��dddgdtd�}tttdd	��dddgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�td
gdddgdtd�}td
gdddgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttdd��dddgdtd�}tttdd��dddgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttdd��dddgdtd�}tttdd��dddgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd	��dddgdtd�}tttd	��dddgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Ni�r'r�rr�z@h)rvr�r�rrA)l����rtr�z=Qq)rtl����z=qQrZ0llz@b)rrnrSr�r?r�r�)r�r�r�rqr�r/r/r0�(test_memoryview_compare_multidim_fortran�
snz;TestBufferProtocol.test_memoryview_compare_multidim_fortranc	Cshttdd��}t|dddg�}t|dddgdd�}t|dddgdtd	�}t|�}t|�}|�||�|�||�|�||�d
gd}d|d<t|dddg�}t|dddgd
d�}t|dddgdtd	�}t|�}t|�}|�||�|�||�|�||�tttd��ddgdd�}|ddd�ddd�f}tttd��ddgdd�}|ddd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdddgdd�}|ddd�ddd�f}tdgdddgdd�}|ddd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��dddgdd�}|dd�ddd�f}tttd��dddgdd�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��dddgdd�}|dd�ddd�f}tttd��dddgdd�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgddddgdd�}|dd�ddd�f}tdgddddgdd�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS) Ni�r'r�rr�z@l)rvr�r)rvr�r�)gffffff
�i��r�rA)g�������i��r�zd b czd h c�(r(z@Ir rtrr)i���i��rz=iiz>iirr)rs123Zb3sZi3s)rnrSr�rr�r?r�r�)	r��lst1�lst2r�r�rqr�r*r+r/r/r0�&test_memoryview_compare_multidim_mixed�
s�
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedcCstttd��dddgdd�}tttd��dddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��dddgdd�}tttd��dddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)	NrArr�rr)rvr�r�z@i)rrnrSr?r�r�)r�r�r�rqr�r/r/r0�+test_memoryview_compare_multidim_zero_shapeCs$z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapecCstdgddddgdd�}tdgdddgdddgdd	�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�|�|��|���td
gdddgd
d�}td
gddgddgdd	�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Ni�r�r�r�z@L)rvr�rr)rvrxr�)r rrWrz=lQz<lQ)rr?r�r�)r�r�r�rqr�r/r/r0�-test_memoryview_compare_multidim_zero_strides]s&z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc	Csztttd��ddgdd�}|ddd�ddd	�f}tttd��ddgd
td�}|ddd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdddgd
td�}d|dd<|ddd�ddd	�f}tdgdddgdttBd�}d|dd<|ddd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��dddgdtd�}|dd�ddd	�f}tttd��dddgdd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgddddgdttBd�}|dd�ddd	�f}tdgddddgdd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��dddgdtd�}|dd�ddd	�f}tttd��dddgdtd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgddddgdttBd�}d|ddd<|dd�ddd	�f}tdgddddgdttBd�}d|ddd<|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�ttdd��}t|dddg�}t|dddgdtd�}t|dddgdt	tBd�}t|�}t|�}|�||�|�||�|�||�dgd}d |d!<t|dddg�}t|dddgd"td�}t|dddgd#t	tBd�}t|�}t|�}|�||�|�||�|�||�dS)$Nr�r�r(z@I)rvr�r�r rtrr)rvr�r�)l����rtz=Qq)r rrr)z>QqrAr)r4rtZBbr)shellor�r �Z5s0sP)ssushir�r i�r'z@lr)ssashimisslicedg�����4@)sramensspicygfffff�"@rz
< 10p 9p dz
> 10p 9p d)
rrnrSr&r?r�r�r�r�r�)	r�r*r�r+r�rqr�r�r�r/r/r0�+test_memoryview_compare_multidim_suboffsetsxs�
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetscCsr�xjdD�]`}tdgddddddg|dd�}tdgddddddg|dttBd	�}d
|ddd
d
d
<t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgddddddg|dd�}tdgddddddg|dttBd	�}d
|ddd
d
d
<t|�}t|�}|�||�|�||�|�||�|�||�|�||�qWdS)N)r6r4r5r7lr�r�r�rr)rvr�)rvr�r�r )lli�ZQLH)r r r )rr�r�r?r�r�)r��	byteorderrJrr9rr/r/r0�!test_memoryview_compare_not_equal�s0"z4TestBufferProtocol.test_memoryview_compare_not_equalcCst�ddddg�}t|�}|��|�tt|�|�t|jd�|�tt|�|�t|j�|�t|j�|�tt	dt
��|�t|jd�|�t|jdd�xd	D]}|�t|j
|�q�Wt�ddddg�}t|�}t|�}|�||�|��|�||�|�||�|�||�dS)
Nrg�������?g������@gffffff
@r
z1.0 in mrr )rIr�r�rwr�rurvrxr�rrr)r=r?r:rrrrr�r�rVrWrKrI�__getattribute__r�r�)r�r9r8r�rr,r-r/r/r0�test_memoryview_check_releaseds*
z1TestBufferProtocol.test_memoryview_check_releasedcsXd�t�dgdd�}t|�}|�||�|�|��|���t�gdgdd�}t|�}|�||�|�|��|���t�fdd�td	�D�d
d
dgdd�}t|�}|�||�|�|��|���t�fd
d�td�D�dd
d
dd
gdd�}t|�}|�||�|�|��|���t�rTGdd�dtj�}|dd�}t|�}|�|��t|��dS)N)i��i@i����i�i'���r�z@h)rvr�r z>hQiLlcsg|]}��qSr/r/)rkr^)rfr/r0rlMsz>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rrr�z=hQiLlcsg|]}��qSr/r/)rkr^)rfr/r0rlRsr�z<hQiLlc@s eZdZdejfdejfgZdS)z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrJrN)r�r�r�r�r�r�r/r/r/r0r�[sr�r�rH)rr?r�r�rSr�r�rD)r�r�r8r�r�r9r/)rfr0�test_memoryview_tobytes>s.&"
z*TestBufferProtocol.test_memoryview_tobytescCs<|�ttitd�|�ttdtd�tdddgdgdgd�}|�tt|td�td	d
dd�}x4d
D],}t|t|�}|�||�|�|d
d	�qbWtd	d
dtd�}x4d
D],}t|t|�}|�||�|�|d
d	�q�Wxdd
D]\}d	|d
<t|t|�}|�||�|�|d
d	�d|d
<|�|d
d�|�|d
d�q�Wtdgdgdtd�}xFd
D]>}t|t|�}|�t	|j
d�|�||�|�|��g��qXWttt
d��dddgdtd�}x2d
D]*}t|t|�}|�t|���ggg��q�Wtdgdgdtd�}x:d
D]2}t|t|�}|�||�|�|��|����qWtdddgdgdtd�}x:d
D]2}t|t|�}|�||�|�|��|����qZWtdddgdgdgtd�}xld
D]d}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��q�W|ddd�}xld
D]d}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��q,Wttt
d��ddgtd�}x2dD]*}t|t|�}|�t|���|����q�W|�tt|td�t|t|�}|�t|���|���ttt
d��ddgttBd�}x2dD]*}t|t|�}|�t|���|����q<W|�tt|td�t|t|�}|�t|���|���ttt
d��ddgttBd�}xDd
D]<}|�tt|t|�t|t|�}|�t|���|����q�Wtdddddgdgdgd�}t|td�}|�|j�dS)Nr�r�rsr rr�)rvrxr�rr/r)rvr�)rsr�r�)rvr�r�rWrr(r)rr)rvrxr�ryrtrr�)rvr�)rsr�)r�r�r�)rr�r�r�r�ZPyBUF_WRITErr�r�rLrKr�rnrSrIr�r&r�r)r�r�rr8r/r/r0�test_memoryview_get_contiguousas�











z1TestBufferProtocol.test_memoryview_get_contiguouscCs�t�d�}t�ddddddg�}t|�}t�|�}td|�}|�|�|�|�	�|�t�d�}t
ttd��dddgdd	�}t|�}t�|�}td|�}|�|�|�|�	�|�dS)
Nrr rr�r�r�rr)rvr�)
r*r'r=r?�io�BytesIOr��readintor�r�rrnrS)r�r�r9r8r�rr�r/r/r0�test_memoryview_serializing�s





z.TestBufferProtocol.test_memoryview_serializingc	Cs�tttd���}t|�}|�t|�t|��|jdddgd�}|�t|�t|��|ddd�}tttd��ddd��}|�t|�t|��tttd��ddd	gtd
�}t|�}|�t|�t|��tttd��ddd	gd�}|ddd�dd�ddd�f}t|�}|�t|�t|��tttd��dd	dgt	d
�}|ddd�dd�ddd�f}t|�}|�t|�t|��tttd��dgdd
�}t|�}tttd��dgdd
�}t|�}|�||�|�t|�t|��tttd��dddgdd
�}t|�}|�
t|j�tttdd��dddgdd
�}t|�}|�
t|j�tttd��dddgdd
�}t|�}|�
t|j�tttdd��dddgdd
�}t|�}|�
t|j�dS)Nrr
r�r�)rvrrArr�)rvr�rtr)rvr�rri����r�rz= Lz< h)
rDrnrSr?r�rrrr�r&rr�__hash__)	r�rr8ZmcZmxr�rJr9rr/r/r0�test_memoryview_hash�sJz'TestBufferProtocol.test_memoryview_hashcCs�tdddg�}t|�}t|�}|�t|j�~|��tdddg�}t|�}t|ttd�}t|ttd�}|�|j	|�|�t|j�~~|��tdddg�}t|�}t|�}t|�}|��|�t|j�~|��tdddg�}t|�}t|�}t|ttd�}t|ttd�}|�|j	|�|��|�t|j�~~|��tdddgdgt
d�}t|�}|jdddd	d
gdgd�t|�}t|�}|�|�
�|�
��t|�}	|�|	�
�|�
��|�|	�
�|�
��|��|	��|��|�|�
�|�
��~|��|��dd
�}
td�}t|��"}|
|�|�|dtd��WdQRXtttd��dddgdd�}t|ttd�}	t|	ttd�}|�|j	|�t|��8}|
|�|�|dd��
�dddgdddggg�WdQRX�x�dtfD�]�}td�}t|���}~t|t|d�}	t|	���}~	t|t|d�}t|��^}
~|
|
�|
|�|
|�|�|dtd��|�|dtd��|�|
dtd��~
WdQRX~WdQRX~WdQRXtd�}t|���}~t|t|d�}	t|	���}~	t|t|d�}t|��b}
~|
|�|
|�|
|
�|�|dtd��|�|dtd��|�|
dtd��~~~
WdQRXWdQRXWdQRX�q�Wtd�}|�t��2t|��}t|�}|dtd�kWdQRXWdQRXdS)Nr rr�)rr�)rvr�r�r�r�r)r()rvc	Sst|��}WdQRXdS)N)r?)rr-r/r/r0�catch22rs
z;TestBufferProtocol.test_memoryview_release.<locals>.catch22s123rr�rr)rvr�r�r�)r�r?rrr�r:r��ND_REDIRECTr�rIr6r7r�r�r9�ordrnrS)r�r9r8r�r�r�r,r-rJrr�ror�r�rr/r/r0�test_memoryview_release0s�

4





*
z*TestBufferProtocol.test_memoryview_releasecCs4tdd�td�D�dgdd�}t�ddd�td�D��}x�||fD]�}t|ttd�}t|ttd�}t|�}|�|j|�|�|j|�|�|j|�|�||�|�||�|�||�|�|dd	�|dd	��|�|dd	�|dd	��|�|dd	�|dd	��~~|�|dd	�|dd	��qBWdS)
NcSsg|]}d|�qS)g�?r/)rkrJr/r/r0rl�sz?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rr)rvr�cSsg|]}d|�qS)g�?r/)rkrJr/r/r0rl�s)rr�r r�)	rrSr=r�r�r?r�rIr�)r�r�r9rJrror8r/r/r0�test_memoryview_redirect�s"z+TestBufferProtocol.test_memoryview_redirectcCs�d}dddddddd	d
ddd
g}|�ttddd�t�}t|�}|j||d|dddgdg|d�	x td�D]}|�|||�qjW~~t�}t|�}~~t�}t|td�}t|td�}t|�}|�	|j
|�|�	|j
|�|j||d|dddgdg|d�	~~~~t�}t|ttd�}t|ttd�}t|�}|�	|j
|�|�	|j
|�|�	|j
|�|j||d|dddgdg|d�	~~~~tdd�}t|�}|j|dd|dddgdg|d�	x"td�D]}|�|||��q�W~~tdd�}t|�}~~tdd�}t|td�}t|td�}t|�}|�	|j
d�|�	|j
|�|j||d|dddgdg|d�	~~~~tdd�}t|ttd�}t|ttd�}t|�}|�	|j
d�|�	|j
|�|�	|j
|�|j||d|dddgdg|d�	~~~~dS)Nrrr rr�r�r�r�r)r(rrWrr)rIrwr-r�rurvrxrT)r)rr�T)Zlegacy_mode)rr�Zstaticarrayr?rrSr�rr�r�rIr�)r�r-rTrJrrror8r/r/r0�$test_memoryview_from_static_exporter�s�









z7TestBufferProtocol.test_memoryview_from_static_exportercCs,tdddgdgttBd�}|�tt|�dS)Nr rr�)r�)rr(ZND_GETBUF_UNDEFINEDrr�r?)r�r�r/r/r0�#test_memoryview_getbuffer_undefined&sz6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs(tdddgdgtd�}|�tt|�dS)Nr rr�)rvr�)rr(rr�r?)r�rJr/r/r0�test_issue_7385,sz"TestBufferProtocol.test_issue_7385N)F)Sr�r�r�r�rrr0r=r>r?r@rCrDrGrMrNrOrRrSrUrXr_r`rgrhrjrlrprrrurvrwrxrzr{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�r�r�r�r�r�r�r�r�r�r�r�rrrr/r/r/r0r��s�
3
2i
-'	0.Y #6!)>
		%qu
8	s<T	5m8>5?H>Ha(#u<^r��__main__)rNN)r)r)r�r&r/)r�r&)r)N)F)F)r�)N)frZunittestZtestr�	itertoolsrrZrandomrrr�warningsr�r=r��os�decimalrZ	fractionsr	Z_testbuffer�ImportErrorrr*r�ZEnvironmentVarGuard�environ�catch_warningsr>r�Z
SHORT_TESTZNATIVEr+r,ZSTANDARDr1rCr-�copyriZARRAYrTZBYTEFMTrYr[rXrKrRrUrbrcrdrgrhrjr�rorprrryrzr~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�rkrmrnZ
skipUnlessZTestCaser�r��mainr/r/r/r0�<module>sT 



$
	





	



	


?

+

!




U