B
    u9ag{                @   s  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 yd dlT W n ek
r   dZY nX yd dlZW n ek
r   dZY nX yd dlZW n ek
r   dZY nX y:e (e_e  d dlmZ W dQ R X W dQ R X W n ek
rF   dZY nX 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ry e dd d ed< d ed< W n ej!k
r   Y nX 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r6x"e$d D ]Z%e#e%e$d e%< qW e& Z'e& Z(xeD ]Z)e)d krLe(e)= qLW e& Z*xeD ]Z)e)d!krte*e)= qtW e'e$d"< e'e$d#< e(e$d$< e*e$d%< e*e$d&< d Z+dZ,d'dd(d)d*d+gd'd,d-d.g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-d0d1 Z.d2d3 Z/d4d5 Z0d6d7 Z1dd9d:Z2dd;d<Z3dd=d>Z4d?d@ Z5dAdB Z6dCdD e$d D Z7dEdF Z8dGdH Z9dIdJ Z:dKdL Z;dMdN Z<dOdP Z=dQdR Z>dSdT Z?dUdV Z@dWdX ZAdYdZ ZBd[d\ ZCd]d^ ZDd_d` ZEdadb ZFdcdd ZGdedf ZHdgdh ZIdidj ZJdkdl ZKdmdn ZLdodp ZMddtduZNdvdw ZOdxdy ZPddzd{ZQd|d} ZRdd~dZSdd ZTdddZUdd ZVdd ZWdd ZXdddZYdddZZdddZ[dddZ\dd Z]dZ^dqZ_dZ`erdZ^dZ_dZ`eZZWe[ZXe\ZeaedeaedG dd dejbZceddkree  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   Ql    q)r      )r      )i   )i i   )r   i   )i   l        )r   l        )l         l            )r   l               i  )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}nfx>d
D ]6}yt | d|> d  P W qL t jk
r   Y qLX qLW |d@ rd|>  d|> fn
dd|> f}|S )zReturn range of a native type.r   )r   r   r   )r   r   r   )l         l            r   r    i  )
r      @   ?                      r   )structpackerror)fmtlhZexp r/   !/usr/lib/python3.7/test_buffer.pynative_type_rangeW   s     

&r1   ) @<>=!r3   Z
bBhHiIlLfdZBbcmz@mar   z@br2   r4   r5   r6   r7   123)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   r>          r   r   r   r   )r   fmtdictbytesboolr*   r+   unpack)modecharobjxr/   r/   r0   randrange_fmt   s    
rK   c             C   sN   |  d\}}g }x|D ]}|t||| qW t|dkrF|d S t|S )zReturn single random item.#r    r   )splitappendrK   lentuple)r-   rI   rG   charsrJ   r   r/   r/   r0   gen_item   s
    
rR   c             C   s@   | dkrt ||S dg|  }xt| D ]}t ||||< q&W |S )z,Return a list of random items (or a scalar).r   )rR   range)r   r-   rI   lstr   r/   r/   r0   	gen_items   s    

rU   c             C   s   t t| t }|d }|d}tdd}xTt|D ]H}t tt| }t t| t }||t	|rh|nd 7 }||| 7 }q6W t
| ||}	t||}
||	|
fS )NrL   ambr   
   r    )r   capMODEstripr   rS   rP   rC   MULTintrU   rR   )r   rI   rG   Zxfmtr-   nmemb_rH   
multiplieritemsitemr/   r/   r0   struct_items   s    


rb   r   c             C   s   |dkrt t| t }|dkr0t tt| }t t| t }|d |t|rR|nd  }t| ||}t||}|	d| | }|||fS )z"Return random format, items, item.NrL   r    rV   )
r   rX   rY   rP   rC   r[   r\   rU   rR   rZ   )r   rI   rG   rH   r_   r-   r`   ra   r/   r/   r0   	randitems   s    
rc   c             c   s<   x6t | t D ]&}x t| D ]}t| |||V  qW qW dS )z1Iterate through supported mode/char combinations.N)rX   rY   rC   rc   )r   rI   rG   rH   r/   r/   r0   	iter_mode   s    rd   c             c   s6   xt | |D ]
}|V  qW |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)rd   rb   )nitemsZtestobjtr/   r/   r0   iter_format   s
    
rg   c             C   s   d| kpd| kpd| kS )Nr   r   r   r/   )r-   r/   r/   r0   is_byte_format   s    rh   c             C   s4   t | }|dks$|dko2| d dko2| |d  tkS )zformat suitable for memoryviewr    r   r   r3   )rO   
MEMORYVIEW)r-   rJ   r/   r/   r0   is_memoryview_format   s    rj   c             C   s   g | ]}t |s|qS r/   )rh   ).0r   r/   r/   r0   
<listcomp>   s    rl   c             C   s   t | t S )z9Tuple items (representing structs) are regarded as atoms.)
isinstancelist)rT   r/   r/   r0   atomp   s    ro   c             C   s
   t | tS )N)rm   rn   )rT   r/   r/   r0   listp   s    rp   c             C   s:   t | dkrdS | d }x| dd D ]}||9 }q&W |S )zProduct of list elements.r   r    N)rO   )rT   rJ   vr/   r/   r0   prod   s    rr   c             C   s   | dkrdS |dkr^t |dd |g }xtt| d ddD ]}||  ||d  9  < q<W nB|gt |dd  }x*td| D ]}||  ||d  9  < qW |S )zOCalculate strides of a contiguous array. Layout is 'C' or
       'F' (Fortran).r   r/   Cr    Nr   )rn   rS   )ndimshapeitemsizeZlayoutstridesr   r/   r/   r0   strides_from_shape   s    ry   c             C   s   t | r| S t|dkr | d S dg|d  }|d rFt| |d  nd}x>t|d D ].}|| }t| |||  |dd ||< qXW |S )zmConvert flat item list to the nested list representation of a
       multidimensional C array with shape 's'.r   r    N)ro   rO   rS   _ca)r`   srT   strider   startr/   r/   r0   rz     s    &rz   c             C   sp   t | r| S t|dkr | d S dg|d  }|d }x4t|d D ]$}t| |d| |dd ||< qDW |S )zsConvert flat item list to the nested list representation of a
       multidimensional Fortran array with shape 's'.r   Nr    )ro   rO   rS   _fa)r`   r{   rT   r|   r   r/   r/   r0   r~     s    $r~   c             C   s2   t | r(d|kr(t|t| kr(tdt| |S )Nr   zprod(shape) != len(items))rp   rr   rO   
ValueErrorrz   )r`   rv   r/   r/   r0   carray*  s     r   c             C   s2   t | r(d|kr(t|t| kr(tdt| |S )Nr   zprod(shape) != len(items))rp   rr   rO   r   r~   )r`   rv   r/   r/   r0   farray/  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/   )rS   )rk   rq   r/   r/   r0   rl   6  s    zindices.<locals>.<listcomp>)r   )rv   	iterablesr/   r/   r0   indices4  s    r   c             C   s.   d}x$t | D ]}||| ||  7 }qW |S )zAConvert multi-dimensional index to the position in the flat list.r   )rS   )ru   indrx   retr   r/   r/   r0   getindex9  s    r   c       	      C   s   |s| S t |}t||dd}t||ddd dd}dgt |  }x>t|D ]2}t|||}t||ddd |}| | ||< qNW |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    rs   Nrt   r   )rO   ry   r   r   )	srcrv   ru   ZsstridesZdstridesdestr   frtor/   r/   r0   	transpose@  s    r   c             C   s6   | g kr| S t | r| gS t| d t| dd  S )zflatten listr   r    N)ro   _flatten)rT   r/   r/   r0   r   O  s
    r   c             C   s   t | r| S t| S )zflatten list or return scalar)ro   r   )rT   r/   r/   r0   flattenW  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)ro   rO   slice_shape)rT   slicesr/   r/   r0   r   ]  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)rk   Zsublst)r   r/   r0   rl   h  s    zmultislice.<locals>.<listcomp>r   )ro   )rT   r   r/   )r   r0   r   d  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    N)m_assign)rk   r   r)lslicesrslicesr/   r0   rl   {  s   zm_assign.<locals>.<listcomp>r   )ro   zip)llstrlstr   r   r/   )r   r   r0   r   j  s    r   c             C   sf   t | |}t ||}t|t|kr(dS x8tt|D ](}|| || krNdS || dkr6dS q6W dS )z9Compare the structure of llst[lslices] and rlst[rslices].rt   r   )r   rO   rS   )r   r   r   r   lshapershaper   r/   r/   r0   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   r/   r/   r0   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k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 )Nr/   )rk   rq   )rw   r/   r0   	<genexpr>  s    z#verify_structure.<locals>.<genexpr>Tc             3   s.   | ]&}| d kr|  | d  V  qdS )r   r    Nr/   )rk   j)rv   rx   r/   r0   r     s    c             3   s.   | ]&}| d kr|  | d  V  qdS )r   r    Nr/   )rk   r   )rv   rx   r/   r0   r     s    )anysumrS   )memlenrw   ru   rv   rx   offsetiminimaxr/   )rw   rv   rx   r0   verify_structure  s    r   c             C   s   x|D ]}| | } qW | S )Nr/   )rT   r   r   r/   r/   r0   get_item  s    
r   c       
      C   s>   |\}}}}}}|}x$t |D ]}	|||	 | |	  7 }qW |S )z-Location of an item in the underlying memory.)rS   )
r   rf   r   rw   ru   rv   rx   r   pr   r/   r/   r0   memory_index  s
    r   c             C   sT   | \}}}}}}d|> }x6t |D ]*}t|| }	d|	> }
||
@ rDdS ||
O }q"W 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   )rf   r   rw   ru   rv   rx   r   Zvisitedr   r   bitr/   r/   r0   is_overlapping  s    
r      r&   r/   c                sb   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|  x,t|D ]}t |	|d  |< qW nt }d	}t d}
|
dkr|
d@ rd
nd}dg| | t | |d  |d < |s4|d  dkr4| |d < x~t|d ddD ]j}| |d  rf |d  nd9 }|r| t | |d  |< n"dt d |  t d|d  |< qFW d }}d krt fddt|D }t fddt|D }|| }|r(| |  }||d |   }n*| | |  }t ddkrN| |  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   TFrt   )r    rt   c             3   s.   | ]&}| d kr|  | d  V  qdS )r   r    Nr/   )rk   r   )rv   rx   r/   r0   r     s    z!rand_structure.<locals>.<genexpr>c             3   s.   | ]&}| d kr|  | d  V  qdS )r   r    Nr/   )rk   r   )rv   rx   r/   r0   r     s    )r   rS   rO   r   )rw   Zvalidmaxdimmaxshaperv   ru   re   r   r   minshaper   r   Z	maxstrideZzero_strider   r   r/   )rv   rx   r0   rand_structure  sT    


"(

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{   r^   Zcontrolr/   r/   r0   randslice_from_slicelen  s    r   c             C   st   dg|  }dg|  }xNt | D ]B}|| }|dkr@td|d nd}t||||< t||||< qW 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    )rS   r   r   rP   )ru   rv   r   r   r   r   r   r/   r/   r0   randslice_from_shape,  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| }	x(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rt||
 ||< ||< || |	|< ||< q||
| ||< ||< || |	|< ||< q|W ||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   TF)r    rt   )r   rS   r   r   r   rP   )r   r   ru   r   r   Z
all_randomr   r   r   r   ZsmallZbigr}   r   r   Zs_smallr^   r   Zs_bigr/   r/   r0   rand_aligned_slices8  s@    
 

 



r   c             C   s$   |\}}}}}}t || d|  dS )zMReturn a list of random items for structure 't' with format
       'fmtchar'.rL   r>   )rU   )r-   rf   r   rw   r^   r/   r/   r0   randitems_from_structurec  s    r   c       
      C   s(   |\}}}}}}	t | ||||	t|B dS )z:Return ndarray from the tuple returned by rand_structure())rv   rx   formatr   flags)r   ND_WRITABLE)
r`   r-   rf   r   r   rw   ru   rv   rx   r   r/   r/   r0   ndarray_from_structurei  s    
r   c             C   sT   |\}}}}}}t |}	x(t| D ]\}
}t||	|
| | q"W t|	||||dS )z>Return numpy_array from the tuple returned by rand_structure())bufferrv   rx   dtyper   )	bytearray	enumerater*   	pack_intonumpy_array)r`   r-   rf   r   rw   ru   rv   rx   r   bufr   rq   r/   r/   r0   numpy_array_from_structureo  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 }x6|D ].}	t||	d }
|
|
krd|fS |	|
 qW |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/   )rk   r   )rw   memr/   r0   rl     s    zcast_items.<locals>.<listcomp>nan)
