a
    ze                    @   sD  d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ zd dlT W n ey   dZY n0 zd dlZW n ey   dZY n0 zd dlZW n ey   dZY n0 zfe He_e  d dlmZ W d   n1 s&0    Y  W d   n1 sF0    Y  W n eyj   dZY n0 zd dlZW n ey   dZY n0 d	Zd d d d d d d d d d d d d d d d
Z ere d= e d= er
z e!dd d e d< d e d< W n ej"y   Y n0 ddddddddddddddd>  dd> fdZ#dd Z$e e e#e#e#e#dZ%erze%d D ]Z&e$e&e%d e&< qbe ' Z(e ' Z)e D ]Z*e*d vre)e*= qe ' Z+e D ]Z*e*d!vre+e*= qe(e%d"< e(e%d#< e)e%d$< e+e%d%< e+e%d&< d Z,dZ-g dg d'fd$gd(gfd(gd(gfd#d"gd(gfd&d%gd(gfd)Z.d*d+ Z/d,d- Z0d.d/ Z1d0d1 Z2dd3d4Z3dd5d6Z4dd7d8Z5d9d: Z6d;d< Z7d=d> e%d D 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 ZNddndoZOdpdq ZPdrds ZQddtduZRdvdw ZSddxdyZTdzd{ ZUdd|d}ZVd~d ZWdd ZXdd ZYdddZZdddZ[dddZ\dddZ]dd Z^dZ_dkZ`dZaerdZ_dZ`dZae[ZXe\ZYe]ZebedebedG dd dejcZdeedkr@ef  dS )    N)support)permutationsproduct)	randrangesamplechoice)Decimal)Fraction)*)ndarrayT)?cbBhHiIlLnNfdPr   r   Q    qr      r      )i   )i    )r   i   )           )r   l        l                     r                    )r   r   r   r   r   r   r   r   r   r   r   r   r   r   c              	   C   s   | dkrd}n| dkrd}n| dkr*d}n| dkrFdd	>  dd	> f}ndd
D ]8}zt | d|> d  W  qW qJ t jy   Y qJ0 qJ|d@ rd|>  d|> fn
dd|> f}|S )zReturn range of a native type.r   r    r   r   r   r&   r   r*   r+   )
r"      @   ?                      r   )structpackerror)fmtlhexp r;   &/usr/lib/python3.9/test/test_buffer.pynative_type_range^   s     
&r=   ) @<>=!r?   Z
bBhHiIlLfdZBbcm@mar   @b)r>   123r>   )r   arraynumpy
memoryviewbytefmtc                 C   sv   t t|  |  }|dkr6t|g}|dkr6|dkr6d}|dkrFt|}|dksV|dkrrt||}t||d }|S )	zWReturn random item for a type specified by a mode and a single
       format character.r   rL          r   r   r   r   )r   fmtdictbytesboolr5   r6   unpack)modecharobjxr;   r;   r<   randrange_fmt   s    
rY   c                 C   sJ   |  d\}}g }|D ]}|t||| qt|dkrB|d S t|S )zReturn single random item.#r*   r   )splitappendrY   lentuple)r8   rW   rU   charsrX   r   r;   r;   r<   gen_item   s
    r`   c                 C   s<   | dkrt ||S dg|  }t| D ]}t ||||< q$|S )z,Return a list of random items (or a scalar).r   )r`   range)r   r8   rW   lstr   r;   r;   r<   	gen_items   s    

rc   c                 C   s   t t| t }|d }|d}tdd}t|D ]H}t tt| }t t| t }||t	|rf|nd 7 }||| 7 }q4t
| ||}	t||}
||	|
fS )NrZ   ambr   
   r*   )r   capMODEstripr   ra   r^   rQ   MULTintrc   r`   )r   rW   rU   Zxfmtr8   nmemb_rV   
multiplieritemsitemr;   r;   r<   struct_items   s    


rp   r   c                 C   s   |du rt t| t }|du r0t tt| }t t| t }|d |t|rR|nd  }t| ||}t||}|	d| | }|||fS )z"Return random format, items, item.NrZ   r*   rd   )
r   rf   rg   r^   rQ   ri   rj   rc   r`   rh   )r   rW   rU   rV   rm   r8   rn   ro   r;   r;   r<   	randitems   s    
rq   c                 c   s4   t | t D ]"}t| D ]}t| |||V  qqdS )z1Iterate through supported mode/char combinations.N)rf   rg   rQ   rq   )r   rW   rU   rV   r;   r;   r<   	iter_mode   s    rr   c                 c   s2   t | |D ]
}|V  q
|dkr"dS t| |V  dS )zwYield (format, items, item) for all possible modes and format
       characters plus one random compound format string.r   N)rr   rp   )nitemsZtestobjtr;   r;   r<   iter_format   s
    ru   c                 C   s   d| v pd| v pd| v S )Nr   r   r   r;   )r8   r;   r;   r<   is_byte_format   s    rv   c                 C   s4   t | }|dks$|dko2| d dko2| |d  tv S )zformat suitable for memoryviewr*   r   r   r?   )r]   
MEMORYVIEW)r8   rX   r;   r;   r<   is_memoryview_format   s    rx   c                 C   s   g | ]}t |s|qS r;   )rv   .0r   r;   r;   r<   
<listcomp>       r{   c                 C   s   t | t S )z9Tuple items (representing structs) are regarded as atoms.
isinstancelistrb   r;   r;   r<   atomp   s    r   c                 C   s
   t | tS Nr}   r   r;   r;   r<   listp   s    r   c                 C   s6   t | dkrdS | d }| dd D ]}||9 }q$|S )zProduct of list elements.r   r*   N)r]   )rb   rX   vr;   r;   r<   prod   s    
r   c                 C   s   | dkrdS |dkrZt |dd |g }t| d ddD ]}||  ||d  9  < q:n>|gt |dd  }td| D ]}||  ||d  9  < qz|S )zOCalculate strides of a contiguous array. Layout is 'C' or
       'F' (Fortran).r   r;   Cr*   Nr   )r   ra   )ndimshapeitemsizeZlayoutstridesr   r;   r;   r<   strides_from_shape  s    r   c                 C   s   t | r| S t|dkr | d S dg|d  }|d rFt| |d  nd}t|d D ].}|| }t| |||  |dd ||< qV|S )zmConvert flat item list to the nested list representation of a
       multidimensional C array with shape 's'.r   r*   N)r   r]   ra   _ca)rn   srb   strider   startr;   r;   r<   r     s    $r   c                 C   sl   t | r| S t|dkr | d S dg|d  }|d }t|d D ]$}t| |d| |dd ||< qB|S )zsConvert flat item list to the nested list representation of a
       multidimensional Fortran array with shape 's'.r   Nr*   )r   r]   ra   _fa)rn   r   rb   r   r   r;   r;   r<   r   $  s    "r   c                 C   s2   t | r(d|vr(t|t| kr(tdt| |S Nr   zprod(shape) != len(items))r   r   r]   
ValueErrorr   rn   r   r;   r;   r<   carray1  s     r   c                 C   s2   t | r(d|vr(t|t| kr(tdt| |S r   )r   r   r]   r   r   r   r;   r;   r<   farray6  s     r   c                 C   s   dd | D }t | S )z(Generate all possible tuples of indices.c                 S   s   g | ]}t |qS r;   ra   rz   r   r;   r;   r<   r{   =  r|   zindices.<locals>.<listcomp>)r   )r   	iterablesr;   r;   r<   indices;  s    r   c                 C   s*   d}t | D ]}||| ||  7 }q|S )zAConvert multi-dimensional index to the position in the flat list.r   r   )r   indr   retr   r;   r;   r<   getindex@  s    r   c           	      C   s   |s| S t |}t||dd}t||ddd dd}dgt |  }t|D ]2}t|||}t||ddd |}| | ||< qL|S )zTranspose 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   ZsstridesZdstridesdestr   frtor;   r;   r<   	transposeG  s    r   c                 C   s6   | g kr| S t | r| gS t| d t| dd  S )zflatten listr   r*   Nr   _flattenr   r;   r;   r<   r   V  s
    r   c                 C   s   t | r| S t| S )zflatten list or return scalarr   r   r;   r;   r<   flatten^  s    r   c                 C   s4   t | rg S t| |d  gt| d |dd  S )zMGet the shape of lst after slicing: slices is a list of slice
       objects.r   r*   N)r   r]   slice_shaperb   slicesr;   r;   r<   r   d  s    r   c                    s&   t | r| S  fdd|  d  D S )z=Multi-dimensional slicing: slices is a list of slice objects.c                    s   g | ]}t | d d qS r*   N)
multislice)rz   Zsublstr   r;   r<   r{   o  r|   zmultislice.<locals>.<listcomp>r   )r   r   r;   r   r<   r   k  s    r   c                    sF   t |r|S  fdd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 || d d d d qS r   )m_assign)rz   r   rlslicesrslicesr;   r<   r{     s   zm_assign.<locals>.<listcomp>r   )r   zipllstrlstr   r   r;   r   r<   r   q  s    r   c                 C   sf   t | |}t ||}t|t|kr(dS tt|D ],}|| || krN dS || dkr4 dS q4dS )z9Compare the structure of llst[lslices] and rlst[rslices].r   r   )r   r]   ra   )r   r   r   r   lshapershaper   r;   r;   r<   cmp_structure  s    

r   c                 C   s(   t | |||dk rtdt| |||S )z:Return llst after assigning: llst[lslices] = rlst[rslices]r   z+lvalue and rvalue have different structures)r   r   r   r   r;   r;   r<   multislice_assign  s    r   c                    s   |  rdS |dk s |  | kr$dS t  fddD r>dS |dkrZ|dkoX oX S dv rfdS tfddt|D }tfddt|D }d|| ko||   | kS )zVerify 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
    Fr   c                 3   s   | ]}|  V  qd S r   r;   r   r   r;   r<   	<genexpr>  r|   z#verify_structure.<locals>.<genexpr>Tc                 3   s.   | ]&}| d kr|  | d  V  qdS r   r*   Nr;   rz   jr   r   r;   r<   r     s   c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   )anysumra   )memlenr   r   r   r   offsetiminimaxr;   )r   r   r   r<   verify_structure  s    r   c                 C   s   |D ]}| | } q| S r   r;   )rb   r   r   r;   r;   r<   get_item  s    
r   c           
      C   s:   |\}}}}}}|}t |D ]}	|||	 | |	  7 }q|S )z-Location of an item in the underlying memory.r   )
