Your IP : 216.73.216.52


Current Path : /opt/alt/python39/lib64/python3.9/test/__pycache__/
Upload File :
Current File : //opt/alt/python39/lib64/python3.9/test/__pycache__/test_buffer.cpython-39.pyc

a

XC?h��@sDddlZddlZddlmZddlmZmZddlmZm	Z	m
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlmZzddlTWney�dZYn0zddlZWney�dZYn0zddlZWney�dZYn0zfe���He_e���ddlmZWd�n1�s&0YWd�n1�sF0YWne�yjdZYn0zddlZWne�y�dZYn0d	Zdddddddddddddddd
�Z e�r�e d=e d=e�r
z e�!d
d�de d<de d
<Wnej"�yYn0ddddddddddddddd>dd>fd�Z#dd�Z$e e e#e#e#e#d�Z%e�rze%dD]Z&e$e&�e%de&<�qbe �'�Z(e �'�Z)e D]Z*e*d v�r�e)e*=�q�e �'�Z+e D]Z*e*d!v�r�e+e*=�q�e(e%d"<e(e%d#<e)e%d$<e+e%d%<e+e%d&<dZ,dZ-gd�gd'�fd$gd(gfd(gd(gfd#d"gd(gfd&d%gd(gfd)�Z.d*d+�Z/d,d-�Z0d.d/�Z1d0d1�Z2d�d3d4�Z3d�d5d6�Z4d�d7d8�Z5d9d:�Z6d;d<�Z7d=d>�e%dD�Z8d?d@�Z9dAdB�Z:dCdD�Z;dEdF�Z<dGdH�Z=dIdJ�Z>dKdL�Z?dMdN�Z@dOdP�ZAdQdR�ZBdSdT�ZCdUdV�ZDdWdX�ZEdYdZ�ZFd[d\�ZGd]d^�ZHd_d`�ZIdadb�ZJdcdd�ZKdedf�ZLdgdh�ZMdidj�ZNd�dndo�ZOdpdq�ZPdrds�ZQd�dtdu�ZRdvdw�ZSd�dxdy�ZTdzd{�ZUd�d|d}�ZVd~d�ZWd�d��ZXd�d��ZYd�d�d��ZZd�d�d��Z[d�d�d��Z\d�d�d��Z]d�d��Z^d�Z_dkZ`d�Zae�rd�Z_d�Z`d�Zae[ZXe\ZYe]Ze�bed��e�bed��Gd�d��d�ejc���Zdeed�k�r@e�f�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�Q������q�r��r�)i�����)i�����)ri)���)rl�l������r���)rr
rrrrrrrrrrrrc	Cs�|dkrd}n�|dkrd}n�|dkr*d}n�|dkrFdd	>dd	>f}ndd
D]8}zt�|d|>d�Wq�WqJtjy�YqJ0qJ|d@r�d|>d|>fn
dd|>f}|S)zReturn range of a native type.r
r rrrr&rr*r+)
r"��@�?� �����r)�struct�pack�error)�fmt�lhZexp�r:�5/opt/alt/python39/lib64/python3.9/test/test_buffer.py�native_type_range^s 
&r<)��@�<�>�=�!r>Z
bBhHiIlLfdZBbc�m�@m�ar�@b)r=�1�2�3r=)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
rK��rrrr)r�fmtdict�bytes�boolr5r6�unpack)�mode�char�obj�xr:r:r;�
randrange_fmt�s
rXcCsJ|�d�\}}g}|D]}|�t|||��qt|�dkrB|dSt|�S)zReturn single random item.�#r*r)�split�appendrX�len�tuple)r8rVrT�charsrWr
r:r:r;�gen_item�s
r_cCs<|dkrt||�Sdg|}t|�D]}t||�||<q$|S)z,Return a list of random items (or a scalar).r)r_�range)rr8rV�lstrr:r:r;�	gen_items�s

rbcCs�tt|t�}|d}|�d�}tdd�}t|�D]H}ttt|��}tt|t�}||t	|rf|nd�7}|||7}q4t
|||�}	t||�}
||	|
fS)NrY�ambr�
r*)r�cap�MODE�striprr`r]rP�MULT�intrbr_)rrVrTZxfmtr8�nmemb�_rU�
multiplier�items�itemr:r:r;�struct_items�s


rorcCs�|durtt|t�}|dur0ttt|��}tt|t�}|d|t|rR|nd�}t|||�}t||�}|�	d�||}|||fS)z"Return random format, items, item.NrYr*rc)
rrerfr]rPrhrirbr_rg)rrVrTrUrlr8rmrnr:r:r;�	randitems�s
rpccs4t|tD]"}t|D]}t||||�VqqdS)z1Iterate through supported mode/char combinations.N)rerfrPrp)rrVrTrUr:r:r;�	iter_mode�srqccs2t||�D]
}|Vq
|dkr"dSt||�VdS)zwYield (format, items, item) for all possible modes and format
       characters plus one random compound format string.rN)rqro)�nitemsZtestobj�tr:r:r;�iter_format�s
rtcCsd|vpd|vpd|vS)Nr
rrr:)r8r:r:r;�is_byte_format�srucCs4t|�}|dks$|dko2|ddko2||dtvS)zformat suitable for memoryviewr*rrr>)r\�
MEMORYVIEW)r8rWr:r:r;�is_memoryview_format�s�rwcCsg|]}t|�s|�qSr:)ru��.0r
r:r:r;�
<listcomp>��rzcCst|t�S)z9Tuple items (representing structs) are regarded as atoms.��
isinstance�list�rar:r:r;�atomp�sr�cCs
t|t�S�Nr|rr:r:r;�listp�sr�cCs6t|�dkrdS|d}|dd�D]}||9}q$|S)zProduct of list elements.rr*N)r\)rarW�vr:r:r;�prod�s
r�cCs�|dkrdS|dkrZt|dd��|g}t|ddd�D]}||||d9<q:n>|gt|dd��}td|�D]}||||d9<qz|S)zOCalculate strides of a contiguous array. Layout is 'C' or
       'F' (Fortran).rr:�Cr*Nr���)r~r`)�ndim�shape�itemsizeZlayout�stridesrr:r:r;�strides_from_shapesr�cCs�t|�r|St|�dkr |dSdg|d}|drFt|�|dnd}t|d�D].}||}t||||�|dd��||<qV|S)zmConvert flat item list to the nested list representation of a
       multidimensional C array with shape 's'.rr*N)r�r\r`�_ca)rm�sra�strider�startr:r:r;r�s$r�cCslt|�r|St|�dkr |dSdg|d}|d}t|d�D]$}t||d|�|dd��||<qB|S)zsConvert flat item list to the nested list representation of a
       multidimensional Fortran array with shape 's'.rNr*)r�r\r`�_fa)rmr�rar�rr:r:r;r�$s"r�cCs2t|�r(d|vr(t|�t|�kr(td��t||�S�Nrzprod(shape) != len(items))r�r�r\�
ValueErrorr��rmr�r:r:r;�carray1s r�cCs2t|�r(d|vr(t|�t|�kr(td��t||�Sr�)r�r�r\r�r�r�r:r:r;�farray6s r�cCsdd�|D�}t|�S)z(Generate all possible tuples of indices.cSsg|]}t|��qSr:�r`�ryr�r:r:r;rz=r{zindices.<locals>.<listcomp>)r)r��	iterablesr:r:r;�indices;sr�cCs*d}t|�D]}|||||7}q|S)zAConvert multi-dimensional index to the position in the flat list.rr�)r��indr�Zretrr:r:r;�getindex@sr�c	Cs�|s|St|�}t||dd�}t||ddd�dd�}dgt|�}t|�D]2}t|||�}t||ddd�|�}||||<qL|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*r�Nr�r)r\r�r�r�)	�srcr�r�ZsstridesZdstrides�destr��fr�tor:r:r;�	transposeGsr�cCs6|gkr|St|�r|gSt|d�t|dd��S)zflatten listrr*N�r��_flattenrr:r:r;r�Vs
r�cCst|�r|St|�S)zflatten list or return scalarr�rr:r:r;�flatten^sr�cCs4t|�rgSt||d�gt|d|dd��S)zMGet the shape of lst after slicing: slices is a list of slice
       objects.rr*N)r�r\�slice_shape�ra�slicesr:r:r;r�dsr�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)ryZsublst�r�r:r;rzor{zmultislice.<locals>.<listcomp>r)r�r�r:r�r;r�ksr�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���qSr�)�m_assign)ryr�r��lslices�rslicesr:r;rz�s�zm_assign.<locals>.<listcomp>r)r��zip��llst�rlstr�r�r:r�r;r�qs�r�cCsft||�}t||�}t|�t|�kr(dStt|��D],}||||krNdS||dkr4dSq4dS)z9Compare the structure of llst[lslices] and rlst[rslices].r�r)r�r\r`)r�r�r�r��lshape�rshaperr:r:r;�
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�r�r�r�r:r:r;�multislice_assign�sr�cs�|�rdS|dks |�|kr$dSt�fdd��D��r>dS|dkrZ|dkoX�oX�Sd�vrfdSt��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|]}|�VqdSr�r:r��r�r:r;�	<genexpr>�r{z#verify_structure.<locals>.<genexpr>Tc3s.|]&}�|dkr�|�|dVqdS�rr*Nr:�ry�j�r�r�r:r;r��s�c3s.|]&}�|dkr�|�|dVqdSr�r:r�r�r:r;r��s�)�any�sumr`)�memlenr�r�r�r��offset�imin�imaxr:)r�r�r�r;�verify_structure�sr�cCs|D]}||}q|Sr�r:)rar�rr:r:r;�get_item�s
r�c
Cs:|\}}}}}}|}t|�D]}	|||	||	7}q|S)z-Location of an item in the underlying memory.r�)
r�rsr�r�r�r�r�r��prr:r:r;�memory_index�s
r�cCsR|\}}}}}}d|>}t|�D],}t||�}	d|	>}
||
@rDdS||
O}q dS)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�)rsr�r�r�r�r�r�Zvisitedr�r�bitr:r:r;�is_overlapping�s