nbytesrr   ru   divmodtobytesrS   rO   r*   rF   rN   )Zexporterr-   rw   rv   Zbytelenr   r   Z	byteitemsr`   rq   ra   r/   )rw   r   r0   
cast_items}  s(    

 
r   c              #   s   xt dD ]} | gV  q
W tdd}tddkr4dnd  fdd	t |D V  tdd}tddkrjd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   )rk   r^   )r   r/   r0   rl     s    z!gencastshapes.<locals>.<listcomp>c                s   g | ]}t  d qS )r   )r   )rk   r^   )r   r/   r0   rl     s    N)rS   r   )r   ru   r/   )r   r0   gencastshapes  s    

r   c             C   s0   t t|  | d t|  | d t|  | d S )z4Generate all possible slices for a single dimension.r    )r   rS   )r   r/   r/   r0   	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   )rk   r   )rv   r/   r0   rl     s    z"genslices_ndim.<locals>.<listcomp>)rS   r   )ru   rv   r   r/   )rv   r0   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/   r0   rslice  s    r   c             c   s"   xt dD ]}t| |V  q
W dS )z.Generate random slices for a single dimension.r   N)rS   r   )r   r   r^   r/   r/   r0   r     s    r   c             #   sx   x,t |D ] }t fddt | D V  q
W x,t |D ] }t fddt | D V  q8W tdd t | D V  dS )z)Generate random slice tuples for 'shape'.c             3   s   | ]}t  | V  qd S )N)r   )rk   r   )rv   r/   r0   r     s    zrslices_ndim.<locals>.<genexpr>c             3   s   | ]}t  | d dV  qdS )T)r   N)r   )rk   r   )rv   r/   r0   r     s    c             s   s   | ]}t d dd V  qdS )r   r    N)r   )rk   r^   r/   r/   r0   r     s    N)rS   rP   )ru   rv   Z
iterationsr^   r/   )rv   r0   rslices_ndim  s
      r   c             c   s0   t | }|d krt|n|}t t||V  d S )N)rP   rO   r   )iterabler   Zpoolr/   r/   r0   rpermutation  s    r   c          
   C   s   y|   }W n  ttfk
r,   |  }Y nX t| trF| 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   rm   r   r   r   printrv   rx   
suboffsetsr   rw   sysstdoutflush)ndrJ   r   r   r/   r/   r0   ndarray_print  s    
r   r   rW      r   z%struct module required for this test.z%ndarray object required for this testc            
   @   s  e Zd Zdd Zddhddhdhdddddf
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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"dDdE Z#dFdG Z$dHdI Z%dJdK Z&dLdM Z'dNdO Z(dPdQ Z)e*j+Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2d`da Z3dbdc Z4e5j6ddde Z7dfdg Z8dhdi Z9djdk Z:dldm Z;dndo Z<dpdq Z=drds Z>dtdu Z?dvdw Z@dxdy ZAdzd{ ZBd|d} ZCd~d ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdS )TestBufferProtocolc             C   s   t  | _d S )N)Zget_sizeof_void_psizeof_void_p)selfr/   r/   r0   setUp  s    zTestBufferProtocol.setUprt   r    Fc       %   	      s8  |rt | }ns t|	}n}d}|jr| |d d}xFtd|D ]8}|| dkr\P || dkrJ|||  || d  7 }qJW |gdd t|d D  }| j}|d dk r| }|gt|dd   }| |j| | 	|j
| | 	|j | 	|j | 	|j| | 	|j| | 	|jt| |
rB|sT| 	|jt| | 	|jt| t|ts|trr| n| }| 	||	 sd S |st }d }xt|D ]|}yLt||}t|	|}t|trtjf| }nt|}|| W n tk
r.   d}P Y nX | 	|| qW |s| 	| | t|}d dd	 |D }| 	|! | rnd
}t"|	}xhd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rTqt|}|rfdnt*} t+|dt, | 	t |   fddt|D }!t|!d dkrdd |!D }!t|!|| d}"| 	t|"t| t+|dt, | 	t |   fddt|D }!t|!d dkrBdd |!D }!t|!|| t%B d}"| 	t|"t| t+|dt, | 	t |   fddt|D }!t|!d dkrdd |!D }!t#|drt%nd}#t|!||#| B d}"| 	t|"t| qW tr4yt|}W n tk
r2   d S X t|trF|jn|}$| |j|$ | 	|j
| | 	|j | 	|j | 	|j| | 	|j| | 	|jt| |
r|s| 	|jt| | 	|jt| |dkrdnt|	}| 	t|| r| n| }| 	||	 | 	|| d S )Nr/   r   r    c             S   s   g | ]}d qS )rt   r/   )rk   rq   r/   r/   r0   rl     s    z-TestBufferProtocol.verify.<locals>.<listcomp>Tr2   c             s   s   | ]}d | V  qdS )z%02xNr/   )rk   r   r/   r/   r0   r   N  s    z,TestBufferProtocol.verify.<locals>.<genexpr>r   )rs   FAr   r   rs   )rv   r   r   )rv   r   c                s   g | ]}t  | qS r/   )r*   unpack_from)rk   r   )contigr-   rw   r/   r0   rl   y  s   c             S   s   g | ]}|d  qS )r   r/   )rk   rq   r/   r/   r0   rl   |  s    )rv   r   r   c                s   g | ]}t  | qS r/   )r*   r   )rk   r   )r   r-   rw   r/   r0   rl     s   c             S   s   g | ]}|d  qS )r   r/   )rk   rq   r/   r/   r0   rl     s    c                s   g | ]}t  | qS r/   )r*   r   )rk   r   )r   r-   rw   r/   r0   rl     s   c             S   s   g | ]}|d  qS )r   r/   )rk   rq   r/   r/   r0   rl     s    )-rr   rO   r   ZassertGreaterrS   r   rn   assertIsrI   assertEqualr   rw   r   readonlyru   rv   rP   rx   rm   r   rj   r   r   r   r   get_pointerr   r*   r+   extendBufferErrorr?   joinhexr   is_contiguousr   
ND_FORTRANget_contiguous
PyBUF_READ
assertTrue
cmp_contigr   py_buffer_to_contiguousPyBUF_FULL_RO)%r   resultrI   rw   r-   r   ru   rv   rx   rT   slicedcastZexpected_lenr   Z
suboffset0r   Zstride0Zrepr   Zbuf_errr   Zitem1Zitem2rJ   r8   r   ZffZ	flattenedorderexpectedZtransr]   roZinitlstyr   exr/   )r   r-   rw   r0   verify  s    