r   rt   r   r   r   r   r   r   pr   r;   r;   r<   memory_index  s
    r   c                 C   sR   | \}}}}}}d|> }t |D ],}t|| }	d|	> }
||
@ rD dS ||
O }q dS )zThe 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   )rt   r   r   r   r   r   r   Zvisitedr   r   bitr;   r;   r<   is_overlapping  s    

r      r1   r;   c                    sZ   st |d }|dkrd|r,| | |dddfS t dd}||  }t ddkrP|  n|}|| |dd|fS d}	t d}
|
dkr|rd}	n|
dkrd}	dg|  t|D ]}t |	|d  |< qnt }d	}t d}
|
dkr|
d@ rd
nd}dg| | t | |d  |d < |s0|d  dkr0| |d < t|d ddD ]j}| |d  r` |d  nd9 }|r| t | |d  |< n"dt d |  t d|d  |< q@d }}d vrt fddt|D }t fddt|D }|| }|r | |  }||d |   }n*| | |  }t ddkrF| |  n|}|| | |fS )zReturn 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*   r   r;      r   d   _   Z   r   TFr   r*   r   c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   z!rand_structure.<locals>.<genexpr>c                 3   s.   | ]&}| d kr|  | d  V  qdS r   r;   r   r   r;   r<   r     s   )r   ra   r]   r   )r   Zvalidmaxdimmaxshaper   r   rs   r   r   minshaper   r   Z	maxstrideZzero_strider   r   r;   r   r<   rand_structure  s\    


"


r   c           
      C   sp   ||  }t |d }| r$|| |  nd}t d|d }|| |  }t|||}t||\}}}}	|	| krlt|S )z=Create a random slice of len slicelen that fits into listlen.r*   )r   sliceslice_indicesRuntimeError)
slicelenZlistlenZmaxstartr   Zmaxstepstepstopr   rl   Zcontrolr;   r;   r<   randslice_from_slicelen&  s    r   c                 C   sp   dg|  }dg|  }t | D ]B}|| }|dkr>td|d nd}t||||< t||||< qt|t|fS )zxCreate two sets of slices for an array x with shape 'shape'
       such that shapeof(x[lslices]) == shapeof(x[rslices]).r   r*   )ra   r   r   r^   )r   r   r   r   r   r   r   r;   r;   r<   randslice_from_shape3  s    

r   c                 C   s  t d| d }d}t d}|dkr(d}n|dkr4d}t ddkrDdnd	}dg| }dg| }dg| }dg| }	t|D ]}t ||d }
t ||d }||
k r|
| }}
|rt |
 |
d }t |
 |
d }d
t d t d|
d  }t|||}t||
\}}}}n&|
dkr$t d|
d nd}t||
}t||}t ddkrp||
 ||< ||< || |	|< ||< qx|
| ||< ||< || |	|< ||< qx||t|t|	fS )zCreate (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*   r   r   r   r   r   P   TFr   )r   ra   r   r   r   r^   )r   r   r   r   r   Z
all_randomr   r   r   r   Zsmallbigr   r   r   Zs_smallrl   r   Zs_bigr;   r;   r<   rand_aligned_slices?  s<    


r   c                 C   s$   |\}}}}}}t || d|  dS )zMReturn a list of random items for structure 't' with format
       'fmtchar'.rZ   rL   )rc   )r8   rt   r   r   rl   r;   r;   r<   randitems_from_structurej  s    r   c           
      C   s(   |\}}}}}}	t | ||||	t|B dS )z:Return ndarray from the tuple returned by rand_structure()r   r   formatr   flags)r   ND_WRITABLE)
rn   r8   rt   r   r   r   r   r   r   r   r;   r;   r<   ndarray_from_structurep  s    
r   c                 C   sP   |\}}}}}}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)rn   r8   rt   r   r   r   r   r   r   bufr   r   r;   r;   r<   numpy_array_from_structurev  s    r   c                    s   | j }|r$t|  |krld|fS nH|g krH| jdks> |krld|fS n$t| \}}|g}|dkrld|fS |   fddtdt D }g }|D ]2}	t||	d }
|
|
krd|f  S |	|
 q|g kr||fS |d |fS )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).Nr   c                    s   g | ]}||   qS r;   r;   )rz   r   r   Zmemr;   r<   r{     r|   zcast_items.<locals>.<listcomp>nan)
nbytesr   r   divmodtobytesra   r]   r5   rT   r\   )Zexporterr8   r   r   Zbytelenr   r   Z	byteitemsrn   r   ro   r;   r   r<   
cast_items  s(    

 r   c                  #   s   t dD ]} | gV  qtdd}tddkr0dnd  fdd	t |D V  tdd}tddkrfdnd  fd
d	t |D V  dS )z Generate shapes to test casting.r/         r   r   r*   r   c                    s   g | ]}t  d qS r   r   rz   rl   r   r;   r<   r{     r|   z!gencastshapes.<locals>.<listcomp>c                    s   g | ]}t  d qS r   r   r   r   r;   r<   r{     r|   N)ra   r   )r   r   r;   r   r<   gencastshapes  s    


r   c                 C   s0   t t|  | d t|  | d t|  | d S )z4Generate all possible slices for a single dimension.r*   )r   ra   )r   r;   r;   r<   	genslices  s    r   c                    s    fddt | D }t| S )z/Generate all possible slice tuples for 'shape'.c                    s   g | ]}t  | qS r;   )r   rz   r   r   r;   r<   r{     r|   z"genslices_ndim.<locals>.<listcomp>)ra   r   )r   r   r   r;   r   r<   genslices_ndim  s    r   Fc                 C   s,   |s| dkrdnd}t || d }t|| S )zGenerate random slice for a single dimension of length n.
       If zero=True, the slices may be empty, otherwise they will
       be non-empty.r   r*   )r   r   )r   allow_emptyZminlenr   r;   r;   r<   rslice  s    r   c                 c   s   t dD ]}t| |V  qdS )z.Generate random slices for a single dimension.r   N)ra   r   )r   r   rl   r;   r;   r<   r     s    r   c                 #   sp   t |D ] }t fddt | D V  qt |D ] }t fddt | D V  q2tdd t | D V  dS )z)Generate random slice tuples for 'shape'.c                 3   s   | ]}t  | V  qd S r   r   r   r   r;   r<   r     r|   zrslices_ndim.<locals>.<genexpr>c                 3   s   | ]}t  | d dV  qdS )T)r   Nr  r   r   r;   r<   r     r|   c                 s   s   | ]}t d dd V  qdS r   )r   r   r;   r;   r<   r     r|   N)ra   r^   )r   r   Z
iterationsrl   r;   r   r<   rslices_ndim  s
    r  c                 c   s0   t | }|d u rt|n|}t t||V  d S r   )r^   r]   r   )iterabler   poolr;   r;   r<   rpermutation  s    r  c              
   C   s   z|   }W n ttfy*   |  }Y n0 t| trD| j}| j}nd}d}td|| j	| j
| j|| j| j|f  tj  dS )zPrint 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~   r   r   r   printr   r   
suboffsetsr   r   sysstdoutflush)ndrX   r   r   r;   r;   r<   ndarray_print  s    

r  r   re      r   z%struct module required for this test.z%ndarray object required for this testc                   @   s  e Zd Zdd ZdddddZdddZd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=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~ ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRe*jSdd ZTdS )TestBufferProtocolc                 C   s   t  | _d S r   )Zget_sizeof_void_psizeof_void_pselfr;   r;   r<   setUp  s    zTestBufferProtocol.setUpF)slicedcastc       
   %         s<  	rt 	 ns tndjrΈd d}tdD ]:}	| dkr\ q| dkrH||  	| d  7 }qH|gdd td D  j}d dk r| }|gtdd   j| 	j
 	j 	j j| 	j 	jt	 
r@sR	jt 	jt ttsztrr n }	| sd S |st }d }t	D ]}zNt|}t|}t|trtjg|R  }nt|}|| W n  ty0   d}Y  qBY n0 	|| q|s	 | t}d dd	 |D }	|! | rnd
}t"}dD ]}}|dkrt#drt#drt$|	}t|	|t%d}n0t#drt#dr|dkrt|	|d}t&t'| 	  | (t) | dkrPqt|}|rbdnt*}t+dt, 	t |   fddt|D }t|d dkrdd |D }t|	|d} 	t| t t }!	|!  tjd d}!	|!  tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkrdd |D }t|	|t%B d} 	t| t tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkr,dd |D }t#dr<t%nd}"t|	|"|B d} 	t| t tjdd}!	|!  qtr8zt}W n ty   Y d S 0 ttrȈjn|f	
fdd	}#|#| |- }$|#|$dd W d    n1 s&0    Y  |  d S )Nr;   r   r*   c                 S   s   g | ]}d qS )r   r;   r   r;   r;   r<   r{     r|   z-TestBufferProtocol.verify.<locals>.<listcomp>Tr>   c                 s   s   | ]}d | V  qdS )z%02xNr;   ry   r;   r;   r<   r   T  r|   z,TestBufferProtocol.verify.<locals>.<genexpr>r   r   FAr  r  r   r   r   r   r   r   c                    s   g | ]}t  | qS r;   r5   unpack_fromr   contigr8   r   r;   r<   r{     s   c                 S   s   g | ]}|d  qS r   r;   r   r;   r;   r<   r{     r|   r   r   r   orderc                    s   g | ]}t  | qS r;   r  r   r   r;   r<   r{     s   c                 S   s   g | ]}|d  qS r"  r;   r   r;   r;   r<   r{     r|   c                    s   g | ]}t  | qS r;   r  r   r   r;   r<   r{     s   c                 S   s   g | ]}|d  qS r"  r;   r   r;   r;   r<   r{     r|   c                    s    | j  | j | j | j | j| | j | jt	 	rns| j
t	
 | jt	 dkrdnt}t| | r n }| |  d S )Nr   r*   )assertIsrW   assertEqualr   r   r   readonlyr   r   r^   r   r  r]   r  r   )rD   expected_readonlyr   rep)exexpected_lenr8   r   rb   r   resultr  r   r  r   r  r;   r<   check_memoryview  s    z3TestBufferProtocol.verify.<locals>.check_memoryview)r)  ).r   r]   r  ZassertGreaterra   r  r   r&  rW   r'  r   r   r   r(  r   r   r^   r   r~   r   rx   r  r   r   r   get_pointerr   r5   r6   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-  rW   r   r8   r(  r   r   r   rb   r  r  Z
suboffset0r   Zstride0r*  r   Zbuf_errr   Zitem1Zitem2rX   rD   r   ZffZ	flattenedr%  expectedZtransrk   roZinitlstyZcontig_bytesr   r.  Zmmr;   )r!  r+  r,  r8   r   rb   r   r-  r  r   r  r   r  r<   verify  s   







&
,zTestBufferProtocol.verifyc                 C   sl  dd }|j r||ts|||tr(|jr|||tr8|jr|||trH|jr|||tsX|j	s|||t
sh|jr|||ts||tr| jtt||d d S t|tst|jr| }nt|td}| }||trdn|j }|j}	|j}
|j}||tsd}	| }||tsd}||tr$|jnd}||t
r:|jnd}t||d}| j|||
|	||||||d
 d S )	Nc                 S   s   | |@ |kS r   r;   )reqflagr;   r;   r<   match  s    z/TestBufferProtocol.verify_getbuf.<locals>.matchgetbufFr>   r*   r;   )	rW   r   r8   r(  r   r   r   rb   r  )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assertRaisesr1  r   r~   rx   r   r  r;  r   r   r   r   r   r@  )r  Zorig_exr+  rA  r  rC  rb   r  r>  r8   r   r   r   r   r;   r;   r<   verify_getbuf  sh    		
z TestBufferProtocol.verify_getbufc                    s  t ttttttttt	t
ttttf}dd tdD dfg ddfg ddfdd tdD d	ff}g g d
fg dg d
fdgg d
fdgdgdfdgdgd
fdgdgdfddgg d
fddgddgdfddgddgdfddgddgdff
}d
ttttB tttB f}d
ttttB f}|D ]\}}t| |D ]x\}}	}
 fdd|	D }	|
 9 }
|D ]J}|	rl|t@ rlqR|s|t@ rqR|r|n|d
 }t|||||	|
d}|r|d d d nd }t|}|rt|}|jd
ks|jdkr|r|	r| || |r.|jdkr.|r.|	r.| || |D ]h}|D ]\}| ||||B  | ||||B  |r:| j||||B dd | j||||B dd q:q2qRq(qg d}t|dgtd}| tt| tdgdg}t|td}| jtt|td | jtt|td | jtt|td | jtt|td | jtt|td | jtt|td t|td}g dg dfD ]}d
tfD ]p}t|||tB d}| t |d | t |d  |D ]2}t||d}| t |d | t |d  qq|qpd S )!Nc                 S   s   g | ]}|d  rdndqS )r   TFr;   rz   rX   r;   r;   r<   r{   
  r|   z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>   r   r*   r   r  r   r   r   r4   r3   	   re      rT  r   r   c                 S   s$   g | ]}|d  rd| nd| qS )r   r%   r$   r;   rS  r;   r;   r<   r{     r|   r   r   )r*   r  r*   r   rW  r   r   r  r   r*   r3   c                    s   g | ]}|  qS r;   r;   r   r   r;   r<   r{   (  r|   )r   r   r   r   r   T)r  r   r   rV  rD  )r*   rT  r*   )r4   r   r4   r  r   )!rM  rN  rO  PyBUF_SIMPLErG  rI  rK  
PyBUF_FULLr;  PyBUF_RECORDSPyBUF_RECORDS_ROPyBUF_STRIDEDPyBUF_STRIDED_ROPyBUF_CONTIGPyBUF_CONTIG_ROra   r   r5  ND_PILrF  rP  r5   calcsizer   rM   r   r'  rR  ND_GETBUF_FAILrQ  r1  r8  r4  )r  requestsZ	items_fmtZ	structureZndflagsZ
real_flagsrn   r8   r   r   r   r   Z_itemsex1ex2m1m2rA  bitsr+  baser  r%  r;   r   r<   test_ndarray_getbuf  s    




$z&TestBufferProtocol.test_ndarray_getbufc           
   	   C   s  t dgdg}t dgdgtd}t |j|jfD ]H}| t|h d | t|g d | t|g d | jt|g ddhd	 | jt|g ddgdhd
 | jt|g ddgg d | jt|dgdgi d | jt|dgdgi d | jt|dgdgi d | jt|dgdgdgtd | jt|dgg td | jt|g dgd	 | jt|dgdgdd | jtj	|dgdgdd | jt|ddgdgdd | jt|g ddgdd t
d }| jt|dg| dg| d	 | jt|dgdgd	 | jt|g ddgd	 | jt|dgdd gd	 | jt|g dddgdd | jt|g ddgdgd
 | jt|dgdgdd gd
 | jt|ddgddgdgd
 | jt|g ddgdgdd | jt|g ddgdd | jt|g ddgddd | jt|g ddgd d | jtj	|dgdgd!d g d"}| jt||ddgd#d$gd%d& | jt|t d'd( | jt|dgdgtd | t|dg q.| jtt d)td* t dgdgtd}| jt|jdgdgtd t d)}| t|jdgdg | t|j t dgdg}|dgdg t|}| t|jdgdg | t|j |  |  | t|j ~| tti g d | ttd)i  t ttd+dgd+ d	}| tt|d%g t ttd,ddgd	}| tt|g d- | tt|ddg | tt|d#dg | tt|d.dg t g ddgdd}t |td*}| t|j t g ddgdd}t |}	t |	}| t|j t d/gdgd0d}| t|j t
}t tt|dg| d	}| t|j t dgdgd	}| ttddddd% | tt|d1d2 | tt|d.d2 | tt|td3 | tt|td4 | tt|td5 | tt|d6d7 t dgdgd	}| ttddddd% | tti | | tt|i  t dgdgd	}| ttddddd% | tti d7 | tt|d8 d S )9NrV  r*   r   >   r*   r   r  )r*   r   rJ   )r*   r   r  r   r*   r   r  r  r   r   r   r   r  rZ  )r   rE  r   r   r   ZXXXr   i  r   r   r  r   ZQLr   rJ   r"   r*   r   r  r   r   rH   r*   r   r  r   r   r   r   r   )r   r   r   r>   z@#$)
r*   r   r  r   r   r   r4   r3   rV  re   rX  r   )r   r   r   r   r      123rD  r   rT  )r   r  r   r)   )r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   r*   ZLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLxyzr     u    Z   r     )r   ND_VAREXPORTpushrQ  r  r5  rc  r   r5   r7   ZND_MAX_NDIMOverflowErrorr   r\  r1  rF  poprM   releaser/  r   ra   r[  r  memoryview_from_bufferr6  r7  UnicodeEncodeErrorr9  r4  )
r  r  Zndmr   r   rn   rD   r+  rg  rh  r;   r;   r<   test_ndarray_exceptionsf  s    z*TestBufferProtocol.test_ndarray_exceptionsc                 C   s   t tdD ]}dgd }tg ddgtd}t||d< tddD ]$}|jg ddgd t|||< qDtdD ]}|||    qr| t|j	 ~qd S )Nr   r   rp  r  rZ  r*   r   )
r   ra   r   r  rM   r  r  rQ  r1  r  )r  permrD   r  r   r;   r;   r<   test_ndarray_linked_list  s    
z+TestBufferProtocol.test_ndarray_linked_listc                 C   sL   t dD ]>\}}}t|}t|d|d}| j|d ||dddd|d	 qd S )Nr   r;   r  TrW   r   r8   r(  r   r   r   rb   )ru   r5   rd  r   r@  )r  r8   Zscalarrl   r   r  r;   r;   r<   test_ndarray_format_scalar  s    
z-TestBufferProtocol.test_ndarray_format_scalarc                 C   sl   t dd}t|D ]T\}}}t|}dtfD ]6}t||g||d}| j|d ||dd|f|f|d	 q.qd S )Nr*   re   r   r  Tr  )r   ru   r5   rd  rc  r   r@  )r  rs   r8   rn   rl   r   r   r  r;   r;   r<   test_ndarray_format_shape  s    


z,TestBufferProtocol.test_ndarray_format_shapec                 C   s   t dd}t|D ]\}}}t|}tddD ]}|dkr>q0t|d d | g}|| g}|dk rp||d  nd}	dtfD ]>}
t|||||	|
d}| j|d ||dd|||d d | d	 q|q0qd S )	Nr*      r   r   r   Tr  )	r   ru   r5   rd  ra   r]   rc  r   r@  )r  rs   r8   rn   rl   r   r   r   r   r   r   r  r;   r;   r<   test_ndarray_format_strides*  s$    


z.TestBufferProtocol.test_ndarray_format_stridesc                 C   s@   g d}t |ddd}t |ttB d}| | t|d d S )NrU  ro  )r*   r  r   rD  )r   rI  rP  r'  r  r   )r  rn   r+  r  r;   r;   r<   test_ndarray_fortran?  s    z'TestBufferProtocol.test_ndarray_fortranc                 C   s^  t dD ]N}dd t |D }t|}t|D ]$}t|\}}}t|}dtfD ]}	|dkrl|	tkrlqVt||||	d}
t|||d}t	||}| j
|
d ||d||||d	 t|rt|||d	}t|ttB d
}
| |
jdk |
 }| j
|d ||d||||d	 t||||	tB d}
t|||d}t||}| j
|
d ||d||||d	 qVq0qd S )Nr   c                 S   s   g | ]}t d dqS )r   re   r   r   r;   r;   r<   r{   G  r|   z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>r   r  r   Tr  r  rD  r;   r  )ra   r   r   rq   r5   rd  rc  r   r   r   r@  rx   rb  rP  r8  r   r  r5  r   )r  r   shape_trs   r   r8   rn   rl   r   r   r  r   rb   r+  mvr;   r;   r<   test_ndarray_multidimE  sJ    



z(TestBufferProtocol.test_ndarray_multidimc                 C   s  t dgdgd}| t|jdd t|}| || | t|jdd t dgdgtd}| t|jd t|}| || | t|jd t dgdgtd}| t|j	d | t|jdd t|}| || | t
|j	d | t
|jdd g d}t |t|gdtd}| tj|jd	d
 | t|jdd t|}| || | t|jd	d
 | t|jdd g d}t |t|gdtd}| t|jd	d
 | tj|jdd d S )Nr*   r   r3   rZ  r)   r*   r   r  r   r   r   r4   r3   r   r  r   i,  )r      )r*   r   ro  )r   r   ZLQ)s    1r  )r   rQ  r  __setitem__rM   r'  r   __delitem__r  __getitem__
IndexErrorr]   r5   r7   r   )r  r  r  rn   r;   r;   r<   test_ndarray_index_invalidr  s:    z-TestBufferProtocol.test_ndarray_index_invalidc                 C   sJ  t ddtd}t|}| || |d }| |d |d }| | |  |d }| |d |d }| | |  | t|jd | t|jd | t|jdd | t|jdd | | d | | d d|d< | | d d|d< | | d d|d< | | d d|d< | | d d S )	Nr*   r;   rZ  .r   r3   rV  r   )	r   r   rM   r'  r  rQ  r  r  r  )r  r  r  rX   r;   r;   r<   test_ndarray_index_scalar  s"    z,TestBufferProtocol.test_ndarray_index_scalarc                 C   sR   t ttdddgtd}t |td}| t|jd | t|jtddd d S )	Nr3   r   r   rZ  rD  r*   r  r   )	r   r   ra   r   ra  rQ  r1  r  r   )r  r+  r  r;   r;   r<   test_ndarray_index_null_strides  s    z2TestBufferProtocol.test_ndarray_index_null_stridesc                 C   s   t dD ]\}}}t|dg|d}tddD ]}| || ||  q,| t|jd | t|jd t|rt|}| || tddD ]}| || ||  q| t|jd | t|jd qt dD ]\}}}t|dgt	|d}t|t
tB d}tddD ]}| || ||  qt|r| }| ||t tddD ]}| || ||  qNqg d}t|dgd}t|td}tddD ]}| || ||  qg d}t|dgd}t|td}tddD ]}| || ||  qd S )	Nr   r  r  r#  rD  rt  r   )ru   r   ra   r'  rQ  r  r  rx   rM   r   ra  rP  r  r&  __eq__NotImplementedrb  r[  )r  r8   rn   rl   r  r   r  r+  r;   r;   r<   !test_ndarray_index_getitem_single  sB    z4TestBufferProtocol.test_ndarray_index_getitem_singlec                 C   s  t dD ]\}}}t|dg|td}tdD ]}|||< |||< q,| | | | t|jd| | t|jd| t	|sqt|dg|td}t
|}| || tdD ]}|||< |||< q| | | | t|jd| | t|jd| qt dD ]\}}}t|dg|td}tddD ]&}||d  ||< ||d  ||< q(| | | t	|snqt|dg|td}t
|}| || tddD ]&}||d  ||< ||d  ||< q| | | qd S )Nr   r  r  r  r   r*   )ru   r   r   ra   r'  r  rQ  r  r  rx   rM   )r  r8   rn   Zsingle_itemr  r   r  r;   r;   r<   !test_ndarray_index_setitem_single  sD    


z4TestBufferProtocol.test_ndarray_index_setitem_singlec              
   C   s  d}t |}t|D ]}t|\}}}dtfD ]}t||||d}t||}	t|d  |d D ]}
| |	|
 ||
   t|d  |d D ]b}| |	|
 | ||
 |   t|d  |d D ](}| |	|
 | | ||
 | |  qqqdt||||t	B d}t
||}	t|d  |d D ]}
| |	|
 ||
   t|d  |d D ]d}| |	|
 | ||
 |   t|d |d D ]*}| |	|
 | | ||
 | |  qq\q,q0qd S )Nr   r  r   r   r  r*   r   )r   r   rq   rc  r   r   ra   r'  r  r5  r   )r  r  rs   r   r8   rn   rl   r   r  rb   r   r   kr;   r;   r<   #test_ndarray_index_getitem_multidim  s.    
 *
 z6TestBufferProtocol.test_ndarray_index_getitem_multidimc                 C   s   t ddd}| ttdt  t|}| || | ttdt  tdD ]\}}}t |dg|d}t|D ]&\}}| |||  | 	||v  qnt
|rLt|}t|D ]&\}}| |||  | 	||v  qqLd S )Nr*   r;   r   z1 in ndz1 in mvr   r  )r   rQ  r  evallocalsrM   r'  ru   r   r8  rx   )r  r  r  r8   rn   rl   r   r   r;   r;   r<   test_ndarray_sequence9  s    z(TestBufferProtocol.test_ndarray_sequencec              	   C   s  g d}t |dgtd}t|}| t|jtddd| | t|jtddd| t |dgtd}t |dgtd}t |td}| t|jtddd| t |dgdtd}t|}| t	|j
tddd | t	|j
tddd t |d	d
gdtd}t|}| t	|j
tdddtdddf | t	|j
tdddtdddf | t|j
d | t|j
dtdddf | t|j
tdddi f | t|j
tdddtdddf | t|j
d t |dgdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |dgdtd}t |dgdd}	t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |d	d
gdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | g dg dg | t|jtddd|dd  t |dgdtd}t |dgdd}t|}t|}| t	|jtdd	d|dd  | | | | t	|jtdd	d|dd  | | | | tttddd	i  | ttdd | t	ttdddd
 t |dgdtd}
| t|
j t |dgdd}t |td}
| t|
j d S )Nr  r3   rZ  r   r*   rD  r   r  r   r   z@%$r   r   r  r4   ru  )r   r   r4   r3   z###########)r   r   rM   rQ  r  r  r   rO  r1  r   r  r	  r'  r  r   rc  add_suboffsetsr[  )r  rn   xlmlr+  xrr  r  mryrrX   r;   r;   r<   test_ndarray_slice_invalidL  s    


"""""
""z-TestBufferProtocol.test_ndarray_slice_invalidc                 C   s  g d}t |dgdtd}t |dgdd}|dd |dd< | | | t|}t|}| || | || |dd |dd< | | | t |ddgdtd}t |ddgdd}|d	d
ddf |d	d
d
d
f< | | t|ddg d S )NrU  rT  r   r  r  rV  r   r  r*   r   )r   r   r'  r  rM   r   )r  rn   rX   r?  r  r  r;   r;   r<   test_ndarray_slice_zero_shape  s    $z0TestBufferProtocol.test_ndarray_slice_zero_shapec                 C   s  d}t |}t|}t|D ]}t|\}}}t|}dtfD ]}	t||||	d}
t||}t	||D ]}d }zt
||}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|
| }W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || qh| | | qhq@qd S Nr  r   r  )r]   r   r   rq   r5   rd  rc  r   r   r  r   	Exception	__class__r&  r'  r  )r  r  r   rs   r   r8   rn   rl   r   r   r  rb   r   listerrr  enderrndslicedr;   r;   r<   test_ndarray_slice_multidim  s.    

z.TestBufferProtocol.test_ndarray_slice_multidimc                 C   s2  d}t |}t|}t|D ]}t|\}}}t|}t|||d}	|	  t|||d}
|
  t|
}t	||}t
||D ]}d }zt||}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|	| }W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || q| | | qqd S )N)r   r  r   r   r  )r]   r   r   rq   r5   rd  r   r  rM   r   r  r   r  r  r&  r'  r  )r  r  r   rs   r   r8   rn   rl   r   r  r+  r  rb   r   r  r  r  r  r  r;   r;   r<   'test_ndarray_slice_redundant_suboffsets  s4    

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc                 C   s  t dD ]\}}}tdD ]}tdD ]}dtfD ]}|tB }t|dg||d}t|dg||d}	t|	}
d }d }|d d  }z0|| }|| }|| ||< t|t|k}W n* ty } z|j}W Y d }~n
d }~0 0 d }z|| ||< W n, ty" } z|j}W Y d }~n
d }~0 0 |r8| 	|t
 n| | | | 	|| t|s`q8d }z|