r��r1r:csZ�s�t|d�}|dkrd|r,|||dddfStdd�}||}td�dkrP|n|}|||dd|fSd}	td�}
|
dkr�|r�d}	n|
dkr�d}	dg|�t|�D]}t|	|d��|<q�nt��}d	}td�}
|
dkr�|
d@r�d
nd}
dg|�|t||d��|d<|
�s0�|ddk�r0|�|d<t|ddd�D]j}|�|d�r`�|dnd9}|
�r�|t||d��|<n"d
td�|td|d��|<�q@d}}d�v�r�t��fdd�t|�D��}t��fdd�t|�D��}||}|�r ||}||d|}n*|||}td�dk�rF||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�TFr��r*r�c3s.|]&}�|dkr�|�|dVqdSr�r:r�r�r:r;r�s�z!rand_structure.<locals>.<genexpr>c3s.|]&}�|dkr�|�|dVqdSr�r:r�r�r:r;r�s�)rr`r\r�)r�Zvalid�maxdim�maxshaper�r�rrr�r��minshaperrZ	maxstrideZzero_strider�r�r:r�r;�rand_structure�s\


"
��

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�rkZcontrolr:r:r;�randslice_from_slicelen&sr�cCspdg|}dg|}t|�D]B}||}|dkr>td|d�nd}t||�||<t||�||<qt|�t|�fS)zxCreate two sets of slices for an array x with shape 'shape'
       such that shapeof(x[lslices]) == shapeof(x[rslices]).rr*)r`rr�r])r�r�r�r�rrr�r:r:r;�randslice_from_shape3s

r�cCs�td|d�}d}td�}|dkr(d}n|dkr4d}td�dkrDdnd	}dg|}dg|}dg|}dg|}	t|�D�]}t||d�}
t||d�}||
kr�|
|}}
|�rt|
|
d�}t|
|
d�}
d
td�td|
d�}t||
|�}t||
�\}}}}n&|
dk�r$td|
d�nd}t||
�}t||�}td�dk�rp||
||<||<|||	|<||<qx|
|||<||<|||	|<||<qx||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��PTFr�)rr`r�r�r�r])r�r�r�r�rZ
all_randomr�r�r�r�ZsmallZbigr�r�r�Zs_smallrkr�Zs_bigr:r:r;�rand_aligned_slices?s<


r�cCs$|\}}}}}}t||d|d�S)zMReturn a list of random items for structure 't' with format
       'fmtchar'.rYrK)rb)r8rsr�r�rkr:r:r;�randitems_from_structurejsr�c
Cs(|\}}}}}}	t|||||	t|Bd�S)z:Return ndarray from the tuple returned by rand_structure()�r�r��formatr��flags)r�ND_WRITABLE)
rmr8rsr�r�r�r�r�r�r�r:r:r;�ndarray_from_structureps
�r�cCsP|\}}}}}}t|�}	t|�D]\}
}t�||	|
||�q t|	||||d�S)z>Return numpy_array from the tuple returned by rand_structure())�bufferr�r��dtyper�)�	bytearray�	enumerater5�	pack_into�numpy_array)rmr8rsr�r�r�r�r�r��bufr�r�r:r:r;�numpy_array_from_structurevs�r�cs�|j}|r$t|��|krld|fSnH|gkrH|jdks>�|krld|fSn$t|��\}}|g}|dkrld|fS|�����fdd�tdt����D�}g}|D]2}	t�||	�d}
|
|
kr�d|fS|�	|
�q�|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:)ryr�r�Zmemr:r;rz�r{zcast_items.<locals>.<listcomp>�nan)
�nbytesr�r��divmod�tobytesr`r\r5rSr[)Zexporterr8r�r�Zbytelenrr�Z	byteitemsrmr�rnr:r�r;�
cast_items�s(

 r�c#s�td�D]}|gVqtdd�}td�dkr0dnd��fdd	�t|�D�Vtdd�}td�dkrfdnd��fd
d	�t|�D�VdS)z Generate shapes to test casting.r/��r�r�r*rcsg|]}t�d��qS�r��r�ryrk�r�r:r;rz�r{z!gencastshapes.<locals>.<listcomp>csg|]}t�d��qSr�r�r�r�r:r;rz�r{N)r`r)rr�r:r�r;�
gencastshapes�s


r�cCs0tt||d�t||d�t||d��S)z4Generate all possible slices for a single dimension.r*)rr`)rr:r:r;�	genslices�sr�cs�fdd�t|�D�}t|�S)z/Generate all possible slice tuples for 'shape'.csg|]}t�|��qSr:)r��ryr�r�r:r;rz�r{z"genslices_ndim.<locals>.<listcomp>)r`r)r�r�r�r:r�r;�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:r;�rslice�sr�ccstd�D]}t||�VqdS)z.Generate random slices for a single dimension.r�N)r`r�)rr�rkr:r:r;r��sr�c#spt|�D] }t�fdd�t|�D��Vqt|�D] }t�fdd�t|�D��Vq2tdd�t|�D��VdS)z)Generate random slice tuples for 'shape'.c3s|]}t�|�VqdSr��r�r�r�r:r;r��r{zrslices_ndim.<locals>.<genexpr>c3s|]}t�|dd�VqdS)T)r�Nr�r�r�r:r;r��r{css|]}tddd�VqdSr�)r�r�r:r:r;r��r{N)r`r])r�r�Z
iterationsrkr:r�r;�rslices_ndim�s
r�ccs0t|�}|durt|�n|}tt||��VdSr�)r]r\r)�iterabler�Zpoolr:r:r;�rpermutation�src
Cs�z|��}Wnttfy*|��}Yn0t|t�rD|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�r}rr�r��printr�r��
suboffsetsr�r��sys�stdout�flush)�ndrWr�r�r:r:r;�
ndarray_print�s