zTestBufferProtocol.verifyc             C   st  dd }dd }|j r ||ts||tr0|jr||tr@|jr||trP|jr||ts`|j	s||t
sp|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
rB|jnd	}t||d}| j||||
|	|||||d

 d S )Nc             S   s   | j dkp| j dkS )Nr2   r   )r   )r  r/   r/   r0   
simple_fmt  s    z4TestBufferProtocol.verify_getbuf.<locals>.simple_fmtc             S   s   | |@ |kS )Nr/   )reqflagr/   r/   r0   match  s    z/TestBufferProtocol.verify_getbuf.<locals>.match)getbufr   r2   r    r/   )	rI   rw   r-   r   ru   rv   rx   rT   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assertRaisesr   r   rm   rj   r   r   r   rw   ru   r   rv   rx   r  )r   Zorig_exr  r  r   r  r
  rT   r   r  r-   rw   ru   rv   rx   r/   r/   r0   verify_getbuf  s>    
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ddddd	d
ddddddgdfddddd	d
ddddddgdfdd tdD dff}g g dfdddg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}x|D ]\}}t| x|D ]\}}	}
 fdd|	D }	|
 9 }
x\|D ]R}|	r|t@ rq|s|t@ rq|r|n|d }t|||||	|
d}|r|d d d nd }t|}|r
t|}|jdks.|jdkr:|r:|	r:| || |rd|jdkrd|rd|	rd| || xt|D ]l}xd|D ]\}| ||||B  | ||||B  |rt| j||||B dd | j||||B dd qtW qjW qW qZW q>W ddddd	d
ddddddg}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}xdddgdddgfD ]}xdtfD ]t}t|||tB d}| t |d | t |d x:|D ]2}t||d}| t |d | t |d qW qW qW d S ) Nc             S   s   g | ]}|d  rdndqS )r   TFr/   )rk   rJ   r/   r/   r0   rl     s    z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>   r   r    r   r   r   r   r   r)   r(   	   rW      r   r   c             S   s$   g | ]}|d  rd| nd| qS )r   l        i   r/   )rk   rJ   r/   r/   r0   rl     s    r   r   rt   c                s   g | ]}|  qS r/   r/   )rk   rq   )rw   r/   r0   rl     s    )r   r   rv   rx   r   T)r   )rv   r   )r  r   rs   )!r  r  r  PyBUF_SIMPLEr  r  r  
PyBUF_FULLr   PyBUF_RECORDSPyBUF_RECORDS_ROPyBUF_STRIDEDPyBUF_STRIDED_ROPyBUF_CONTIGPyBUF_CONTIG_ROrS   r   r   ND_PILr  r  r*   calcsizer   r?   ru   r   r  ND_GETBUF_FAILr  r   r   r   )r   requestsZ	items_fmtZ	structureZndflagsZ
real_flagsr`   r-   rv   rx   r   r   Z_itemsex1ex2m1m2r  bitsr  baser   r  r/   )rw   r0   test_ndarray_getbuf  s    


$

&
z&TestBufferProtocol.test_ndarray_getbufc       
      C   s  t dgdg}t dgdgtd}xt |j|jfD ]z}| t|dddh | t|dddg | t|dddg | jt|dddgdhd | jt|dddgdgdhd	 | jt|dddg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|dddgdgdd t
d }| jt|dg| dg| d | jt|dgdgd | jt|dddgdgd | jt|dgdd gd | jt|dddddgddgdd
 | jt|dddg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|ddddgdgdgdd | jt|dddgdgdd
 | jt|dddgdgddd | jt|dddgdgdd | jtj	|dgdgdd ddddddd d!dd"g
}| 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 q2W | 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 dddg | 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|dddg | tt|ddg | tt|d#dg | tt|d,dg t dddgdgdd}t |td)}| t|j t dddgdgdd}t |}	t |	}| t|j t d-gdgd.d}| 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|d/d0 | tt|d,d0 | tt|td1 | tt|td2 | tt|td3 | tt|d4d5 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 d5 | tt|d6 d S )7Nr  r    )r   r   r   r<   )r   r   )rv   )rv   rx   )rv   r   )rv   r   )rv   r   )rv   r  )rv   rx   r   ZXXXr   i  r   )r   r   ZQLrt   r   r   r   r:   )rv   rx   r   )rv   r   r   r2   )r    r   r   z@#$r   r)   r(   rW   r  )rv   rx   r   r   )r   s   123)r  r   r  l            )r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    r    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xyzrs   i  u    Z   r      )r   ND_VAREXPORTpushr  r   r   r&  r   r*   r,   ZND_MAX_NDIMOverflowErrorr   r  r   r  popr?   releaser   rn   rS   r  r   memoryview_from_bufferr   r   UnicodeEncodeErrorr   r   )
r   r   Zndmr   r   r`   r8   r  r*  r+  r/   r/   r0   test_ndarray_exceptionsL  s    "z*TestBufferProtocol.test_ndarray_exceptionsc             C   s   xt tdD ]}dgd }tdddgdgtd}t||d< x4tddD ]&}|jdddgdgd t|||< qJW x tdD ]}|||    q~W | t|j	 ~qW d S )Nr   r   r    r   r   )rv   r   )rv   )
r   rS   r   r6  r?   r7  r:  r  r   r9  )r   Zpermr8   r   r   r/   r/   r0   test_ndarray_linked_list  s    
z+TestBufferProtocol.test_ndarray_linked_listc             C   sP   xJt dD ]>\}}}t|}t|d|d}| j|d ||dddd|d	 q
W d S )Nr   r/   )rv   r   r    )rI   rw   r-   r   ru   rv   rx   rT   )rg   r*   r'  r   r  )r   r-   Zscalarr^   rw   r   r/   r/   r0   test_ndarray_format_scalar  s    
z-TestBufferProtocol.test_ndarray_format_scalarc             C   st   t dd}xdt|D ]X\}}}t|}xBdtfD ]6}t||g||d}| j|d ||dd|f|f|d	 q2W qW d S )Nr    rW   r   )rv   r   r   )rI   rw   r-   r   ru   rv   rx   rT   )r   rg   r*   r'  r&  r   r  )r   re   r-   r`   r^   rw   r   r   r/   r/   r0   test_ndarray_format_shape  s    


z,TestBufferProtocol.test_ndarray_format_shapec             C   s   t dd}xt|D ]\}}}t|}xtddD ]}|dkrBq4t|d d | g}|| g}|dk rt||d  nd}	xJdtfD ]>}
t|||||	|
d}| j|d ||dd|||d d | d	 qW q4W qW d S )Nr       r   r   )rv   rx   r   r   r   )rI   rw   r-   r   ru   rv   rx   rT   )	r   rg   r*   r'  rS   rO   r&  r   r  )r   re   r-   r`   r^   rw   r   rv   rx   r   r   r   r/   r/   r0   test_ndarray_format_strides  s     