| |
|< W n, ty } z|j}W Y d }~n
d }~0 0 |r| 	|t
 q8| |
 | | |
| | 	|| | j|
|	|j|d|j|j|j| d	 q8q*qqd S )Nr   r   r  Fr  )ru   r   rc  r   r   rM   r]   r  r  r&  r   r'  r  rx   r@  r   r   r   r   )r  r8   rn   rl   lslicer   r   r   r  r+  r  ZlsterrZdiff_structurerb   ZlvalZrvalr  r  Zmverrr;   r;   r<    test_ndarray_slice_assign_single  sX    
z3TestBufferProtocol.test_ndarray_slice_assign_singlec                 C   s*  d}t |}t|}t|D ]}t|\}}}dtfD ]}ttD ]}t||\}	}
t||||t	B d}t
||}d }zt|||	|
}W n* ty } z|j}W Y d }~n
d }~0 0 d }z||
 ||	< W n* ty } z|j}W Y d }~n
d }~0 0 |s|r| || qD| | | qDq8qd S r  )r]   r   r   rq   rc  ra   
ITERATIONSr   r   r   r   r   r  r  r&  r'  r  )r  r  r   rs   r   r8   rn   rl   r   r   r   r  rb   r  r-  r  r  r;   r;   r<   "test_ndarray_slice_assign_multidim2  s2    
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc                 C   s8  t tD ](}td D ]}t|}t|dttd}| t	|  t
||}t|||}| }t|}t|r| }	| |	| |d dkrt|||td}
|
 }| || t|
}t|r| ||
 | }	| |	| tr|d }d|v rqt|||}| j|d |j|d|j|j|j| d		 qqd S )
