Your IP : 216.73.216.85


Current Path : /opt/alt/python310/lib64/python3.10/test/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/test/__pycache__/test_buffer.cpython-310.opt-2.pyc

o

0�i<��@sJddlZddlZddlmZddlmZddlmZmZddl	m
Z
mZmZddl
Z
ddlZddlZddlZddlZddlmZddlmZzddlTWneyWdZYnwzddlZWneyidZYnwzddlZWney{dZYnwz1e���#e_e
���dd	lmZWd�n1s�wYWd�n1s�wYWney�dZYnwzddl Z Wney�dZ Ynwd
Z!dddddddddddddddd�Z"er�e"d=e"d
=e�rze�#dd�de"d<de"d<Wnej$�yYnwddddddddddddddd>dd>fd�Z%dd�Z&e"e"e%e%e%e%d�Z'e�r>e'd D]Z(e&e(�e'd e(<�q2e"�)�Z*e"�)�Z+e"D]Z,e,d!v�rRe+e,=�qHe"�)�Z-e"D]Z,e,d"v�rde-e,=�qZe*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*�Z0d+d,�Z1d-d.�Z2d/d0�Z3d1d2�Z4d�d4d5�Z5d	d6d7�Z6d	d8d9�Z7d:d;�Z8d<d=�Z9d>d?�e'd D�Z:d@dA�Z;dBdC�Z<dDdE�Z=dFdG�Z>dHdI�Z?dJdK�Z@dLdM�ZAdNdO�ZBdPdQ�ZCdRdS�ZDdTdU�ZEdVdW�ZFdXdY�ZGdZd[�ZHd\d]�ZId^d_�ZJd`da�ZKdbdc�ZLddde�ZMdfdg�ZNdhdi�ZOdjdk�ZPd�dodp�ZQdqdr�ZRdsdt�ZSd�dudv�ZTdwdx�ZUd�dydz�ZVd{d|�ZWd�d}d~�ZXdd��ZYd�d��ZZd�d��Z[d�d�d��Z\d�d�d��Z]d�d�d��Z^d�d�d��Z_d�d��Z`d�ZadlZbd�Zce!�r�d�Zad�Zbd�Zce]ZZe^Z[e_Ze�ded��e�ded��Gd�d��d�eje���Zfegd�k�r�e�h�dSdS)��N)�support)�	os_helper)�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
rrrrrrrrrrrrrc	Cs�	|dkr	d}|S|dkrd}|S|dkrd}|S|dkr(dd	>dd	>f}|Sd
D]}z
t�|d|>d�Wn
tjyCYq*w|d@rQd|>d|>fndd|>f}|S)Nrr!r
rrr'rr+r,)
r#��@�?� �����r)�struct�pack�error)�fmt�lhZexp�r;�7/opt/alt/python310/lib64/python3.10/test/test_buffer.py�native_type_range_s,�
��	��&r=)��@�<�>�=�!r?Z
bBhHiIlLfdZBbc�m�@m�ar�@b)r>�1�2�3r>)r�array�numpy�
memoryview�bytefmtcCsx	tt||�}|dkrt|g�}|dkr|dkrd}|dkr$t|�}|dks,|dkr:t�||�}t�||�d}|S)	NrrL��r
rrr)r�fmtdict�bytes�boolr6r7�unpack)�mode�char�obj�xr;r;r<�
randrange_fmt�s
rYcCsL	|�d�\}}g}|D]}|�t|||��qt|�dkr"|dSt|�S)N�#r+r)�split�appendrY�len�tuple)r9rWrU�charsrXrr;r;r<�gen_item�sr`cCs>	|dkr
t||�Sdg|}t|�D]	}t||�||<q|S�Nr)r`�range)rr9rW�lstrr;r;r<�	gen_items�s

rdcCs�tt|t�}|d}|�d�}tdd�}t|�D]$}ttt|��}tt|t�}||t	|r3|nd�7}|||7}qt
|||�}	t||�}
||	|
fS)NrZ�ambr �
r+)r�cap�MODE�striprrbr^rQ�MULT�intrdr`)rrWrUZxfmtr9�nmemb�_rV�
multiplier�items�itemr;r;r<�struct_items�s



rqrcCs�	|dur
tt|t�}|durttt|��}tt|t�}|d|t|r*|nd�}t|||�}t||�}|�	d�||}|||fS)NrZr+re)
rrgrhr^rQrjrkrdr`ri)rrWrUrVrnr9rorpr;r;r<�	randitems�s

rrccs8�	t|tD]}t|D]
}t||||�VqqdS�N)rgrhrQrr)rrWrUrVr;r;r<�	iter_mode�s���rtccs6�	t||�D]}|Vq|dkrdSt||�VdS)Nr)rtrq)�nitemsZtestobj�tr;r;r<�iter_format�s�rwcCsd|vpd|vpd|vS)Nrrrr;)r9r;r;r<�is_byte_format�srxcCs6	t|�}|dks|dko|ddko||dtvS)Nr+r rr?)r]�
MEMORYVIEW)r9rXr;r;r<�is_memoryview_format�s
�rzcCsg|]}t|�s|�qSr;)rx��.0rr;r;r<�
<listcomp>��r}cCs	t|t�Srs��
isinstance�list�rcr;r;r<�atomp�sr�cCs
t|t�Srsrr�r;r;r<�listp��
r�cCs8	t|�dkr	dS|d}|dd�D]}||9}q|S�Nrr+)r])rcrX�vr;r;r<�prod�s
r�cCs�	|dkrdS|dkr/t|dd��|g}t|ddd�D]}||||d9<q|S|gt|dd��}td|�D]}||||d9<q?|S)Nrr;�Cr+r ���)r�rb)�ndim�shape�itemsizeZlayout�stridesrr;r;r<�strides_from_shapes�r�cCs�	t|�r|St|�dkr|dSdg|d}|dr$t|�|dnd}t|d�D]}||}t||||�|dd��||<q,|Sr�)r�r]rb�_ca)ro�src�strider�startr;r;r<r�s$r�cCsn	t|�r|St|�dkr|dSdg|d}|d}t|d�D]}t||d|�|dd��||<q"|Sr�)r�r]rb�_fa)ror�rcr�rr;r;r<r�%s"r�cC�2t|�rd|vrt|�t|�krtd��t||�S�Nrzprod(shape) != len(items))r�r�r]�
ValueErrorr��ror�r;r;r<�carray2� 
r�cCr�r�)r�r�r]r�r�r�r;r;r<�farray7r�r�cCs	dd�|D�}t|�S)NcSsg|]}t|��qSr;�rb�r|r�r;r;r<r}>�zindices.<locals>.<listcomp>)r)r��	iterablesr;r;r<�indices<sr�cCs,	d}t|�D]}|||||7}q|Srar�)r��indr�Zretrr;r;r<�getindexAs
r�c	Cs�	|s|St|�}t||dd�}t||ddd�dd�}dgt|�}t|�D]}t|||�}t||ddd�|�}||||<q'|S)Nr+r�r�r)r]r�r�r�)	�srcr�r�ZsstridesZdstrides�destr��fr�tor;r;r<�	transposeHsr�cCs8	|gkr|St|�r|gSt|d�t|dd��Sr��r��_flattenr�r;r;r<r�Wsr�cCs	t|�r|St|�Srsr�r�r;r;r<�flatten_sr�cCs6	t|�rgSt||d�gt|d|dd��Sr�)r�r]�slice_shape�rc�slicesr;r;r<r�es(r�cs(	t|�r|S�fdd�|�dD�S)Ncsg|]}t|�dd���qS�r+N)�
multislice)r|Zsublst�r�r;r<r}pszmultislice.<locals>.<listcomp>r)r�r�r;r�r<r�lsr�csH	t|�r|S��fdd�t|�d|�d�D�}||�d<|S)Nc	s.g|]\}}t||�dd��dd���qSr�)�m_assign)r|r�r��lslices�rslicesr;r<r}�s"�zm_assign.<locals>.<listcomp>r)r��zip��llst�rlstr�r�r;r�r<r�rs�r�cCsh	t||�}t||�}t|�t|�krdStt|��D]}||||kr(dS||dkr1dSqdS)Nr�r)r�r]rb)r�r�r�r��lshape�rshaperr;r;r<�
cmp_structure�s

�r�cCs*	t||||�dkrtd��t||||�S)Nrz+lvalue and rvalue have different structures)r�r�r�r�r;r;r<�multislice_assign�sr�cs�	|�rdS|dks|�|krdSt�fdd��D��r dS|dkr.|dko-�o-�Sd�vr4dSt��fdd�t|�D��}t��fdd�t|�D��}d||ko]||�|kS)NFrc3s�|]}|�VqdSrsr;r��r�r;r<�	<genexpr>���z#verify_structure.<locals>.<genexpr>Tc3�0�|]}�|dkr�|�|dVqdS�rr+Nr;�r|�j�r�r�r;r<r������c3�0�|]}�|dkr�|�|dVqdSr�r;r�r�r;r<r��r�)�any�sumrb)�memlenr�r�r�r��offset�imin�imaxr;)r�r�r�r<�verify_structure�sr�cCs|D]}||}q|Srsr;)rcr�rr;r;r<�get_item�s
r�c
Cs<	|\}}}}}}|}t|�D]}	|||	||	7}q|Srsr�)
r�rvr�r�r�r�r�r��prr;r;r<�memory_index�sr�cCsT	|\}}}}}}d|>}t|�D]}t||�}	d|	>}
||
@r#dS||
O}qdS)Nr+TF)r�r�)rvr�r�r�r�r�r�Zvisitedr�r�bitr;r;r<�is_overlapping�s

r��r2r;csP	�s^t|d�}|dkr3|r|||dddfStdd�}||}td�dkr)|n|}|||dd|fSd}	td�}
|
dkrB|rBd}	n|
dkrHd}	dg|�t|�D]}t|	|d��|<qQnt��}d	}td�}
|
dkrr|
d@rrd
nd}
dg|�|t||d��|d<|
s��|ddkr�|�|d<t|ddd�D]2}|�|dr��|dnd9}|
r�|t||d��|<q�d
td�|td|d��|<q�d}}d�vr�t��fdd�t|�D��}t��fdd�t|�D��}||}|�r||}||d|}n|||}td�dk�r||n|}|||��|fS)Nr+rr;�r �d�_�Zr�TFr��r+r�c3r�r�r;r�r�r;r<r�r�z!rand_structure.<locals>.<genexpr>c3r�r�r;r�r�r;r<r�r�)rrbr]r�)r�Zvalid�maxdim�maxshaper�r�rur�r��minshaperrZ	maxstrideZzero_strider�r�r;r�r<�rand_structure�s`