z.TestBufferProtocol.test_ndarray_format_stridesc             C   sT   ddddddddd	d
ddg}t |ddd}t |ttB d}| | t|d d S )Nr    r   r   r   r   r   r)   r(   r  rW   r  r  )r   r   )r    r   )rv   rx   )r  )r   r  r  r   r   r   )r   r`   r  r   r/   r/   r0   test_ndarray_fortran%  s    z'TestBufferProtocol.test_ndarray_fortranc             C   sp  xht dD ]Z}dd t |D }t|}x8t|D ]*}t|\}}}t|}x
dtfD ]}	|dkrx|	tkrxqbt||||	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	 qbW q8W qW d S )Nr   c             S   s   g | ]}t d dqS )r   rW   )r   )rk   r^   r/   r/   r0   rl   -  s    z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>r   )rv   r   r   rs   r    )rI   rw   r-   r   ru   rv   rx   rT   )rv   r   )r  r/   r   )rS   rr   r   rc   r*   r'  r&  r   ry   r   r  rj   r%  r  r   rx   r;  r   r   )r   ru   shape_tre   rv   r-   r`   r^   rw   r   r   rx   rT   r  mvr/   r/   r0   test_ndarray_multidim+  sB    



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 ddddd	d
ddg}t |t|gdtd}| tj|jdd | t|jdd t|}| || | t|jdd | t|jdd dddg}t |t|gdtd}| t|jdd | tj|jdd d S )Nr    )rv   r(   )rv   r   l            r   r   r   r   r   r)   r   )rv   r   r   i,  )r      )r    r   )r   r   )r   r   ZLQ)s    1rH  )r   r  r   __setitem__r?   r   r   __delitem__r8  __getitem__
IndexErrorrO   r*   r,   r   )r   r   rF  r`   r/   r/   r0   test_ndarray_index_invalidX  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/   )rv   r   .r   r(   r  r   )	r   r   r?   r   r   r  r   rK  rI  )r   r   rF  rJ   r/   r/   r0   test_ndarray_index_scalar  s2            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 )	Nr(   r   r   )rv   r   )r  r    r   r   )	r   rn   rS   r   r$  r  r   rK  r   )r   r  r   r/   r/   r0   test_ndarray_index_null_strides  s    z2TestBufferProtocol.test_ndarray_index_null_stridesc             C   s,  xt dD ]\}}}t|dg|d}x&tddD ]}| || ||  q0W | t|jd | t|jd t|r
t|}| || x&tddD ]}| || ||  qW | t|jd | t|jd q
W xt dD ]\}}}t|dgt	|d}t|t
tB d}x(tddD ]}| || ||  qW t|r| }| ||t x(tddD ]}| || ||  qbW qW ddd	d
dg}t|dgd}t|td}x(tddD ]}| || ||  qW ddd	d
dg}t|dgd}t|td}x(tddD ]}| || ||  q
W d S )Nr   )rv   r   rB  i)rv   r   r   )r  r    r   r   r   )rv   )rg   r   rS   r   r  rL  rK  rj   r?   r   r$  r  r;  r   __eq__NotImplementedr%  r  )r   r-   r`   r^   r   r   rF  r  r/   r/   r0   !test_ndarray_index_getitem_single  sB    z4TestBufferProtocol.test_ndarray_index_getitem_singlec             C   s  xt dD ]\}}}t|dg|td}x tdD ]}|||< |||< q2W | | | | t|jd| | t|jd| t	|sqt|dg|td}t
|}| || x tdD ]}|||< |||< qW | | | | t|jd| | t|jd| qW xt dD ]\}}}t|dg|td}x4tddD ]&}||d  ||< ||d  ||< q:W | | | t	|sqt|dg|td}t
|}| || x4tddD ]&}||d  ||< ||d  ||< qW | | | qW d S )Nr   )rv   r   r   irB  r   r    )rg   r   r   rS   r   r   r  rL  rI  rj   r?   )r   r-   r`   Zsingle_itemr   r   rF  r/   r/   r0   !test_ndarray_index_setitem_single  sD    
z4TestBufferProtocol.test_ndarray_index_setitem_singlec          
   C   s  d}t |}xt|D ]}t|\}}}xdtfD ]}t||||d}t||}	xt|d  |d D ]}
| |	|
 ||
   x~t|d  |d D ]f}| |	|
 | ||
 |   x@t|d  |d D ](}| |	|
 | | ||
 | |  qW qW qnW t||||t	B d}t
||}	xt|d  |d D ]}
| |	|
 ||
   xt|d  |d D ]h}| |	|
 | ||
 |   x@t|d |d D ]*}| |	|
 | | ||
 | |  qW qtW qBW q8W qW d S )N)r   r   r   r   )rv   r   r   r    r   )rr   r   rc   r&  r   r   rS   r   r   r   r   )r   rE  re   rv   r-   r`   r^   r   r   rT   r   r   kr/   r/   r0   #test_ndarray_index_getitem_multidim   s,    
 0
 z6TestBufferProtocol.test_ndarray_index_getitem_multidimc             C   s   t ddd}| ttdt  t|}| || | ttdt  xtdD ]\}}}t |dg|d}x2t|D ]&\}}| |||  | 	||k qrW t
|rNt|}x2t|D ]&\}}| |||  | 	||k qW qNW d S )Nr    r/   )rv   z1 in ndz1 in mvr   )rv   r   )r   r  r   evallocalsr?   r   rg   r   r   rj   )r   r   rF  r-   r`   r^   r   rq   r/   r/   r0   test_ndarray_sequence  s    z(TestBufferProtocol.test_ndarray_sequencec          	   C   s  ddddddddg}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  | | ddddgddddg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    r   r   r   r   r   r)   r(   )rv   r   r   )r  r   )rv   r   r   z@%$r   r   )rv   r   z###########)r   r   r?   r  r   rI  r   r  r   r   rK  r   r   r   r   r&  add_suboffsetsr  )r   r`   xlmlr  xrr   rF  mryrrJ   r/   r/   r0   test_ndarray_slice_invalid2  s    