Nr?   Tr   r   r   r   rn  r  Fr  )ra   r  rQ   r5   rd  r   MAXDIMMAXSHAPEr8  r   r   r   r  rM   rx   r'  rc  r   r   r@  r   r   r   r   )r  rl   r8   r   rt   rn   rX   Zxlistr  Zmvlistr?  Zylistr   zr;   r;   r<   test_ndarray_randomS  sB    

z&TestBufferProtocol.test_ndarray_randomc                 C   s   t tD ]}td D ]}t|}t|dttd}| t	|  t
||}d}zt|||}W n* ty } z|j}W Y d }~n
d }~0 0 | | trd}	zt|||}
W q ty } z|j}	W Y d }~qd }~0 0 qqd S )Nr?   Fr  )ra   r  rQ   r5   rd  r   r  r  assertFalser   r   r   r  r  r8  r   r   )r  rl   r8   r   rt   rn   r  rX   r  Z	numpy_errr?  r;   r;   r<   test_ndarray_random_invalid|  s*    


z.TestBufferProtocol.test_ndarray_random_invalidc                 C   s2  t tD ]"}td D ]}t|}tttd\}}}}t|d|d}t|d|d}	| 	t
|  | 	t
|	  t||}
t||	}t|
||}t|||	}|| ||< | }| }t|}t|}| | | | | | |d dkr|	d dkrt|
||td}t|||	td}|| ||< | }| }| || | || t|}t|}| | | | | | trd|v sd|v rqt|
||}t|||	}|| ||< t|st|	s| j|d |j|d|j|j|j| d		 | j|d |j|d|j|j|j| d		 qqd S )
Nr?   r  Tr   r   r   rn  Fr  )ra   r  rQ   r5   rd  r   r  r  r   r8  r   r   r   r  rM   r'  rc  r   r   r   r@  r   r   r   r   )r  rl   r8   r   r   r   r   r   tltrZlitemsZritemsr  r  ZxllistZxrlistr  r  Zylr  ZyllistZyrlistZzlZzrr;   r;   r<    test_ndarray_random_slice_assign  sb    