�
 
�
�
r�c
Csr	||}t|d�}|r|||nd}td|d�}|||}t|||�}t||�\}}}}	|	|kr7t�|S�Nr+)r�slice�
slice_indices�RuntimeError)
�slicelenZlistlenZmaxstartr�Zmaxstep�step�stopr�rmZcontrolr;r;r<�randslice_from_slicelen'sr�cCsr	dg|}dg|}t|�D]!}||}|dkr td|d�nd}t||�||<t||�||<qt|�t|�fSr�)rbrr�r^)r�r�r�r�rrr�r;r;r<�randslice_from_shape4s

r�cCs�	td|d�}d}td�}|dkrd}n|dkrd}td�dkr#dnd	}dg|}dg|}dg|}dg|}	t|�D]�}t||d�}
t||d�}||
krV|
|}}
|r�t|
|
d�}t|
|
d�}
d
td�td|
d�}t||
|�}t||
�\}}}}n|
dkr�td|
d�nd}t||
�}t||�}td�dkr�||
||<||<|||	|<||<q=|
|||<||<|||	|<||<q=||t|�t|	�fS)Nr+r r�r�rr��PTFr�)rrbr�r�r�r^)r�r�r�r�rZ
all_randomr�r�r�r�ZsmallZbigr�r�r�Zs_smallrmr�Zs_bigr;r;r<�rand_aligned_slices@s>


r�cCs&	|\}}}}}}t||d|d�S)NrZrL)rd)r9rvr�r�rmr;r;r<�randitems_from_structureksr�c
Cs*	|\}}}}}}	t|||||	t|Bd�S)N�r�r��formatr��flags)r�ND_WRITABLE)
ror9rvr�r�r�r�r�r�r�r;r;r<�ndarray_from_structureqs

�r�cCsR	|\}}}}}}t|�}	t|�D]\}
}t�||	|
||�qt|	||||d�S)N)�bufferr�r��dtyper�)�	bytearray�	enumerater6�	pack_into�numpy_array)ror9rvr�r�r�r�r�r��bufr�r�r;r;r<�numpy_array_from_structurews�r�cs�	|j}|rt|��|krd|fSn$|gkr%|jdks �|kr$d|fSnt|��\}}|g}|dkr7d|fS|�����fdd�tdt����D�}g}|D]}	t�||	�d}
|
|
krcd|fS|�	|
�qO|gkrq||fS|d|fS)Nrcsg|]
}�||���qSr;r;)r|r�r�Zmemr;r<r}��zcast_items.<locals>.<listcomp>�nan)
�nbytesr�r��divmod�tobytesrbr]r6rTr\)Zexporterr9r�r�Zbytelenrr�Z	byteitemsror�rpr;r�r<�
cast_items�s.�� r�c#s��	td�D]}|gVqtdd�}td�dkrdnd��fdd	�t|�D�Vtdd�}td�dkr5dnd��fd
d	�t|�D�VdS)Nr0��r�r�r+r c�g|]}t�d��qS�r��r�r|rm�r�r;r<r}��z!gencastshapes.<locals>.<listcomp>crrrrrr;r<r}�r)rbr)rr�r;rr<�
gencastshapes�s�


rcCs2	tt||d�t||d�t||d��Sr�)rrb)rr;r;r<�	genslices�s0rcs 	�fdd�t|�D�}t|�S)Ncsg|]}t�|��qSr;)r�r|r�r�r;r<r}�r~z"genslices_ndim.<locals>.<listcomp>)rbr)r�r�r�r;r	r<�genslices_ndim�sr
FcCs.	|s|dkr	dnd}t||d�}t||�Sr�)rr�)r�allow_emptyZminlenr�r;r;r<�rslice�s
rccs"�	td�D]}t||�VqdS)Nr�)rbr)rrrmr;r;r<r��s
��r�c#st�	t|�D]}t�fdd�t|�D��Vqt|�D]}t�fdd�t|�D��Vqtdd�t|�D��VdS)Nc3s�|]	}t�|�VqdSrs�rrr	r;r<r����zrslices_ndim.<locals>.<genexpr>c3s �|]}t�|dd�VqdS)T)rNr
rr	r;r<r��s�css�|]	}tddd�VqdSr�)r�rr;r;r<r��r)rbr^)r�r�Z
iterationsrmr;r	r<�rslices_ndim�s�rccs2�t|�}|dur
t|�n|}tt||��VdSrs)r^r]r)�iterabler�Zpoolr;r;r<�rpermutation�s�rc
Cs�	z|��}Wnttfy|��}Ynwt|t�r#|j}|j}nd}d}td||j	|j
|j||j|j
|f�tj��dS)N�unknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s))�tolist�	TypeError�NotImplementedErrorr�r�rr�r��printr�r��
suboffsetsr�r��sys�stdout�flush)�ndrXr�r�r;r;r<�
ndarray_print�s"�

��rr�rf�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�|_dSrs)Zget_sizeof_void_p�
sizeof_void_p��selfr;r;r<�setUpszTestBufferProtocol.setUpF)�sliced�castc
%
s*�	r	t�	���n	�st���n��d��jrg���d�d}td��D]}
�	|
dkr.n�|
dkrA|�|
�	|
d7}q$|gdd�t�d�D���j}�ddkr\|}|gt�dd������j|���	�j
����	�j����	�j�����j
|���	�j����	�jt�	���
r��s���	�jt�����	�jt���t�t�s�t��rɈr����n���}��	|���s�dS|�s�t�}d}t�	�D]<}z&t�|�}t�|�}t|t�r�tj�g|�R�}nt��|�}|�|�Wn
t�yd}Ynw��	||�q�|�s���	���|�t��}d� dd	�|D��}��	|�!�|���r<�nd
}t"��}dD�]s}�}|dk�rht#�d
��rZt#�d��rgt$|�	�}t|�	|t%d�}nt#�d
��ryt#�d��r�|dk�r�t|�	|d�}t&�t'|����	���|���(t)�|���dk�r��qDt|�}|�r�dnt*}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�}!��	|!���qDt���rzt��}Wnt�y�YdSwt�t��rوjn��|f���������	�
��fdd�	}#|#|�|�-��}$|#|$dd�Wd�n	1�swY|��dSdS)Nr;rr+cS�g|]}d�qS)r�r;r�r;r;r<r}�z-TestBufferProtocol.verify.<locals>.<listcomp>Tr>css�|]}d|VqdS)z%02xNr;r{r;r;r<r�Ur�z,TestBufferProtocol.verify.<locals>.<genexpr>r�r��F�Ar(r)r��r�r�r��r�r�c�g|]}t���|���qSr;�r6Zunpack_fromr��contigr9r�r;r<r}���cS�g|]}|d�qS�rr;r�r;r;r<r}�r��r�r�r���ordercr,r;r-rr.r;r<r}�r0cSr1r2r;r�r;r;r<r}�r�cr,r;r-rr.r;r<r}�r0cSr1r2r;r�r;r;r<r}�r�cs���|j����|j����|j����|j����|j|���|j����|jt	����	r7�s@��|j
t	�
����|jt	����dkrOdnt��}��t|�|��ra��
�n���}��|����|��dSr�)�assertIsrW�assertEqualr�r�r��readonlyr�r�r^r�rr]rr�)rD�expected_readonlyr�rep)�ex�expected_lenr9r�rcr��resultr!r�r#r�rr;r<�check_memoryview�sz3TestBufferProtocol.verify.<locals>.check_memoryview)r9).r�r]rZ
assertGreaterrbrr�r6rWr7r�r�r�r8r�r�r^r�r�rrzrr�r�r��get_pointerr�r6r7�extend�BufferErrorrM�join�hexr��
is_contiguousr��
ND_FORTRAN�get_contiguous�
PyBUF_READ�
assertTrue�
cmp_contigr��py_buffer_to_contiguous�
PyBUF_FULL_RO�
toreadonly)%r!r=rWr�r9r8r�r�r�rcr#r$Z
suboffset0rZstride0r:rZbuf_errr�Zitem1Zitem2rXrDrZffZ	flattenedr5�expectedZtransrl�roZinitlst�yZcontig_bytesrr>Zmmr;)
r/r;r<r9r�rcr�r=r!r�r#r�rr<�verifys
�


�

�
���

����
�&
��zTestBufferProtocol.verifycCsddd�}|jr||t�s>||t�r|jr>||t�r|jr>||t�r$|jr>||t�s,|j	s>||t
�s4|jr>||t�sI||t�rI|j
tt||d�dSt|t�sSt|j�rX|��}n
t|td�}|��}||t�ridn|j}|j}	|j}
|j}||t�s�d}	|��}||t�s�d}||t�r�|jnd}||t
�r�|jnd}
t||d�}|j|||
|	||||
||d�
dS)	NcSs||@|kSrsr;)�req�flagr;r;r<�match�sz/TestBufferProtocol.verify_getbuf.<locals>.match��getbufFr>r+r;)	rWr�r9r8r�r�r�rcr#)r8�PyBUF_WRITABLE�PyBUF_C_CONTIGUOUS�c_contiguous�PyBUF_F_CONTIGUOUS�f_contiguous�PyBUF_ANY_CONTIGUOUS�
contiguous�PyBUF_INDIRECTr�
PyBUF_STRIDES�PyBUF_ND�PyBUF_FORMAT�assertRaisesrArr�rzr�rrKr�r�r�r�r�rP)r!Zorig_exr;rQr#rSrcrrNr9r�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]�\}}	}
�fdd�|	D�}	|
�9}
|D]�}|	r�|t@r�q�|s�|t@r�q�|r�|n|d
}t|||||	|
d�}
|r�|
ddd�nd}t|
�}|r�t|�}|
jd
ks�|
jdkr�|r�|	r�|�||
�|�r|jdk�r|�r|	�r|�||�|D]4}|D].}|�|
|
||B�|�|
|||B�|�r:|j||||Bdd�|j||||Bdd��q
�q	q�q�q�gd�}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]8}t|||tBd�}|�t |d��|�t |d ��|D]}t||d�}|�t |d��|�t |d ���qȐq��q�dS)!NcSsg|]
}|dr
dnd�qS)r TFr;�r|rXr;r;r<r}r�z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>�r
�r+r rr�r�r�r5r4�	rf�rdrrcSs$g|]}|drd|nd|�qS)r r&r%r;rcr;r;r<r}s$rr)r+rr+r�rgr�r ���rr����r+r4csg|]}|��qSr;r;r�r�r;r<r})r�)r�r�r�r�r�T)r#�r�r�rfrT)r+rdr+)r5rr5r(r�)!r]r^r_�PyBUF_SIMPLErWrYr[�
PyBUF_FULLrK�
PyBUF_RECORDS�PyBUF_RECORDS_RO�
PyBUF_STRIDED�PyBUF_STRIDED_RO�PyBUF_CONTIG�PyBUF_CONTIG_ROrbr�rE�ND_PILrVr`r6�calcsizerrMr�r7rb�ND_GETBUF_FAILrarArHrD)r!�requestsZ	items_fmtZ	structureZndflagsZ
real_flagsror9r�r�r�r�Z_items�ex1�ex2�m1�m2rQ�bitsr;�baserr5r;r�r<�test_ndarray_getbuf�s��

�
���
��������!���z&TestBufferProtocol.test_ndarray_getbufc
	Cs�tdgdg�}tdgdgtd�}t|j|jfD�]�}|�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)9Nrfr+�r�>r+r r)r+r rJ)r+r �rr��r+r rrr	r��r�r�r+rj)r�rU�r�r�r�ZXXXri�r�r rr ZQLr�rJr#�r+r rr�r�rH�r+r rr��r�r�r�r�)r�r�r�r>z@#$)
r+r rr�r�r�r5r4rfrf���rhr�)r�r�r�r�r��123rTr�rd)r rr�r*)�r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+r+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�pushrarrErsr�r6r8ZND_MAX_NDIM�
OverflowErrorr�rlrArV�poprM�releaser?r�rbrkr�memoryview_from_bufferrFrG�UnicodeEncodeErrorrIrD)
r!rZndmrrrorDr;rwrxr;r;r<�test_ndarray_exceptionsgs��������z*TestBufferProtocol.test_ndarray_exceptionscCs�ttd��D]F}dgd}tgd�dgtd�}t|�|d<tdd�D]}|jgd�dgd�t|�||<q"td�D]
}|||��q9|�t|j	�~qdS)Nr�rr�rrjr+r	)
rrbrr�rMr�r�rarAr�)r!ZpermrDrrr;r;r<�test_ndarray_linked_lists
�z+TestBufferProtocol.test_ndarray_linked_listcCsLtd�D]\}}}t�|�}t|d|d�}|j|d||dddd|d�	qdS)Nrr;r+T�rWr�r9r8r�r�r�rc)rwr6rtrrP)r!r9Zscalarrmr�rr;r;r<�test_ndarray_format_scalars
��z-TestBufferProtocol.test_ndarray_format_scalarc
Csltdd�}t|�D]*\}}}t�|�}dtfD]}t||g||d�}|j|d||dd|f|f|d�	qq	dS)Nr+rfrr*Tr�)rrwr6rtrsrrP)r!rur9rormr�r�rr;r;r<�test_ndarray_format_shapes


���z,TestBufferProtocol.test_ndarray_format_shapecCs�tdd�}t|�D]V\}}}t�|�}tdd�D]F}|dkrqt|dd|��g}||g}|dkr8||dnd}	dtfD]}
t|||||	|
d�}|j|d||dd|||dd|�d�	q>qq	dS)	Nr+����r�rr�Tr�)	rrwr6rtrbr]rsrrP)r!rur9rormr�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)Nrer)r+rr�rT)rrYr`r7rr�)r!ror;rr;r;r<�test_ndarray_fortran@sz'TestBufferProtocol.test_ndarray_fortrancCsXtd�D]�}dd�t|�D�}t|�}t|�D]�}t|�\}}}t�|�}dtfD]~}	|dkr4|	tkr4q)t||||	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�	q)qqdS)
Nr�cSsg|]}tdd��qS)r rfrrr;r;r<r}Hrz<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>rr*r�Tr�r+rTr;r()rbr�rrrr6rtrsrr�r�rPrzrrr`rHr�r�rEr�)r!r��shape_trur�r9rormr�r�rr�rcr;�mvr;r;r<�test_ndarray_multidimFsP

���
����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	r4rjr*�r+r rr�r�r�r5r4rr*r i,)r���)�r+r r)r�r�ZLQ)s1r�)rrar�__setitem__rMr7r��__delitem__r��__getitem__�
IndexErrorr]r6r8r�)r!rr�ror;r;r<�test_ndarray_index_invalidss: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;rj.rr4rfr�)	rr�rMr7rrarr�r�)r!rr�rXr;r;r<�test_ndarray_index_scalar�s"z,TestBufferProtocol.test_ndarray_index_scalarcCsRtttd��ddgtd�}t|td�}|�t|jd�|�t|jtddd��dS)	Nr4r r�rjrTr+rr�)	rr�rbr�rqrarAr�r�)r!r;rr;r;r<�test_ndarray_index_null_strides�sz2TestBufferProtocol.test_ndarray_index_null_stridescCs�td�D]_\}}}t|dg|d�}tdd�D]}|�||||�q|�t|jd�|�t|jd�t|�rct|�}|�||�tdd�D]}|�||||�qF|�t|jd�|�t|jd�qtd�D]K\}}}t|dgt	|d�}t|t
tBd�}tdd�D]}|�||||�q�t|�r�|��}|�
|�|�t�tdd�D]}|�||||�q�qhgd�}t|dgd�}t|td�}tdd�D]}|�||||�q�gd�}t|dgd�}t|td�}tdd�D]}|�||||�q�dS)	Nr�r+r����r3rTr�r	)rwrrbr7rar�r�rzrMr�rqr`r�r6�__eq__�NotImplementedrrrk)r!r9rormrrr�r;r;r;r<�!test_ndarray_index_getitem_single�sH���z4TestBufferProtocol.test_ndarray_index_getitem_singlecCs�td�D]x\}}}t|dg|td�}td�D]
}|||<|||<q|�|��|�|�t|jd|�|�t|jd|�t	|�s@qt|dg|td�}t
|�}|�||�td�D]
}|||<|||<qW|�|��|�|�t|jd|�|�t|jd|�qtd�D]f\}}}t|dg|td�}tdd�D]}||d||<||d||<q�|�|��|�t	|�s�q�t|dg|td�}t
|�}|�||�tdd�D]}||d||<||d||<q�|�|��|�q�dS)Nr�r*r�r�r�r+)rwrr�rbr7rrar�r�rzrM)r!r9roZsingle_itemrrr�r;r;r<�!test_ndarray_index_setitem_single�sF