��rr�rd�r�z%struct module required for this test.z%ndarray object required for this testc@s�eZdZdd�Zddd�dd�Zd�dd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Zd=d>�Z d?d@�Z!dAdB�Z"dCdD�Z#dEdF�Z$dGdH�Z%dIdJ�Z&dKdL�Z'dMdN�Z(dOdP�Z)e*j+Z+dQdR�Z,dSdT�Z-dUdV�Z.dWdX�Z/dYdZ�Z0d[d\�Z1d]d^�Z2d_d`�Z3dadb�Z4e5j6dcdd��Z7dedf�Z8dgdh�Z9didj�Z:dkdl�Z;dmdn�Z<dodp�Z=dqdr�Z>dsdt�Z?dudv�Z@dwdx�ZAdydz�ZBd{d|�ZCd}d~�ZDdd��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPd�d��ZQd�d��ZRe*jSd�d���ZTd�S)��TestBufferProtocolcCst�|_dSr�)Zget_sizeof_void_p�
sizeof_void_p��selfr:r:r;�setUp�szTestBufferProtocol.setUpF)�sliced�castc
%
s<�	rt�	���n�s t���n��d��jrΈ��d�d}td��D]:}
�	|
dkr\q��|
dkrH|�|
�	|
d7}qH|gdd�t�d�D���j}�ddkr�|}|gt�dd������j|���	�j
����	�j����	�j�����j
|���	�j����	�jt�	���
�r@��sR��	�jt�����	�jt���t�t��szt���r���r����n���}��	|����s�dS|�s�t�}d}t�	�D]�}zNt�|�}t�|�}t|t��r�tj�g|�R�}nt��|�}|�|�Wn t�y0d}Y�qBYn0��	||��q�|�s���	���|�t��}d� dd	�|D��}��	|�!�|���r��nd
}t"��}dD�]�}�}|dk�r�t#�d
��r�t#�d��rt$|�	�}t|�	|t%d�}n0t#�d
��rt#�d��r|dk�rt|�	|d�}t&�t'|����	���|���(t)�|���dk�rP�q�t|�}|�rbdnt*}t+�dt,����	t��|�����fdd�t|�D�}t|d�dk�r�dd�|D�}t|�	|�d�} ��	t| �t���t����}!��	|!��t��jdd�}!��	|!��t��jdd�}!��	|!��t+�dt,����	t��|�����fdd�t|�D�}t|d�dk�r�dd�|D�}t|�	|t%B�d�} ��	t| �t���t��jdd�}!��	|!��t+�d
t,����	t��|�����fdd�t|�D�}t|d�dk�r,dd�|D�}t#�d��r<t%nd}"t|�	|"|B�d�} ��	t| �t���t��jd
d�}!��	|!���q�t���r8zt��}Wnt�y�YdS0t�t��rȈjn��|f���������	�
��fdd�	}#|#|�|�-��}$|#|$dd�Wd�n1�s&0Y|��dS)Nr:rr*cSsg|]}d�qS)r�r:r�r:r:r;rzr{z-TestBufferProtocol.verify.<locals>.<listcomp>Tr=css|]}d|VqdS)z%02xNr:rxr:r:r;r�Tr{z,TestBufferProtocol.verify.<locals>.<genexpr>r�r��F�Arrr��r�r�r��r�r�csg|]}t���|���qSr:�r5Zunpack_fromr���contigr8r�r:r;rzs�cSsg|]}|d�qS�rr:r�r:r:r;rz�r{�r�r�r���ordercsg|]}t���|���qSr:rr�rr:r;rz�s�cSsg|]}|d�qSrr:r�r:r:r;rz�r{csg|]}t���|���qSr:rr�rr:r;rz�s�cSsg|]}|d�qSrr:r�r:r:r;rz�r{cs���|j����|j����|j����|j����|j|���|j����|jt	����	rn�s���|j
t	�
����|jt	����dkr�dnt��}��t|�|��rˆ�
�n���}��|����|��dS)Nrr*)�assertIsrV�assertEqualr�r�r��readonlyr�r�r]r�rr\rr�)rC�expected_readonlyr�rep)�ex�expected_lenr8r�rar��resultrr�rr�rr:r;�check_memoryview�sz3TestBufferProtocol.verify.<locals>.check_memoryview)r$).r�r\rZ
assertGreaterr`rr~r!rVr"r�r�r�r#r�r�r]r�r}rrwrr�r�r��get_pointerr�r5r6�extend�BufferErrorrL�join�hexr��
is_contiguousr��
ND_FORTRAN�get_contiguous�
PyBUF_READ�
assertTrue�
cmp_contigr��py_buffer_to_contiguous�
PyBUF_FULL_RO�
toreadonly)%rr(rVr�r8r#r�r�r�rarrZ
suboffset0rZstride0r%rZbuf_errr�Zitem1Zitem2rWrCrZffZ	flattenedr �expectedZtransrj�roZinitlst�yZcontig_bytesrr)Zmmr:)
rr&r'r8r�rar�r(rr�rr�rr;�verifys




�
���
����
&
,zTestBufferProtocol.verifycCsldd�}|jr||t�s|||t�r(|jr|||t�r8|jr|||t�rH|jr|||t�sX|j	s|||t
�sh|jr|||t�s�||t�r�|j
tt||d�dSt|t�s�t|j�r�|��}nt|td�}|��}||t�r�dn|j}|j}	|j}
|j}||t��sd}	|��}||t��sd}||t��r$|jnd}||t
��r:|jnd}
t||d�}|j|||
|	||||
||d�
dS)	NcSs||@|kSr�r:)�req�flagr:r:r;�match�sz/TestBufferProtocol.verify_getbuf.<locals>.match��getbufFr=r*r:)	rVr�r8r#r�r�r�rar)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,rr}rwr�rr6r�r�r�r�r�r;)rZorig_exr&r<rr>rarr9r8r�r�r�r�r:r:r;�
verify_getbuf�sh����������	�	���

�z TestBufferProtocol.verify_getbufcs�tttttttttt	t
ttt
tf}dd�td�D�dfgd�dfgd�dfdd�td�D�d	ff}ggd
fgd�gd
fdggd
fdgdgd
fdgdgd
fdgdgd
fddggd
fddgddgd
fddgddgdfddgddgdff
}d
ttttBtttBf}d
ttttBf}|D�]�\}}t�|��|D�]x\}}	}
�fdd�|	D�}	|
�9}
|D�]J}|	�rl|t@�rl�qR|�s�|t@�r��qR|�r�|n|d
}t|||||	|
d�}
|�r�|
ddd�nd}t|
�}|�r�t|�}|
jd
k�s�|
jdk�r|�r|	�r|�||
�|�r.|jdk�r.|�r.|	�r.|�||�|D]h}|D]\}|�|
|
||B�|�|
|||B�|�r:|j||||Bdd�|j||||Bdd��q:�q2�qR�q(�qgd�}t|dgtd�}|�tt|�tdgdg�}t|td�}|jtt|td�|jtt|td�|jtt|td�|jtt|td�|jtt|td�|jtt|td�t|td�}gd�gd�fD]�}d
tfD]p}t|||tBd�}|�t |d��|�t |d ��|D]2}t||d�}|�t |d��|�t |d ���q��q|�qpdS)!NcSsg|]}|drdnd�qS)rTFr:�ryrWr:r:r;rz
r{z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>�r�r*rr
r�r�r�r4r3�	rd�rOrrcSs$g|]}|drd|nd|�qS)rr%r$r:rNr:r:r;rz
r{rr)r*r
r*r�rRr�r���r
r����r*r3csg|]}|��qSr:r:r�r�r:r;rz(r{)r�r�r�r�r�T)r�r�r�rQr?)r*rOr*)r4rr4rr�)!rHrIrJ�PyBUF_SIMPLErBrDrF�
PyBUF_FULLr6�
PyBUF_RECORDS�PyBUF_RECORDS_RO�
PyBUF_STRIDED�PyBUF_STRIDED_RO�PyBUF_CONTIG�PyBUF_CONTIG_ROr`r�r0�ND_PILrArKr5�calcsizerrLr�r"rM�ND_GETBUF_FAILrLr,r3r/)r�requestsZ	items_fmtZ	structureZndflagsZ
real_flagsrmr8r�r�r�r�Z_items�ex1�ex2�m1�m2r<�bitsr&�baserr r:r�r;�test_ndarray_getbuf�s��

�
���

�$��z&TestBufferProtocol.test_ndarray_getbufc
	Cs�tdgdg�}tdgdgtd�}t|j|jfD�]H}|�t|hd��|�t|gd��|�t|gd��|jt|gd�dhd	�|jt|gd�dgdhd
�|jt|gd�dggd�|jt|dgdgid�|jt|dgdgid
�|jt|dgdgid�|jt|dgdgdgtd�|jt|dggtd
�|jt|gdgd	�|jt|dgdgdd�|jtj	|dgdgdd�|jt|ddgdgdd�|jt|gd�dgdd�t
d}|jt|dg|dg|d	�|jt|dgdgd	�|jt|gd�dgd	�|jt|dgddgd	�|jt|gd�ddgdd�|jt|gd�dgdgd
�|jt|dgdgddgd
�|jt|ddgddgdgd
�|jt|gd�dgdgdd�|jt|gd�dgdd�|jt|gd�dgddd�|jt|gd�dgd d�|jtj	|dgdgd!d�gd"�}|jt||ddgd#d$gd%d&�|jt|t�d'd(�|jt|dgdgt
d�|�t|dg�q.|jttd)td*�tdgdgtd�}|jt|jdgdgtd�td)�}|�t|jdgdg�|�t|j�tdgdg�}|�dgdg�t|�}|�t|jdgdg�|�t|j�|��|��|�t|j�~|�ttigd��|�ttd)i�tttd+��dgd+d	�}|�tt|d%g�tttd,��ddgd	�}|�tt|gd-��|�tt|ddg�|�tt|d#dg�|�tt|d.dg�tgd�dgdd�}t|td*�}|�t|j�tgd�dgdd�}t|�}	t|	�}|�t|j�td/gdgd0d�}|�t|j�t
}ttt|��dg|d	�}|�t|j�tdgdgd	�}|�ttddddd%�|�tt|d1d2�|�tt|d.d2�|�tt|td3�|�tt|td4�|�tt|td5�|�tt|d6d7�tdgdgd	�}|�ttddddd%�|�tti|�|�tt|i�tdgdgd	�}|�ttddddd%�|�ttid7�|�tt|d8�dS)9NrQr*�r�>r*rr
)r*rrI)r*r�r
r��r*rr
r
r�r��r�r�rrU)r�r@�r�r�r�ZXXXri�r�rr
rZQLr�rIr"�r*rr
r�r�rG�r*rr
r��r�r�r�r�)r�r�r�r=z@#$)
r*rr
r�r�r�r4r3rQrd���rSr�)r�r�r�r�r��123r?r�rO)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*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*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�xyzr��u �Z�r��)r�ND_VAREXPORT�pushrLrr0r^r�r5r7ZND_MAX_NDIM�
OverflowErrorr�rWr,rA�poprL�releaser*r~r`rVr�memoryview_from_bufferr1r2�UnicodeEncodeErrorr4r/)
rrZndmr
rrmrCr&rbrcr:r:r;�test_ndarray_exceptionsfs��������z*TestBufferProtocol.test_ndarray_exceptionscCs�ttd��D]�}dgd}tgd�dgtd�}t|�|d<tdd�D]$}|jgd�dgd�t|�||<qDtd�D]}|||��qr|�t|j	�~qdS)Nr�rrkr
rUr*r�)
rr`rrzrLr{r~rLr,r})rZpermrCrrr:r:r;�test_ndarray_linked_lists
z+TestBufferProtocol.test_ndarray_linked_listcCsLtd�D]>\}}}t�|�}t|d|d�}|j|d||dddd|d�	qdS)Nrr:rT�rVr�r8r#r�r�r�ra)rtr5r_rr;)rr8Zscalarrkr�rr:r:r;�test_ndarray_format_scalars
�z-TestBufferProtocol.test_ndarray_format_scalarc
Csltdd�}t|�D]T\}}}t�|�}dtfD]6}t||g||d�}|j|d||dd|f|f|d�	q.qdS)Nr*rdrrTr�)rrtr5r_r^rr;)rrrr8rmrkr�r�rr:r:r;�test_ndarray_format_shapes


�z,TestBufferProtocol.test_ndarray_format_shapecCs�tdd�}t|�D]�\}}}t�|�}tdd�D]�}|dkr>q0t|dd|��g}||g}|dkrp||dnd}	dtfD]>}
t|||||	|
d�}|j|d||dd|||dd|�d�	q|q0qdS)	Nr*����r�rr�Tr�)	rrtr5r_r`r\r^rr;)rrrr8rmrkr�r�r�r�r�r�rr:r:r;�test_ndarray_format_strides*s$


��z.TestBufferProtocol.test_ndarray_format_stridescCs@gd�}t|ddd�}t|ttBd�}|�|��t|d��dS)NrPrj)r*r
r�r?)rrDrKr"rr�)rrmr&rr:r:r;�test_ndarray_fortran?sz'TestBufferProtocol.test_ndarray_fortrancCs^td�D�]N}dd�t|�D�}t|�}t|�D�]$}t|�\}}}t�|�}dtfD]�}	|dkrl|	tkrlqVt||||	d�}
t|||d�}t	||�}|j
|
d||d||||d�	t|��rt|||d	�}
t|
tt
Bd
�}
|�|
jdk�|
��}|j
|d||d||||d�	t||||	tBd�}
t|||d�}t||�}|j
|
d||d||||d�	qVq0qdS)
Nr�cSsg|]}tdd��qS)rrdr�r�r:r:r;rzGr{z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>rrr�Tr�rr?r:r)r`r�rrpr5r_r^rr�r�r;rwr]rKr3r�rr0r�)rr��shape_trrr�r8rmrkr�r�rr�rar&�mvr:r:r;�test_ndarray_multidimEsJ