z3TestBufferProtocol.test_ndarray_random_slice_assignc                 C   sv   g d}t |ddgtd}t |}| |jt@  | |j| | |jd | |j	 | |j
 | |j d S )NrU  r  r   rZ  )r   r   )r   rc  r8  r   r&  rW   r'  r  r  rH  rJ  rL  )r  rn   r  r+  r;   r;   r<   test_ndarray_re_export  s    z)TestBufferProtocol.test_ndarray_re_exportc              	   C   s   dt fD ]}tg ddg|d}t|}| || | | g  | | g  tg dg d|d}| | g  tg dg d|d}| | g g g g tg dg d|d}| | g g g gg g g gg g g gg qd S )Nr   rp  rZ  r   r  r  r  r   r  )r  r  r   rc  r   rM   r'  r  r  r   r  r  r;   r;   r<   test_ndarray_zero_shape  s    
z*TestBufferProtocol.test_ndarray_zero_shapec                 C   sd   dt fD ]V}tdgdgdg|d}t|}| || | | g d | | g d qd S )Nr   r*   r   rr  )r*   r*   r*   r*   r*   r  r  r;   r;   r<   test_ndarray_zero_strides  s    z,TestBufferProtocol.test_ndarray_zero_stridesc                 C   s>   t ttddgdd}| |jd | | g d d S )N   r  r4   rq  )r4   r3   rV  )r   r   ra   r'  r   r  r  r  r;   r;   r<   test_ndarray_offset  s    z&TestBufferProtocol.test_ndarray_offsetc                 C   s>   dt fD ]0}tttddg|d}| }| || qd S Nr   r  rZ  )rc  r   r   ra   r  r'  )r  r   r  rD   r;   r;   r<   #test_ndarray_memoryview_from_buffer  s    z6TestBufferProtocol.test_ndarray_memoryview_from_bufferc                 C   sP   dt fD ]B}tttddg|d}tdD ]}| || t||g q,qd S r  )rc  r   r   ra   r'  r/  )r  r   r  r   r;   r;   r<   test_ndarray_get_pointer
  s    z+TestBufferProtocol.test_ndarray_get_pointerc                 C   s\   t ttdg dd}t |ttB d}| | |  t|}| | |  d S )Nr  )r   r   r   r   rD  )r   r   ra   rO  rP  r'  r  rM   )r  r+  r  rD   r;   r;   r<    test_ndarray_tolist_null_strides  s
    z3TestBufferProtocol.test_ndarray_tolist_null_stridesc                 C   s   |  tdd tttdddgd}tttdddgd}|  t|| tdgdgdd	}| t|d
 | td
| d S )Nry  s   456rT  r  r   r   r*   r   r  rP   )r  r9  r   r   ra   r8  )r  rX   r?  r;   r;   r<   test_ndarray_cmp_contig  s    z*TestBufferProtocol.test_ndarray_cmp_contigc                 C   s  t  dg d}t|}| tt| tttd}tttddgd}| t|t| tttdddgd}| t|t| tttdg dd}| t|t| tt	ttdddgd}tttdddgt
d}| t|t| tt	ttdg d	d}tttdg d	t
d}| t|t| tttd}tttdg d
td}| t|t| tttdg d
dd}| t|t|  d S )Nr   rp  rT  r   r  r   )r  r   r   rZ  r   r  r   r   r   r  r  )rK   r   rQ  r   hashrR   r   ra   r'  r   r5  rc  r   )r  rF   r  r   r;   r;   r<   test_ndarray_hash%  s*    z$TestBufferProtocol.test_ndarray_hashc                 C   sT  t ttttttttt	t
tf}| tti dt tdddtd}dD ]*}|D ] }t|||}| ||  qJqBtdgdgdtd}dD ]&}|D ]}t|||}| |d	 qqtttd
g ddtd}dD ]&}|D ]}t|||}| |d	 qqdtfD ]}tdgdgd|tB d}| }dD ]*}|D ]}t|||}| || q.q&tg ddgd|tB d}| }dD ]*}|D ]}t|||}| || qqxqtg ddgdgtd}| }dD ].}ttfD ]}t|||}| || q֐q|d d d }| }dD ]J}|D ]>}zt|||}W n tyJ   Y qY n0 | || qqttd}dtfD ]}t|ddg|tB d}trtt|ddgd|dkrdndd}	|tkrtt|ddgddgtd}
|
 }n| }|D ]}zt|d|}W n ty&   Y qY n0 | || tdd |D ddgtd}| t|t| tr| ||	jdd q|dkrtt|ddgddgtd}
nt|ddgtd}
|
 }ttt ttfD ]}zt|d|}W n ty    Y qY n0 | || tdd |D ddgttB d}| t|t| tr| ||	jdd q|tkrt|ddgtd}
|
 }n| }ttt ttfD ]}zt|d|}W n ty   Y qY n0 | || tdd |D ddg|tB d}| t|t| tr| ||	jdd qqttttdddgttB d}t|dt}| ||  tdd |D ddgtd}| t|t| t|dt}tt|ddgddgtd}
| ||
  td d |D ddgttB d}| t|t| t|dt}| ||  td!d |D ddgtd}| t|t| d S )"Nr  rV  r;   r   r  r  r*   r   r|   r3   r   r   r4   r   rp  r  r   r   rr  r   rT  r   rZ  r   r   )r   r   r   r%  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{     r|   zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>r$  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{   	  r|   r  c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{   	  r|   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{   *	  r|   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{   1	  r|   c                 S   s   g | ]}|qS r;   r;   r   r;   r;   r<   r{   7	  r|   )rM  rN  rO  r[  r\  r;  r]  r^  r_  r`  ra  rb  rQ  r  r:  r   r   r'  r   r   ra   r5  r1  r   r   r   rM   Ztostringrc  )r  rf  r  r%  Zrequestr   r   Zndbytesrb   ZnarX   r=  r?  r;   r;   r<   test_py_buffer_to_contiguousJ  s    B


 

  z/TestBufferProtocol.test_py_buffer_to_contiguousc                 C   s|  dg fg ddgft tdg dfg}|D ]H\}}t||d}t|}| |j | |j t|}t||dd}t	||}| j
||dd	d
||||d	 t|}	| j
|	|dd	d
||||d	 t|ttB d}
| |
jd |
 }| j
|d dd	d
||||d	 t|td}
| |
jd | |
jd | |
jd |
 }|dkrR|gn|}| j
|d dd	d
d|jgd|d	 q,|D ]\}}t||td}t|}| |j | |j t|}t||dd}t||}| j
||dd	d
||||d	 t|}	| j
|	|dd	d
||||d	 q||dd  D ]v\}}t||td}t|}t|}t	||}| j
||dd	d
|||j|d	 t|}	| j
|	|dd	d
|||j|d	 q"| ttdd | tti  tg ddgd}t|td}
| tt|
 t|ttB d}
| tt|
 tdgd dgd dd}
| tt|
 | t|
j | tt|
td | tt|
td | tt|
d d d td d S )NrV  rp  r  r  r  r   r*   r   r   Tr  rD  r;   r>   r   r*   rZ  r     9rX   r"   r   r  r   )r   ra   r   rM   r8  rH  rL  r]   r   r   r@  rb  rP  r'  r   r  r[  r   r   r   r5  rJ  r   rc  rQ  r  r1  r   r6  r7  )r  Zitems_shapern   r   r+  rD   r   r   rb   rj  r  r;   r;   r<   test_memoryview_construction:	  s    (





z/TestBufferProtocol.test_memoryview_constructionc                 C   s   g d}g dg dg dfD ]4}t ||d}| |j t|}| t|jd qtddD ]<\}}}td}||}| |	 d | |
 g  q^d S )	Nrp  r  r  r   r   r*   rM   r|   )r   r8  rH  rM   rQ  r  r  ru   r'  r   r  )r  rn   r   r+  msrcr8   rl   rD   r;   r;   r<   test_memoryview_cast_zero_shape	  s    
z2TestBufferProtocol.test_memoryview_cast_zero_shapec                 C   s   | j }tj}d}d}ttd}|td||d|   t|ddgdd	}|t|||d|   t|g d
dd	}|t|||d|   d S )NzPnin 2P2n2i5P PZ3nr3   r|   r*   r   r   r   r  )r   r   r   r  )check_sizeofr   Zcalcvobjsizer   ra   rM   r   )r  checkZvsizeZbase_structZper_dimrn   rF   r;   r;   r<   test_memoryview_sizeof	  s    z)TestBufferProtocol.test_memoryview_sizeofc                 C   s  G dd dt }G dd dt }dd }|d|ddtd	d
tddg dh dddiddddd tddtdtddddd|dd g}tddD ]6\}}}t|dg|td}t|dg|td}	t|}
t	
||	d| ||
d< | |
d |	d  t	|}d|v rq|D ]}d }zt	
||	|| W n t	jyH   t	j}Y n0 d }z||
d < W n0 ttfy } z|j}W Y d }~n
d }~0 0 |s|r| |d  | |d  n| |
d  |	d   qqd S )!Nc                   @   s   e Zd Zdd Zdd ZdS )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTc                 S   s
   || _ d S r   valr  r  r;   r;   r<   __init__	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__c                 S   s   | j S r   r  r  r;   r;   r<   __int__	  s    zETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)__name__
__module____qualname__r  r  r;   r;   r;   r<   INT	  s   r  c                   @   s   e Zd Zdd Zdd ZdS )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXc                 S   s
   || _ d S r   r  r  r;   r;   r<   r  	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__c                 S   s   | j S r   r  r  r;   r;   r<   	__index__	  s    zGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r  r  r  r  r  r;   r;   r;   r<   IDX	  s   r  c                   S   s   dS )Nr4   r;   r;   r;   r;   r<   r   	  r|   z;TestBufferProtocol.test_memoryview_struct_module.<locals>.frV  y@      @z-21.1gffffff(@r   r   rp  >   r   r   r   r4   r3   r;   rV  TF   as   abcrF   abcc                 S   s   | S r   r;   )rX   r;   r;   r<   <lambda>	  r|   zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>re   rM   r  r   r   r*   )objectr   r	   Ellipsisr   ru   r   r   rM   r5   r   r'  rd  r7   r  r   r  ZassertIsNot)r  r  r  r   valuesr8   rn   ro   r+  r  rD   r   r   Z