"""""$""z-TestBufferProtocol.test_ndarray_slice_invalidc             C   s  ddddddddd	d
ddg}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 )Nr    r   r   r   r   r   r)   r(   r  rW   r  r  r   )rv   r   r   )rv   r   )r   r   r   r   r?   r   )r   r`   rJ   r  r[  r]  r/   r/   r0   test_ndarray_slice_zero_shape  s    $z0TestBufferProtocol.test_ndarray_slice_zero_shapec             C   s"  d}t |}t|}xt|D ]}t|\}}}t|}xdtfD ]}	t||||	d}
t||}xt	||D ]}d }yt
||}W n( tk
r } z
|j}W d d }~X Y nX d }y|
| }W n( tk
r } z
|j}W d d }~X Y nX |s|r| || qp| | | qpW qFW q W d S )N)r   r   r   r   )rv   r   r   )rO   rr   r   rc   r*   r'  r&  r   r   r   r   	Exception	__class__r   r   r   )r   rE  ru   re   rv   r-   r`   r^   rw   r   r   rT   r   listerrr   enderrndslicedr/   r/   r0   test_ndarray_slice_multidim  s.    


z.TestBufferProtocol.test_ndarray_slice_multidimc             C   s:  d}t |}t|}xt|D ]}t|\}}}t|}t|||d}	|	  t|||d}
|
  t|
}t	||}xt
||D ]}d }yt||}W n( tk
r } z
|j}W d d }~X Y nX d }y|	| }W n* tk
r } z
|j}W d d }~X Y nX |s|r| || q| | | qW q W d S )N)r   r   r   r   )rv   r   )rO   rr   r   rc   r*   r'  r   rY  r?   r   r   r   ra  rb  r   r   r   )r   rE  ru   re   rv   r-   r`   r^   rw   r   r  rF  rT   r   rc  r   rd  re  rf  r/   r/   r0   'test_ndarray_slice_redundant_suboffsets  s4    

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc             C   s$  xt dD ]\}}}x tdD ]}xtdD ]}xdtfD ]}|tB }t|dg||d}t|dg||d}	t|	}
d }d }|d d  }y0|| }|| }|| ||< t|t|k}W n( tk
r } z
|j}W d d }~X Y nX d }y|| ||< W n* tk
r. } z
|j}W d d }~X Y nX |rD| 	|t
 n| | | | 	|| t|slqHd }y|
| |
|< W n* tk
r } z
|j}W d d }~X Y nX |r| 	|t
 qH| |
 | | |
| | 	|| | j|
|	|j|d|j|j|j| d	 qHW q6W q$W qW d S )Nr   r   )rv   r   r   )rI   rw   r-   r   ru   rv   rx   rT   )rg   r   r&  r   r   r?   rO   ra  rb  r   r   r   r   rj   r  rw   ru   rv   rx   )r   r-   r`   r^   lslicer   r   r   r   r  rF  ZlsterrZdiff_structurerT   ZlvalZrvalrd  re  Zmverrr/   r/   r0    test_ndarray_slice_assign_single  sV    
z3TestBufferProtocol.test_ndarray_slice_assign_singlec             C   s4  d}t |}t|}xt|D ]
}t|\}}}xdtfD ]}xttD ]}t||\}	}
t||||t	B d}t
||}d }yt|||	|
}W n( tk
r } z
|j}W d d }~X Y nX d }y||
 ||	< W n( tk
r } z
|j}W d d }~X Y nX |s|r| || qL| | | qLW q>W q W d S )N)r   r   r   r   )rv   r   r   )rO   rr   r   rc   r&  rS   
ITERATIONSr   r   r   r   r   ra  rb  r   r   r   )r   rE  ru   re   rv   r-   r`   r^   r   r   r   r   rT   rc  r   rd  re  r/   r/   r0   "test_ndarray_slice_assign_multidim  s0    
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc             C   sF  x>t tD ]0}x(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|krqt|||}| j|d |j|d|j|j|j| d	 qW qW d S )	Nr3   T)r   r   r   r   )r   r   )rI   rw   r-   r   ru   rv   rx   rT   )rS   rk  rC   r*   r'  r   MAXDIMMAXSHAPEr   r   r   r   r   r?   rj   r   r&  r   r   r  rw   ru   rv   rx   )r   r^   r-   rw   rf   r`   rJ   ZxlistrF  Zmvlistr  Zylistrv   zr/   r/   r0   test_ndarray_random9  s>    


z&TestBufferProtocol.test_ndarray_randomc             C   s   xt tD ]}xtd D ]}t|}t|dttd}| t	|  t
||}d}yt|||}W n( tk
r } z
|j}W d d }~X Y nX | | trd}	yt|||}
W q tk
r } z
|j}	W d d }~X Y qX qW q
W d S )Nr3   F)r   r   )rS   rk  rC   r*   r'  r   rm  rn  assertFalser   r   r   ra  rb  r   r   r   )r   r^   r-   rw   rf   r`   re  rJ   rd  Z	numpy_errr  r/   r/   r0   test_ndarray_random_invalidb  s(    


z.TestBufferProtocol.test_ndarray_random_invalidc             C   s>  x6t tD ](}x 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|ksd|krqt|
||}t|||	}|| ||< t|s
t|	s
| j|d |j|d|j|j|j| d	 | j|d |j|d|j|j|j| d	 qW qW d S )	Nr3   )r   r   T)rv   r   r   )r   )rI   rw   r-   r   ru   rv   rx   rT   )rS   rk  rC   r*   r'  r   rm  rn  r   r   r   r   r   r   r?   r   r&  r   r   r   r  rw   ru   rv   rx   )r   r^   r-   rw   r   r   r   r   tltrZlitemsZritemsrZ  r\  ZxllistZxrlistr[  r]  Zylr^  ZyllistZyrlistZzlZzrr/   r/   r0    test_ndarray_random_slice_assign~  s\    


z3TestBufferProtocol.test_ndarray_random_slice_assignc             C   s   ddddddddd	d
ddg}t |ddgtd}t |}| |jt@  | |j| | |jd | |j	 | |j
 | |j d S )Nr    r   r   r   r   r   r)   r(   r  rW   r  r  )rv   r   )r   rt   )r   r&  r   r   r   rI   r   r   rq  r  r  r  )r   r`   r   r  r/   r/   r0   test_ndarray_re_export  s    z)TestBufferProtocol.test_ndarray_re_exportc          	   C   s   xdt fD ]}tdddgdg|d}t|}| || | | g  | | g  tdddgdddg|d}| | g  tdddgdddg|d}| | g g g g tdddgdddg|d}| | g g g gg g g gg g g gg q
W d S )Nr   r    r   r   )rv   r   )r&  r   r?   r   r   )r   r   r   rF  r/   r/   r0   test_ndarray_zero_shape  s    
z*TestBufferProtocol.test_ndarray_zero_shapec          	   C   st   xndt fD ]b}tdgdgdg|d}t|}| || | | dddddg | | dddddg q
W d S )Nr   r    r   )rv   rx   r   )r&  r   r?   r   r   )r   r   r   rF  r/   r/   r0   test_ndarray_zero_strides  s    z,TestBufferProtocol.test_ndarray_zero_stridesc             C   s@   t ttddgdd}| |jd | | dddg d S )N   r   r)   )rv   r   r(   r  )r   rn   rS   r   r   r   )r   r   r/   r/   r0   test_ndarray_offset  s    z&TestBufferProtocol.test_ndarray_offsetc             C   sB   x<dt fD ]0}tttddg|d}| }| || q
W d S )Nr   r   )rv   r   )r&  r   rn   rS   r;  r   )r   r   r   r8   r/   r/   r0   #test_ndarray_memoryview_from_buffer  s    z6TestBufferProtocol.test_ndarray_memoryview_from_bufferc             C   sX   xRdt fD ]F}tttddg|d}x(tdD ]}| || t||g q0W q
W d S )Nr   r   )rv   r   )r&  r   rn   rS   r   r   )r   r   r   r   r/   r/   r0   test_ndarray_get_pointer  s    z+TestBufferProtocol.test_ndarray_get_pointerc             C   s^   t ttddddgd}t |ttB d}| | |  t|}| | |  d S )Nry  r   r   )rv   )r  )r   rn   rS   r  r  r   r   r?   )r   r  r   r8   r/   r/   r0    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 )Ns   123s   456r  r   r   )rv   r    r   )rv   r   rB   )rq  r   r   rn   rS   r   )r   rJ   r  r/   r/   r0   test_ndarray_cmp_contig  s    z*TestBufferProtocol.test_ndarray_cmp_contigc             C   s  t  ddddg}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dddg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dddgd}tttddddgt
d}| t|t| tttd}tttddddgtd}| t|t| tttddddgdd	}| t|t|  d S )
Nr   r    r   r   r  )rv   r   )rv   r   )rv   r   )r=   r   r  r   hashrD   rn   rS   r   r   r   r&  r   )r   r9   r   r   r/   r/   r0   test_ndarray_hash  s*    z$TestBufferProtocol.test_ndarray_hashc             C   s  t ttttttttt	t
tf}| tti dt tdddtd}x6dD ].}x(|D ] }t|||}| ||  qNW qDW tdgdgdtd}x2dD ]*}x$|D ]}t|||}| |d	 qW qW tttd
dddgdtd}x2dD ]*}x$|D ]}t|||}| |d	 qW qW xdtgD ]}tdgdgd|tB d}| }x6dD ].}x&|D ]}t|||}| || qNW qDW tdddgdgd|tB d}| }x6dD ].}x&|D ]}t|||}| || qW qW qW tdddgdgdgtd}| }x:dD ]2}x*ttgD ]}t|||}| || qW q W |d d d }| }xVdD ]N}xF|D ]>}yt|||}W n tk
r   w\Y nX | || q\W qRW ttd}xdtgD ]}t|ddg|tB d}trtt|ddgd|dkrdndd}	|tkr4tt|ddgddgtd}
|
 }n| }x|D ]}yt|d|}W n tk
rp   wBY nX | || tdd |D ddgtd}| t|t| trB| ||	jdd qBW |dkrtt|ddgddgtd}
nt|ddgtd}
|
 }xttt ttgD ]}yt|d|}W n tk
rN   w Y nX | || tdd |D ddgttB d}| t|t| tr | ||	jdd q W |tkrt|ddgtd}
|
 }n| }xttt ttgD ]}yt|d|}W n tk
r   wY nX | || tdd |D ddg|tB d}| t|t| tr| ||	jdd qW qW t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   r  r/   r   )rv   r   r   )rs   r   r   r    r       r(   r   r)   r   r   r   )rv   rx   r   rt   r  r   )rv   r   r   rs   )r   rv   r   r  c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl     s    zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>)r  c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl     s    r   c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl   	  s    c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl   	  s    c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl   	  s    c             S   s   g | ]}|qS r/   r/   )rk   rq   r/   r/   r0   rl   	  s    )r  r  r  r  r  r   r   r!  r"  r#  r$  r%  r  r   r   r   r   r   r   rn   rS   r   r   r   r   r   r?   Ztostringr&  )r   r)  r   r  Zrequestr   r   ZndbytesrT   ZnarJ   r  r  r/   r/   r0   test_py_buffer_to_contiguous0  s    













B






 


   z/TestBufferProtocol.test_py_buffer_to_contiguousc             C   s  dg fdddgdgft tddddgfg}xR|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rZ|gn|}| j
|d dd	dd|jgd|d
	 q4W x|D ]\}}t||td}t|}| |j | |j t|}t||dd}t||}| j
||dd	d||||d
	 t|}	| j
|	|dd	d||||d
	 qW x|dd  D ]v\}}t||td}t|}t|}t	||}| j
||dd	d|||j|d
	 t|}	| j
|	|dd	d|||j|d
	 q2W | ttdd | tti  tdddg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 )Nr  r    r   r   rA  r   )rv   rs   r   )rI   rw   r-   r   ru   rv   rx   rT   )r  r/   r2   r   )r    )rv   r   r      9rJ   r   r   )rv   r   rt   )rn   rS   r   r?   r   r  r  rO   ry   r   r  r%  r  r   rx   r;  r  r   rv   r   r   r  r   r&  r  r   r   r   r   r   )r   Zitems_shaper`   rv   r  r8   ru   rx   rT   r-  r   r/   r/   r0   test_memoryview_construction 	  s    ,



z/TestBufferProtocol.test_memoryview_constructionc             C   s   dddg}xTdddgdddgdddgfD ]4}t ||d}| |j t|}| t|jd q(W xJtddD ]<\}}}td}||}| |	 d | |
 g  qlW d S )	Nr    r   r   r   )rv   r   r?   r  )r   r   r  r?   r  r   r   rg   r   r   r   )r   r`   rv   r  msrcr-   r^   r8   r/   r/   r0   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|dddgdd	}|t|||d
|   d S )NzPnin 2P2n2i5P PZ3nr(   r  r    r   r   r   )rv   r   r   )check_sizeofr   Zcalcvobjsizern   rS   r?   r   )r   ZcheckZvsizeZbase_structZper_dimr`   r9   r/   r/   r0   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dddgdddhddiddddd tddtdtddddd|dd g}xJtddD ]:\}}}t|dg|td}t|dg|td}	t|}
t	
||	d | ||
d < | |
d  |	d   t	|}d!|krqx|D ]}d }yt	
||	|| W n t	jk
rT   t	j}Y nX d }y||
d< W n. ttfk
r } z
|j}W d d }~X Y nX |s|r| |d  | |d  n| |
d |	d  qW qW d 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 )N)val)r   r  r/   r/   r0   __init__	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__c             S   s   | j S )N)r  )r   r/   r/   r0   __int__	  s    zETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)__name__
__module____qualname__r  r  r/   r/   r/   r0   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 )N)r  )r   r  r/   r/   r0   r  	  s    zFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__c             S   s   | j S )N)r  )r   r/   r/   r0   	__index__	  s    zGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r  r  r  r  r  r/   r/   r/   r0   IDX	  s   r  c               S   s   dS )Nr)   r/   r/   r/   r/   r0   r   	  r  z;TestBufferProtocol.test_memoryview_struct_module.<locals>.fr  y@      @z-21.1gffffff(@r   r   r    r   r   r   r)   r(   r/   )r  TF   as   abcr9   abcc             S   s   | S )Nr/   )rJ   r/   r/   r0   <lambda>	  r  zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rW   r?   )rv   r   r   r   r   )objectr   r	   rQ  r   rg   r   r   r?   r*   r   r   r'  r,   r   r   rb  ZassertIsNot)r   r  r  r   valuesr-   r`   ra   r  r   r8   rw   rq   Z
struct_errZmv_errrd  r/   r/   r0   test_memoryview_struct_module	  sF    


z0TestBufferProtocol.test_memoryview_struct_modulec             C   s@   t dddgdgdgd}| |j t|}| t|jd d S )Nr    r   r   r   )rv   rx   r   )r   rq  r  r?   r  r   r   )r   r  r  r/   r/   r0   !test_memoryview_cast_zero_strides	  s    z4TestBufferProtocol.test_memoryview_cast_zero_stridesc             C   s  xt D ]}tdrd| n|}t|}xft D ]^}tdrBd| n|}t|}tttdd| g|d}t|}| t	|j
|d| g q.W qW xtdD ]\}}	}
t|	dg|d}t|}x`tdD ]T\}}
}
t|s| t|j
|d| g qt|st|s| t	|j
|d| g qW qW t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	ddd
ddg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	ddd
ddg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dd
dddddgd | jt|j
ddd
dddddgd | jt	|j
ddd
ddddd gd ttd!d" td#D d
ddd$gd}t|}| jt	|j
d%dd
ddg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%dd
ddgd ttd(d" td#D d#gd}t|}| jt	|j
ddd
ddgd tttd&d&gd%d}t|}tttd&d&gdd}t|}tjd)krJ| t	|j
dddd*d+d,d-d.g | t|j
dd/d/d0d0dg | t|j
d%d/d/d0d0dg nN| t	|j
ddd1g | t|j
dd0d0dddg | t|j
d%d0d0dddg d S )2Nr   r3   r$   )rv   r   r    r   r   r   x   r   r   )rv   Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBI)r    r   )r   r   ZIIr   r(   r   l            rt   r   r)   rJ   c             S   s   g | ]}d qS )r  r/   )rk   r^   r/   r/   r0   rl   C
  s    zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i  r  r   r   c             S   s   g | ]}d qS )r  r/   )rk   r^   r/   r/   r0   rl   M
  s    c             S   s   g | ]}d qS )r  r/   )rk   r^   r/   r/   r0   rl   R
  s    l    I   r!   iQ  iAj i	 i   i   i)NON_BYTE_FORMATr   r*   r'  r   rn   rS   r?   r  r   r   rg   rj   r   rh   r   rK  rI  r   r8  r   maxsize)r   ZsfmtZsformatZssizeZdfmtZdformatZdsizer  r  Zsitemsr^   Zsize_hZsize_dr8   r   r,  r-  r/   r/   r0   test_memoryview_cast_invalid	  s    



 

 """&  