�z4TestBufferProtocol.test_ndarray_index_setitem_singlec

Cs�d}t|�}t|�D]�}t|�\}}}dtfD]�}t||||d�}t||�}	t|d|d�D]J}
|�|	|
||
���t|d|d�D]1}|�|	|
|||
|���t|d|d�D]}|�|	|
||||
||�qdqHq0t||||t	Bd�}t
||�}	t|d|d�D]I}
|�|	|
||
���t|d|d�D]0}|�|	|
|||
|���t|d|d�D]}|�|	|
||||
||�q�q�q�qq
dS)N�r rr�rr*r+r )r�rrrrsrr�rbr7rrEr�)
r!r�rur�r9rormr�rrcrr��kr;r;r<�#test_ndarray_index_getitem_multidims<
 &���
 &�����z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCs�tddd�}|�ttdt��t|�}|�||�|�ttdt��td�D]E\}}}t|dg|d�}t|�D]\}}|�|||�|�	||v�q7t
|�rkt|�}t|�D]\}}|�|||�|�	||v�qWq&dS)Nr+r;r	z1 in ndz1 in mvr�r+)rrar�eval�localsrMr7rwr�rHrz)r!rr�r9rormrr�r;r;r<�test_ndarray_sequence:s"��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�r4rjrr+rTrr*r r�z@%$rrr+r5r�)r�r�r5r4z###########)rr�rMrarr�r�r_rAr�r�rr7rr�rs�add_suboffsetsrk)r!ro�xl�mlr;�xrrr��mr�yrrXr;r;r<�test_ndarray_slice_invalidMs�
�
�
�"""""
�""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)Nrerdrr*r+rfr�rr+r )rr�r7rrMr�)r!rorXrOr�r�r;r;r<�test_ndarray_slice_zero_shape�s$z0TestBufferProtocol.test_ndarray_slice_zero_shapec
Csd}t|�}t|�}t|�D]z}t|�\}}}t�|�}dtfD]g}	t||||	d�}
t||�}t	||�D]R}d}
zt
||�}WntyT}z	|j}
WYd}~nd}~wwd}z|
|}Wntyr}z	|j}WYd}~nd}~ww|sw|
r~|�
||
�q4|�|��|�q4q qdS�Nr�rr*)r]r�rrrr6rtrsrr�rr��	Exception�	__class__r6r7r)r!r�r�rur�r9rormr�r�rrcr��listerrr#�e�nderr�ndslicedr;r;r<�test_ndarray_slice_multidim�s<

�������z.TestBufferProtocol.test_ndarray_slice_multidimcCs,d}t|�}t|�}t|�D]�}t|�\}}}t�|�}t|||d�}	|	��t|||d�}
|
��t|
�}t	||�}t
||�D]R}
d}zt||
�}Wnty`}z	|j
}WYd}~nd}~wwd}z|	|
}Wnty~}z	|j
}WYd}~nd}~ww|s�|r�|�||�q@|�|��|�q@qdS)N)r rr�r r+)r]r�rrrr6rtrr�rMr�rr�r�r�r6r7r)r!r�r�rur�r9rormr�rr;r�rcr�r�r#r�r�r�r;r;r<�'test_ndarray_slice_redundant_suboffsets�s@

������z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetscCstd�D]�\}}}td�D]�}td�D]�}dtfD]�}|tB}t|dg||d�}t|dg||d�}	t|	�}
d}d}|dd�}
z|
|}|
|}|
||
|<t|�t|�k}Wntyl}z	|j}WYd}~nd}~wwd}z||||<Wnty�}z	|j}WYd}~nd}~ww|r�|�	|t
�n|�|��|
�|�	||�t
|�s�qd}z|
||
|<Wnty�}z	|j}WYd}~nd}~ww|r�|�	|t
�q|�|
��|
�|�|
|�|�	||�|j|
|	|j|d|j|j|j|��d�	qqq
qdS)Nr�rr*Fr�)rwrrsr�rrMr]r�r�r6r�r7rrzrPr�r�r�r�)r!r9rorm�lslicerr�rrr;r�ZlsterrZdiff_structurercZlvalZrvalr�r�Zmverrr;r;r<� test_ndarray_slice_assign_single�sl�����������z3TestBufferProtocol.test_ndarray_slice_assign_singlec
Cs$d}t|�}t|�}t|�D]�}t|�\}}}dtfD]s}tt�D]l}t||�\}	}
t||||t	Bd�}t
||�}d}
z	t|||	|
�}WntyY}z	|j
}
WYd}~nd}~wwd}z||
||	<Wntyy}z	|j
}WYd}~nd}~ww|s~|
r�|�||
�q!|�|��|�q!qqdSr�)r]r�rrrrsrb�
ITERATIONSr�rr�r�r�r�r�r6r7r)r!r�r�rur�r9rormr�r�r�rrcr�r=r�r�r;r;r<�"test_ndarray_slice_assign_multidim3s@�
�������z5TestBufferProtocol.test_ndarray_slice_assign_multidimc
Cs4tt�D]�}tdD]�}t�|�}t|dttd�}|�t	|��t
||�}t|||�}|��}t
|�}t|�rA|��}	|�|	|�|ddkrqt|||td�}
|
��}|�||�t
|
�}t|�rq|�||
�|��}	|�|	|�tr�|d}d|vr|q
t|||�}
|j|d|
j|d|
j|
j|
j|
��d	�	q
qdS)
Nr?T�r�r�r rr~rFr�)rbr�rQr6rtr��MAXDIM�MAXSHAPErHr�r�r�rrMrzr7rsr�r�rPr�r�r�r�)r!rmr9r�rvrorXZxlistr�ZmvlistrOZylistr��zr;r;r<�test_ndarray_randomTsH
�
����z&TestBufferProtocol.test_ndarray_randomcCs�tt�D]j}tdD]c}t�|�}t|dttd�}|�t	|��t
||�}d}zt|||�}WntyD}z	|j
}WYd}~nd}~ww|�|�trmd}	zt|||�}
Wntyk}z	|j
}	WYd}~nd}~ww	q
qdS)Nr?Fr�)rbr�rQr6rtr�r�r��assertFalser�r�r�r�r�rHr�r�)r!rmr9r�rvror�rXr�Z	numpy_errrOr;r;r<�test_ndarray_random_invalid}s8
�
��
�����z.TestBufferProtocol.test_ndarray_random_invalidc
Cs*tt�D�]
}tdD�]}t�|�}tttd�\}}}}t|d|d�}t|d|d�}	|�	t
|��|�	t
|	��t||�}
t||	�}t|
||�}t|||	�}
|
|||<|�
�}|
�
�}t|�}t|
�}|�|�
�|�|�|�
�|�|ddkr�|	ddkr�t|
||td�}t|||	td�}||||<|�
�}|�
�}|�||�|�||�t|�}t|�}|�|�
�|�|�|�
�|�t�rd|vs�d|vr�qt|
||�}t|||	�}||||<t|�s�t|	�s�|j|d|j|d|j|j|j|�
�d	�	|j|
d|j|d|j|j|j|�
�d	�	qqdS)
Nr?r�Tr	r rr~Fr�)rbr�rQr6rtr�r�r�r�rHr�r�r�rrMr7rsr�r�r�rPr�r�r�r�)r!rmr9r�r�r�r�r�ZtlZtrZlitemsZritemsr�r�ZxllistZxrlistr�r�Zylr�ZyllistZyrlistZzlZzrr;r;r<� test_ndarray_random_slice_assign�sh


�


�
����z3TestBufferProtocol.test_ndarray_random_slice_assigncCsvgd�}t|ddgtd�}t|�}|�|jt@�|�|j|�|�|jd�|�|j	�|�|j
�|�|j�dS)Nrerr�rj)rr�)rrsrHr�r6rWr7rr�rXrZr\)r!rorr;r;r;r<�test_ndarray_re_export�sz)TestBufferProtocol.test_ndarray_re_exportc	Cs�dtfD]n}tgd�dg|d�}t|�}|�||�|�|��g�|�|��g�tgd�gd�|d�}|�|��g�tgd�gd�|d�}|�|��gggg�tgd�gd�|d�}|�|��ggggggggggggg�qdS)Nrr�rj�rrr�rrr)rrr�rsrrMr7r�r!r�rr�r;r;r<�test_ndarray_zero_shape�s
��z*TestBufferProtocol.test_ndarray_zero_shapecCsddtfD]+}tdgdgdg|d�}t|�}|�||�|�|��gd��|�|��gd��qdS)Nrr+r�r�)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�rr5r�)r5r4rf)rr�rbr7r�r�r!rr;r;r<�test_ndarray_offsetsz&TestBufferProtocol.test_ndarray_offsetcCs>dtfD]}tttd��dg|d�}|��}|�||�qdS�Nrrrj)rsrr�rbr�r7)r!r�rrDr;r;r<�#test_ndarray_memoryview_from_buffers
�z6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsPdtfD]!}tttd��dg|d�}td�D]}|�||t||g��qqdSr�)rsrr�rbr7r?)r!r�rrr;r;r<�test_ndarray_get_pointers��z+TestBufferProtocol.test_ndarray_get_pointercCs\tttd��gd�d�}t|ttBd�}|�|��|���t|�}|�|��|���dS)Nr�)r r r�r	rT)rr�rbr_r`r7rrM)r!r;rrDr;r;r<� test_ndarray_tolist_null_stridess
z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs�|�tdd��tttd��ddgd�}tttd��ddgd�}|�t||��tdgdgdd	�}|�t|d
��|�td
|��dS)Nr�s456rdrr�r	r+rr+rP)r�rIrr�rbrH)r!rXrOr;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)Nrr�rdr	rr�)rr r rj�r rr �r r rr+)
rKrrar��hashrRr�rbr7r�rErsr�)r!rFrrr;r;r<�test_ndarray_hash&s*z$TestBufferProtocol.test_ndarray_hashc
Cs8tttttttttt	t
tf}|�t
tidt�tdddtd�}dD]}|D]}t|||�}|�||���q%q!tdgdgdtd�}dD]}|D]}t|||�}|�|d	�qGqCtttd
��gd�dtd�}dD]}|D]}t|||�}|�|d	�qkqgdtfD]O}tdgdgd|tBd�}|��}dD]}|D]}t|||�}|�||�q�q�tgd
�dgd|tBd�}|��}dD]}|D]}t|||�}|�||�q�q�qtgd
�dgdgtd�}|��}dD]}ttfD]}t|||�}|�||�q�q�|ddd�}|��}dD]#}|D]}zt|||�}Wnt�yY�qw|�||��q�qttd��}dtfD�]U}t|ddg|tBd�}t�rVtt|�ddgd|dk�rRdndd�}	|tk�rntt|ddg�ddgtd�}
|
��}n|��}|D]B}zt|d|�}Wnt�y�Y�qtw|�||�tdd�|D�ddgtd�}|�t|�t|��t�r�|�||	jdd���qt|dk�r�tt|ddg�ddgtd�}
n	t|ddgtd�}
|
��}tttttfD]D}zt|d|�}Wnt�y�Y�q�w|�||�tdd�|D�ddgttBd�}|�t|�t|��t�r"|�||	jdd���q�|tk�r7t|ddgtd�}
|
��}n|��}tttttfD]D}zt|d|�}Wnt�yWY�qBw|�||�tdd�|D�ddg|tBd�}|�t|�t|��t�r�|�||	jdd���qB�q2tttd��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)"Nr(rfr;rr*r'r+r�r4�r rr5rr�rrr r�r�rdr�rjrr�)r�r�r�r5cS�g|]}|�qSr;r;r�r;r;r<r}�r&zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>r4cSr�r;r;r�r;r;r<r}	r&r)cSr�r;r;r�r;r;r<r}	r&cSr�r;r;r�r;r;r<r}+	r&cSr�r;r;r�r;r;r<r}2	r&cSr�r;r;r�r;r;r<r}8	r&)r]r^r_rkrlrKrmrnrorprqrrrarrJrr�r7r�r�rbrErAr�r�r�rMZtostringrs)
r!rvrr5ZrequestrrZndbytesrcZnarXrMrOr;r;r<�test_py_buffer_to_contiguousKs������������G�
�
��
��� �

�� �� z/TestBufferProtocol.test_py_buffer_to_contiguouscCsvdgfgd�dgfttd��gd�fg}|D]�\}}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
�|
��}|dkr�|gn|}|j
|ddd	d
d|jgd|d�	q|D]K\}}t||td�}t|�}|�|j�|�|j�t|�}t||dd�}t||�}|j
||dd	d
||||d�	t|�}	|j
|	|dd	d
||||d�	q�|dd�D];\}}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)Nrfr�rr�r�r	r+r�rTr�rTr;r>r�r+rjr(�9rXr#rr+r�)r�rbrrMrHrXr\r]r�r�rPrrr`r7r�r�rkr�r�r�rErZr�rsrarrAr�rFrG)r!Zitems_shaperor�r;rDr�r�rcrzrr;r;r<�test_memoryview_construction;	s�(
���
�
��
�
� z/TestBufferProtocol.test_memoryview_constructioncCs�gd�}gd�gd�gd�fD]}t||d�}|�|j�t|�}|�t|jd�qtdd�D]\}}}td�}|�|�}|�|�	�d�|�|�
�g�q/dS)	Nr�r�r�r	rr+rMr�)rrHrXrMrarr$rwr7r�r)r!ror�r;�msrcr9rmrDr;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 PZ3nr4r�r+r r�rr+)r r r r)�check_sizeofrZcalcvobjsizer�rbrMr)r!ZcheckZvsizeZbase_structZper_dimrorFr;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]�\}}}t|dg|td�}t|dg|td�}	t|�}
t	�
||	d|�||
d<|�|
d|	d�t	�|�}d|vr�qI|D]W}d}
z
t	�
||	||�Wn
t	j
y�t	j
}
Ynwd}z||
d <Wnttfy�}z	|j}WYd}~nd}~ww|
s�|r�|�|
d�|�|d�q�|�|
d |	d �q�qIdS)!Nc@�eZdZdd�Zdd�ZdS)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTcS�
||_dSrs��val�r!r�r;r;r<�__init__�	r�zFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__cS�|jSrsr�r r;r;r<�__int__�	�zETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)�__name__�
__module__�__qualname__r�r�r;r;r;r<�INT�	�r�c@r�)z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXcSr�rsr�r�r;r;r<r��	r�zFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__cSr�rsr�r r;r;r<�	__index__�	r�zGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r�r�r�r�rr;r;r;r<�IDX�	r�rcSsdS)Nr5r;r;r;r;r<r�	�z;TestBufferProtocol.test_memoryview_struct_module.<locals>.frfy������@@z-21.1gffffff(@r�r r�>r�r�r�r5r4r;�rfTF�asabcrF�abccSs|Srsr;)rXr;r;r<�<lambda>�	rzBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rfrMr*rrr+)�objectr	r
�Ellipsisr�rwrr�rMr6r�r7rtr8rr�r�ZassertIsNot)r!r�rr�valuesr9rorpr;rrDr�r�Z
struct_errZmv_errr�r;r;r<�test_memoryview_struct_module�	sR�

�����z0TestBufferProtocol.test_memoryview_struct_modulecCs>tgd�dgdgd�}|�|j�t|�}|�t|jd�dS)Nr�rrr�r)rr�rXrMrarr$)r!r;r�r;r;r<�!test_memoryview_cast_zero_strides	
sz4TestBufferProtocol.test_memoryview_cast_zero_stridesc	Cs2tD]C}td�rd|n|}t�|�}tD]/}td�rd|n|}t�|�}tttd��d|g|d�}t|�}|�t	|j
|d|g�qqtd�D]@\}}	}
t|	dg|d�}t|�}td�D]*\}}
}
t|�su|�t
|j
|d|g�q_t|�s�t|�s�|�t	|j
|d|g�q_qJt�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�rv|�t	|j
dgd+��|�t
|j
dgd,��|�t
|j
d%gd-��dS|�t	|j
ddd.g�|�t
|j
dgd/��|�t
|j
d%gd0��dS)1Nr r?r0r+r+rrr��xr�r	r�Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIr�rZIIrr4rr*r�)r rr�r�r�r5r�)r rr�r�r�r5r)r rr�r�r�r5rXcSr%rr;rr;r;r<r}^
r&zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i�)rr�r5rgr)r rr�r�r#cSr%rr;rr;r;r<r}h
r&cSr%rr;rr;r;r<r}m
r&l����)r5r5�Ir-iQiAji��	)�r�rr4)rrrrr ��)rrr0r0r )rrr0r4r )�NON_BYTE_FORMATrr6rtrr�rbrMrarr$rwrzr�rxrr�r�rr�r�maxsize)r!ZsfmtZsformatZssizeZdfmtZdformatZdsizer;r�ZsitemsrmZsize_hZsize_drDrryrzr;r;r<�test_memoryview_cast_invalid
s�

�����


 "  ������z/TestBufferProtocol.test_memoryview_cast_invalidcsjddd�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]\}	}
}t|
dg|	t	d�}t|�}||||
|	�q�dS)NrcSst|���Srs�r�r��r;r;r;r<r�
sz9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>rcS�dd�t|���D�S)NcSs g|]}|dkr|dn|�qS)r-r"r;rcr;r;r<r}�
s �MTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>rrr;r;r<r�
rrcSr)NcSsg|]	}tt|�d��qS)zlatin-1)rR�chrrcr;r;r<r}�
srrrr;r;r<r�
rc

s�t�|�}�D]?\}}|�|�}||�}�j||d|ddd|gd|dd�
|�|�}	��|	|�|��}�j|	|||dddg|f|dd�
qdS)Nr+Fr1r�T�	rWr�r9r8r�r�r�rcr$)r6rtr$rPr7r)
r;rDror9�srcsizerNZto_bytelistrzrc�m3�Zbytespecr!r;r<�iter_roundtrip�
s$

�

��z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtriprrfr+r+Tr�rr	rr;r1rKrMr*)
r6rtrr�rMr$rPrwrKr�)r!rrr;Z	destitemsZ	destshaperDrzZdestsizer9rormr;rr<�test_memoryview_cast�
sD


�

�
��z'TestBufferProtocol.test_memoryview_castcCs�t�D]�}tdD]�}|dkrq	dtd�|}t�|�}t|�|}t|�r)dnd}t||�D]�\}}}	t�|�}
|dkrA|gng}||
g}t|||d�}
t	|
�}t
|
|||d	�\}}|durk|�t|j
||�q0|d
krpq0t|||d�}|j
||d	�}t|�}|j}|��}|j||
||d||||dd�
|�
|�}|j
||d	�}t|�}|
j}|
��}|j||
|
|d||||dd�
|j||
|
|d||||dd�
q0q	qt�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�dSdS)Nr?r
)r>r?r rMrNrr+r	r�Trc@s eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrXrON)r�r�r��ctypes�c_long�c_double�_fields_r;r;r;r<�BEPoint�
�r$r�g33333i@rr+Fr�r;�333333�?r)rrQrr6rtr�rxrwrrMr�rarr$r]r�rrPr �BigEndianStructurer7rWr�r6r8r�r�r�rr")r!Z_tshaperVZtfmtZtsizerrWr9rorm�sizer�Ztshaper;rDZtitemsrrzr�r�rcrZm4r$�pointryrXr;r;r<�test_memoryview_cast_1D_ND�
s�



�
����6



�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�r ri	)rgr r5rr�rr+)r r�)r5rgr:�12345r+r�)r�b�c�d�e�f)	rKr�rbrMr7rrrar)r!rFrDr;r;r;r<�test_memoryview_tolists(z)TestBufferProtocol.test_memoryview_tolistcCsHttd��}|��}|�|�d��|��|��}|�|�d��dS)Nrfz<memoryz	<released)rMr��__repr__rH�
startswithr�)r!rDr�r;r;r<�test_memoryview_repr3sz'TestBufferProtocol.test_memoryview_reprcCs|dD]%}td�}t�|d|dg�}t|�}|�d|�|�d|�|�d|�qtdgdd�}t|�}|�ttdt��dS)	N)rrg���?g@g"@rr�z9.0 in m)	�floatrKrMZassertInrrarr�r�)r!r9�infr;rDr;r;r<�test_memoryview_sequence<sz+TestBufferProtocol.test_memoryview_sequenceccsN�|�t��}dVWd�n1swY|�t|j�d|f�dS)Nz#index out of bounds on dimension %d)rar�r7�strZ	exception)r!�dim�cmr;r;r<�assert_out_of_bounds_errorJs���z-TestBufferProtocol.assert_out_of_bounds_errorc	CsFtdgdd�}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�n1s�wYq�dD]}|jdd��|d|fWd�n1s�wYq�|�t
|jd�|�t
|jd�|�t|jd �|�t|jd!�|�t|jd�|�t|jd�dS)"N�)@rr+r;.rr�Ziiir5rjr*�@����r4rdrr��rr)r rr�rg�r�r�)r�ri�rrir+�r:�r�r�r �r*rr)�rrr�r?r?)rrMr7rarr�rr�rbr�r�r<)r!r;rD�indexr;r;r<�test_memoryview_indexQsN����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�t
dD]5}|dks�|dkr�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]R\}}|dk�r+�q |dk�r2�q |�|�}|\}	}
|d'k�sE|dk�rOdd(dd(}	}
|d)k�rh|�t|jd|	d"�|�t|jdd*�|�t|jd|
��q |�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�n	1�s�wY�q�d7D] }|jdd6��d|d|f<Wd�n	1�swY�q|�t	|jd8d�|�t	|jd9d�|�t|jd:d�|�t|jd;d�|�t|jdddg�dS)<Nr=rr*g�6@r;g�7@.rg33333�8@r5r	r rfrjr*rfr>r?rgr@r�r4�r?rr
r�rr�)�1�2�3r�)TFTTr+�xr�rr�rrrr�s��ZxLr+r�rdr��*)rr+�+rBrCrDrErFr)rGrH)rr�rMr7rarr�r�rbr�rQrr6ror$r�rr<)r!r;rDr9rrryZ_rangerz�lo�hirIr;r;r<�test_memoryview_assign�s�
��




����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�dt
fD]�}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�<|�||�|�||�q�dS)Nrdrjrr r+r;r�rg)r�r�r�r��r�r�rr�rh)rr�rbr�rMrar�r�r�r�r�rrrsr7)r!r;rDr�rwrxryrzr;r;r<�test_memoryview_slice�sP
�
�
 �
��((,,�z(TestBufferProtocol.test_memoryview_slicecCs�dd�}tdd�D]�}t|d�D]�\}}}t|�D]�}t|�D]�}t�||�}t�||�}	t|	�}
|�|
|�|�|
��|���|�|
��|���|�t|
�t|��||||	|
|�d}d}z||}
||}||||<t|
�t|�k}Wnt	y�}z	|j
}WYd}~nd}~wwd}z|
||
|<Wnt	y�}z	|j
}WYd}~nd}~ww|r�|�|t�q|s�|r�|�||�q|�|
|�|�|
��|���|�|
��|���||||	|
|�qqqq	dS)Nc	Ss�t|�D]K\}}||}||}|�||�|||<||kr+|�||�|�||�n|�||�|�||�|||<|�||�|�||�|||<|||<qdSrs)r�r7�assertNotEqual)	ZtestcaserFrrD�
singleitemrrmZai�mir;r;r<�cmptest-s 
�z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr+r�rK)
rbrwrrKrMr7rr�r]r�r�r6r�)r!rZrr9rorXr�rrFrrDZ	array_errZhave_resizeZalZarr�Zm_errr;r;r<�test_memoryview_array+sV��������z(TestBufferProtocol.test_memoryview_arrayc	Cs�t�dgd��}t�dgd��}t|�}t|�}dD]}|�t||�|�t�|�t||�|�t�qt|�}|��|�||�|�||�|�||�t|�}t|�}|��|�||�|�||�t|�}|�|gd��tdgdgdt	d�}d	t
d
�f|d<|�t|�|�t�dd
�}t|�}|�||�|�||�tr�Gdd�dtj�}|dd�}t|�}t|�}|�||�|�||�|�||�|�
t|j�dSdS)Nrr�)r+r r5)�__lt__�__le__�__gt__�__ge__rAr+zl x d xr*r�r�r�ur�c@� eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrXrON�r�r�r�r r!r#r;r;r;r<r$�r%r$r�r�)rKrMr6�getattrr�r�r7rWrr�r6r r'rarr)	r!rFrr��w�attrrr$r)r;r;r<�%test_memoryview_compare_special_casesisH
�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��@Lr+rr*i�r;rdrjr�rf)rgr&r+ZLf5sZhf5s)i?���r&r+)rr�rMr7r��__ne__rWr�r�rbrs)r!�nd1�nd2r�rdrr;rDr;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)rnrprrrr r�rhz!hz<lr*zh  0cz>  h)rrMr7rWrs�r!rjrkr�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�r�rrsr+i|���i?���z= h0cz@   i�rrMr7rtr;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�rhr+rvrrr�)rvrvz@ Liz!L  hrwrtr;r;r<�$test_memoryview_compare_zero_strides?
s$z7TestBufferProtocol.test_memoryview_compare_zero_stridesc
Cs�d}tdD]7}t|dd|�\}}}dtfD]&}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�qqd}td�D]4}	t|�\}}}dtfD]&}t||g||d�}t|�}|�||�|ddd�}t|�}|�||�qQqDdS)	NrfrErMr?rr*r�r�)rQrrrsrrMr7rb)
r!rrVr9rorXr�rrDrmr;r;r<�&test_memoryview_compare_random_formatsY
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��r3�rr r�rsr+rr��rr+r z=f q xxL)g333333�r+r z< f 2Qr�r�rr5z! b B xLz
= Qx l xxLr�rr�rbrMr7rWrtr;r;r<�"test_memoryview_compare_multidim_cu
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{r3)r�r rrsr*rr��rr�r�r r�=Qq)r�rz=qQr�rr|Z0llrG)rr�rbrErMr7rWrtr;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{r3r|�@lr+rr*)gffffff
����rOr�)g�������r�rOr�zd b czd h c�(r4�@Irr+r�rhr)rr%�rgr z=iiz>iir�rr)r r��r�rr Zb3sZi3s)r�rbr�rrErMr7rW)	r!�lst1�lst2rjrkr�rdrwrxr;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�)rrr rr+)r�rr z@irrtr;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�rhr+rGrr�r�rfr r�z=lQrz<lQ)rrMr7rrtr;r;r<�-test_memoryview_compare_multidim_zero_stridesvs&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)(Nr�r�r4r�r+rr+r�rhrr*r�r�)r+rhr r5z>Qqr�r�rr|)r�r�ZBbr�r)shellor�r+�)rrrZ5s0sP)ssushir�r+r{r3r�r)ssashimisslicedg�����4@)sramensspicygfffff�"@rgz
< 10p 9p dz
> 10p 9p d)
rr�rbrsrMr7rWr�r�rE)	r!rwrjrxrkr�rdr�r�r;r;r<�+test_memoryview_compare_multidim_suboffsets�s�������
�z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetscCsRdD]�}tdgdgd�|dd�}tdgdgd�|dttBd�}d|d	d
ddd<t|�}t|�}|�||�|�||�|�||�|�||�|�||�tdgdgd�|dd�}tdgdgd�|dttBd�}d
|d	d
ddd<t|�}t|�}|�||�|�||�|�||�|�||�|�||�qdS)N)rBr@rArCr(r)rr�r r r rr+r*r+r r)r(r&r$ZQLH)r+r+r+)rr�rErMr7rW)r!�	byteorderrXrOrFrr;r;r<�!test_memoryview_compare_not_equals8����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
|�qPt�dgd��}t|�}t|�}|�||�|��|�||�|�||�|�||�dS)Nr)g�������?g������@gffffff
@rz1.0 in mrr+)rWr�r8r�r�r�r�r�rrXrZr\)rKrMr�rar�r$rrr�r�r�r�r��__getattribute__r7rW)r!rFrDrerryrzr;r;r<�test_memoryview_check_released/s*z1TestBufferProtocol.test_memoryview_check_releasedcsRd�t�dgdd�}t|�}|�||�|�|��|���t�gdgdd�}t|�}|�||�|�|��|���t�fdd�td	�D�gd
�dd�}t|�}|�||�|�|��|���t�fdd�td
�D�gd�dd�}t|�}|�||�|�|��|���tr�Gdd�dtj�}|dd�}t|�}|�|��t|��dSdS)Nrmr�rsr+r+z>hQiLlc�g|]}��qSr;r;r�rvr;r<r}fr&z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rdr�z=hQiLlcr�r;r;rr�r;r<r}kr&r)r�r r rr z<hQiLlc@ra)z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrXrONrbr;r;r;r<r$tr%r$r�r�)rrMr7r�rbr r'rR)r!rrDr$r)rFr;r�r<�test_memoryview_tobytesWs2$�
�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�qPdD].}d|d	<t|t|�}|�||�|�|d	d�d|d	<|�|d	d�|�|d	d�qitdgdgd
td
�}dD]}t|t|�}|�t	|j
d�|�||�|�|��g�q�ttt
d��gd�d
td
�}dD]}t|t|�}|�t|���ggg�q�tdgdgdtd
�}dD]}t|t|�}|�||�|�|��|���q�tgd�dgdtd
�}dD]}t|t|�}|�||�|�|��|����qtgd�dgdgtd�}dD]2}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��qB|ddd�}dD]2}t|t|�}|�||�|�|��|���|�t|jdd�|�|dd�|�|dd��q~tt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|���|����qDtgd�dgdgd�}t|td�}|�|j�dS)Nr(rOr�r�r r�r)rfr;rr+r'r*rfr+rr4r�rrrr�r�r�rdr�rj)r�r))r(r)r�)rarrFrGrAZPyBUF_WRITErr7r�r�r�rr�rbr�rErsrHrX)r!rr5rDr;r;r<�test_memoryview_get_contiguouszs����z1TestBufferProtocol.test_memoryview_get_contiguouscCs�t�d�}t�dgd��}t|�}t�|�}td|�}|�|�|�|�	�|�t�d�}t
ttd��gd�dd�}t|�}t�|�}td|�}|�|�|�|�	�|�dS)Nrr�r�rrdr�r+)
r6rtrKrM�io�BytesIOr��readintor7r�rr�rb)r!r(rFrDr�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)Nrdrrr�r	rhr�r|rjr r�)r r�rrr+rr�rr�r�rz= Lz< h)
rRr�rbrMr7r�r$rrErsrar��__hash__)	r!rrDZmcZmxrrXrFrOr;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�n	1�s#wYtttd
��gd�dd�}t|ttd�}	t|	ttd�}|�|j	|�t|��}|
|�|�|dd��
�gd�gd�gg�Wd�n	1�sqwYdtfD�]}td
�}t|��p}~t|t|d�}	t|	��R}~	t|t|d�}t|��4}
~|
|
�|
|�|
|�|�|dtd��|�|dtd��|�|
dtd��~
Wd�n	1�s�wY~Wd�n	1�s�wY~Wd�n	1�s�wYtd
�}t|��p}~t|t|d�}	t|	��S}~	t|t|d�}t|��6}
~|
|�|
|�|
|
�|�|dtd��|�|dtd��|�|
dtd��~~~
Wd�n	1�sZwYWd�n	1�sjwYWd�n	1�szwY�qztd
�}|�t��2t|��}t|�}|dtd�kWd�n1�s�wYWd�dSWd�dS1�s�wYdS)Nr��rUr�rrj)r�r�r�r5r4r�r	cSs.t|��	}Wd�dS1swYdSrs)rM)rrzr;r;r<�catch22�s
"�z;TestBufferProtocol.test_memoryview_release.<locals>.catch22r�rrLrdr�rr+r+r})rr�r�rMr rN)r�rMrrarAr�rK�ND_REDIRECTr6rWr�r�r7rr��ordr�rb)r!rFrDrrjrkryrzrXrOr�r�r�rr;r;r;r<�test_memoryview_releaseIs�
�
(�


�	�
�


������
 ��$�z*TestBufferProtocol.test_memoryview_releasecCs0tdd�td�D�dgdd�}t�ddd�td�D��}||fD]u}t|ttd�}t|ttd�}t|�}|�|j|�|�|j|�|�|j|�|�||�|�||�|�||�|�|dd	�|dd	��|�|dd	�|dd	��|�|dd	�|dd	��~~|�|dd	�|dd	��q dS)
NcS�g|]}d|�qS�r5r;rcr;r;r<r}�r�z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rdrr+cSr�r�r;rcr;r;r<r}�r�r�r+r)	rrbrKrKr�rMr6rWr7)r!rrFrXrOr�rDr;r;r<�test_memoryview_redirect�s$�z+TestBufferProtocol.test_memoryview_redirectcCs�d}gd�}|�ttddd�t�}t|�}|j||d|dddgdg|d�	td�D]
}|�|||�q*~~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+r rr�r�r�r5r4rfrfrgr+r rTrdr�rTr�)Zlegacy_mode)rarZstaticarrayrMrPrbr7rrKr6rWr�)r!r9rcrXrOrr�rDr;r;r<�$test_memoryview_from_static_exporter�s�
�
�
�

�


�

�z7TestBufferProtocol.test_memoryview_from_static_exportercCs*tgd�dgttBd�}|�tt|�dS)Nr�rr~)rruZND_GETBUF_UNDEFINEDrarArMr�r;r;r<�#test_memoryview_getbuffer_undefined?sz6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs&tgd�dgtd�}|�tt|�dS)Nr�rrj)rrurarArM)r!rXr;r;r<�test_issue_7385Esz"TestBufferProtocol.test_issue_7385cCs&dD]}|�t�|�t�|��qdS)N)r>ZiiZ3s)r7�	_testcapiZPyBuffer_SizeFromFormatr6rt)r!r�r;r;r<�test_pybuffer_size_from_formatIs
��z1TestBufferProtocol.test_pybuffer_size_from_formatN�F)Ur�r�r�r"rPrbr}r�r�r�r�r�r�r�r�r�r�r�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
rrrr*r1r4r8�
contextlib�contextmanagerr<rJrTrVr[rfrlrurxryrzr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�Zcpython_onlyr�r;r;r;r<r�s��
K0i
-'	0.Y #6!)>
		%qu
8	s<T	
5m8>5?H>Ha(#u<^r�__main__)rNN)r�r2r;)r�r2r2rsr�r)ir�Zunittest�testrZtest.supportr�	itertoolsrrZrandomrrr�warningsrrKr��os�decimalr	Z	fractionsr
Z_testbuffer�ImportErrorrr6r ZEnvironmentVarGuard�environ�catch_warningsrLr�r�Z
SHORT_TESTZNATIVEr7r8ZSTANDARDr=rQr9�copyryZARRAYr�ZBYTEFMTrhrjrgrYr`rdrqrrrtrwrxrzrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr
rr�rrrr�r�r�Z
skipUnlessZTestCaserr��mainr;r;r;r<�<module>s� ���������	���
�	
�
�

�	



	


?
+

!






o