struct_errZmv_errr  r;   r;   r<   test_memoryview_struct_module	  sH    

z0TestBufferProtocol.test_memoryview_struct_modulec                 C   s>   t g ddgdgd}| |j t|}| t|jd d S )Nrp  r  r   r   r   )r   r  rH  rM   rQ  r  r  )r  r+  r  r;   r;   r<   !test_memoryview_cast_zero_strides
  s    z4TestBufferProtocol.test_memoryview_cast_zero_stridesc              	   C   s0  t D ]}tdrd| n|}t|}t D ]^}tdr>d| n|}t|}tttdd| g|d}t|}| t	|j
|d| g q*qtdD ]\}}	}
t|	dg|d}t|}tdD ]T\}}
}
t|s| t|j
|d| g qt|st|s| t	|j
|d| g qqtd}td}tttd| dd|gdd}t|}| jt	|j
dd|gdd tttd	g d
d}t|}| t	|j
 | t	|j
ddd | 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 tddgdgdd}t|}| t|jd | t|jdd | t|j tttd	g d
d}t|}| jt	|j
di d tttd	d	gd}t|}| jt|j
ddgd | jt|j
ddgd | jt|j
dg dd | jt|j
dg dd | jt	|j
dg d d ttd!d" td#D g d$d}t|}| jt	|j
d%g d&d tttd'd'gd%d}t|}| t|j
d%dgd'  ttd(d" td#D d#gd}t|}| jt	|j
d%g d&d ttd)d" td#D d#gd}t|}| jt	|j
dg d&d tttd'd'gd%d}t|}tttd'd'gdd}t|}tjd*kr| t	|j
dg d+ | t|j
dg d, | t|j
d%g d- nB| t	|j
ddd.g | t|j
dg d/ | t|j
d%g d0 d S )1Nr   r?   r/   r  r*   r   r   r   x   rt  r   r  Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIr  ro  ZIIr   r3   r   r)   r   )r   r  r   r   r   r4   r   )r   r  r   r   r   r4   r   )r   r  r   r   r   r4   rX   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r{   ]
  r|   zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i  )r  r   r4   rW  r   )r   r  r   r   r"   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r{   g
  r|   c                 S   s   g | ]}d qS r  r;   r   r;   r;   r<   r{   l
  r|   l    )r4   r4   I   r,   iQ  iAj i	 )   r     r  r3   )r  r  r  r  r   )r  r  r/   r/   r   )r  r  r/   r3   r   )NON_BYTE_FORMATr   r5   rd  r   r   ra   rM   rQ  r  r  ru   rx   r   rv   r	  r  r  r  r  r  maxsize)r  ZsfmtZsformatZssizeZdfmtZdformatZdsizer+  r  Zsitemsrl   Zsize_hZsize_drD   r  ri  rj  r;   r;   r<   test_memoryview_cast_invalid
  s    



 "  z/TestBufferProtocol.test_memoryview_cast_invalidc                    sl  ddd fddd fddd ff  fdd	}t d
}tdg d
d}t|dd\}}t|}|d}j||dddd|d|dd
 t d
}tdg| |gdd}t|d
|g d\}}t|}|jd
g d}j|||d
dddd|dd
 tddD ],\}	}
}t|	|
}t|}||||
|	 qtddD ]4\}	}
}t|
dg|	t	d}t|}||||
|	 q2d S )Nr   c                 S   s   t |  S r   r   r   r+  r;   r;   r<   r  
  r|   z9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>r   c                 S   s   dd t |  D S )Nc                 S   s    g | ]}|d kr|d n|qS )r,   r!   r;   rS  r;   r;   r<   r{   
  r|   MTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>r  r  r;   r;   r<   r  
  r|   r   c                 S   s   dd t |  D S )Nc                 S   s   g | ]}t t|d qS )zlatin-1)rR   chrrS  r;   r;   r<   r{   
  r|   r  r  r  r;   r;   r<   r  
  r|   c           
         s   t |} D ]~\}}||}|| }j|| d|ddd| gd|dd
 ||}	|	|  |  }j|	| ||dddg|f|dd
 qd S )Nr*   Fr0   r  T	rW   r   r8   r(  r   r   r   rb   r  )r5   rd  r  r@  r'  r  )
r+  rD   rn   r8   srcsizerN   Zto_bytelistrj  rb   m3Zbytespecr  r;   r<   iter_roundtrip
  s"    



z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtripr   rV  r  r*   Tr  r  r   r   r;   r0   rK   rM   r  )
r5   rd  r   r   rM   r  r@  ru   rK   r   )r  r	  r  r+  Z	destitemsZ	destshaperD   rj  Zdestsizer8   rn   rl   r;   r  r<   test_memoryview_cast
  sB    





z'TestBufferProtocol.test_memoryview_castc                 C   s  t  D ]}td D ]}|dkr$qdtd | }t|}t|| }t|rVdnd}t||D ]:\}}}	t|}
|dkr|gng }||
g }t|||d}t	|}t
||||d	\}}|d u r| t|j|| qd|d
krqdt|||d}|j||d	}t|}|j}| }| j||||d||||dd
 ||}|j||d	}t|}|j}| }| j|||
|d||||dd
 | j|||
|d||||dd
 qdqqtrG dd dtj}|dd}t	|}|d}| |j| | |jd | |jd | |jd | |j|jf | |jd | |jd td}t	|}|d}| |j| | |jd | |jd | |jd | |j|jf | |jd | |jd d S )Nr?   r   )r>   r?   r   rM   rN   r   r  r   r   Tr  c                   @   s    e Zd ZdejfdejfgZdS )z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrX   r?  N)r  r  r  ctypesc_longc_double_fields_r;   r;   r;   r<   BEPoint
  s   r  r   g33333i@r   r*   Fr  r;   333333?r   )r   rQ   r   r5   rd  r   rv   ru   r   rM   r   rQ  r  r  r]   r   r  r@  r  BigEndianStructurer'  rW   r   r&  r(  r   r   r   r  r  )r  Z_tshaperV   ZtfmtZtsizer   rW   r8   rn   rl   sizer   Ztshaper+  rD   Ztitemsr  rj  r   r   rb   r  Zm4r  pointri  rX   r;   r;   r<   test_memoryview_cast_1D_ND
  s    







z-TestBufferProtocol.test_memoryview_cast_1D_NDc                 C   s2  t  dttdd}t|}| || | | |  |dd d }|dd d }| || | | |  tttdg ddd	}t|}| | |  td
dgdgdd	}t|}| t|j tdgdgdd	}t|}| t|j tg dddgdd	}t|}| t|j d S )Nr   r  r   r   r  i	  )rW  r   r4   r  r   r   r  )r   r   )r4   rW  r9      12345r*   r   )r     b   c   d   e   f)	rK   r   ra   rM   r'  r  r   rQ  r	  )r  rF   rD   r+  r;   r;   r<   test_memoryview_tolist  s(    z)TestBufferProtocol.test_memoryview_tolistc                 C   sH   t td}| }| |d |  | }| |d d S )NrV  z<memoryz	<released)rM   r   __repr__r8  
startswithr  )r  rD   r   r;   r;   r<   test_memoryview_repr2  s    z'TestBufferProtocol.test_memoryview_reprc                 C   s|   dD ]J}t d}t|d|dg}t|}| d| | d| | d| qtdg dd}t|}| ttdt  d S )	N)r   r   g            ?g      @g      "@r   rx  z9.0 in m)	floatrK   rM   ZassertInr   rQ  r  r  r  )r  r8   infr+  rD   r;   r;   r<   test_memoryview_sequence;  s    z+TestBufferProtocol.test_memoryview_sequencec                 c   sL   |  t}d V  W d    n1 s&0    Y  | t|jd|f  d S )Nz#index out of bounds on dimension %d)rQ  r  r'  str	exception)r  dimcmr;   r;   r<   assert_out_of_bounds_errorI  s
    $z-TestBufferProtocol.assert_out_of_bounds_errorc              	   C   sN  t dg dd}t|}| |d d | |d | | |d | | t|jd t dg dd}t|}| t|jd t ttd	d	gt	d
}t|}| t
|jd | t|jd | t|jd | t
|jd | t
|jd t ttdddgt	d
}t|}| |d d | |d d | |d d | |d d | |d d dD ]@}| jdd ||df  W d    n1 s0    Y  qbdD ]@}| jdd |d|f  W d    n1 s0    Y  q| t
|jd | t
|jd | t|jd  | t|jd! | t|jd | t|jd d S )"N      )@r   r  r;   .r   rp  Ziiir4   rZ  r)          @        r3   rT  r  r   r   r   )r   r   rs  rW  r   r   )rw  rY  r  rY  r*   r%  r   r  r   r)   r   r(   r   r   r   r*  r*  )r   rM   r'  rQ  r  r  r	  r   ra   r   r  r'  )r  r+  rD   indexr;   r;   r<   test_memoryview_indexP  sF    00z(TestBufferProtocol.test_memoryview_indexc              	   C   s  t dg dtd}t|}d|d< | |d d d|d< | |d d | t|jdd	 t ttd
d
gd}t|}| t|jdd t ttd
d
gtd}t|}| t	|jdd | t|jdd | t|jdd | t	|jdd | t	|jdd t
d D ]r}|dks|dkr(qt g ddg|td}t|}tdd}d||< | || d | || ||  qt g ddgdtd}t|}d|d< | |d d t g d dgdtd}t|}d!|d"< | |d" d! t d#gd"gdtd}t|}| t|jdd$ t ttd%g d&td}t|}t
d  D ]\}}|dkr^qH|dkrlqH||}|\}	}
|d'ks|dkrdd(  dd(  }	}
|d)kr| t|jd|	d"  | t|jdd* | t|jd|
 qH|d}| t|jdd+ t ttd"d"gd,td}t|}| t|jdd" t d-gd"gd.td}t|}| t|jdd" t ttd/dd0gtd}t|}d1|d2< | |d d" d1 d3|d4< | |d d d3 d5D ]@}| jd"d6 d||df< W d    n1 s0    Y  qd7D ]@}| jdd6 d|d|f< W d    n1 sD0    Y  q| t	|jd8d | t	|jd9d | t|jd:d | t|jd;d | t|jdddg d S )<Nr(  r   r  g     6@r;   g     7@.r   g333338@r4   r   r   re   rZ  r)   rV  r)  r*  rW  r+  r  r3      r?   r   r   rp  r  rw  )   1   2   3r  )TFTTr*      xr   r  rt  r   r  r   rz  s   ZxLr  r   rT  r   *   )r   r*   +   r-  r.  r/  r0  r1  r(   r2  r3  )r   r   rM   r'  rQ  r  r  r   ra   r  rQ   r   r&  rn   r  r   r	  r'  )r  r+  rD   r8   r   r  ri  Z_rangerj  lohir4  r;   r;   r<   test_memoryview_assign  s    