z/TestBufferProtocol.test_memoryview_cast_invalidc                st  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
 x:tddD ],\}	}
}t|	|
}t|}||||
|	 qW xBtddD ]4\}	}
}t|
dg|	t	d}t|}||||
|	 q8W d S )Nr   c             S   s   t |  S )N)rn   r   )r  r/   r/   r0   r  l
  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/   )rk   rJ   r/   r/   r0   rl   m
  s    zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rn   r   )r  r/   r/   r0   r  m
  r  r   c             S   s   dd t |  D S )Nc             S   s   g | ]}t t|d qS )zlatin-1)rD   chr)rk   rJ   r/   r/   r0   rl   n
  s    zMTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>)rn   r   )r  r/   r/   r0   r  n
  r  c       
         s   t |}x D ]~\}}||}|| }j|| d|ddd| gd|dd
 ||}	|	|  |  }j|	| ||dddg|f|dd
 qW d S )Nr    r   r%   )r    T)	rI   rw   r-   r   ru   rv   rx   rT   r   )r*   r'  r   r  r   r   )
r  r8   r`   r-   srcsizer@   Zto_bytelistr-  rT   m3)bytespecr   r/   r0   iter_roundtripq
  s    




z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtripr   r  )rv   r   r    )r    T)	rI   rw   r-   r   ru   rv   rx   rT   r   )rv   r   r/   r%   r=   r?   )rv   r   r   )
r*   r'  r   r   r?   r   r  rg   r=   r   )r   r  r  r  Z	destitemsZ	destshaper8   r-  Zdestsizer-   r`   r^   r/   )r  r   r0   test_memoryview_castj
  s<    






z'TestBufferProtocol.test_memoryview_castc             C   s  xt  D ]}xtd D ]}dtd | }t|}t|| }t|rTdnd}xJt||D ]:\}}}	t|}
|dkr|gng }||
g }t|||d}t	|}t
||||d\}}|d kr| t|j|| qf|d	krqft|||d}|j||d}t|}|j}| }| j||||d
||||dd
 ||}|j||d}t|}|j}| }| j|||
|d
||||dd
 | j|||
|d
||||dd
 qfW qW q
W 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 )Nr3   )r2   r3   r   r?   r@   r   )rv   r   )rv   r   r    T)	rI   rw   r-   r   ru   rv   rx   rT   r   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>.BEPointrJ   r  N)r  r  r  ctypesc_longc_double_fields_r/   r/   r/   r0   BEPoint
  s   r  r   g33333i@r   )r    r/   g333333?r   )r   rC   r   r*   r'  rr   rh   rg   r   r?   r   r  r   r   rO   rx   r   r  r  BigEndianStructurer   rI   rw   r   ru   rv   r   r   r  )r   Z_tshaperH   ZtfmtZtsizer   rI   r-   r`   r^   sizerv   Ztshaper  r8   Ztitemsr   r-  ru   rx   rT   r  Zm4r  pointr,  rJ   r/   r/   r0   test_memoryview_cast_1D_ND
  s|    









z-TestBufferProtocol.test_memoryview_cast_1D_NDc             C   s@  t  dttdd}t|}| || | | |  |dd d }|dd d }| || | | |  tttdddddd	g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ddddddgddgdd}t|}| t|j d S )Nr   ir   r   r   i	  r  r)   r   r   )rv   r   )r   r   )r)   r  r.   s   12345r    r{   r     b   c   d   e   f)	r=   rn   rS   r?   r   r   r   r  r   )r   r9   r8   r  r/   r/   r0   test_memoryview_tolist
  s(     z)TestBufferProtocol.test_memoryview_tolistc             C   sH   t td}| }| |d |  | }| |d d S )Nr  z<memoryz	<released)r?   r   __repr__r   
startswithr:  )r   r8   r   r/   r/   r0   test_memoryview_repr  s    z'TestBufferProtocol.test_memoryview_reprc             C   s   xRdD ]J}t d}t|d|dg}t|}| d| | d| | d| qW tdg dd}t|}| ttdt  d S )	N)r   r   g      g      ?g      @g      "@r   )r   z9.0 in m)	floatr=   r?   ZassertInr   r  r   rV  rW  )r   r-   infr  r8   r/   r/   r0   test_memoryview_sequence  s    
z+TestBufferProtocol.test_memoryview_sequencec          	   c   s8   |  t}d V  W d Q R X | t|jd|f  d S )Nz#index out of bounds on dimension %d)r  rL  r   strZ	exception)r   dimcmr/   r/   r0   assert_out_of_bounds_error+  s    z-TestBufferProtocol.assert_out_of_bounds_errorc          
   C   s*  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 x2dD ]*}| jdd ||df  W d Q R X qdW x2dD ]*}| jdd |d|f  W d Q R X qW | t
|jd | t
|jd | t|jd  | t|jd! | t|jd | t|jd d S )"Ng      )@r   )rv   r   r/   .r   )r    r   r   Ziiir)   )rv   r   l            g       @g        ir(   r  r   r   )r   r   )r   r   )r   r   r  )rt   rt   )r1  r  )r   r  r    )r  )r   rB  r   )l            r   )r   l            )r   r   r   )g        g        )r   r?   r   r  r   rK  r   rn   rS   r   rL  r  )r   r  r8   indexr/   r/   r0   test_memoryview_index2  sF    

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 xt
d D ]t}|dks|dkr*qt dddgdg|td}t|}tdd}d||< | || d | || ||  qW t ddd gdgdtd}t|}d!|d< | |d d! t d"d#d"g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&dddd'd(gtd}t|}xt
d  D ]\}}|dkrpqZ|dkr~qZ||}|\}	}
|d)ks|dkrdd*  dd*  }	}
|d+kr| t|jd|	d  | t|jdd, | t|jd|
 qZW |d}| t|jdd- t ttddgd.td}t|}| t|jdd t d/gdgd0td}t|}| t|jdd t ttd1dd'gtd}t|}d2|d3< | |d d d2 d4|d5< | |d d d4 x2d6D ]*}| jdd7 d||df< W d Q R X qW x2d8D ]*}| jdd7 d|d|f< W d Q R X qW | t	|jd9d | t	|jd:d | t|jd;d | t|jd<d | t|jdddg d S )=Ng      )@r   )rv   r   r   g     6@r/   g     7@.r   g333338@r)   )rv   r   rW   )rv   r   l            r  g       @g        r  iry  r(      r3   r   r   r    r   r1     1   2   3r  TF   xr   r  r   r   r   i   r   r2  s   ZxLs   12345r{   r  *   )r   r    +   )rt   rt   )r   r  )r  )r   rB  )l            r   )r   l            )r   r   r   )g        g        )r   r   r?   r   r  r   rI  rn   rS   rL  rC   r   r`   r   r   r   r  )r   r  r8   r-   r   r   r,  Z_ranger-  lohir  r/   r/   r0   test_memoryview_assigng  s    
 






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 xzd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< | || | || qFW d S )Nr  )rv   r   r   r   r    r/   rt   r  )rv   rx   r   r      )rv   rx   r   r   r   r   r  )r   rn   rS   r   r?   r  r   rK  r   rI  r   r   r   r&  r   )r   r  r8   r   r*  r+  r,  r-  r/   r/   r0   test_memoryview_slice  sD    

$((,,z(TestBufferProtocol.test_memoryview_slicec             C   s  dd }xt ddD ]}xt|dD ]\}}}xt|D ]}xt|D ]}t||}t||}	t|	}
| |
| | |
 |  | |
 |  | t|