�
��
�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�gd�}t|t|�gdtd�}|�tj
|jd	d
�|�t|jdd�t|�}|�||�|�t|jd	d
�|�t|jdd�gd�}t|t|�gd
td�}|�t|jd	d
�|�tj
|jdd�dS)Nr*r�r3rUr)�r*rr
r�r�r�r4r3rrri,)r���)�r*rrj)r�r�ZLQ)s1r�)rrLr�__setitem__rLr"r��__delitem__r|�__getitem__�
IndexErrorr\r5r7r�)rrr�rmr:r:r;�test_ndarray_index_invalidrs: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:rU.rr3rQr�)	rr�rLr"rrLrr�r�)rrr�rWr:r:r;�test_ndarray_index_scalar�s"z,TestBufferProtocol.test_ndarray_index_scalarcCsRtttd��ddgtd�}t|td�}|�t|jd�|�t|jtddd��dS)	Nr3rr�rUr?r*r
r�)	rr~r`r�r\rLr,r�r�)rr&rr:r:r;�test_ndarray_index_null_strides�sz2TestBufferProtocol.test_ndarray_index_null_stridescCstd�D]�\}}}t|dg|d�}tdd�D]}|�||||�q,|�t|jd�|�t|jd�t|�rt|�}|�||�tdd�D]}|�||||�q�|�t|jd�|�t|jd�qtd�D]�\}}}t|dgt	|d�}t|t
tBd�}tdd�D]}|�||||��qt|�r�|��}|�
|�|�t�tdd�D]}|�||||��qNq�gd�}t|dgd�}t|td�}tdd�D]}|�||||��q�gd�}t|dgd�}t|td�}tdd�D]}|�||||��q�dS)	Nr�rr����rr?ror�)rtrr`r"rLr�r�rwrLr�r\rKrr!�__eq__�NotImplementedr]rV)rr8rmrkrrr�r&r:r:r;�!test_ndarray_index_getitem_single�sBz4TestBufferProtocol.test_ndarray_index_getitem_singlecCs�td�D]�\}}}t|dg|td�}td�D]}|||<|||<q,|�|��|�|�t|jd|�|�t|jd|�t	|�s�qt|dg|td�}t
|�}|�||�td�D]}|||<|||<q�|�|��|�|�t|jd|�|�t|jd|�qtd�D]�\}}}t|dg|td�}tdd�D]&}||d||<||d||<�q(|�|��|�t	|��sn�qt|dg|td�}t
|�}|�||�tdd�D]&}||d||<||d||<�q�|�|��|��qdS)Nr�rr�r�r�r*)rtrr�r`r"rrLr�r�rwrL)rr8rmZsingle_itemrrr�r:r:r;�!test_ndarray_index_setitem_single�sD


z4TestBufferProtocol.test_ndarray_index_setitem_singlec

Cs�d}t|�}t|�D�]�}t|�\}}}dtfD�]�}t||||d�}t||�}	t|d|d�D]�}
|�|	|
||
���t|d|d�D]b}|�|	|
|||
|���t|d|d�D](}|�|	|
||||
||�q�q�qdt||||t	Bd�}t
||�}	t|d|d�D]�}
|�|	|
||
���t|d|d�D]d}|�|	|
|||
|���t|d|d�D]*}|�|	|
||||
||��q��q\�q,q0qdS)N�rr
r�rrr*r)r�rrpr^rr�r`r"rr0r�)
rr�rrr�r8rmrkr�rrarr��kr:r:r;�#test_ndarray_index_getitem_multidims.
 *�
 z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCs�tddd�}|�ttdt��t|�}|�||�|�ttdt��td�D]�\}}}t|dg|d�}t|�D]&\}}|�|||�|�	||v�qnt