00z)TestBufferProtocol.test_memoryview_assignc              	   C   s  t ttddgtd}t|}| t|jtddd | t|j	tdddt
ddg | t|jd t ttddgtd}t|}| t|jtdddtdddf | t|j	tdddtdddft
ddg | t|jtdddi f | t|j	tdddi ft
ddg | t|j	tddddg dtfD ]l}t ttddgdgdt|B d	}t ttd
dgdg|d}t|}t|}|dd |dd< |dd |dd< | || | || |dd d d d |dd d d d< |dd d d d |dd d d d< | || | || |ddd d d d |ddd d d d< |ddd d d d |ddd d d d< | || | || qBd S )NrT  rZ  r   r   r*   r;   r   rW  )r   r   r   r      rr  r   r  r   rX  )r   r   ra   r   rM   rQ  r   r  r   r  r   r	  r  rc  r'  )r  r+  rD   r   rg  rh  ri  rj  r;   r;   r<   test_memoryview_slice  sN    


 
((,,z(TestBufferProtocol.test_memoryview_slicec                 C   s  dd }t ddD ]}t|dD ]\}}}t|D ]}t|D ]}t||}t||}	t|	}
| |
| | |
 |  | |
 |  | t|
t| || ||	|
| d }d }z0|| }|| }|| ||< t|t|k}W n, t	y& } z|j
}W Y d }~n
d }~0 0 d }z|
| |
|< W n, t	yh } z|j
}W Y d }~n
d }~0 0 |r~| |t qD|s|r| || qD| |
| | |
 |  | |
 |  || ||	|
| qDq6q"qd S )Nc           	      S   s   t |D ]\}}|| }|| }| || |||< ||krV| || | || n| || | || |||< | || | || |||< |||< qd S r   )r   r'  assertNotEqual)	ZtestcaserF   r   rD   
singleitemr   rl   Zaimir;   r;   r<   cmptest,  s    z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr*   r   rK   )ra   ru   r   rK   rM   r'  r  r   r]   r  r  r&  r   )r  rE  r   r8   rn   rC  r  r   rF   r   rD   Z	array_errZhave_resizeZalarr  Zm_errr;   r;   r<   test_memoryview_array*  sF    z(TestBufferProtocol.test_memoryview_arrayc           	      C   s  t  dg d}t  dg d}t|}t|}dD ]0}| t|||t | t|||t q4t|}|  | || | || | || t|}t|}|  | || | || t|}| |g d tdgdgdt	d}d	t
d
f|d< | t|| t  dd}t|}| || | || trG dd dtj}|dd}t|}t|}| || | || | || | t|j d S )Nr   rp  )r*   r   r4   )__lt____le____gt____ge__r,  r*   zl x d xr  r   r   r   urz  c                   @   s    e Zd ZdejfdejfgZdS )zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrX   r?  Nr  r  r  r  r  r  r;   r;   r;   r<   r    s   r  r   r  )rK   rM   r&  getattrr  r  r'  rB  r   r   r   r  r  rQ  r	  r  )	r  rF   r   r   wattrr  r  r  r;   r;   r<   %test_memoryview_compare_special_casesh  sF    
z8TestBufferProtocol.test_memoryview_compare_special_casesc                 C   sn  t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d|d< | || | || | || | || | || | || | || | || t tt	ddgtt
B d	}t tt	ddgtt
B d	}t|}| || d
|d< | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d S )N  @Lr  r   r  i  r;   rT  rZ  r   rV  )rR  r  r  ZLf5sZhf5s)i?r  r  )r   r   rM   r'  r  __ne__rB  r  r   ra   rc  )r  nd1nd2r   rO  r  r+  rD   r;   r;   r<   !test_memoryview_compare_ndim_zero  sj    z4TestBufferProtocol.test_memoryview_compare_ndim_zeroc                 C   sX  t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  d S )N@    'r   @hr  )rY  rZ  r[  r\  i  z<iz>h)rY  r[  r]  r  r   r   rX  z!hz<lr  zh  0cz>  h)r   rM   r'  rB  rc  r  rU  rV  r   rO  r;   r;   r<    test_memoryview_compare_ndim_one  sl       z3TestBufferProtocol.test_memoryview_compare_ndim_onec                 C   s   t ddgdgdd}t ddgdgdd}t|}t|}| || | || | || | || | || t ddgdgdd}t ddgdgd	d}t|}t|}| || | || | || | || | || d S )
N  r{  r   r^  r  i|i?z= h0cz@   ir   rM   r'  r_  r;   r;   r<   "test_memoryview_compare_zero_shape$  s$    z5TestBufferProtocol.test_memoryview_compare_zero_shapec                 C   s   t g ddgdd}t dgdgdgdd}t|}t|}| || | || | || | || | || t d	gd dgd
d}t d	gdgdgdd}t|}t|}| || | || | || | || | || d S )N)ra  ra  ra  ra  r   rS  r  ra  r   r   rv  )ra  ra  z@ Liz!L  hrb  r_  r;   r;   r<   $test_memoryview_compare_zero_strides>  s$    z7TestBufferProtocol.test_memoryview_compare_zero_stridesc           
      C   s   d}t d D ]n}t|dd|\}}}dtfD ]L}t||g||d}t|}| || |d d d }t|}| || q,qd}tdD ]h}	t|\}}}dtfD ]L}t||g||d}t|}| || |d d d }t|}| || qqd S )	Nre   rE   rM   r?   r   r  rw  r   )rQ   rq   rc  r   rM   r'  ra   )
r  r   rV   r8   rn   rC  r   r  rD   rl   r;   r;   r<   &test_memoryview_compare_random_formatsX  s(    z9TestBufferProtocol.test_memoryview_compare_random_formatsc                 C   s  t ttddg ddd}t ttddg ddd}t|}t|}| || | || | || | || | || t dgd g dd	d}t d
gd g ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t dgd ddgdd}t dgd ddgdd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr2   r  r   r   r^  r  r   r  r   r*   r   z=f q xxL)g333333r*   r   z< f 2Qr  r      r  r4   z! b B xLz
= Qx l xxLr   r   r   ra   rM   r'  rB  r_  r;   r;   r<   "test_memoryview_compare_multidim_ct  sZ    z5TestBufferProtocol.test_memoryview_compare_multidim_cc                 C   s  t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t dgd	 d
dgdtd}t dgd	 d
dgdtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttdg ddtd}t ttdg ddtd}t|}t|}| || | || | || | || | || d S )Nrf  r2   )r   r   r  r^  r  r   r  r   r   r   r   r  =Qq)r   r   z=qQr  r   rg  Z0llrG   )r   r   ra   r5  rM   r'  rB  r_  r;   r;   r<   (test_memoryview_compare_multidim_fortran  s    z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc           	      C   sP  t tdd}t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || dgd	 }d
|d< t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || tt tdddgdd}|dddd d df }tt tdddgdd}|dddd d df }t|}t|}| || | || | || | || | || tdgd ddgdd}|dddd d df }tdgd ddgdd}|dddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tdgd	 g d d!d}|ddd d df }tdgd	 g d d"d}|ddd d df }t|}t|}| || | || | || | || | || d S )#Nrf  r2   rg  @lr  r   r  )gffffff
r:  r  )grp  r:  r   zd b czd h c(   r3   @Ir  r*   r   rX  r   )r  r$      rW  r   z=iiz>iir  r   r   )r   ry  r   r  r   Zb3sZi3s)r   ra   r   r   r5  rM   r'  rB  )	r  lst1lst2rU  rV  r   rO  rg  rh  r;   r;   r<   &test_memoryview_compare_multidim_mixed  s    
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedc                 C   s  t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr  )r   r  r   r   r  )r   r   r   z@irj  r_  r;   r;   r<   +test_memoryview_compare_multidim_zero_shape[  s$    z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapec                 C   s  t dgd g ddd}t dgg dg ddd}t|}t|}| || | || | || | || | || | | |  t d	gd
 ddgdd}t d	gddgddgdd}t|}t|}| || | || | || | || | || d S )Nra  r   )r   r   r   rS  r  r2  r   rv  r  re   r   r   z=lQr   z<lQ)r   rM   r'  r  r_  r;   r;   r<   -test_memoryview_compare_multidim_zero_stridesu  s&    z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc           	      C   s^  t ttdddgdd}|dddd d d	f }t ttdddgd
td}|dddd d d	f }t|}t|}| || | || | || | || | || t dgd ddgdtd}d|d d< |dddd d d	f }t dgd ddgdttB d}d|d d< |dddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}|ddd d d	f }t dgd g ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddtd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}d|d d d< |ddd d d	f }t dgd g ddttB d}d|d d d< |ddd d d	f }t|}t|}| || | || | || | || | || ttdd }t|g d}t |g dd!td}t |g dd"t	tB d}t|}t|}| || | || | || d#gd }d$|d%< t|g d}t |g dd&td}t |g dd't	tB d}t|}t|}| || | || | || d S )(Nrq  r   r3   rr  r  r  r*   r   rX  r   r  rl  rm  )r*   rX  r   r4   z>Qqr  r  r   rg  )r}  r   ZBbrt  r   )s   hellor|   r*      )r  r  r  Z5s0sP)s   sushir|   r*   rf  r2   ro  r   )s   sashimis   slicedg4@)s   ramens   spicygfffff"@rW  z
< 10p 9p dz
> 10p 9p d)
r   r   ra   rc  rM   r'  rB  r   r   r5  )	r  rg  rU  rh  rV  r   rO  ru  rv  r;   r;   r<   +test_memoryview_compare_multidim_suboffsets  s    
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetsc                 C   sT  dD ]H}t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || qd S )N)rB   r@   rA   rC   r'   r  )r  r   r   r   r   r   r  r  r*   r   r  )r'   r%   r#   ZQLH)r*   r*   r*   )r   r   r5  rM   r'  rB  )r  	byteorderrX   r?  rF   r   r;   r;   r<   !test_memoryview_compare_not_equal  s6    
z4TestBufferProtocol.test_memoryview_compare_not_equalc                 C   s  t  dg d}t|}|  | tt| | t|jd | tt| | t|j | t|j | tt	dt
  | t|jd | t|jdd dD ]}| t|j| qt  dg d}t|}t|}| || |  | || | || | || d S )Nr   )g?g@gffffff