t| || ||	|
| d }d }y0|| }|| }|| ||< t|t|k}W n* t	k
r4 } z
|j
}W d d }~X Y nX d }y|
| |
|< W n* t	k
rt } z
|j
}W d d }~X Y nX |r| |t qT|s|r| || qT| |
| | |
 |  | |
 |  || ||	|
| qTW qBW q*W qW d S )Nc       	      S   s   xt |D ]\}}|| }|| }| || |||< ||krX| || | || n| || | || |||< | || | || |||< |||< q
W d S )N)r   r   assertNotEqual)	Ztestcaser9   r   r8   
singleitemr   r^   Zaimir/   r/   r0   cmptest  s    z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr    r   r=   )rS   rg   r   r=   r?   r   r   r   rO   ra  rb  r   r   )r   r  r   r-   r`   r  ri  r   r9   r   r8   Z	array_errZhave_resizeZalarrd  Zm_errr/   r/   r0   test_memoryview_array  sF    z(TestBufferProtocol.test_memoryview_arrayc       	      C   s  t  ddddg}t  ddddg}t|}t|}x8dD ]0}| t|||t | t|||t q:W t|}|  | || | || | || t|}t|}|  | || | || t|}| |dddg 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   r    r   r   r)   )__lt____le____gt____ge__)r   r   zl x d x)rv   r   r   rt   r   r   ur2  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>.BEPointrJ   r  N)r  r  r  r  r  r  r/   r/   r/   r0   r  u  s   r  r   rH  )r=   r?   r   getattrrQ  r:  r   r  r   r   r  r  r  r  r   r   )	r   r9   r   rq   wattrr   r  r  r/   r/   r0   %test_memoryview_compare_special_casesJ  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 )Ni  z@L)rv   r   r   )rv   r   r   i  r/   r  )rv   r   r   r  )i  g333333?s   12345ZLf5sZhf5s)i?g333333?s   12345)r   r   r?   r   rq  __ne__r  rP  rn   rS   r&  )r   nd1nd2rq   r  r   r  r8   r/   r/   r0   !test_memoryview_compare_ndim_zero  sf    z4TestBufferProtocol.test_memoryview_compare_ndim_zeroc             C   s  t dddddgdgdd}t ddddd	gdgdd}t|}t|}| || | || | || | || | || t dddddgdgd
d}t ddddd	gdgdd}t|}t|}| || | || | || | || | || t dddgdgdd}t dddddgdgdd}t|}t|}| ||d d d  | |d d d | | ||d d d  | |d d d |d d d  t dddgdgdd}t dddddgdgdd}t|}t|}| ||d d d  | |d d d | | ||d d d  | |d d d |d d d  t dddgdgdd}t dddddgdgdtd}t|}t|}| ||d d d  | |d d d | | ||d d d  | |d d d |d d d  t dddgdgdd}t dddddgdgdtd}t|}t|}| ||d d d  | |d d d | | ||d d d  | |d d d |d d d  d S )Nii@  ii  i'r   z@h)rv   r   i  z<iz>hr   r   rt   r  z!hz<l)rv   r   r   zh  0cz>  h)r   r?   r   r  r&  )r   r  r  rq   r  r/   r/   r0    test_memoryview_compare_ndim_one  sh       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 )
Ni  i  r   z@h)rv   r   i|i?z= h0cz@   i)r   r?   r   )r   r  r  rq   r  r/   r/   r0   "test_memoryview_compare_zero_shape  s$    z5TestBufferProtocol.test_memoryview_compare_zero_shapec             C   s   t ddddg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 )Ni  r   z@L)rv   r   r   r   )rv   rx   r   )i  i  z@ Liz!L  h)r   r?   r   )r   r  r  rq   r  r/   r/   r0   $test_memoryview_compare_zero_strides   s$    z7TestBufferProtocol.test_memoryview_compare_zero_stridesc       
      C   s  d}x~t d D ]r}t|dd|\}}}xXdtfD ]L}t||g||d}t|}| || |d d d }t|}| || q0W qW d}xxtdD ]l}	t|\}}}xXdtfD ]L}t||g||d}t|}| || |d d d }t|}| || qW qW d S )	NrW   z@mr?   r3   r   )rv   r   r   r1  r   )rC   rc   r&  r   r?   r   rS   )
r   r   rH   r-   r`   r  r   r   r8   r^   r/   r/   r0   &test_memoryview_compare_random_formats:  s(    z9TestBufferProtocol.test_memoryview_compare_random_formatsc             C   s  t ttdddddgdd}t ttdd	dddgdd}t|}t|}| || | || | || | || | || t d
gd	 dddgdd}t dgd	 dddgdd}t|}t|}| || | || | || | || | || t ttd	dddgdd}t ttd	dddg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	dddgdd}t ttd	dddgdd}t|}t|}| || | || | || | || | || d S )Nir'   r   r   r   z@h)rv   r   r   rA  )r   r    r   z=f q xxL)g333333r    r   z< f 2Qr      r)   z! b B xLz
= Qx l xxLr   )r   rn   rS   r?   r   r  )r   r  r  rq   r  r/   r/   r0   "test_memoryview_compare_multidim_cV  sZ    z5TestBufferProtocol.test_memoryview_compare_multidim_cc             C   s  t ttdddddgdtd}t ttdd	dddg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dddgdtd}t ttdddddgdtd}t|}t|}| || | || | || | || | || t ttdddddgdtd}t ttdddddgdtd}t|}t|}| || | || | || | || | || t ttd	dddgdtd}t ttd	dddgdtd}t|}t|}| || | || | || | || | || d S )Nir'   r   r   r   z@h)rv   r   r   r   rA  )l    rt   r   z=Qq)rt   l    z=qQr   Z0llz@b)r   rn   rS   r   r?   r   r  )r   r  r  rq   r  r/   r/   r0   (test_memoryview_compare_multidim_fortran  sn    z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc       	      C   sh  t tdd}t|dddg}t|dddgdd}t|dddgdtd	}t|}t|}| || | || | || d
gd }d|d< t|dddg}t|dddgdd}t|dddg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dddgdd}|ddd d df }tt tddddgdd}|ddd d df }t|}t|}| || | || | || | || | || tt tddddgdd}|ddd d df }tt tddddgdd}|ddd d df }t|}t|}| || | || | || | || | || tdgd dddgdd}|ddd d df }tdgd dddgdd}|ddd d df }t|}t|}| || | || | || | || | || d S ) Nir'   r   r   r   z@l)rv   r   r   )rv   r   r   )gffffff
ir  rA  )gir  zd b czd h c(   r(   z@Ir    rt   r  r   )ii      r  z=iiz>iir   r   )r   s   123Zb3sZi3s)rn   rS   r   r   r   r?   r   r  )	r   lst1lst2r  r  rq   r  r*  r+  r/   r/   r0   &test_memoryview_compare_multidim_mixed  s    
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedc             C   s  t ttddddgdd}t ttddddgdd}t|}t|}| || | || | || | || | || t ttddddgdd}t ttddddgdd}t|}t|}| || | || | || | || | || d S )	NrA  r   r   r   r   )rv   r   r   z@i)r   rn   rS   r?   r   r  )r   r  r  rq   r  r/   r/   r0   +test_memoryview_compare_multidim_zero_shape=  s$    z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapec             C   s  t dgd dddgdd}t dgdddgdddg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 )Ni  r   r   r   z@L)rv   r   r   r   )rv   rx   r   )r    r   rW   r   z=lQz<lQ)r   r?   r   r   )r   r  r  rq   r  r/   r/   r0   -test_memoryview_compare_multidim_zero_stridesW  s&    z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc       	      C   sz  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dddgdtd}|ddd d d	f }t ttddddgdd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd dddgdttB d}|ddd d d	f }t dgd dddgdd}|ddd d d	f }t|}t|}| || | || | || | || | || t ttddddgdtd}|ddd d d	f }t ttddddgdtd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd dddgdttB d}d|d d d< |ddd d d	f }t dgd dddgdttB d}d|d d d< |ddd d d	f }t|}t|}| || | || | || | || | || ttdd}t|dddg}t |dddgdtd}t |dddgdt	tB d}t|}t|}| || | || | || dgd }d |d!< t|dddg}t |dddgd"td}t |dddgd#t	tB d}t|}t|}| || | || | || d S )$Nr  r   r(   z@I)rv   r   r   r    rt   r  r   )rv   r   r   )l    rt   z=Qq)r    r  r   r)   z>QqrA  r   )r4  rt   ZBbr   )s   hellor  r       Z5s0sP)s   sushir  r    ir'   z@lr   )s   sashimis   slicedg4@)s   ramens   spicygfffff"@r  z
< 10p 9p dz
> 10p 9p d)
r   rn   rS   r&  r?   r   r  r   r   r   )	r   r*  r  r+  r  rq   r  r  r  r/   r/   r0   +test_memoryview_compare_multidim_suboffsetsr  s    
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetsc             C   sr  xjdD ]`}t dgd dddddg|d d}t dgd dddddg|d ttB d	}d
|d d d
 d
 d
< t|}t|}| || | || | || | || | || t dgd dddddg|d d}t dgd dddddg|d ttB d	}d|d d d
 d
 d
< t|}t|}| || | || | || | || | || qW d S )N)r6   r4   r5   r7   l            r  r   r   r   r   )rv   r   )rv   r   r   r    )l            l        i   ZQLH)r    r    r    )r   r   r   r?   r   r  )r   	byteorderrJ   r  r9   r   r/   r/   r0   !test_memoryview_compare_not_equal  s0    "z4TestBufferProtocol.test_memoryview_compare_not_equalc             C   s  t  ddddg}t|}|  | tt| | t|jd | tt| | t|j | t|j | tt	dt
  | t|jd | t|jdd xd	D ]}| t|j| qW t  ddddg}t|}t|}| || |  | || | || | || d S )
Nr   g?g@gffffff
@r   z1.0 in mr   r    )rI   r   r   rw   r   ru   rv   rx   r   r  r  r  )r=   r?   r:  r  r   r   r   r   r   rV  rW  rK  rI  __getattribute__r   r  )r   r9   r8   r  r   r,  r-  r/   r/   r0   test_memoryview_check_released  s*    
z1TestBufferProtocol.test_memoryview_check_releasedc                sX  d t  dgdd}t|}| || | | |  t  gdgdd}t|}| || | | |  t  fddtd	D d
d
dgdd}t|}| || | | |  t  fddtdD dd
d
dd
gdd}t|}| || | | |  trTG dd dtj}|dd}t|}| | t| d S )N)ii@  ii  i'r   z@h)rv   r   r    z>hQiLlc                s   g | ]} qS r/   r/   )rk   r^   )rf   r/   r0   rl   G  s    z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>r  r   r   z=hQiLlc                s   g | ]} qS r/   r/   )rk   r^   )rf   r/   r0   rl   L  s    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>.BEPointrJ   r  N)r  r  r  r  r  r  r/   r/   r/   r0   r  U  s   r  r   rH  )r   r?   r   r   rS   r  r  rD   )r   r   r8   r  r  r9   r/   )rf   r0   test_memoryview_tobytes8  s.    &"
z*TestBufferProtocol.test_memoryview_tobytesc             C   s<  |  tti td |  ttdtd tdddgdgdgd}|  tt|td td	d
dd}x4dD ],}t|t|}| || | |d
 d	 qbW td	d
dtd}x4dD ],}t|t|}| || | |d
 d	 qW xddD ]\}d	|d
< t|t|}| || | |d
 d	 d|d
< | |d
 d | |d
 d qW tdgdgdtd}xFdD ]>}t|t|}|  t	|j
d | || | | g  qXW tttddddgdtd}x2dD ]*}t|t|}| t| g g g qW tdgdgdtd}x:dD ]2}t|t|}| || | | |  qW tdddgdgdtd}x:dD ]2}t|t|}| || | | |  qZW tdddgdgdgtd}xldD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d qW |d d d }xldD ]d}t|t|}| || | | |  |  t|jdd | |d d | |d d q,W tttdddgtd}x2dD ]*}t|t|}| t| |  qW |  tt|td t|t|}| t| |  tttdddgttB d}x2dD ]*}t|t|}| t| |  q<W |  tt|td t|t|}| t| |  tttdddgttB d}xDdD ]<}|  tt|t| t|t|}| t| |  qW tdddddgdgdgd}t|td}| |j d S )Nr   r  rs   r    r   r   )rv   rx   r   r  r/   r   )rv   r   )rs   r   r   )rv   r   r   rW   r   r(   r)   r   r   )rv   rx   r   ry  rt   r  r   )rv   r   )rs   r   )r   r   r   )r  r   r   r   r   ZPyBUF_WRITEr   r   r   rL  rK  r   rn   rS   rI  r   r&  r   r  )r   r   r  r8   r/   r/   r0   test_memoryview_get_contiguous[  s    











z1TestBufferProtocol.test_memoryview_get_contiguousc             C   s   t d}tddddddg}t|}t|}td| }|| | |	 | t d}t
ttddddgdd	}t|}t|}td| }|| | |	 | d S )
Nr   r    r   r   r   r   r   r  )rv   r   )r*   r'  r=   r?   ioBytesIOr   readintor   r   r   rn   rS   )r   r  r9   r8   r   r   r   r/   r/   r0   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ddd	gtd
}t|}| t|t| tttdddd	gd}|d d dd d d d df }t|}| t|t| tttddd	dg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dddgdd}t|}| 
t|j tttdddddgdd}t|}| 
t|j tttddddgdd}t|}| 
t|j tttdddddgdd}t|}| 
t|j d S )Nr  r   r   r   )rv   r  rA  r   r   )rv   r   rt   r   )rv   r   r   r   ir   r   z= Lz< h)rD   rn   rS   r?   r   r  r   r   r   r&  r  r   __hash__)	r   r   r8   ZmcZmxr   rJ   r9   r  r/   r/   r0   test_memoryview_hash  sJ    z'TestBufferProtocol.test_memoryview_hashc             C   s  t dddg}t|}t|}| t|j ~|  t dddg}t|}t|ttd}t|ttd}| |j	| | t|j ~~|  t dddg}t|}t|}t|}|  | t|j ~|  t dddg}t|}t|}t|ttd}t|ttd}| |j	| |  | t|j ~~|  tdddgdgt
d}t|}|jdddd	d
gdgd t|}t|}| | |  t|}	| |	 |  | |	 |  |  |	  |  | | |  ~|  |  dd }
t d}t|"}|
| | |d td W d Q R X tttddddgdd}t|ttd}	t|	ttd}| |j	| t|8}|
| | |dd  dddgdddggg W d Q R X xdtfD ]}t d}t|}~t|t|d}	t|	}~	t|t|d}t|^}~|
| |
| |
| | |d td | |d td | |d td ~W d Q R X ~W d Q R X ~W d Q R X t d}t|}~t|t|d}	t|	}~	t|t|d}t|b}~|
| |
| |
| | |d td | |d td | |d td ~~~W d Q R X W d Q R X W d Q R X qW t d}| t2 t|}t|}|d tdk W d Q R X W d Q R X d S )Nr    r   r   )r  r   )rv   r   r   r   r   r)   r(   )rv   c          	   S   s   t | }W d Q R X d S )N)r?   )r   r-  r/   r/   r0   catch22l  s    
z;TestBufferProtocol.test_memoryview_release.<locals>.catch22s   123r   r  r  r   )rv   r   r  r  )r   r?   r   r  r   r:  r   ND_REDIRECTr   rI   r6  r7  r   r   r9  ordrn   rS   )r   r9   r8   r   r  r  r,  r-  rJ   r  r  ro  r   r  r  r/   r/   r0   test_memoryview_release*  s    

4





*
z*TestBufferProtocol.test_memoryview_releasec             C   s4  t dd tdD dgdd}tddd tdD }x||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	  qBW d S )
Nc             S   s   g | ]}d | qS )g      ?r/   )rk   rJ   r/   r/   r0   rl     s    z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>r  r   )rv   r   c             S   s   g | ]}d | qS )g      ?r/   )rk   rJ   r/   r/   r0   rl     s    )r  r   r    r   )	r   rS   r=   r   r  r?   r   rI   r   )r   r   r9   rJ   r  ro  r8   r/   r/   r0   test_memoryview_redirect  s"    z+TestBufferProtocol.test_memoryview_redirectc             C   s  d}dddddddd	d
dddg}|  ttddd t }t|}| j||d|dddgdg|d	 x tdD ]}| || | qjW ~~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	 x"tdD ]}| || | qW ~~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   r)   r(   r  rW   r  r  )rI   rw   r-   r   ru   rv   rx   rT   )r  )r  r   T)Zlegacy_mode)r  r   Zstaticarrayr?   r  rS   r   r   r   r   rI   r  )r   r-   rT   rJ   r  r   ro  r8   r/   r/   r0   $test_memoryview_from_static_exporter  s    









z7TestBufferProtocol.test_memoryview_from_static_exporterc             C   s,   t dddgdgttB d}| tt| d S )Nr    r   r   )r   )r   r(  ZND_GETBUF_UNDEFINEDr  r   r?   )r   r   r/   r/   r0   #test_memoryview_getbuffer_undefined   s    z6TestBufferProtocol.test_memoryview_getbuffer_undefinedc             C   s(   t dddgdgtd}| tt| d S )Nr    r   r   )rv   r   )r   r(  r  r   r?   )r   rJ   r/   r/   r0   test_issue_7385&  s    z"TestBufferProtocol.test_issue_7385N)F)Sr  r  r  r   r  r  r0  r=  r>  r?  r@  rC  rD  rG  rM  rN  rO  rR  rS  rU  rX  r_  r`  rg  rh  rj  rl  rp  rr  ru  rv  rw  rx  rz  r{  r|  r}  r~  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  
contextlibcontextmanagerr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r/   r/   r/   r0   r     s   
 3
2i 
-'	0.Y #6!)>		% qu8	s<P	5m8>5?H>Ha (#u< ^r   __main__)r   NN)r   )r   )r   r&   r/   )r   r&   )r   )N)F)F)r   )N)fr  ZunittestZtestr   	itertoolsr   r   Zrandomr   r   r   warningsr   r=   r  osdecimalr   Z	fractionsr	   Z_testbufferImportErrorr   r*   r  ZEnvironmentVarGuardenvironcatch_warningsr>   r   Z
SHORT_TESTZNATIVEr+   r,   ZSTANDARDr1   rC   r-   copyri   ZARRAYrT  ZBYTEFMTrY   r[   rX   rK   rR   rU   rb   rc   rd   rg   rh   rj   r  ro   rp   rr   ry   rz   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rk  rm  rn  Z
skipUnlessZTestCaser   r  mainr/   r/   r/   r0   <module>   sT   



$
	





	



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