|�rLt|�}t|�D]&\}}|�|||�|�	||v�q�qLdS)Nr*r:r�z1 in ndz1 in mvr�r)rrLr�eval�localsrLr"rtr�r3rw)rrr�r8rmrkrr�r:r:r;�test_ndarray_sequence9sz(TestBufferProtocol.test_ndarray_sequencec	Cs�gd�}t|dgtd�}t|�}|�t|jtddd�|�|�t|jtddd�|�t|dgtd�}t|dgtd�}t|td�}|�t|jtddd�|�t|dgdtd�}t|�}|�t	|j
tddd��|�t	|j
tddd��t|d	d
gdtd�}t|�}|�t	|j
tddd�tddd�f�|�t	|j
tddd�tddd�f�|�t|j
d�|�t|j
dtddd�f�|�t|j
tddd�if�|�t|j
tddd�tddd�f�|�t|j
d�t|dgdtd�}t|dgd
d�}t|�}t|�}|�t	|jtddd�|dd��|�|�
�|�|�t	|jtddd�|dd��|�|�
�|�t|dgdtd�}t|dgdd�}	t|�}t|�}|�t	|jtddd�|dd��|�|�
�|�|�t	|jtddd�|dd��|�|�
�|�t|d	d
gd
td�}t|dgd
d�}t|�}t|�}|�t	|jtddd�|dd��|�|�
�gd�gd�g�|�t|jtddd�|dd��t|dgd
td�}t|dgd
d�}t|�}t|�}|�t	|jtdd	d�|dd��|�|�
�|�|�t	|jtdd	d�|dd��|�|�
�|�|�tttddd	�i�|�ttdd�|�t	ttddd�d
�t|dgd
td�}
|�t|
j�t|dgdd�}t|td�}
|�t|
j�dS)Nr�r3rUrr*r?rrrr�z@%$rrrr4rp)r�r�r4r3z###########)rr�rLrLrr�r�rJr,r�r�rr"rr�r^�add_suboffsetsrV)rrm�xl�mlr&�xrrr��mr�yrrWr:r:r;�test_ndarray_slice_invalidLs�
�
�
�"""""
�""z-TestBufferProtocol.test_ndarray_slice_invalidcCsgd�}t|dgdtd�}t|dgdd�}|dd�|dd�<|�|��|�t|�}t|�}|�||�|�||�|dd�|dd�<|�|��|�t|ddgdtd�}t|ddgdd�}|d	d
�dd�f|d	d
�d
d
�f<|�|��t|ddg��dS)NrPrOrrrrQr�r
r*r)rr�r"rrLr�)rrmrWr:r�r�r:r:r;�test_ndarray_slice_zero_shape�s$z0TestBufferProtocol.test_ndarray_slice_zero_shapec
Csd}t|�}t|�}t|�D]�}t|�\}}}t�|�}dtfD]�}	t||||	d�}
t||�}t	||�D]�}d}
zt
||�}Wn*ty�}z|j}
WYd}~n
d}~00d}z|
|}Wn*ty�}z|j}WYd}~n
d}~00|s�|
r�|�
||
�qh|�|��|�qhq@qdS�Nr�rr)r\r�rrpr5r_r^rr�r�r��	Exception�	__class__r!r"r)rr�r�rrr�r8rmrkr�r�rrar��listerrr�e�nderr�ndslicedr:r:r;�test_ndarray_slice_multidim�s.

z.TestBufferProtocol.test_ndarray_slice_multidimcCs2d}t|�}t|�}t|�D�]}t|�\}}}t�|�}t|||d�}	|	��t|||d�}
|
��t|
�}t	||�}t
||�D]�}
d}zt||
�}Wn*ty�}z|j
}WYd}~n
d}~00d}z|	|
}Wn*ty�}z|j
}WYd}~n
d}~00|�s|�r|�||�q�|�|��|�q�qdS)N)rr
r�rr)r\r�rrpr5r_rr�rLr�r�r�r�r�r!r"r)rr�r�rrr�r8rmrkr�rr&r�rar�r�rr�r�r�r:r:r;�'test_ndarray_slice_redundant_suboffsets�s4

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetscCstd�D�]\}}}td�D�]�}td�D�]�}dtfD�]�}|tB}t|dg||d�}t|dg||d�}	t|	�}
d}d}|dd�}
z0|
|}|
|}|
||
|<t|�t|�k}Wn*ty�}z|j}WYd}~n
d}~00d}z||||<Wn,t�y"}z|j}WYd}~n
d}~00|�r8|�	|t
�n|�|��|
�|�	||�t
|��s`q8d}z|
||
|<Wn,t�y�}z|j}WYd}~n
d}~00|�r�|�	|t
�q8|�|
��|
�|�|
|�|�	||�|j|
|	|j|d|j|j|j|��d�	q8q*qqdS)Nr�rrFr�)rtr�r^r�rrLr\r�r�r!r�r"rrwr;r�r�r�r�)rr8rmrk�lslicer�r�rrr&r�ZlsterrZdiff_structureraZlvalZrvalr�r�Zmverrr:r:r;� test_ndarray_slice_assign_single�sX
�z3TestBufferProtocol.test_ndarray_slice_assign_singlec
Cs*d}t|�}t|�}t|�D�]}t|�\}}}dtfD]�}tt�D]�}t||�\}	}
t||||t	Bd�}t
||�}d}
zt|||	|
�}Wn*ty�}z|j
}
WYd}~n
d}~00d}z||
||	<Wn*ty�}z|j
}WYd}~n
d}~00|�s|
�r|�||
�qD|�|��|�qDq8qdSr�)r\r�rrpr^r`�
ITERATIONSr�rr�r�r�r�r�r!r"r)rr�r�rrr�r8rmrkr�r�r�rrar�r(r�r�r:r:r;�"test_ndarray_slice_assign_multidim2s2�
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc
Cs8tt�D�](}tdD�]}t�|�}t|dttd�}|�t	|��t
||�}t|||�}|��}t
|�}t|�r�|��}	|�|	|�|ddkr�t|||td�}
|
��}|�||�t
|
�}t|�r�|�||
�|��}	|�|	|�tr|d}d|vr�qt|||�}
|j|d|
j|d|
j|
j|
j|
��d	�	qqdS)
Nr>T�r�r�rrrir
Fr�)r`r�rPr5r_r��MAXDIM�MAXSHAPEr3r�r�r�rrLrwr"r^r�r�r;r�r�r�r�)rrkr8r�rsrmrWZxlistr�Zmvlistr:Zylistr��zr:r:r;�test_ndarray_randomSsB
�
�z&TestBufferProtocol.test_ndarray_randomcCs�tt�D]�}tdD]�}t�|�}t|dttd�}|�t	|��t
||�}d}zt|||�}Wn*ty�}z|j
}WYd}~n
d}~00|�|�trd}	zt|||�}
Wqty�}z|j
}	WYd}~qd}~00qqdS)Nr>Fr�)r`r�rPr5r_r�r�r��assertFalser�r�r�r�r�r3r�r�)rrkr8r�rsrmr�rWr�Z	numpy_errr:r:r:r;�test_ndarray_random_invalid|s*
�

z.TestBufferProtocol.test_ndarray_random_invalidc
Cs2tt�D�]"}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|vsd|v�r�qt|
||�}t|||	�}||||<t|��st|	��s|j|d|j|d|j|j|j|�
�d	�	|j|
d|j|d|j|j|j|�
�d	�	qqdS)
Nr>r�Tr�rrriFr�)r`r�rPr5r_r�r�r�r�r3r�r�r�rrLr"r^r�r�r�r;r�r�r�r�)rrkr8r�r�r�r�r�ZtlZtrZlitemsZritemsr�r�ZxllistZxrlistr�r�Zylr�ZyllistZyrlistZzlZzrr:r:r;� test_ndarray_random_slice_assign�sb

�



�
�z3TestBufferProtocol.test_ndarray_random_slice_assigncCsvgd�}t|ddgtd�}t|�}|�|jt@�|�|j|�|�|jd�|�|j	�|�|j
�|�|j�dS)NrPr
r�rU)rr�)rr^r3r�r!rVr"rr�rCrErG)rrmrr&r:r:r;�test_ndarray_re_export�sz)TestBufferProtocol.test_ndarray_re_exportc	Cs�dtfD]�}tgd�dg|d�}t|�}|�||�|�|��g�|�|��g�tgd�gd�|d�}|�|��g�tgd�gd�|d�}|�|��gggg�tgd�gd�|d�}|�|��ggggggggggggg�qdS)NrrkrU�rr
r
�r
rr
)r
r
r�r^rrLr"r�rr�rr�r:r:r;�test_ndarray_zero_shape�s
�z*TestBufferProtocol.test_ndarray_zero_shapecCsddtfD]V}tdgdgdg|d�}t|�}|�||�|�|��gd��|�|��gd��qdS)Nrr*r�rm)r*r*r*r*r*r�r�r:r:r;�test_ndarray_zero_strides�sz,TestBufferProtocol.test_ndarray_zero_stridescCs>tttd��dgdd�}|�|jd�|�|��gd��dS)N�r
r4rl)r4r3rQ)rr~r`r"r�r�rrr:r:r;�test_ndarray_offset�sz&TestBufferProtocol.test_ndarray_offsetcCs>dtfD]0}tttd��dg|d�}|��}|�||�qdS�Nrr
rU)r^rr~r`rr")rr�rrCr:r:r;�#test_ndarray_memoryview_from_buffersz6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsPdtfD]B}tttd��dg|d�}td�D]}|�||t||g��q,qdSr�)r^rr~r`r"r*)rr�rrr:r:r;�test_ndarray_get_pointer
sz+TestBufferProtocol.test_ndarray_get_pointercCs\tttd��gd�d�}t|ttBd�}|�|��|���t|�}|�|��|���dS)Nr�)rrr�r�r?)rr~r`rJrKr"rrL)rr&rrCr:r:r;� test_ndarray_tolist_null_stridess
z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs�|�tdd��tttd��ddgd�}tttd��ddgd�}|�t||��tdgdgdd	�}|�t|d
��|�td
|��dS)Nrts456rOr
r�r�r*rrrO)r�r4rr~r`r3)rrWr:r:r:r;�test_ndarray_cmp_contigsz*TestBufferProtocol.test_ndarray_cmp_contigcCs�t�dgd��}t|�}|�tt|�tttd���}tttd��dgd�}|�t|�t|��tttd��ddgd�}|�t|�t|��tttd��gd�d�}|�t|�t|��tt	ttd��ddgd��}tttd��ddgt
d�}|�t|�t|��tt	ttd��gd	�d��}tttd��gd	�t
d�}|�t|�t|��tttd���}tttd��gd
�td�}|�t|�t|��tttd��gd
�dd�}|�t|�t|����dS)NrrkrOr�r
r�)r
rrrU�rr
r�rrr
r)
rJrrLr��hashrQr~r`r"r�r0r^r�)rrErrr:r:r;�test_ndarray_hash%s*z$TestBufferProtocol.test_ndarray_hashc
CsTtttttttttt	t
tf}|�t
tidt�tdddtd�}dD]*}|D] }t|||�}|�||���qJqBtdgdgdtd�}dD]&}|D]}t|||�}|�|d	�q�q�tttd
��gd�dtd�}dD]&}|D]}t|||�}|�|d	�q�q�dtfD]�}tdgdgd|tBd�}|��}dD]*}|D]}t|||�}|�||��q.�q&tgd
�dgd|tBd�}|��}dD]*}|D]}t|||�}|�||��q��qxq�tgd
�dgdgtd�}|��}dD].}ttfD]}t|||�}|�||��q֐q�|ddd�}|��}dD]J}|D]>}zt|||�}Wnt�yJY�qYn0|�||��q�qttd��}dtfD�]�}t|ddg|tBd�}t�r�tt|�ddgd|dk�r�dndd�}	|tk�r�tt|ddg�ddgtd�}
|
��}n|��}|D]�}zt|d|�}Wnt�y&Y�q�Yn0|�||�tdd�|D�ddgtd�}|�t|�t|��t�r�|�||	jdd���q�|dk�r�tt|ddg�ddgtd�}
nt|ddgtd�}
|
��}tttttfD]�}zt|d|�}Wnt�yY�q�Yn0|�||�tdd�|D�ddgttBd�}|�t|�t|��t�r�|�||	jdd���q�|tk�r�t|ddgtd�}
|
��}n|��}tttttfD]�}zt|d|�}Wnt�y�Y�q�Yn0|�||�tdd�|D�ddg|tBd�}|�t|�t|��t�r�|�||	jdd���q��qttttd��ddgttBd�}t|dt�}|�||���tdd�|D�ddgtd�}|�t|�t|��t|dt�}tt|ddg�ddgtd�}
|�||
���td d�|D�ddgttBd�}|�t|�t|��t|dt�}|�||���td!d�|D�ddgtd�}|�t|�t|��dS)"NrrQr:rrrr*rr{r3�rrr4rrkr
rrrmr�rOr�rUrr�)r�r�r�r cSsg|]}|�qSr:r:r�r:r:r;rz�r{zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>rcSsg|]}|�qSr:r:r�r:r:r;rz	r{rcSsg|]}|�qSr:r:r�r:r:r;rz	r{cSsg|]}|�qSr:r:r�r:r:r;rz*	r{cSsg|]}|�qSr:r:r�r:r:r;rz1	r{cSsg|]}|�qSr:r:r�r:r:r;rz7	r{)rHrIrJrVrWr6rXrYrZr[r\r]rLrr5rr�r"r�r~r`r0r,r�r�r�rLZtostringr^)
rrarr ZrequestrrZndbytesraZnarWr8r:r:r:r;�test_py_buffer_to_contiguousJs����B�
�

�� 

�  z/TestBufferProtocol.test_py_buffer_to_contiguouscCs|dgfgd�dgfttd��gd�fg}|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�rR|gn|}|j
|ddd	d
d|jgd|d�	q,|D]�\}}t||td�}t|�}|�|j�|�|j�t|�}t||dd�}t||�}|j
||dd	d
||||d�	t|�}	|j
|	|dd	d
||||d�	�q||dd�D]v\}}t||td�}t|�}t|�}t	||�}|j
||dd	d
|||j|d�	t|�}	|j
|	|dd	d
|||j|d�	�q"|�ttdd�|�tti�tgd�dgd�}t|td�}
|�tt|
�t|ttBd�}
|�tt|
�tdgddgddd�}
|�tt|
�|�t|
j�|�tt|
td�|�tt|
td�|�tt|
ddd�td�dS)NrQrkr
r�r�r�r*r�rTr�r?r:r=r�r*rUr�9rWr"rrr�)r~r`rrLr3rCrGr\r�r�r;r]rKr"r�rrVr�r�r�r0rEr�r^rLrr,r�r1r2)rZitems_shapermr�r&rCr�r�rarerr:r:r;�test_memoryview_construction:	s�(
���
�
��

��
z/TestBufferProtocol.test_memoryview_constructioncCs�gd�}gd�gd�gd�fD]4}t||d�}|�|j�t|�}|�t|jd�qtdd�D]<\}}}td�}|�|�}|�|�	�d�|�|�
�g�q^dS)	Nrkr�r�r�r
r*rLr{)rr3rCrLrLrrrtr"r�r)rrmr�r&�msrcr8rkrCr:r:r;�test_memoryview_cast_zero_shape�	s
z2TestBufferProtocol.test_memoryview_cast_zero_shapecCs�|j}tj}d}d}ttd��}|td�||d|��t|ddgdd	�}|t|�||d|��t|gd
�dd	�}|t|�||d|��dS)NzPnin 2P2n2i5P PZ3nr3r{r*rr�rr)rrrr
)�check_sizeofrZcalcvobjsizer~r`rLr)rZcheckZvsizeZbase_structZper_dimrmrEr:r:r;�test_memoryview_sizeof�	sz)TestBufferProtocol.test_memoryview_sizeofcCs�Gdd�dt�}Gdd�dt�}dd�}|d�|d�dtd	�d
tdd�gd
�hd�ddidddddtddtd�td�dddd|dd�g}tdd�D�]6\}}}t|dg|td�}t|dg|td�}	t|�}
t	�
||	d|�||
d<|�|
d|	d�t	�|�}d|v�rq�|D]�}d}
zt	�
||	||�Wnt	j
�yHt	j
}
Yn0d}z||
d <Wn0ttf�y�}z|j}WYd}~n
d}~00|
�s�|�r�|�|
d�|�|d�n|�|
d |	d ��qq�dS)!Nc@seZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTcSs
||_dSr���val�rr�r:r:r;�__init__�	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__cSs|jSr�r�rr:r:r;�__int__�	szETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)�__name__�
__module__�__qualname__r�r�r:r:r:r;�INT�	sr�c@seZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXcSs
||_dSr�r�r�r:r:r;r��	szFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__cSs|jSr�r�rr:r:r;�	__index__�	szGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r�r�r�r�r�r:r:r:r;�IDX�	sr�cSsdS)Nr4r:r:r:r:r;r�	r{z;TestBufferProtocol.test_memoryview_struct_module.<locals>.frQy������@@z-21.1gffffff(@r�rrk>r�r�r�r4r3r:�rQTF�asabcrE�abccSs|Sr�r:)rWr:r:r;�<lambda>�	r{zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rdrLrrrr*)�objectrr	�Ellipsisr�rtrr�rLr5r�r"r_r7rr�r�ZassertIsNot)rr�r�r�valuesr8rmrnr&rrCr�r�Z
struct_errZmv_errr�r:r:r;�test_memoryview_struct_module�	sH�

z0TestBufferProtocol.test_memoryview_struct_modulecCs>tgd�dgdgd�}|�|j�t|�}|�t|jd�dS)Nrkr
rr�r
)rr�rCrLrLrr)rr&r�r:r:r;�!test_memoryview_cast_zero_strides
sz4TestBufferProtocol.test_memoryview_cast_zero_stridesc	Cs0tD]�}td�rd|n|}t�|�}tD]^}td�r>d|n|}t�|�}tttd��d|g|d�}t|�}|�t	|j
|d|g�q*qtd�D]�\}}	}
t|	dg|d�}t|�}td�D]T\}}
}
t|�s�|�t
|j
|d|g�q�t|�s�t|�s�|�t	|j
|d|g�q�q�t�d�}t�d�}tttd|��dd|gdd�}t|�}|jt	|j
dd|gdd�tttd	��gd
�d�}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	��gd
�d�}t|�}
|jt	|
j
did�tttd	��d	gd�}t|�}
|jt|
j
ddgd�|jt
|
j
ddgd�|jt
|
j
dgd�d�|jt
|
j
dgd�d�|jt	|
j
dgd �d�ttd!d"�td#�D��gd$�d�}t|�}
|jt	|
j
d%gd&�d�tttd'��d'gd%d�}t|�}
|�t
|
j
d%dgd'�ttd(d"�td#�D��d#gd�}t|�}
|jt	|
j
d%gd&�d�ttd)d"�td#�D��d#gd�}t|�}
|jt	|
j
dgd&�d�tttd'��d'gd%d�}t|�}tttd'��d'gdd�}t|�}tjd*k�r�|�t	|j
dgd+��|�t
|j
dgd,��|�t
|j
d%gd-��nB|�t	|j
ddd.g�|�t
|j
dgd/��|�t
|j
d%gd0��dS)1Nrr>r/rr*rrr��xror�r
�Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIr�rjZIIrr3rr)r�)rr
r�r�r�r4r�)rr
r�r�r�r4r)rr
r�r�r�r4rWcSsg|]}d�qSr�r:r�r:r:r;rz]
r{zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i�)r
r�r4rRr)rr
r�r�r"cSsg|]}d�qSr�r:r�r:r:r;rzg
r{cSsg|]}d�qSr�r:r�r:r:r;rzl
r{l����)r4r4�Ir,iQiAji��	)�r��r�r3)r�r�r�r�r��)r�r�r/r/r)r�r�r/r3r)�NON_BYTE_FORMATrr5r_rr~r`rLrLrrrtrwr�rurr�r�rr|r�maxsize)rZsfmtZsformatZssizeZdfmtZdformatZdsizer&r�ZsitemsrkZsize_hZsize_drCrrdrer:r:r;�test_memoryview_cast_invalid
s�

��

 "  ������z/TestBufferProtocol.test_memoryview_cast_invalidcslddd�fddd�fddd�ff���fdd	�}t�d
�}tdgd
d�}t|dd
�\}}t|�}|�d�}�j||d
ddd
|d|dd�
t�d
�}tdg||gdd�}t|d
|gd�\}}t|�}|jd
gd�}�j|||d
dddd|dd�
tdd�D],\}	}
}t�|	|
�}t|�}||||
|	�q�tdd�D]4\}	}
}t|
dg|	t	d�}t|�}||||
|	��q2dS)NrcSst|���Sr��r~r��r&r:r:r;r��
r{z9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>rcSsdd�t|���D�S)NcSs g|]}|dkr|dn|�qS)r,r!r:rNr:r:r;rz�
r{�MTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>r�r�r:r:r;r��
r{r
cSsdd�t|���D�S)NcSsg|]}tt|�d��qS)zlatin-1)rQ�chrrNr:r:r;rz�
r{r�r�r�r:r:r;r��
r{c

s�t�|�}�D]~\}}|�|�}||�}�j||d|ddd|gd|dd�
|�|�}	��|	|�|��}�j|	|||dddg|f|dd�
qdS)Nr*Fr0r�T�	rVr�r8r#r�r�r�rar)r5r_rr;r"r)
r&rCrmr8�srcsizerMZto_bytelistrera�m3�Zbytespecrr:r;�iter_roundtrip�
s"

�

�z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtriprrQrr*Tr�r�r�rr:r0rJrLr)
r5r_rr�rLrr;rtrJr�)rrr�r&Z	destitemsZ	destshaperCreZdestsizer8rmrkr:r�r;�test_memoryview_cast�
sB


�

�
�z'TestBufferProtocol.test_memoryview_castcCs�t�D�]�}tdD�]�}|dkr$qdtd�|}t�|�}t|�|}t|�rVdnd}t||�D�]:\}}}	t�|�}
|dkr�|gng}||
g}t|||d�}
t	|
�}t
|
|||d	�\}}|dur�|�t|j
||�qd|d
kr�qdt|||d�}|j
||d	�}t|�}|j}|��}|j||
||d||||dd�
|�
|�}|j
||d	�}t|�}|
j}|
��}|j||
|
|d||||dd�
|j||
|
|d||||dd�
qdqqt�r�Gd
d�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)Nr>r)r=r>rrLrMrrr�r�Tr�c@s eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrWr:N)r�r�r��ctypes�c_long�c_double�_fields_r:r:r:r;�BEPoint�
srr�g33333i@rr*Fr�r:�333333�?r
)r�rPrr5r_r�rurtrrLr�rLrrr\r�rr;r�BigEndianStructurer"rVr�r!r#r�r�r�rr)rZ_tshaperUZtfmtZtsizerrVr8rmrk�sizer�Ztshaper&rCZtitemsrrer�r�rar�Zm4r�pointrdrWr:r:r;�test_memoryview_cast_1D_ND�
s�


�
��



z-TestBufferProtocol.test_memoryview_cast_1D_NDcCs2t�dttdd���}t|�}|�||�|�|��|���|ddd�}|ddd�}|�||�|�|��|���tttd��gd�dd	�}t|�}|�|��|���td
dgdgdd	�}t|�}|�t|j�td
gdgdd	�}t|�}|�t|j�tgd�ddgdd	�}t|�}|�t|j�dS)Nrr�r�rr
i	)rRrr4r
r�rr)rr�)r4rRr9�12345r*r�)r��b�c�d�e�f)	rJr~r`rLr"rrrLr)rrErCr&r:r:r;�test_memoryview_tolists(z)TestBufferProtocol.test_memoryview_tolistcCsHttd��}|��}|�|�d��|��|��}|�|�d��dS)NrQz<memoryz	<released)rLr��__repr__r3�
startswithr~)rrCr�r:r:r;�test_memoryview_repr2sz'TestBufferProtocol.test_memoryview_reprcCs|dD]J}td�}t�|d|dg�}t|�}|�d|�|�d|�|�d|�qtdgdd�}t|�}|�ttdt��dS)	N)rrg���?g@g"@rrsz9.0 in m)	�floatrJrLZassertInrrLrr�r�)rr8�infr&rCr:r:r;�test_memoryview_sequence;sz+TestBufferProtocol.test_memoryview_sequenceccsL|�t��}dVWd�n1s&0Y|�t|j�d|f�dS)Nz#index out of bounds on dimension %d)rLr�r"�strZ	exception)r�dim�cmr:r:r;�assert_out_of_bounds_errorIs
$�z-TestBufferProtocol.assert_out_of_bounds_errorc	CsNtdgdd�}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�dD]@}|jdd��||dfWd�n1�s�0Y�qbdD]@}|jdd��|d|fWd�n1�s�0Y�q�|�t
|jd�|�t
|jd�|�t|jd �|�t|jd!�|�t|jd�|�t|jd�dS)"N�)@rrr:.rrkZiiir4rUr)�@����r3rOr
r��rr)rrrnrR�r�r�)rrrT�r
rTr*�r�r�r�r�r)rr(�rrr�r r )rrLr"rLrr�rr~r`r�r�r)rr&rC�indexr:r:r;�test_memoryview_indexPsF00z(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�t
dD]r}|dk�s|dk�r(�qtgd�dg|td�}t|�}tdd�}d||<|�||d�|�||||��qtgd�dgdtd�}t|�}d|d<|�|dd�tgd �dgdtd�}t|�}d!|d"<|�|d"d!�td#gd"gdtd�}t|�}|�t|jdd$�tttd%��gd&�td�}t|�}t
d�
�D]�\}}|dk�r^�qH|dk�rl�qH|�|�}|\}	}
|d'k�s�|dk�r�dd(dd(}	}
|d)k�r�|�t|jd|	d"�|�t|jdd*�|�t|jd|
��qH|�d�}|�t|jdd+�tttd"��d"gd,td�}t|�}|�t|jdd"�td-gd"gd.td�}t|�}|�t|jdd"�tttd/��dd0gtd�}t|�}d1|d2<|�|dd"d1�d3|d4<|�|ddd3�d5D]@}|jd"d6��d||df<Wd�n1�s�0Y�q�d7D]@}|jdd6��d|d|f<Wd�n1�sD0Y�q|�t	|jd8d�|�t	|jd9d�|�t|jd:d�|�t|jd;d�|�t|jdddg�dS)<Nrrrg�6@r:g�7@.rg33333�8@r4r�rrdrUr)rQrr rRr!r�r3�r>r
rrkr
rr)�1�2�3r�)TFTTr*�xr�r�rorr�rrus��ZxLrr�rOr��*)rr*�+r#r$r%r&r'r(r(r))rr�rLr"rLrr�r~r`r�rPrr!rmrr�rr)rr&rCr8rrrdZ_rangere�lo�hir*r:r:r;�test_memoryview_assign�s�
��




00z)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�dt
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�<|�||�|�||��qBdS)NrOrUrrr*r:r�rR)r�r�r�r��rmr�r
r�rS)rr~r`r�rLrLr�r�r�r�r�rrr^r")rr&rCr�rbrcrdrer:r:r;�test_memoryview_slice�sN
�
�
 �
��((,,z(TestBufferProtocol.test_memoryview_slicecCs�dd�}tdd�D�]�}t|d�D�]�\}}}t|�D�]�}t|�D�]�}t�||�}t�||�}	t|	�}
|�|
|�|�|
��|���|�|
��|���|�t|
�t|��||||	|
|�d}d}z0||}
||}||||<t|
�t|�k}Wn,t	�y&}z|j
}WYd}~n
d}~00d}z|
||
|<Wn,t	�yh}z|j
}WYd}~n
d}~00|�r~|�|t�qD|�s�|�r�|�||�qD|�|
|�|�|
��|���|�|
��|���||||	|
|�qDq6q"qdS)Nc	Ss�t|�D]�\}}||}||}|�||�|||<||krV|�||�|�||�n|�||�|�||�|||<|�||�|�||�|||<|||<qdSr�)r�r"�assertNotEqual)	ZtestcaserErrC�
singleitemrrkZai�mir:r:r;�cmptest,sz9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr*r�rJ)
r`rtr�rJrLr"rr�r\r�r�r!r�)rr;rr8rmr9r�r�rErrCZ	array_errZhave_resizeZalZarr�Zm_errr:r:r;�test_memoryview_array*sFz(TestBufferProtocol.test_memoryview_arrayc	Cs�t�dgd��}t�dgd��}t|�}t|�}dD]0}|�t||�|�t�|�t||�|�t�q4t|�}|��|�||�|�||�|�||�t|�}t|�}|��|�||�|�||�t|�}|�|gd��tdgdgdt	d�}d	t
d
�f|d<|�t|�|�t�dd
�}t|�}|�||�|�||�t�r�Gdd�dtj�}|dd�}t|�}t|�}|�||�|�||�|�||�|�
t|j�dS)Nrrk)r*rr4)�__lt__�__le__�__gt__�__ge__r"r*zl x d xrr�r�r�uruc@s eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrWr:N�r�r�r�rrrr:r:r:r;r�srr�r�)rJrLr!�getattrr�r~r"r8rr�rrrrLrr)	rrErr��w�attrrrr
r:r:r;�%test_memoryview_compare_special_casesh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)N��@Lrrri�r:rOrUr�rQ)rGrrZLf5sZhf5s)i?���rr)rr�rLr"r��__ne__r8r�r~r`r^)r�nd1�nd2r�rDrr&rCr:r:r;�!test_memoryview_compare_ndim_zero�sj��z4TestBufferProtocol.test_memoryview_compare_ndim_zerocCsXtgd�dgdd�}tgd�dgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tgd�dgdd�}tgd�dgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tgd�d	gdd�}tgd�dgdd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tgd�d	gd
d�}tgd�dgdd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tgd�d	gdd�}tgd�dgdtd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��tgd�d	gdd�}tgd�dgdtd�}t|�}t|�}|�||ddd
��|�|ddd
�|�|�||ddd
��|�|ddd�|ddd��dS)N�����@�����'���r��@hr)rNrOrPrQi�z<iz>h)rNrPrRr
rr�rSz!hz<lrzh  0cz>  h)rrLr"r8r^�rrJrKr�rDr:r:r;� test_memoryview_compare_ndim_one�sl  � �z3TestBufferProtocol.test_memoryview_compare_ndim_onecCs�tddgdgdd�}tddgdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tddgdgdd�}tddgdgd	d�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)
N�rvrrSri|���i?���z= h0cz@   i�rrLr"rTr:r:r;�"test_memoryview_compare_zero_shape$
s$z5TestBufferProtocol.test_memoryview_compare_zero_shapecCs�tgd�dgdd�}tdgdgdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�td	gddgd
d�}td	gdgdgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)N)rVrVrVrVr�rHrrVrrrq)rVrVz@ Liz!L  hrWrTr:r:r;�$test_memoryview_compare_zero_strides>
s$z7TestBufferProtocol.test_memoryview_compare_zero_stridesc
Cs�d}tdD]n}t|dd|�\}}}dtfD]L}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�q,qd}td�D]h}	t|�\}}}dtfD]L}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�q�q�dS)	NrdrDrLr>rrrrr�)rPrpr^rrLr"r`)
rrrUr8rmr9r�rrCrkr:r:r;�&test_memoryview_compare_random_formatsX
s(z9TestBufferProtocol.test_memoryview_compare_random_formatscCs�tttdd��gd�dd�}tttdd��gd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdgd�d	d�}td
gdgd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��gd�d
d�}tttd��gd�d
d�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdddgdd�}tdgdddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��gd�d
d�}tttd��gd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)N��r2�r
rr�rSrrr��rr*rz=f q xxL)g333333�r*rz< f 2Qr�r�r
r4z! b B xLz
= Qx l xxLr�rr~r`rLr"r8rTr:r:r;�"test_memoryview_compare_multidim_ct
sZz5TestBufferProtocol.test_memoryview_compare_multidim_ccCs�tttdd��gd�dtd�}tttdd��gd�dtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgd	d
dgdtd�}td
gd	d
dgdtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttdd��gd�dtd�}tttdd��gd�dtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttdd��gd�dtd�}tttdd��gd�dtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��gd�dtd�}tttd��gd�dtd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Nr[r2)r�rr
rSrrr��rr�r�rr
�=Qq)r�rz=qQr�rr\Z0llrF)rr~r`r0rLr"r8rTr:r:r;�(test_memoryview_compare_multidim_fortran�
s�����������z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc	CsPttdd��}t|gd��}t|gd�dd�}t|gd�dtd�}t|�}t|�}|�||�|�||�|�||�dgd	}d
|d<t|gd��}t|gd�dd�}t|gd�d
td�}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	��gd�dd�}|dd�ddd�f}tttd	��gd�dd�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd	��gd�dd�}|dd�ddd�f}tttd	��gd�dd�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgd	gd �d!d�}|dd�ddd�f}tdgd	gd �d"d�}|dd�ddd�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)#Nr[r2r\�@lrrr)gffffff
����r0r�)g�������rer0r�zd b czd h c�(r3�@Ir
r*r�rSr)r�r$�rRrz=iiz>iir�rr)rrt�r�r
rZb3sZi3s)r~r`r�rr0rLr"r8)	r�lst1�lst2rJrKr�rDrbrcr:r:r;�&test_memoryview_compare_multidim_mixed�
s�
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedcCstttd��gd�dd�}tttd��gd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��gd�dd�}tttd��gd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)Nr�)rr
rrr)r�rrz@ir_rTr:r:r;�+test_memoryview_compare_multidim_zero_shape[s$z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapecCstdgdgd�dd�}tdggd�gd�dd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�|�|��|���td	gd
ddgd
d�}td	gddgddgdd�}t|�}t|�}|�||�|�||�|�||�|�||�|�||�dS)NrVr�)r�r�r�rHrr(rrqr�rdrr�z=lQrz<lQ)rrLr"rrTr:r:r;�-test_memoryview_compare_multidim_zero_stridesus&z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc	Cs^tttd��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��gd�dtd�}|dd�ddd	�f}tttd��gd�dd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdgd�dttBd�}|dd�ddd	�f}tdgdgd�dd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tttd��gd�dtd�}|dd�ddd	�f}tttd��gd�dtd�}|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdgd�dttBd�}d|ddd<|dd�ddd	�f}tdgdgd�dttBd�}d|ddd<|dd�ddd	�f}t|�}t|�}|�||�|�||�|�||�|�||�|�||�ttdd ��}t|gd��}t|gd�d!td�}t|gd�d"t	tBd�}t|�}t|�}|�||�|�||�|�||�d#gd}d$|d%<t|gd��}t|gd�d&td�}t|gd�d't	tBd�}t|�}t|�}|�||�|�||�|�||�dS)(Nrfr�r3rgrr
r*r�rSrrrarb)r*rSrr4z>Qqr�r�rr\)rxr�ZBbrir)shellor{r*�)r
r
r
Z5s0sP)ssushir{r*r[r2rdr)ssashimisslicedg�����4@)sramensspicygfffff�"@rRz
< 10p 9p dz
> 10p 9p d)
rr~r`r^rLr"r8r�r�r0)	rrbrJrcrKr�rDrjrkr:r:r;�+test_memoryview_compare_multidim_suboffsets�s�������
�z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetscCsTdD�]H}tdgdgd�|dd�}tdgdgd�|dttBd�}d|d	d
ddd<t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdgd�|dd�}tdgdgd�|dttBd�}d
|d	d
ddd<t|�}t|�}|�||�|�||�|�||�|�||�|�||�qdS)N)rAr?r@rBr'r�)r
r�rrrrrrr*rr
)r'r%r#ZQLH)r*r*r*)rr�r0rLr"r8)r�	byteorderrWr:rErr:r:r;�!test_memoryview_compare_not_equals6
���z4TestBufferProtocol.test_memoryview_compare_not_equalcCst�dgd��}t|�}|��|�tt|�|�t|jd�|�tt|�|�t|j�|�t|j�|�tt	dt
��|�t|jd�|�t|jdd�dD]}|�t|j
|�q�t�dgd��}t|�}t|�}|�||�|��|�||�|�||�|�||�dS)Nr)g�������?g������@gffffff
@r
z1.0 in mrr*)rVr�r#r�r�r�r�r�rrCrErG)rJrLr~rLr�rrrr�r�r�r�r��__getattribute__r"r8)rrErCrErrdrer:r:r;�test_memoryview_check_released.s*z1TestBufferProtocol.test_memoryview_check_releasedcsPd�t�dgdd�}t|�}|�||�|�|��|���t�gdgdd�}t|�}|�||�|�|��|���t�fdd�td	�D�gd
�dd�}t|�}|�||�|�|��|���t�fdd�td
�D�gd�dd�}t|�}|�||�|�|��|���t�rLGdd�dtj�}|dd�}t|�}|�|��t|��dS)NrMr�rSrr*z>hQiLlcsg|]}��qSr:r:r��rsr:r;rzer{z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rOr�z=hQiLlcsg|]}��qSr:r:r�rur:r;rzjr{r�)r�rrr
rz<hQiLlc@s eZdZdejfdejfgZdS)z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrWr:NrBr:r:r:r;rssrr�r�)rrLr"r�r`rrrQ)rrrCrr
rEr:rur;�test_memoryview_tobytesVs0$�
z*TestBufferProtocol.test_memoryview_tobytescCs�|�ttitd�|�ttdtd�tgd�dgdgd�}|�tt|td�tdd	d
d�}dD],}t|t|�}|�||�|�|d	d�q^tdd	d
td
�}dD],}t|t|�}|�||�|�|d	d�q�dD]\}d|d	<t|t|�}|�||�|�|d	d�d|d	<|�|d	d�|�|d	d�q�tdgdgd
td
�}dD]>}t|t|�}|�t	|j
d�|�||�|�|��g��qHttt
d��gd�d
td
�}dD]*}t|t|�}|�t|���ggg��q�tdgdgdtd
�}dD]2}t|t|�}|�||�|�|��|����q�tgd�dgdtd
�}dD]2}t|t|�}|�||�|�|��|����q:tgd�dgdgtd�}dD]d}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��q�|ddd�}dD]d}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��qttt
d��ddgtd�}dD]*}t|t|�}|�t|���|����q�|�tt|td�t|t|�}|�t|���|���ttt
d��ddgttBd�}dD]*}t|t|�}|�t|���|����q
|�tt|td�t|t|�}|�t|���|���ttt
d��ddgttBd�}dD]<}|�tt|t|�t|t|�}|�t|���|����q�tgd�dgdgd�}t|td�}|�|j�dS)Nrr0r�rkrr�rrQr:rrrrrdr*rr3r�rr
rrmr�r�rOr�rU)r�r)rrro)rLrr1r2r,ZPyBUF_WRITErr"r�r�r�rr~r`r�r0r^r3rC)rrr rCr:r:r;�test_memoryview_get_contiguousys����z1TestBufferProtocol.test_memoryview_get_contiguouscCs�t�d�}t�dgd��}t|�}t�|�}td|�}|�|�|�|�	�|�t�d�}t
ttd��gd�dd�}t|�}t�|�}td|�}|�|�|�|�	�|�dS)Nrror�rrOr�r)
r5r_rJrL�io�BytesIOr��readintor"r�rr~r`)rr	rErCr�rrr:r:r;�test_memoryview_serializing�s





z.TestBufferProtocol.test_memoryview_serializingc	Cs�tttd���}t|�}|�t|�t|��|jdddgd�}|�t|�t|��|ddd�}tttd��ddd��}|�t|�t|��tttd��gd�td	�}t|�}|�t|�t|��tttd��gd�d�}|ddd
�dd�ddd�f}t|�}|�t|�t|��tttd��gd�t	d	�}|ddd
�dd�ddd�f}t|�}|�t|�t|��tttd��dgd
d�}t|�}tttd��dgdd�}t|�}|�||�|�t|�t|��tttd��gd�dd�}t|�}|�
t|j�tttdd��gd�dd�}t|�}|�
t|j�tttd��gd�dd�}t|�}|�
t|j�tttdd��gd�dd�}t|�}|�
t|j�dS)NrOr
r
r�r�rSr�r\rUrr�)rr�r
rrrr�rr�r�rz= Lz< h)
rQr~r`rLr"r�rrr0r^rLr��__hash__)	rrrCZmcZmxrrWrEr:r:r:r;�test_memoryview_hashsJz'TestBufferProtocol.test_memoryview_hashcCs|tgd��}t|�}t|�}|�t|j�~|��tgd��}t|�}t|ttd�}t|ttd�}|�|j	|�|�t|j�~~|��tgd��}t|�}t|�}t|�}|��|�t|j�~|��tgd��}t|�}t|�}t|ttd�}t|ttd�}|�|j	|�|��|�t|j�~~|��tgd�dgt
d�}t|�}|jgd�dgd�t|�}t|�}|�|�
�|�
��t|�}	|�|	�
�|�
��|�|	�
�|�
��|��|	��|��|�|�
�|�
��~|��|��dd	�}
td
�}t|��,}|
|�|�|dtd��Wd�n1�sF0Ytttd
��gd�dd�}t|ttd�}	t|	ttd�}|�|j	|�t|��>}|
|�|�|dd��
�gd�gd�gg�Wd�n1�s�0YdtfD�]
}td
�}t|���}~t|t|d�}	t|	���}~	t|t|d�}t|��h}
~|
|
�|
|�|
|�|�|dtd��|�|dtd��|�|
dtd��~
Wd�n1�s�0Y~Wd�n1�s�0Y~Wd�n1�s�0Ytd
�}t|���}~t|t|d�}	t|	���}~	t|t|d�}t|��l}
~|
|�|
|�|
|
�|�|dtd��|�|dtd��|�|
dtd��~~~
Wd�n1�s�0YWd�n1�s�0YWd�n1�s�0Y�q�td
�}|�t��Rt|��(}t|�}|dtd�kWd�n1�sN0YWd�n1�sn0YdS)Nrk�r@r�r
rU)r�r�r�r4r3r�r�cSs,t|��}Wd�n1s0YdSr�)rL)rrer:r:r;�catch22�s
z;TestBufferProtocol.test_memoryview_release.<locals>.catch22rtrr-rOr�rrr*r])r
r�r�r.rr/)r�rLrrLr,r~r6�ND_REDIRECTr!rVrzr{r"rr}�ordr~r`)rrErCrrJrKrdrerWr:rr�r�r�r&r:r:r;�test_memoryview_releaseHs�
4
F


"""