@r   z1.0 in mr   r*   )rW   r   r(  r   r   r   r   r   r  rH  rJ  rL  )rK   rM   r  rQ  r   r  r   r  r   r  r  r  r  __getattribute__r'  rB  )r  rF   rD   rP  r   ri  rj  r;   r;   r<   test_memoryview_check_released.  s*    z1TestBufferProtocol.test_memoryview_check_releasedc                    sP  d t  dgdd}t|}| || | | |  t  gdgdd}t|}| || | | |  t  fddtd	D g d
dd}t|}| || | | |  t  fddtdD g ddd}t|}| || | | |  trLG dd dtj}|dd}t|}| | t| d S )NrX  r   r^  r  r*   z>hQiLlc                    s   g | ]} qS r;   r;   r   rt   r;   r<   r{   e  r|   z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>rT  r  z=hQiLlc                    s   g | ]} qS r;   r;   r   r  r;   r<   r{   j  r|   r  )r   r   r   r  r   z<hQiLlc                   @   s    e Zd ZdejfdejfgZdS )z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrX   r?  NrM  r;   r;   r;   r<   r  s  s   r  r   r  )r   rM   r'  r   ra   r  r  rR   )r  r  rD   r  r  rF   r;   r  r<   test_memoryview_tobytesV  s0    $
z*TestBufferProtocol.test_memoryview_tobytesc                 C   s  |  tti td |  ttdtd tg ddgdgd}|  tt|td tdd	d
d}dD ],}t|t|}| || | |d	 d q^tdd	d
td}dD ],}t|t|}| || | |d	 d qdD ]\}d|d	< t|t|}| || | |d	 d d|d	< | |d	 d | |d	 d qtdgdgd
td}dD ]>}t|t|}|  t	|j
d | || | | g  qHtttdg dd
td}dD ]*}t|t|}| t| g g g qtdgdgdtd}dD ]2}t|t|}| || | | |  qtg ddgdtd}dD ]2}t|t|}| || | | |  q:tg ddgdgtd}dD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d q|d d d }dD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d qtttdddgtd}dD ]*}t|t|}| t| |  q|  tt|td t|t|}| t| |  tttdddgttB d}dD ]*}t|t|}| t| |  q
|  tt|td t|t|}| t| |  tttdddgttB d}dD ]<}|  tt|t| t|t|}| t| |  qtg ddgdgd}t|td}| |j d S )Nr  r:  r   rp  r   r   r  rV  r;   r   r  r  r  re   r*   r   r3   r  r   r  r   rr  r  r   rT  r   rZ  )r   r  )r  r  rt  )rQ  r  r6  r7  r1  ZPyBUF_WRITEr   r'  r   r  r  r  r   ra   r  r5  rc  r8  rH  )r  r  r%  rD   r;   r;   r<   test_memoryview_get_contiguousy  s    z1TestBufferProtocol.test_memoryview_get_contiguousc                 C   s   t d}tdg d}t|}t|}td| }|| | |	 | t d}t
ttdg ddd}t|}t|}td| }|| | |	 | d S )Nr   rt  r   r   rT  r  r  )r5   rd  rK   rM   ioBytesIOr   readintor'  r   r   r   ra   )r  r  rF   rD   r   r   r  r;   r;   r<   test_memoryview_serializing  s    





z.TestBufferProtocol.test_memoryview_serializingc           	      C   s  t ttd}t|}| t|t| |jdddgd}| t|t| |d d d }t ttdd d d }| t|t| tttdg dtd	}t|}| t|t| tttdg dd}|d d d
d d d d df }t|}| t|t| tttdg dt	d	}|d d d
d d d d df }t|}| t|t| tttddgdd}t|}tttddgdd}t|}| || | t|t| tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j d S )NrT  r   r  r   r   rX  r  rg  rZ  r   r   )r   r   r  r   r  r   r  r   r  r   r   z= Lz< h)rR   r   ra   rM   r'  r  r  r   r5  rc  rQ  r   __hash__)	r  r   rD   ZmcZmxr  rX   rF   r?  r;   r;   r<   test_memoryview_hash  sJ    z'TestBufferProtocol.test_memoryview_hashc                 C   s|  t g d}t|}t|}| t|j ~|  t g d}t|}t|ttd}t|ttd}| |j	| | t|j ~~|  t g d}t|}t|}t|}|  | t|j ~|  t g d}t|}t|}t|ttd}t|ttd}| |j	| |  | t|j ~~|  tg ddgt
d}t|}|jg ddgd t|}t|}| | |  t|}	| |	 |  | |	 |  |  |	  |  | | |  ~|  |  dd	 }
t d
}t|,}|
| | |d td W d    n1 sF0    Y  tttdg ddd}t|ttd}	t|	ttd}| |j	| t|>}|
| | |dd  g dg dgg W d    n1 s0    Y  dtfD ]
}t d
}t|}~t|t|d}	t|	}~	t|t|d}t|h}~|
| |
| |
| | |d td | |d td | |d td ~W d    n1 s0    Y  ~W d    n1 s0    Y  ~W d    n1 s0    Y  t d
}t|}~t|t|d}	t|	}~	t|t|d}t|l}~|
| |
| |
| | |d td | |d td | |d td ~~~W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  qt d
}| tR t|(}t|}|d tdk W d    n1 sN0    Y  W d    n1 sn0    Y  d S )Nrp  rE  r   r  rZ  )r   r   r   r4   r3   r   r   c                 S   s,   t | }W d    n1 s0    Y  d S r   )rM   )r   rj  r;   r;   r<   catch22  s    
z;TestBufferProtocol.test_memoryview_release.<locals>.catch22ry  r   r7  rT  r  r   r  r*   rh  )r  r   r   r8  r   r9  )r   rM   r   rQ  r1  r  r;  ND_REDIRECTr&  rW   r  r  r'  r  r  ordr   ra   )r  rF   rD   r  rU  rV  ri  rj  rX   r?  r  r  r   r  r+  r;   r;   r<   test_memoryview_releaseH  s    
4
F


"""


j
z*TestBufferProtocol.test_memoryview_releasec                 C   s0  t dd tdD dgdd}tddd tdD }||fD ]}t |ttd}t |ttd}t|}| |j| | |j| | |j| | || | || | || | |dd	 |dd	  | |dd	 |dd	  | |dd	 |dd	  ~~| |dd	 |dd	  q@d S )
Nc                 S   s   g | ]}d | qS r  r;   rS  r;   r;   r<   r{     r|   z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>rT  r   r  c                 S   s   g | ]}d | qS r  r;   rS  r;   r;   r<   r{     r|   r  r*   r  )	r   ra   rK   r;  r  rM   r&  rW   r'  )r  r  rF   rX   r?  r  rD   r;   r;   r<   test_memoryview_redirect  s"    z+TestBufferProtocol.test_memoryview_redirectc                 C   s  d}g d}|  ttddd t }t|}| j||d|dddgdg|d	 tdD ]}| || | qT~~t }t|}~~t }t|td	}t|td	}t|}| 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~t }t|ttd
}t|ttd
}t|}| 	|j
| | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|}| j|d d|dddgdg|d	 tdD ]}| || | q~~tdd}t|}~~tdd}t|td	}t|td	}t|}| 	|j
d  | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|ttd
}t|ttd
}t|}| 	|j
d  | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~d S )Nr   )r   r*   r   r  r   r   r   r4   r3   rV  re   rW  r*   r   r  TrT  r  rD  r  )Zlegacy_mode)rQ  r  ZstaticarrayrM   r@  ra   r'  r   r;  r&  rW   r  )r  r8   rb   rX   r?  r   r  rD   r;   r;   r<   $test_memoryview_from_static_exporter  s    









z7TestBufferProtocol.test_memoryview_from_static_exporterc                 C   s*   t g ddgttB d}| tt| d S )Nrp  r  rn  )r   re  ZND_GETBUF_UNDEFINEDrQ  r1  rM   r  r;   r;   r<   #test_memoryview_getbuffer_undefined>  s    z6TestBufferProtocol.test_memoryview_getbuffer_undefinedc                 C   s&   t g ddgtd}| tt| d S )Nrp  r  rZ  )r   re  rQ  r1  rM   )r  rX   r;   r;   r<   test_issue_7385D  s    z"TestBufferProtocol.test_issue_7385c                 C   s&   dD ]}|  t|t| qd S )N)r>   iiZ3s)r'  	_testcapiZPyBuffer_SizeFromFormatr5   rd  )r  r   r;   r;   r<   test_pybuffer_size_from_formatH  s    z1TestBufferProtocol.test_pybuffer_size_from_formatN)F)Ur  r  r  r  r@  rR  rm  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r   r
  r  r  r  r"  
contextlibcontextmanagerr'  r5  r?  rA  rG  rQ  rW  r`  rc  rd  re  rk  rn  rw  rx  ry  r{  r}  r  r  r  r  r  r  r  r  r  r  Zcpython_onlyr  r;   r;   r;   r<   r    s    K
0i 
-'	0.Y #6!)>		% qu8	s<T	
5m8>5?H>Ha (#u< ^r  __main__)r   NN)r   )r   )r   r1   r;   )r   r1   )r   )N)F)F)r   )N)gr  Zunittesttestr   	itertoolsr   r   randomr   r   r   warningsr  rK   r  osZdecimalr   Z	fractionsr	   Z_testbufferImportErrorr   r5   r  ZEnvironmentVarGuardenvironcatch_warningsrL   r   r  Z
SHORT_TESTZNATIVEr6   r7   ZSTANDARDr=   rQ   r8   copyrw   ZARRAYr  ZBYTEFMTrg   ri   rf   rY   r`   rc   rp   rq   rr   ru   rv   rx   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   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
skipUnlessZTestCaser  r  mainr;   r;   r;   r<   <module>   sf   


P

	
	





	



	
?
+

!





                            o