j
z*TestBufferProtocol.test_memoryview_releasecCs0tdd�td�D�dgdd�}t�ddd�td�D��}||fD]�}t|ttd�}t|ttd�}t|�}|�|j|�|�|j|�|�|j|�|�||�|�||�|�||�|�|dd	�|dd	��|�|dd	�|dd	��|�|dd	�|dd	��~~|�|dd	�|dd	��q@dS)
NcSsg|]}d|�qS�rr:rNr:r:r;rz�r{z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rOrrcSsg|]}d|�qSr�r:rNr:r:r;rz�r{r~r*r
)	rr`rJr6r�rLr!rVr")rrrErWr:r�rCr:r:r;�test_memoryview_redirect�s"z+TestBufferProtocol.test_memoryview_redirectcCs�d}gd�}|�ttddd�t�}t|�}|j||d|dddgdg|d�	td�D]}|�|||�qT~~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�	td�D]}|�|||��q�~~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)Nr)rr*rr
r�r�r�r4r3rQrdrRr*rr
TrOr�r?r~)Zlegacy_mode)rLrZstaticarrayrLr;r`r"rr6r!rVr�)rr8rarWr:rr�rCr:r:r;�$test_memoryview_from_static_exporter�s�
�
�
�

�


�

�z7TestBufferProtocol.test_memoryview_from_static_exportercCs*tgd�dgttBd�}|�tt|�dS)Nrkr
ri)rr`ZND_GETBUF_UNDEFINEDrLr,rLr�r:r:r;�#test_memoryview_getbuffer_undefined>sz6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs&tgd�dgtd�}|�tt|�dS)Nrkr
rU)rr`rLr,rL)rrWr:r:r;�test_issue_7385Dsz"TestBufferProtocol.test_issue_7385cCs&dD]}|�t�|�t�|��qdS)N)r=ZiiZ3s)r"�	_testcapiZPyBuffer_SizeFromFormatr5r_)rr�r:r:r;�test_pybuffer_size_from_formatHs�z1TestBufferProtocol.test_pybuffer_size_from_formatN)F)Ur�r�r�rr;rMrhr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�rrrrr�
contextlib�contextmanagerrr+r5r7r<rFrLrUrXrYrZr`rcrlrmrnrprrrtrvrwr{r}r�r�r�r�r�Zcpython_onlyr�r:r:r:r;r�s��K
0i
-'	0.Y #6!)>
		%qu
8	s<T	
5m8>5?H>Ha(#u<^r�__main__)rNN)r)r)r�r1r:)r�r1)r)N)F)F)r�)N)gr�Zunittest�testr�	itertoolsrrZrandomrrr�warningsrrJrx�os�decimalrZ	fractionsr	Z_testbuffer�ImportErrorrr5rZEnvironmentVarGuard�environ�catch_warningsrKr�r�Z
SHORT_TESTZNATIVEr6r7ZSTANDARDr<rPr8�copyrvZARRAYr�ZBYTEFMTrfrhrerXr_rbrorprqrtrurwr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr�r�r�Z
skipUnlessZTestCaserr��mainr:r:r:r;�<module>sf 


�P

	��
�	





�	



	


?

+

!





o