o
    GXh<                    @   sJ  d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZmZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ zd dlT W n eyW   dZY nw zd dlZW n eyi   dZY nw zd dlZW n ey{   dZY nw z1e #e_e  d d	lmZ W d   n1 sw   Y  W d   n1 sw   Y  W n ey   dZY nw zd dl Z W n ey   dZ Y nw d
Z!d d d d d d d d d d d d d d d dZ"ere"d= e"d= erze#dd d e"d< d e"d< W n ej$y   Y nw 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r>e'd  D ]Z(e&e(e'd  e(< q2e") Z*e") Z+e"D ]Z,e,d!vrRe+e,= qHe") Z-e"D ]Z,e,d"vrde-e,= qZe*e'd#< e*e'd$< e+e'd%< e-e'd&< e-e'd'< d Z.dZ/g dg d(fd%gd)gfd)gd)gfd$d#gd)gfd'd&gd)gfd*Z0d+d, Z1d-d. Z2d/d0 Z3d1d2 Z4dd4d5Z5d	d6d7Z6d	d8d9Z7d:d; Z8d<d= Z9d>d? e'd  D Z:d@dA Z;dBdC Z<dDdE Z=dFdG Z>dHdI Z?dJdK Z@dLdM ZAdNdO ZBdPdQ ZCdRdS ZDdTdU ZEdVdW ZFdXdY ZGdZd[ ZHd\d] ZId^d_ ZJd`da ZKdbdc ZLddde ZMdfdg ZNdhdi ZOdjdk ZPddodpZQdqdr ZRdsdt ZSddudvZTdwdx ZUddydzZVd{d| ZWdd}d~ZXdd ZYdd ZZdd Z[dddZ\dddZ]dddZ^dddZ_dd Z`dZadlZbdZce!rdZadZbdZce]ZZe^Z[e_ZededededG dd dejeZfegdkreh  dS dS )    N)support)	os_helper)permutationsproduct)	randrangesamplechoice)Decimal)Fraction)*)ndarrayT)?cbBhHiIlLnNfdPr   r   Q    qr      r      )i   )i    )r   i   )           )r   l        l                     r                    )r   r   r   r   r   r   r   r   r   r   r   r   r   r   c              	   C   s   	 | dkr	d}|S | dkrd}|S | dkrd}|S | dkr(dd	>  dd	> f}|S d
D ]}zt | d|> d  W  n
 t jyC   Y q*w |d@ rQd|>  d|> fndd|> f}|S )Nr   r!   r   r   r   r'   r   r+   r,   )
r#      @   ?                      r   )structpackerror)fmtlhexp r<   :/opt/python-3.10.19/usr/lib/python3.10/test/test_buffer.pynative_type_range_   s,   	&r>   ) @<>=!r@   Z
bBhHiIlLfdZBbcm@mar   @b)r?   123r?   )r   arraynumpy
memoryviewbytefmtc                 C   sx   	 t t|  |  }|dkrt|g}|dkr|dkrd}|dkr$t|}|dks,|dkr:t||}t||d }|S )	Nr   rM          r   r   r   r   )r   fmtdictbytesboolr6   r7   unpack)modecharobjxr<   r<   r=   randrange_fmt   s   
rZ   c                 C   sL   	 |  d\}}g }|D ]}|t||| qt|dkr"|d S t|S )N#r+   r   )splitappendrZ   lentuple)r9   rX   rV   charsrY   r   r<   r<   r=   gen_item   s   ra   c                 C   s>   	 | dkr
t ||S dg|  }t| D ]	}t ||||< q|S Nr   )ra   range)r   r9   rX   lstr   r<   r<   r=   	gen_items   s   

re   c                 C   s   t t| t }|d }|d}tdd}t|D ]$}t tt| }t t| t }||t	|r3|nd 7 }||| 7 }qt
| ||}	t||}
||	|
fS )Nr[   ambr    
   r+   )r   capMODEstripr   rc   r_   rR   MULTintre   ra   )r   rX   rV   Zxfmtr9   nmemb_rW   
multiplieritemsitemr<   r<   r=   struct_items   s   



rr   r   c                 C   s   	 |d u rt t| t }|d u rt tt| }t t| t }|d |t|r*|nd  }t| ||}t||}|	d| | }|||fS )Nr[   r+   rf   )
r   rh   ri   r_   rR   rk   rl   re   ra   rj   )r   rX   rV   rW   ro   r9   rp   rq   r<   r<   r=   	randitems   s   

rs   c                 c   s8    	 t | t D ]}t| D ]
}t| |||V  qqd S N)rh   ri   rR   rs   )r   rX   rV   rW   r<   r<   r=   	iter_mode   s   ru   c                 c   s6    	 t | |D ]}|V  q|dkrd S t| |V  d S )Nr   )ru   rr   )nitemsZtestobjtr<   r<   r=   iter_format   s   rx   c                 C   s   d| v pd| v pd| v S )Nr   r   r   r<   )r9   r<   r<   r=   is_byte_format   s   ry   c                 C   s6   	 t | }|dks|dko| d dko| |d  tv S )Nr+   r    r   r@   )r^   
MEMORYVIEW)r9   rY   r<   r<   r=   is_memoryview_format   s
   r{   c                 C   s   g | ]}t |s|qS r<   )ry   .0r   r<   r<   r=   
<listcomp>       r~   c                 C   s   	 t | t S rt   
isinstancelistrd   r<   r<   r=   atomp   s   r   c                 C   s
   t | tS rt   r   r   r<   r<   r=   listp      
r   c                 C   s8   	 t | dkr	dS | d }| dd  D ]}||9 }q|S Nr   r+   )r^   )rd   rY   vr<   r<   r=   prod   s   
r   c                 C   s   	 | dkrdS |dkr/t |dd  |g }t| d ddD ]}||  ||d  9  < q|S |gt |d d  }td| D ]}||  ||d  9  < q?|S )Nr   r<   Cr+   r    )r   rc   )ndimshapeitemsizeZlayoutstridesr   r<   r<   r=   strides_from_shape  s   r   c                 C   s   	 t | r| S t|dkr| d S dg|d  }|d r$t| |d  nd}t|d D ]}|| }t| |||  |dd  ||< q,|S r   )r   r^   rc   _ca)rp   srd   strider   startr<   r<   r=   r     s   $r   c                 C   sn   	 t | r| S t|dkr| d S dg|d  }|d }t|d D ]}t| |d | |dd  ||< q"|S r   )r   r^   rc   _fa)rp   r   rd   r   r   r<   r<   r=   r   %  s   "r   c                 C   2   t | rd|vrt|t| krtdt| |S Nr   zprod(shape) != len(items))r   r   r^   
ValueErrorr   rp   r   r<   r<   r=   carray2      
r   c                 C   r   r   )r   r   r^   r   r   r   r<   r<   r=   farray7  r   r   c                 C   s   	 dd | D }t | S )Nc                 S   s   g | ]}t |qS r<   rc   r}   r   r<   r<   r=   r~   >      zindices.<locals>.<listcomp>)r   )r   	iterablesr<   r<   r=   indices<  s   r   c                 C   s,   	 d}t | D ]}||| ||  7 }q|S rb   r   )r   indr   retr   r<   r<   r=   getindexA  s
   r   c           	      C   s   	 |s| S t |}t||dd}t||d d d dd}dgt |  }t|D ]}t|||}t||d d d |}| | ||< q'|S )Nr+   r   r   r   )r^   r   r   r   )	srcr   r   ZsstridesZdstridesdestr   frtor<   r<   r=   	transposeH  s   r   c                 C   s8   	 | g kr| S t | r| gS t| d t| dd   S r   r   _flattenr   r<   r<   r=   r   W  s   r   c                 C   s   	 t | r| S t| S rt   r   r   r<   r<   r=   flatten_  s   r   c                 C   s6   	 t | rg S t| |d  gt| d |dd   S r   )r   r^   slice_shaperd   slicesr<   r<   r=   r   e  s   (r   c                    s(   	 t | r| S  fdd|  d  D S )Nc                    s   g | ]}t | d d qS r+   N)
multislice)r}   Zsublstr   r<   r=   r~   p  s    zmultislice.<locals>.<listcomp>r   )r   r   r<   r   r=   r   l  s   r   c                    sH   	 t |r|S  fddt|  d  |d  D }||  d < | S )Nc              	      s.   g | ]\}}t || d d d d qS r   )m_assign)r}   r   rlslicesrslicesr<   r=   r~     s    "zm_assign.<locals>.<listcomp>r   )r   zipllstrlstr   r   r<   r   r=   r   r  s   r   c                 C   sh   	 t | |}t ||}t|t|krdS tt|D ]}|| || kr( dS || dkr1 dS qdS )Nr   r   )r   r^   rc   )r   r   r   r   lshapershaper   r<   r<   r=   cmp_structure  s   

r   c                 C   s*   	 t | |||dk rtdt| |||S )Nr   z+lvalue and rvalue have different structures)r   r   r   r   r<   r<   r=   multislice_assign  s   r   c                    s   	 |  rdS |dk s|  | krdS t  fddD r dS |dkr.|dko- o- S dv r4dS tfddt|D }tfddt|D }d|| ko]||   | kS )NFr   c                 3   s    | ]}|  V  qd S rt   r<   r   r   r<   r=   	<genexpr>      z#verify_structure.<locals>.<genexpr>Tc                 3   0    | ]}| d kr|  | d  V  qdS r   r+   Nr<   r}   jr   r   r<   r=   r         c                 3   0    | ]}| d kr|  | d  V  qdS r   r<   r   r   r<   r=   r     r   )anysumrc   )memlenr   r   r   r   offsetiminimaxr<   )r   r   r   r=   verify_structure  s   r   c                 C   s   |D ]}| | } q| S rt   r<   )rd   r   r   r<   r<   r=   get_item  s   
r   c           
      C   s<   	 |\}}}}}}|}t |D ]}	|||	 | |	  7 }q|S rt   r   )
r   rw   r   r   r   r   r   r   pr   r<   r<   r=   memory_index  s   r   c                 C   sT   	 | \}}}}}}d|> }t |D ]}t|| }	d|	> }
||
@ r# dS ||
O }qdS )Nr+   TF)r   r   )rw   r   r   r   r   r   r   Zvisitedr   r   bitr<   r<   r=   is_overlapping  s   

r      r2   r<   c                    sP  	  s^t |d }|dkr3|r| | |dddfS t dd}||  }t ddkr)|  n|}|| |dd|fS d}	t d}
|
dkrB|rBd}	n|
dkrHd}	dg|  t|D ]}t |	|d  |< qQnt }d	}t d}
|
dkrr|
d@ rrd
nd}dg| | t | |d  |d < |s|d  dkr| |d < t|d ddD ]2}| |d  r |d  nd9 }|r| t | |d  |< qdt d |  t d|d  |< qd }}d vrt fddt|D }t fddt|D }|| }|r| |  }||d |   }n| | |  }t ddkr| |  n|}|| | |fS )Nr+   r   r<      r    d   _   Z   r   TFr   r+   r   c                 3   r   r   r<   r   r   r<   r=   r     r   z!rand_structure.<locals>.<genexpr>c                 3   r   r   r<   r   r   r<   r=   r     r   )r   rc   r^   r   )r   Zvalidmaxdimmaxshaper   r   rv   r   r   minshaper   r   Z	maxstrideZzero_strider   r   r<   r   r=   rand_structure  s`   


 


r   c           
      C   sr   	 ||  }t |d }| r|| |  nd}t d|d }|| |  }t|||}t||\}}}}	|	| kr7t|S Nr+   )r   sliceslice_indicesRuntimeError)
slicelenZlistlenZmaxstartr   Zmaxstepstepstopr   rn   Zcontrolr<   r<   r=   randslice_from_slicelen'  s   r   c                 C   sr   	 dg|  }dg|  }t | D ]!}|| }|dkr td|d nd}t||||< t||||< qt|t|fS r   )rc   r   r   r_   )r   r   r   r   r   r   r   r<   r<   r=   randslice_from_shape4  s   

r   c                 C   s  	 t d| d }d}t d}|dkrd}n|dkrd}t ddkr#dnd	}dg| }dg| }dg| }dg| }	t|D ]}t ||d }
t ||d }||
k rV|
|}}
|rt |
 |
d }t |
 |
d }d
t d t d|
d  }t|||}t||
\}}}}n|
dkrt d|
d nd}t||
}t||}t ddkr||
||< ||< |||	|< ||< q=|
|||< ||< |||	|< ||< q=||t|t|	fS )Nr+   r    r   r   r   r   P   TFr   )r   rc   r   r   r   r_   )r   r   r   r   r   Z
all_randomr   r   r   r   Zsmallbigr   r   r   Zs_smallrn   r   Zs_bigr<   r<   r=   rand_aligned_slices@  s>   


r   c                 C   s&   	 |\}}}}}}t || d|  dS )Nr[   rM   )re   )r9   rw   r   r   rn   r<   r<   r=   randitems_from_structurek  s   r   c           
      C   s*   	 |\}}}}}}	t | ||||	t|B dS )Nr   r   formatr   flags)r   ND_WRITABLE)
rp   r9   rw   r   r   r   r   r   r   r   r<   r<   r=   ndarray_from_structureq  s
   
r   c                 C   sR   	 |\}}}}}}t |}	t| D ]\}
}t||	|
| | qt|	||||dS )N)bufferr   r   dtyper   )	bytearray	enumerater6   	pack_intonumpy_array)rp   r9   rw   r   r   r   r   r   r   bufr   r   r<   r<   r=   numpy_array_from_structurew  s   r   c                    s   	 | j }|rt|  |krd |fS n$|g kr%| jdks  |kr$d |fS nt| \}}|g}|dkr7d |fS |   fddtdt D }g }|D ]}	t||	d }
|
|
krcd|f  S |	|
 qO|g krq||fS |d |fS )Nr   c                    s   g | ]
}||   qS r<   r<   )r}   r   r   Zmemr<   r=   r~         zcast_items.<locals>.<listcomp>nan)
nbytesr   r   divmodtobytesrc   r^   r6   rU   r]   )Zexporterr9   r   r   Zbytelenr   r   Z	byteitemsrp   r   rq   r<   r   r=   
cast_items  s.    r   c                  #   s    	 t dD ]} | gV  qtdd}tddkrdnd  fdd	t |D V  tdd}tddkr5dnd  fd
d	t |D V  d S )Nr0         r   r   r+   r    c                       g | ]}t  d qS r   r   r}   rn   r   r<   r=   r~         z!gencastshapes.<locals>.<listcomp>c                    r  r  r  r  r  r<   r=   r~     r  )rc   r   )r   r   r<   r  r=   gencastshapes  s   


r	  c                 C   s2   	 t t|  | d t|  | d t|  | d S r   )r   rc   )r   r<   r<   r=   	genslices  s   0r
  c                    s    	  fddt | D }t| S )Nc                    s   g | ]}t  | qS r<   )r
  r}   r   r   r<   r=   r~     r   z"genslices_ndim.<locals>.<listcomp>)rc   r   )r   r   r   r<   r  r=   genslices_ndim  s   r  Fc                 C   s.   	 |s| dkr	dnd}t || d }t|| S r   )r   r   )r   allow_emptyZminlenr   r<   r<   r=   rslice  s   
r  c                 c   s"    	 t dD ]}t| |V  qd S )Nr   )rc   r  )r   r  rn   r<   r<   r=   r     s
   r   c                 #   st    	 t |D ]}t fddt | D V  qt |D ]}t fddt | D V  qtdd t | D V  d S )Nc                 3   s    | ]	}t  | V  qd S rt   r  r  r  r<   r=   r         zrslices_ndim.<locals>.<genexpr>c                 3   s     | ]}t  | d dV  qdS )T)r  Nr  r  r  r<   r=   r     s    c                 s   s    | ]	}t d dd V  qdS r   )r   r  r<   r<   r=   r     r  )rc   r_   )r   r   Z
iterationsrn   r<   r  r=   rslices_ndim  s   r  c                 c   s2    t | }|d u rt|n|}t t||V  d S rt   )r_   r^   r   )iterabler   poolr<   r<   r=   rpermutation  s   r  c              
   C   s   	 z|   }W n ttfy   |  }Y nw t| tr#| j}| j}nd}d}td|| j	| j
| j|| j| j|f  tj  d S )Nunknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s))tolist	TypeErrorNotImplementedErrorr   r   r   r   r   printr   r   
suboffsetsr   r   sysstdoutflush)ndrY   r   r   r<   r<   r=   ndarray_print  s"   

r   r   rg      r  z%struct module required for this test.z%ndarray object required for this testc                   @   s  e Zd Zdd ZdddddZdddZd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Z d?d@ Z!dAdB Z"dCdD Z#dEdF Z$dGdH Z%dIdJ Z&dKdL Z'dMdN Z(dOdP Z)e*j+Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4e5j6dcdd Z7dedf Z8dgdh Z9didj Z:dkdl Z;dmdn Z<dodp Z=dqdr Z>dsdt Z?dudv Z@dwdx ZAdydz ZBd{d| ZCd}d~ ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRe*jSdd ZTdS )TestBufferProtocolc                 C   s   t  | _d S rt   )Zget_sizeof_void_psizeof_void_pselfr<   r<   r=   setUp   s   zTestBufferProtocol.setUpF)slicedcastc       
   %         s*  	r	t 	 n	stndjrgd d}tdD ]}	| dkr. n| dkrA||  	| d  7 }q$|gdd td D  j}d dk r\| }|gtdd   j| 	j
 	j 	j j| 	j 	jt	 
rs	jt 	jt ttstrɈr n }	| sd S |st }d }t	D ]<}z&t|}t|}t|trtjg|R  }nt|}|| W n ty   d}Y  nw 	|| q|s	 | t}d dd	 |D }	|! | r<nd
}t"}dD ]s}}|dkrht#drZt#drgt$|	}t|	|t%d}nt#dryt#dr|dkrt|	|d}t&t'| 	  | (t) | dkrqDt|}|rdnt*}t+dt, 	t |   fddt|D }t|d dkrdd |D }t|	|d} 	t| t t }!	|!  tjd d}!	|!  tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkr<dd |D }t|	|t%B d} 	t| t tjdd}!	|!  t+dt, 	t |   fddt|D }t|d dkrdd |D }t#drt%nd}"t|	|"|B d} 	t| t tjdd}!	|!  qDtrzt}W n ty   Y d S w ttrوjn|f	
fdd	}#|#| |- }$|#|$dd W d    n	1 sw   Y  |  d S d S )Nr<   r   r+   c                 S      g | ]}d qS )r   r<   r   r<   r<   r=   r~         z-TestBufferProtocol.verify.<locals>.<listcomp>Tr?   c                 s   s    | ]}d | V  qdS )z%02xNr<   r|   r<   r<   r=   r   U  r   z,TestBufferProtocol.verify.<locals>.<genexpr>r   r   FAr,  r-  r   r   r   r   r   r   c                       g | ]}t  | qS r<   r6   unpack_fromr  contigr9   r   r<   r=   r~         c                 S      g | ]}|d  qS r   r<   r   r<   r<   r=   r~     r   r   r   r   orderc                    r0  r<   r1  r  r3  r<   r=   r~     r5  c                 S   r6  r7  r<   r   r<   r<   r=   r~     r   c                    r0  r<   r1  r  r3  r<   r=   r~     r5  c                 S   r6  r7  r<   r   r<   r<   r=   r~     r   c                    s    | j  | j | j | j | j| | j | jt	 	r7s@| j
t	
 | jt	 dkrOdnt}t| | ra n }| |  d S r   )assertIsrX   assertEqualr   r   r   readonlyr   r   r_   r   r  r^   r  r   )rE   expected_readonlyr   rep)exexpected_lenr9   r   rd   r   resultr%  r   r'  r   r  r<   r=   check_memoryview  s   z3TestBufferProtocol.verify.<locals>.check_memoryview)r>  ).r   r^   r  ZassertGreaterrc   r#  r   r;  rX   r<  r   r   r   r=  r   r   r_   r   r   r   r{   r  r   r   r   get_pointerr   r6   r7   extendBufferErrorrN   joinhexr   is_contiguousr   
ND_FORTRANget_contiguous
PyBUF_READ
assertTrue
cmp_contigr   py_buffer_to_contiguousPyBUF_FULL_RO
toreadonly)%r%  rB  rX   r   r9   r=  r   r   r   rd   r'  r(  Z
suboffset0r   Zstride0r?  r   Zbuf_errr   Zitem1Zitem2rY   rE   r   ZffZ	flattenedr:  expectedZtransrm   roZinitlstyZcontig_bytesr   rC  Zmmr<   )r4  r@  rA  r9   r   rd   r   rB  r%  r   r'  r   r  r=   verify  s  









&
zTestBufferProtocol.verifyc                 C   sd  dd }|j r||ts>||tr|jr>||tr|jr>||tr$|jr>||ts,|j	s>||t
s4|jr>||tsI||trI| jtt||d d S t|tsSt|jrX| }n
t|td}| }||tridn|j }|j}	|j}
|j}||tsd}	| }||tsd}||tr|jnd}||t
r|jnd}t||d}| j|||
|	||||||d
 d S )	Nc                 S   s   | |@ |kS rt   r<   )reqflagr<   r<   r=   match  s   z/TestBufferProtocol.verify_getbuf.<locals>.matchgetbufFr?   r+   r<   )	rX   r   r9   r=  r   r   r   rd   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assertRaisesrF  r   r   r{   r   r  rP  r   r   r   r   r   rU  )r%  Zorig_exr@  rV  r'  rX  rd   r  rS  r9   r   r   r   r   r<   r<   r=   verify_getbuf  sh   		



z TestBufferProtocol.verify_getbufc                    s  t ttttttttt	t
ttttf}dd tdD dfg ddfg ddfdd tdD d	ff}g g d
fg dg d
fdgg d
fdgdgdfdgdgd
fdgdgdfddgg d
fddgddgdfddgddgdfddgddgdff
}d
ttttB tttB f}d
ttttB f}|D ]\}}t| |D ]\}}	}
 fdd|	D }	|
 9 }
|D ]}|	r|t@ rq|s|t@ rq|r|n|d
 }t|||||	|
d}|r|d d d nd }t|}|rt|}|jd
ks|jdkr|r|	r| || |r|jdkr|r|	r| || |D ]4}|D ].}| ||||B  | ||||B  |r:| j||||B dd | j||||B dd qq	qqqg d}t|dgtd}| tt| tdgdg}t|td}| jtt|td | jtt|td | jtt|td | jtt|td | jtt|td | jtt|td t|td}g dg dfD ]@}d
tfD ]8}t|||tB d}| t |d | t |d  |D ]}t||d}| t |d | t |d  qȐqqd S )!Nc                 S   s   g | ]
}|d  r
dndqS )r    TFr<   r}   rY   r<   r<   r=   r~     r   z:TestBufferProtocol.test_ndarray_getbuf.<locals>.<listcomp>   r   r+   r    r!  r  r   r  r5   r4   	   rg      ri  r   r   c                 S   s$   g | ]}|d  rd| nd| qS )r    r&   r%   r<   rh  r<   r<   r=   r~     s   $ r   r   )r+   r!  r+   r   rl  r  r    r!  r  r+   r4   c                    s   g | ]}|  qS r<   r<   r   r   r<   r=   r~   )  r   )r   r   r   r   r   T)r'  r   r   rk  rY  )r+   ri  r+   )r5   r   r5   r,  r   )!rb  rc  rd  PyBUF_SIMPLEr\  r^  r`  
PyBUF_FULLrP  PyBUF_RECORDSPyBUF_RECORDS_ROPyBUF_STRIDEDPyBUF_STRIDED_ROPyBUF_CONTIGPyBUF_CONTIG_ROrc   r   rJ  ND_PILr[  re  r6   calcsizer   rN   r   r<  rg  ND_GETBUF_FAILrf  rF  rM  rI  )r%  requestsZ	items_fmtZ	structureZndflagsZ
real_flagsrp   r9   r   r   r   r   Z_itemsex1ex2m1m2rV  bitsr@  baser  r:  r<   r   r=   test_ndarray_getbuf  s   



!z&TestBufferProtocol.test_ndarray_getbufc           
   	   C   s  t dgdg}t dgdgtd}t |j|jfD ]}| t|h d | t|g d | t|g d | jt|g ddhd	 | jt|g ddgdhd
 | jt|g ddgg d | jt|dgdgi d | jt|dgdgi d | jt|dgdgi d | jt|dgdgdgtd | jt|dgg td | jt|g dgd	 | jt|dgdgdd | jtj	|dgdgdd | jt|ddgdgdd | jt|g ddgdd t
d }| jt|dg| dg| d	 | jt|dgdgd	 | jt|g ddgd	 | jt|dgdd gd	 | jt|g dddgdd | jt|g ddgdgd
 | jt|dgdgdd gd
 | jt|ddgddgdgd
 | jt|g ddgdgdd | jt|g ddgdd | jt|g ddgddd | jt|g ddgd d | jtj	|dgdgd!d g d"}| jt||ddgd#d$gd%d& | jt|t d'd( | jt|dgdgtd | t|dg q| jtt d)td* t dgdgtd}| jt|jdgdgtd t d)}| t|jdgdg | t|j t dgdg}|dgdg t|}| t|jdgdg | t|j |  |  | t|j ~| tti g d | ttd)i  t ttd+dgd+ d	}| tt|d%g t ttd,ddgd	}| tt|g d- | tt|ddg | tt|d#dg | tt|d.dg t g ddgdd}t |td*}| t|j t g ddgdd}t |}	t |	}| t|j t d/gdgd0d}| t|j t
}t tt|dg| d	}| t|j t dgdgd	}| ttddddd% | tt|d1d2 | tt|d.d2 | tt|td3 | tt|td4 | tt|td5 | tt|d6d7 t dgdgd	}| ttddddd% | tti | | tt|i  t dgdgd	}| ttddddd% | tti d7 | tt|d8 d S )9Nrk  r+   r   >   r+   r    r!  )r+   r    rK   )r+   r    r!  r  r+   r    r!  r!  r  r   r   r   r/  ro  )r   rZ  r   r   r   ZXXXr   i  r   r    r!  r    ZQLr   rK   r#   r+   r    r!  r  r   rI   r+   r    r!  r  r   r   r   r  )r   r   r   r?   z@#$)
r+   r    r!  r  r   r  r5   r4   rk  rg   rm  r   )r   r   r   r   r      123rY  r   ri  )r    r!  r  r*   )r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   r+   ZLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLxyzr     u    Z   r-     )r   ND_VAREXPORTpushrf  r  rJ  rx  r   r6   r8   ZND_MAX_NDIMOverflowErrorr   rq  rF  r[  poprN   releaserD  r   rc   rp  r  memoryview_from_bufferrK  rL  UnicodeEncodeErrorrN  rI  )
r%  r  Zndmr   r   rp   rE   r@  r|  r}  r<   r<   r=   test_ndarray_exceptionsg  s   z*TestBufferProtocol.test_ndarray_exceptionsc                 C   s   t tdD ]F}dgd }tg ddgtd}t||d< tddD ]}|jg ddgd t|||< q"tdD ]
}|||    q9| t|j	 ~qd S )Nr   r   r  r!  ro  r+   r  )
r   rc   r   r  rN   r  r  rf  rF  r  )r%  permrE   r  r   r<   r<   r=   test_ndarray_linked_list  s   
z+TestBufferProtocol.test_ndarray_linked_listc                 C   sL   t dD ]\}}}t|}t|d|d}| j|d ||dddd|d	 qd S )Nr   r<   r/  TrX   r   r9   r=  r   r   r   rd   )rx   r6   ry  r   rU  )r%  r9   Zscalarrn   r   r  r<   r<   r=   test_ndarray_format_scalar  s   
z-TestBufferProtocol.test_ndarray_format_scalarc                 C   sl   t dd}t|D ]*\}}}t|}dtfD ]}t||g||d}| j|d ||dd|f|f|d	 qq	d S )Nr+   rg   r   r.  Tr  )r   rx   r6   ry  rx  r   rU  )r%  rv   r9   rp   rn   r   r   r  r<   r<   r=   test_ndarray_format_shape  s   


z,TestBufferProtocol.test_ndarray_format_shapec                 C   s   t dd}t|D ]V\}}}t|}tddD ]F}|dkrqt|d d | g}|| g}|dk r8||d  nd}	dtfD ]}
t|||||	|
d}| j|d ||dd|||d d | d	 q>qq	d S )	Nr+      r   r   r   Tr  )	r   rx   r6   ry  rc   r^   rx  r   rU  )r%  rv   r9   rp   rn   r   r   r   r   r   r   r  r<   r<   r=   test_ndarray_format_strides+  s*   


z.TestBufferProtocol.test_ndarray_format_stridesc                 C   s@   g d}t |ddd}t |ttB d}| | t|d d S )Nrj  r  )r+   r!  r   rY  )r   r^  re  r<  r  r   )r%  rp   r@  r  r<   r<   r=   test_ndarray_fortran@  s   z'TestBufferProtocol.test_ndarray_fortranc                 C   sX  t dD ]}dd t |D }t|}t|D ]}t|\}}}t|}dtfD ]~}	|dkr4|	tkr4q)t||||	d}
t|||d}t	||}| j
|
d ||d||||d	 t|rt|||d	}t|ttB d
}
| |
jdk |
 }| j
|d ||d||||d	 t||||	tB d}
t|||d}t||}| j
|
d ||d||||d	 q)qqd S )Nr   c                 S   s   g | ]}t d dqS )r    rg   r  r  r<   r<   r=   r~   H  r  z<TestBufferProtocol.test_ndarray_multidim.<locals>.<listcomp>r   r.  r   Tr  r/  rY  r<   r,  )rc   r   r   rs   r6   ry  rx  r   r   r   rU  r{   rw  re  rM  r   r  rJ  r   )r%  r   shape_trv   r   r9   rp   rn   r   r   r  r   rd   r@  mvr<   r<   r=   test_ndarray_multidimF  sP   


z(TestBufferProtocol.test_ndarray_multidimc                 C   s  t dgdgd}| t|jdd t|}| || | t|jdd t dgdgtd}| t|jd t|}| || | t|jd t dgdgtd}| t|j	d | t|jdd t|}| || | t
|j	d | t
|jdd g d}t |t|gdtd}| tj|jd	d
 | t|jdd t|}| || | t|jd	d
 | t|jdd g d}t |t|gdtd}| t|jd	d
 | tj|jdd d S )Nr+   r  r4   ro  r*   r+   r    r!  r  r   r  r5   r4   r   r.  r    i,  )r      )r+   r    r  )r   r  ZLQ)s    1r  )r   rf  r  __setitem__rN   r<  r   __delitem__r  __getitem__
IndexErrorr^   r6   r8   r   )r%  r  r  rp   r<   r<   r=   test_ndarray_index_invalids  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<   ro  .r   r4   rk  r   )	r   r   rN   r<  r  rf  r  r  r  )r%  r  r  rY   r<   r<   r=   test_ndarray_index_scalar  s"   z,TestBufferProtocol.test_ndarray_index_scalarc                 C   sR   t ttdddgtd}t |td}| t|jd | t|jtddd d S )	Nr4   r    r  ro  rY  r+   r!  r   )	r   r   rc   r   rv  rf  rF  r  r   )r%  r@  r  r<   r<   r=   test_ndarray_index_null_strides  s   z2TestBufferProtocol.test_ndarray_index_null_stridesc                 C   s  t dD ]_\}}}t|dg|d}tddD ]}| || ||  q| t|jd | t|jd t|rct|}| || tddD ]}| || ||  qF| t|jd | t|jd qt dD ]K\}}}t|dgt	|d}t|t
tB d}tddD ]}| || ||  qt|r| }| ||t tddD ]}| || ||  qqhg d}t|dgd}t|td}tddD ]}| || ||  qg d}t|dgd}t|td}tddD ]}| || ||  qd S )	Nr   r/  r  r8  rY  r  r  )rx   r   rc   r<  rf  r  r  r{   rN   r   rv  re  r  r;  __eq__NotImplementedrw  rp  )r%  r9   rp   rn   r  r   r  r@  r<   r<   r=   !test_ndarray_index_getitem_single  sH   z4TestBufferProtocol.test_ndarray_index_getitem_singlec                 C   s  t dD ]x\}}}t|dg|td}tdD ]
}|||< |||< q| | | | t|jd| | t|jd| t	|s@qt|dg|td}t
|}| || tdD ]
}|||< |||< qW| | | | t|jd| | t|jd| qt dD ]f\}}}t|dg|td}tddD ]}||d  ||< ||d  ||< q| | | t	|sqt|dg|td}t
|}| || tddD ]}||d  ||< ||d  ||< q| | | qd S )Nr   r.  r  r  r  r+   )rx   r   r   rc   r<  r  rf  r  r  r{   rN   )r%  r9   rp   Zsingle_itemr  r   r  r<   r<   r=   !test_ndarray_index_setitem_single  sF   

z4TestBufferProtocol.test_ndarray_index_setitem_singlec              
   C   s  d}t |}t|D ]}t|\}}}dtfD ]}t||||d}t||}	t|d  |d D ]J}
| |	|
 ||
   t|d  |d D ]1}| |	|
 | ||
 |   t|d  |d D ]}| |	|
 | | ||
 | |  qdqHq0t||||t	B d}t
||}	t|d  |d D ]I}
| |	|
 ||
   t|d  |d D ]0}| |	|
 | ||
 |   t|d |d D ]}| |	|
 | | ||
 | |  qqqqq
d S )Nr    r!  r   r   r.  r+   r    )r   r   rs   rx  r   r   rc   r<  r  rJ  r   )r%  r  rv   r   r9   rp   rn   r   r  rd   r   r   kr<   r<   r=   #test_ndarray_index_getitem_multidim  s<   
 &
 &z6TestBufferProtocol.test_ndarray_index_getitem_multidimc                 C   s   t ddd}| ttdt  t|}| || | ttdt  tdD ]E\}}}t |dg|d}t|D ]\}}| |||  | 	||v  q7t
|rkt|}t|D ]\}}| |||  | 	||v  qWq&d S )Nr+   r<   r  z1 in ndz1 in mvr   r/  )r   rf  r  evallocalsrN   r<  rx   r   rM  r{   )r%  r  r  r9   rp   rn   r   r   r<   r<   r=   test_ndarray_sequence:  s"   z(TestBufferProtocol.test_ndarray_sequencec              	   C   s  g d}t |dgtd}t|}| t|jtddd| | t|jtddd| t |dgtd}t |dgtd}t |td}| t|jtddd| t |dgdtd}t|}| t	|j
tddd | t	|j
tddd t |d	d
gdtd}t|}| t	|j
tdddtdddf | t	|j
tdddtdddf | t|j
d | t|j
dtdddf | t|j
tdddi f | t|j
tdddtdddf | t|j
d t |dgdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |dgdtd}t |dgdd}	t|}t|}| t	|jtddd|dd  | | | | t	|jtddd|dd  | | | t |d	d
gdtd}t |dgdd}t|}t|}| t	|jtddd|dd  | | g dg dg | t|jtddd|dd  t |dgdtd}t |dgdd}t|}t|}| t	|jtdd	d|dd  | | | | t	|jtdd	d|dd  | | | | tttddd	i  | ttdd | t	ttdddd
 t |dgdtd}
| t|
j t |dgdd}t |td}
| t|
j d S )Nr  r4   ro  r   r+   rY  r   r.  r    r  z@%$r   r   r/  r5   r  )r   r  r5   r4   z###########)r   r   rN   rf  r  r  r   rd  rF  r   r  r  r<  r  r   rx  add_suboffsetsrp  )r%  rp   xlmlr@  xrr  r  mryrrY   r<   r<   r=   test_ndarray_slice_invalidM  s   


"""""
""z-TestBufferProtocol.test_ndarray_slice_invalidc                 C   s  g d}t |dgdtd}t |dgdd}|dd |dd< | | | t|}t|}| || | || |dd |dd< | | | t |ddgdtd}t |ddgdd}|d	d
ddf |d	d
d
d
f< | | t|ddg d S )Nrj  ri  r   r.  r/  rk  r  r!  r+   r    )r   r   r<  r  rN   r   )r%  rp   rY   rT  r  r  r<   r<   r=   test_ndarray_slice_zero_shape  s   $z0TestBufferProtocol.test_ndarray_slice_zero_shapec                 C   s  d}t |}t|}t|D ]z}t|\}}}t|}dtfD ]g}	t||||	d}
t||}t	||D ]R}d }zt
||}W n tyT } z	|j}W Y d }~nd }~ww d }z|
| }W n tyr } z	|j}W Y d }~nd }~ww |sw|r~| || q4| | | q4q qd S Nr  r   r.  )r^   r   r   rs   r6   ry  rx  r   r   r  r   	Exception	__class__r;  r<  r  )r%  r  r   rv   r   r9   rp   rn   r   r   r  rd   r   listerrr'  enderrndslicedr<   r<   r=   test_ndarray_slice_multidim  s<   

z.TestBufferProtocol.test_ndarray_slice_multidimc                 C   s,  d}t |}t|}t|D ]}t|\}}}t|}t|||d}	|	  t|||d}
|
  t|
}t	||}t
||D ]R}d }zt||}W n ty` } z	|j}W Y d }~nd }~ww d }z|	| }W n ty~ } z	|j}W Y d }~nd }~ww |s|r| || q@| | | q@qd S )N)r    r!  r   r    r/  )r^   r   r   rs   r6   ry  r   r  rN   r   r  r   r  r  r;  r<  r  )r%  r  r   rv   r   r9   rp   rn   r   r  r@  r  rd   r   r  r'  r  r  r  r<   r<   r=   'test_ndarray_slice_redundant_suboffsets  s@   

z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetsc                 C   s   t dD ]\}}}tdD ]}tdD ]}dtfD ]}|tB }t|dg||d}t|dg||d}	t|	}
d }d }|d d  }z|| }|| }|| ||< t|t|k}W n tyl } z	|j}W Y d }~nd }~ww d }z|| ||< W n ty } z	|j}W Y d }~nd }~ww |r| 	|t
 n| | | | 	|| t|sqd }z|
| |
|< W n ty } z	|j}W Y d }~nd }~ww |r| 	|t
 q| |
 | | |
| | 	|| | j|
|	|j|d|j|j|j| d	 qqqqd S )Nr   r   r.  Fr  )rx   r
  rx  r   r   rN   r^   r  r  r;  r   r<  r  r{   rU  r   r   r   r   )r%  r9   rp   rn   lslicer  r   r   r  r@  r  ZlsterrZdiff_structurerd   ZlvalZrvalr  r  Zmverrr<   r<   r=    test_ndarray_slice_assign_single  sl   z3TestBufferProtocol.test_ndarray_slice_assign_singlec                 C   s$  d}t |}t|}t|D ]}t|\}}}dtfD ]s}ttD ]l}t||\}	}
t||||t	B d}t
||}d }z	t|||	|
}W n tyY } z	|j}W Y d }~nd }~ww d }z||
 ||	< W n tyy } z	|j}W Y d }~nd }~ww |s~|r| || q!| | | q!qqd S r  )r^   r   r   rs   rx  rc   
ITERATIONSr   r   r   r   r   r  r  r;  r<  r  )r%  r  r   rv   r   r9   rp   rn   r   r   r   r  rd   r  rB  r  r  r<   r<   r=   "test_ndarray_slice_assign_multidim3  s@   
z5TestBufferProtocol.test_ndarray_slice_assign_multidimc                 C   s4  t tD ]}td D ]}t|}t|dttd}| t	|  t
||}t|||}| }t|}t|rA| }	| |	| |d dkrqt|||td}
|
 }| || t|
}t|rq| ||
 | }	| |	| tr|d }d|v r|q
t|||}| j|d |j|d|j|j|j| d		 q
qd S )
Nr@   Tr   r   r    r   r  r!  Fr  )rc   r  rR   r6   ry  r   MAXDIMMAXSHAPErM  r   r   r   r  rN   r{   r<  rx  r   r   rU  r   r   r   r   )r%  rn   r9   r   rw   rp   rY   Zxlistr  ZmvlistrT  Zylistr   zr<   r<   r=   test_ndarray_randomT  sH   

z&TestBufferProtocol.test_ndarray_randomc                 C   s   t tD ]j}td D ]c}t|}t|dttd}| t	|  t
||}d}zt|||}W n tyD } z	|j}W Y d }~nd }~ww | | trmd}	zt|||}
W n tyk } z	|j}	W Y d }~nd }~ww 	 q
qd S )Nr@   Fr  )rc   r  rR   r6   ry  r   r  r  assertFalser   r   r   r  r  rM  r   r   )r%  rn   r9   r   rw   rp   r  rY   r  Z	numpy_errrT  r<   r<   r=   test_ndarray_random_invalid}  s8   


z.TestBufferProtocol.test_ndarray_random_invalidc                 C   s*  t tD ]}td D ]}t|}tttd\}}}}t|d|d}t|d|d}	| 	t
|  | 	t
|	  t||}
t||	}t|
||}t|||	}|| ||< | }| }t|}t|}| | | | | | |d dkr|	d dkrt|
||td}t|||	td}|| ||< | }| }| || | || t|}t|}| | | | | | trd|v sd|v rqt|
||}t|||	}|| ||< t|st|	s| j|d |j|d|j|j|j| d		 | j|d |j|d|j|j|j| d		 qqd S )
Nr@   r  Tr  r    r   r  Fr  )rc   r  rR   r6   ry  r   r  r  r   rM  r   r   r   r  rN   r<  rx  r   r   r   rU  r   r   r   r   )r%  rn   r9   r   r   r   r   r   tltrZlitemsZritemsr  r  ZxllistZxrlistr  r  Zylr  ZyllistZyrlistZzlZzrr<   r<   r=    test_ndarray_random_slice_assign  sh   






z3TestBufferProtocol.test_ndarray_random_slice_assignc                 C   sv   g d}t |ddgtd}t |}| |jt@  | |j| | |jd | |j	 | |j
 | |j d S )Nrj  r!  r  ro  )r   r   )r   rx  rM  r   r;  rX   r<  r  r  r]  r_  ra  )r%  rp   r  r@  r<   r<   r=   test_ndarray_re_export  s   z)TestBufferProtocol.test_ndarray_re_exportc              	   C   s   dt fD ]n}tg ddg|d}t|}| || | | g  | | g  tg dg d|d}| | g  tg dg d|d}| | g g g g tg dg d|d}| | g g g gg g g gg g g gg qd S )Nr   r  ro  r   r!  r!  r!  r   r!  )r!  r!  r   rx  r   rN   r<  r  r%  r   r  r  r<   r<   r=   test_ndarray_zero_shape  s   
z*TestBufferProtocol.test_ndarray_zero_shapec                 C   sd   dt fD ]+}tdgdgdg|d}t|}| || | | g d | | g d qd S )Nr   r+   r   r  )r+   r+   r+   r+   r+   r  r  r<   r<   r=   test_ndarray_zero_strides  s   z,TestBufferProtocol.test_ndarray_zero_stridesc                 C   s>   t ttddgdd}| |jd | | g d d S )N   r!  r5   r  )r5   r4   rk  )r   r   rc   r<  r   r  r%  r  r<   r<   r=   test_ndarray_offset   s   z&TestBufferProtocol.test_ndarray_offsetc                 C   s>   dt fD ]}tttddg|d}| }| || qd S Nr   r!  ro  )rx  r   r   rc   r  r<  )r%  r   r  rE   r<   r<   r=   #test_ndarray_memoryview_from_buffer  s
   z6TestBufferProtocol.test_ndarray_memoryview_from_bufferc                 C   sP   dt fD ]!}tttddg|d}tdD ]}| || t||g qqd S r  )rx  r   r   rc   r<  rD  )r%  r   r  r   r<   r<   r=   test_ndarray_get_pointer  s   z+TestBufferProtocol.test_ndarray_get_pointerc                 C   s\   t ttdg dd}t |ttB d}| | |  t|}| | |  d S )Nr  )r    r    r   r  rY  )r   r   rc   rd  re  r<  r  rN   )r%  r@  r  rE   r<   r<   r=    test_ndarray_tolist_null_strides  s
   z3TestBufferProtocol.test_ndarray_tolist_null_stridesc                 C   s   |  tdd tttdddgd}tttdddgd}|  t|| tdgdgdd	}| t|d
 | td
| d S )Nr  s   456ri  r!  r  r  r+   r   r/  rQ   )r  rN  r   r   rc   rM  )r%  rY   rT  r<   r<   r=   test_ndarray_cmp_contig  s   z*TestBufferProtocol.test_ndarray_cmp_contigc                 C   s  t  dg d}t|}| tt| tttd}tttddgd}| t|t| tttdddgd}| t|t| tttdg dd}| t|t| tt	ttdddgd}tttdddgt
d}| t|t| tt	ttdg d	d}tttdg d	t
d}| t|t| tttd}tttdg d
td}| t|t| tttdg d
dd}| t|t|  d S )Nr   r  ri  r  r!  r  )r!  r    r    ro  r    r!  r    r    r    r!  r/  )rL   r   rf  r   hashrS   r   rc   r<  r   rJ  rx  r   )r%  rG   r  r   r<   r<   r=   test_ndarray_hash&  s*   z$TestBufferProtocol.test_ndarray_hashc                 C   s8  t ttttttttt	t
tf}| tti dt tdddtd}dD ]}|D ]}t|||}| ||  q%q!tdgdgdtd}dD ]}|D ]}t|||}| |d	 qGqCtttd
g ddtd}dD ]}|D ]}t|||}| |d	 qkqgdtfD ]O}tdgdgd|tB d}| }dD ]}|D ]}t|||}| || qqtg ddgd|tB d}| }dD ]}|D ]}t|||}| || qqqtg ddgdgtd}| }dD ]}ttfD ]}t|||}| || qq|d d d }| }dD ]#}|D ]}zt|||}W n ty   Y qw | || qqttd}dtfD ]U}t|ddg|tB d}trVtt|ddgd|dkrRdndd}	|tkrntt|ddgddgtd}
|
 }n| }|D ]B}zt|d|}W n ty   Y qtw | || tdd |D ddgtd}| t|t| tr| ||	jdd qt|dkrtt|ddgddgtd}
n	t|ddgtd}
|
 }ttt ttfD ]D}zt|d|}W n ty   Y qw | || tdd |D ddgttB d}| t|t| tr"| ||	jdd q|tkr7t|ddgtd}
|
 }n| }ttt ttfD ]D}zt|d|}W n tyW   Y qBw | || tdd |D ddg|tB d}| t|t| tr| ||	jdd qBq2t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,  rk  r<   r   r.  r+  r+   r       r4   r    r   r5   r   r  r!  r   r    r  r   ri  r  ro  r   r   )r   r   r   r:  c                 S      g | ]}|qS r<   r<   r   r<   r<   r=   r~     r*  zCTestBufferProtocol.test_py_buffer_to_contiguous.<locals>.<listcomp>r9  c                 S   r  r<   r<   r   r<   r<   r=   r~   	  r*  r-  c                 S   r  r<   r<   r   r<   r<   r=   r~   	  r*  c                 S   r  r<   r<   r   r<   r<   r=   r~   +	  r*  c                 S   r  r<   r<   r   r<   r<   r=   r~   2	  r*  c                 S   r  r<   r<   r   r<   r<   r=   r~   8	  r*  )rb  rc  rd  rp  rq  rP  rr  rs  rt  ru  rv  rw  rf  r  rO  r   r   r<  r   r   rc   rJ  rF  r   r   r   rN   Ztostringrx  )r%  r{  r  r:  Zrequestr   r   Zndbytesrd   ZnarY   rR  rT  r<   r<   r=   test_py_buffer_to_contiguousK  s  G


 

  z/TestBufferProtocol.test_py_buffer_to_contiguousc                 C   sv  dg fg ddgft tdg dfg}|D ]\}}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r|gn|}| j
|d dd	d
d|jgd|d	 q|D ]K\}}t||td}t|}| |j | |j t|}t||dd}t||}| j
||dd	d
||||d	 t|}	| j
|	|dd	d
||||d	 q|dd  D ];\}}t||td}t|}t|}t	||}| j
||dd	d
|||j|d	 t|}	| j
|	|dd	d
|||j|d	 q| ttdd | tti  tg ddgd}t|td}
| tt|
 t|ttB d}
| tt|
 tdgd dgd dd}
| tt|
 | t|
j | tt|
td | tt|
td | tt|
d d d td d S )Nrk  r  r!  r  r  r  r+   r   r   Tr  rY  r<   r?   r   r+   ro  r,     9rY   r#   r   r/  r   )r   rc   r   rN   rM  r]  ra  r^   r   r   rU  rw  re  r<  r   r  rp  r   r   r   rJ  r_  r   rx  rf  r  rF  r   rK  rL  )r%  Zitems_shaperp   r   r@  rE   r   r   rd   r  r  r<   r<   r=   test_memoryview_construction;	  s   (




 z/TestBufferProtocol.test_memoryview_constructionc                 C   s   g d}g dg dg dfD ]}t ||d}| |j t|}| t|jd qtddD ]\}}}td}||}| |	 d | |
 g  q/d S )	Nr  r  r  r  r   r+   rN   r  )r   rM  r]  rN   rf  r  r(  rx   r<  r   r  )r%  rp   r   r@  msrcr9   rn   rE   r<   r<   r=   test_memoryview_cast_zero_shape	  s   
z2TestBufferProtocol.test_memoryview_cast_zero_shapec                 C   s   | j }tj}d}d}ttd}|td||d|   t|ddgdd	}|t|||d|   t|g d
dd	}|t|||d|   d S )NzPnin 2P2n2i5P PZ3nr4   r  r+   r    r  r   r/  )r    r    r    r!  )check_sizeofr   Zcalcvobjsizer   rc   rN   r   )r%  checkZvsizeZbase_structZper_dimrp   rG   r<   r<   r=   test_memoryview_sizeof	  s   z)TestBufferProtocol.test_memoryview_sizeofc                 C   s  G dd dt }G dd dt }dd }|d|ddtd	d
tddg dh dddiddddd tddtdtddddd|dd g}tddD ]\}}}t|dg|td}t|dg|td}	t|}
t	
||	d| ||
d< | |
d |	d  t	|}d|v rqI|D ]W}d }z
t	
||	|| W n t	jy   t	j}Y nw d }z||
d < W n ttfy } z	|j}W Y d }~nd }~ww |s|r| |d  | |d  q| |
d  |	d   qqId S )!Nc                   @      e Zd Zdd Zdd ZdS )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.INTc                 S   
   || _ d S rt   valr%  r  r<   r<   r=   __init__	  r   zFTestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__init__c                 S      | j S rt   r  r$  r<   r<   r=   __int__	     zETestBufferProtocol.test_memoryview_struct_module.<locals>.INT.__int__N)__name__
__module____qualname__r   r  r<   r<   r<   r=   INT	      r  c                   @   r  )z=TestBufferProtocol.test_memoryview_struct_module.<locals>.IDXc                 S   r  rt   r  r  r<   r<   r=   r   	  r   zFTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__init__c                 S   r  rt   r  r$  r<   r<   r=   	__index__	  r  zGTestBufferProtocol.test_memoryview_struct_module.<locals>.IDX.__index__N)r  r  r  r   r	  r<   r<   r<   r=   IDX	  r  r
  c                   S   s   dS )Nr5   r<   r<   r<   r<   r=   r   	      z;TestBufferProtocol.test_memoryview_struct_module.<locals>.frk  y@      @z-21.1gffffff(@r   r    r  >   r  r   r  r5   r4   r<   rk  TF   as   abcrG   abcc                 S   s   | S rt   r<   )rY   r<   r<   r=   <lambda>	  r  zBTestBufferProtocol.test_memoryview_struct_module.<locals>.<lambda>rg   rN   r.  r   r   r+   )objectr	   r
   Ellipsisr   rx   r   r   rN   r6   r   r<  ry  r8   r  r   r  ZassertIsNot)r%  r  r
  r   valuesr9   rp   rq   r@  r  rE   r   r   Z
struct_errZmv_errr  r<   r<   r=   test_memoryview_struct_module	  sR   

z0TestBufferProtocol.test_memoryview_struct_modulec                 C   s>   t g ddgdgd}| |j t|}| t|jd d S )Nr  r!  r   r   r   )r   r  r]  rN   rf  r  r(  )r%  r@  r  r<   r<   r=   !test_memoryview_cast_zero_strides	
  s   z4TestBufferProtocol.test_memoryview_cast_zero_stridesc              	   C   s2  t D ]C}tdrd| n|}t|}t D ]/}tdrd| n|}t|}tttdd| g|d}t|}| t	|j
|d| g qqtdD ]@\}}	}
t|	dg|d}t|}tdD ]*\}}
}
t|su| t|j
|d| g q_t|st|s| t	|j
|d| g q_qJtd}td}tttd| dd|gdd}t|}| jt	|j
dd|gdd tttd	g d
d}t|}| t	|j
 | t	|j
ddd | t	|j
i  | t|j
d | t|j
d | t|j
d | t|j
d | t|j
d | t|j
d | t|j
d | t|j
d | t|j
d tddgdgdd}t|}| t|jd | t|jdd | t|j tttd	g d
d}t|}| jt	|j
di d tttd	d	gd}t|}| jt|j
ddgd | jt|j
ddgd | jt|j
dg dd | jt|j
dg dd | jt	|j
dg d d ttd!d" td#D g d$d}t|}| jt	|j
d%g d&d tttd'd'gd%d}t|}| t|j
d%dgd'  ttd(d" td#D d#gd}t|}| jt	|j
d%g d&d ttd)d" td#D d#gd}t|}| jt	|j
dg d&d tttd'd'gd%d}t|}tttd'd'gdd}t|}tjd*krv| t	|j
dg d+ | t|j
dg d, | t|j
d%g d- d S | t	|j
ddd.g | t|j
dg d/ | t|j
d%g d0 d S )1Nr    r@   r0   r/  r+   r   r   r  x   r  r  r!  Xz@Xz@XYz=Bz!Lz<Pz>lZBIZxBIr  r  ZIIr   r4   r   r*   r   )r    r!  r  r   r  r5   r   )r    r!  r  r   r  r5   r   )r    r!  r  r   r  r5   rY   c                 S   r)  r  r<   r  r<   r<   r=   r~   ^
  r*  zCTestBufferProtocol.test_memoryview_cast_invalid.<locals>.<listcomp>i  )r!  r   r5   rl  r   )r    r!  r  r   r#   c                 S   r)  r  r<   r  r<   r<   r=   r~   h
  r*  c                 S   r)  r  r<   r  r<   r<   r=   r~   m
  r*  l    )r5   r5   I   r-   iQ  iAj i	 )   r     r  r4   )r  r  r  r  r    )r  r  r0   r0   r    )r  r  r0   r4   r    )NON_BYTE_FORMATr   r6   ry  r   r   rc   rN   rf  r  r(  rx   r{   r   ry   r  r  r  r  r  r  maxsize)r%  ZsfmtZsformatZssizeZdfmtZdformatZdsizer@  r  Zsitemsrn   Zsize_hZsize_drE   r  r~  r  r<   r<   r=   test_memoryview_cast_invalid
  s   




 "  z/TestBufferProtocol.test_memoryview_cast_invalidc                    sj  ddd fddd fddd ff  fdd	}t d
}tdg d
d}t|dd\}}t|}|d}j||dddd|d|dd
 t d
}tdg| |gdd}t|d
|g d\}}t|}|jd
g d}j|||d
dddd|dd
 tddD ]\}	}
}t|	|
}t|}||||
|	 q}tddD ]\}	}
}t|
dg|	t	d}t|}||||
|	 qd S )Nr   c                 S   s   t |  S rt   r   r   r@  r<   r<   r=   r  
  s    z9TestBufferProtocol.test_memoryview_cast.<locals>.<lambda>r   c                 S      dd t |  D S )Nc                 S   s    g | ]}|d kr|d n|qS )r-   r"   r<   rh  r<   r<   r=   r~   
  s     MTestBufferProtocol.test_memoryview_cast.<locals>.<lambda>.<locals>.<listcomp>r  r  r<   r<   r=   r  
  r  r   c                 S   r   )Nc                 S   s   g | ]	}t t|d qS )zlatin-1)rS   chrrh  r<   r<   r=   r~   
  s    r!  r  r  r<   r<   r=   r  
  r  c           
         s   t |} D ]?\}}||}|| }j|| d|ddd| gd|dd
 ||}	|	|  |  }j|	| ||dddg|f|dd
 qd S )Nr+   Fr1   r  T	rX   r   r9   r=  r   r   r   rd   r(  )r6   ry  r(  rU  r<  r  )
r@  rE   rp   r9   srcsizerO   Zto_bytelistr  rd   m3Zbytespecr%  r<   r=   iter_roundtrip
  s$   



z?TestBufferProtocol.test_memoryview_cast.<locals>.iter_roundtripr   rk  r/  r+   Tr  r#  r  r   r<   r1   rL   rN   r.  )
r6   ry  r   r   rN   r(  rU  rx   rL   r   )r%  r'  r$  r@  Z	destitemsZ	destshaperE   r  Zdestsizer9   rp   rn   r<   r&  r=   test_memoryview_cast
  sD   





z'TestBufferProtocol.test_memoryview_castc                 C   s  t  D ]}td D ]}|dkrq	dtd | }t|}t|| }t|r)dnd}t||D ]\}}}	t|}
|dkrA|gng }||
g }t|||d}t	|}t
||||d	\}}|d u rk| t|j|| q0|d
krpq0t|||d}|j||d	}t|}|j}| }| j||||d||||dd
 ||}|j||d	}t|}|j}| }| j|||
|d||||dd
 | j|||
|d||||dd
 q0q	qtr`G 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 d S )Nr@   r   )r?   r@   r    rN   rO   r   r/  r  r   Tr#  c                   @   s    e Zd ZdejfdejfgZdS )z>TestBufferProtocol.test_memoryview_cast_1D_ND.<locals>.BEPointrY   rT  N)r  r  r  ctypesc_longc_double_fields_r<   r<   r<   r=   BEPoint
      r-  r   g33333i@r   r+   Fr  r<   333333?r   )r	  rR   r   r6   ry  r   ry   rx   r   rN   r   rf  r  r(  r^   r   r  rU  r)  BigEndianStructurer<  rX   r   r;  r=  r   r   r   r  r+  )r%  Z_tshaperW   ZtfmtZtsizer   rX   r9   rp   rn   sizer   Ztshaper@  rE   Ztitemsr  r  r   r   rd   r%  Zm4r-  pointr~  rY   r<   r<   r=   test_memoryview_cast_1D_ND
  s   




6



z-TestBufferProtocol.test_memoryview_cast_1D_NDc                 C   s2  t  dttdd}t|}| || | | |  |dd d }|dd d }| || | | |  tttdg ddd	}t|}| | |  td
dgdgdd	}t|}| t|j tdgdgdd	}t|}| t|j tg dddgdd	}t|}| t|j d S )Nr   r  r  r    r!  i	  )rl  r    r5   r!  r   r   r/  )r    r   )r5   rl  r:      12345r+   r   )r     b   c   d   e   f)	rL   r   rc   rN   r<  r  r   rf  r  )r%  rG   rE   r@  r<   r<   r=   test_memoryview_tolist  s(   z)TestBufferProtocol.test_memoryview_tolistc                 C   sH   t td}| }| |d |  | }| |d d S )Nrk  z<memoryz	<released)rN   r   __repr__rM  
startswithr  )r%  rE   r   r<   r<   r=   test_memoryview_repr3  s   z'TestBufferProtocol.test_memoryview_reprc                 C   s|   dD ]%}t d}t|d|dg}t|}| d| | d| | d| qtdg dd}t|}| ttdt  d S )	N)r   r   g            ?g      @g      "@r   r  z9.0 in m)	floatrL   rN   ZassertInr   rf  r  r  r  )r%  r9   infr@  rE   r<   r<   r=   test_memoryview_sequence<  s   z+TestBufferProtocol.test_memoryview_sequencec                 c   sN    |  t}d V  W d    n1 sw   Y  | t|jd|f  d S )Nz#index out of bounds on dimension %d)rf  r  r<  str	exception)r%  dimcmr<   r<   r=   assert_out_of_bounds_errorJ  s   z-TestBufferProtocol.assert_out_of_bounds_errorc              	   C   sF  t dg dd}t|}| |d d | |d | | |d | | t|jd t dg dd}t|}| t|jd t ttd	d	gt	d
}t|}| t
|jd | t|jd | t|jd | t
|jd | t
|jd t ttdddgt	d
}t|}| |d d | |d d | |d d | |d d | |d d dD ]}| jdd ||df  W d    n1 sw   Y  qdD ]}| jdd |d|f  W d    n1 sw   Y  q| t
|jd | t
|jd | t|jd  | t|jd! | t|jd | t|jd d S )"N      )@r   r/  r<   .r   r  Ziiir5   ro  r*          @        r4   ri  r!  r  r   r   )r    r   r  rl  r   r   )r  rn  r!  rn  r+   rD  r  r  r    r*   r   r)   r   r   r   rI  rI  )r   rN   r<  rf  r  r  r  r   rc   r   r  rF  )r%  r@  rE   indexr<   r<   r=   test_memoryview_indexQ  sN   z(TestBufferProtocol.test_memoryview_indexc              	   C   s  t dg dtd}t|}d|d< | |d d d|d< | |d d | t|jdd	 t ttd
d
gd}t|}| t|jdd t ttd
d
gtd}t|}| t	|jdd | t|jdd | t|jdd | t	|jdd | t	|jdd t
d D ]5}|dks|dkrqt g ddg|td}t|}tdd}d||< | || d | || ||  qt g ddgdtd}t|}d|d< | |d d t g d dgdtd}t|}d!|d"< | |d" d! t d#gd"gdtd}t|}| t|jdd$ t ttd%g d&td}t|}t
d  D ]R\}}|dkr+q |dkr2q ||}|\}	}
|d'ksE|dkrOdd(  dd( }	}
|d)krh| t|jd|	d"  | t|jdd* | t|jd|
 q |d}| t|jdd+ t ttd"d"gd,td}t|}| t|jdd" t d-gd"gd.td}t|}| t|jdd" t ttd/dd0gtd}t|}d1|d2< | |d d" d1 d3|d4< | |d d d3 d5D ] }| jd"d6 d||df< W d    n	1 sw   Y  qd7D ] }| jdd6 d|d|f< W d    n	1 sw   Y  q| t	|jd8d | t	|jd9d | t|jd:d | t|jd;d | t|jdddg d S )<NrG  r   r.  g     6@r<   g     7@.r   g333338@r5   r  r    rg   ro  r*   rk  rH  rI  rl  rJ  r  r4      r@   r   r   r  r!  r  )   1   2   3r  )TFTTr+      xr   r  r  r   r  r   r  s   ZxLr4  r   ri  r  *   )r   r+   +   rL  rM  rN  rO  rP  r)   rQ  rR  )r   r   rN   r<  rf  r  r  r   rc   r  rR   r   r;  rp   r(  r   r  rF  )r%  r@  rE   r9   r   r  r~  Z_ranger  lohirS  r<   r<   r=   test_memoryview_assign  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 dtfD ]}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< | || | || qd S )Nri  ro  r   r    r+   r<   r   rl  )r   r   r   r      r  r   r!  r  rm  )r   r   rc   r   rN   rf  r   r  r   r  r   r  r  rx  r<  )r%  r@  rE   r   r|  r}  r~  r  r<   r<   r=   test_memoryview_slice  sP   


 
((,,z(TestBufferProtocol.test_memoryview_slicec                 C   s  dd }t ddD ]}t|dD ]\}}}t|D ]}t|D ]}t||}t||}	t|	}
| |
| | |
 |  | |
 |  | t|
t| || ||	|
| d }d }z|| }|| }|| ||< t|t|k}W n t	y } z	|j
}W Y d }~nd }~ww d }z|
| |
|< W n t	y } z	|j
}W Y d }~nd }~ww |r| |t q|s|r| || q| |
| | |
 |  | |
 |  || ||	|
| qqqq	d S )Nc           	      S   s   t |D ]K\}}|| }|| }| || |||< ||kr+| || | || n| || | || |||< | || | || |||< |||< qd S rt   )r   r<  assertNotEqual)	ZtestcaserG   r   rE   
singleitemr   rn   Zaimir<   r<   r=   cmptest-  s    
z9TestBufferProtocol.test_memoryview_array.<locals>.cmptestr+   r   rL   )rc   rx   r
  rL   rN   r<  r  r   r^   r  r  r;  r   )r%  rd  r   r9   rp   rb  r  r  rG   r   rE   Z	array_errZhave_resizeZalarr  Zm_errr<   r<   r=   test_memoryview_array+  sV   z(TestBufferProtocol.test_memoryview_arrayc           	      C   s  t  dg d}t  dg d}t|}t|}dD ]}| t|||t | t|||t qt|}|  | || | || | || t|}t|}|  | || | || t|}| |g d tdgdgdt	d}d	t
d
f|d< | t|| t  dd}t|}| || | || trG dd dtj}|dd}t|}t|}| || | || | || | t|j d S d S )Nr   r  )r+   r    r5   )__lt____le____gt____ge__rK  r+   zl x d xr.  r   r   r   ur  c                   @       e Zd ZdejfdejfgZdS )zITestBufferProtocol.test_memoryview_compare_special_cases.<locals>.BEPointrY   rT  Nr  r  r  r)  r*  r,  r<   r<   r<   r=   r-    r.  r-  r   r  )rL   rN   r;  getattrr  r  r<  ra  r   r   r?  r)  r0  rf  r  r  )	r%  rG   r   r   wattrr  r-  r2  r<   r<   r=   %test_memoryview_compare_special_casesi  sH   
z8TestBufferProtocol.test_memoryview_compare_special_casesc                 C   sn  t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d|d< | || | || | || | || | || | || | || | || t tt	ddgtt
B d	}t tt	ddgtt
B d	}t|}| || d
|d< | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || t dg dd}t dg dtd}t|}t|}| || | || | || | || | || | || | || | || d S )N  @Lr/  r   r.  i  r<   ri  ro  r   rk  )rr  r/  r4  ZLf5sZhf5s)i?r/  r4  )r   r   rN   r<  r  __ne__ra  r  r   rc   rx  )r%  nd1nd2r   ro  r  r@  rE   r<   r<   r=   !test_memoryview_compare_ndim_zero  sj   z4TestBufferProtocol.test_memoryview_compare_ndim_zeroc                 C   sX  t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g ddgdd}t g ddgdd}t|}t|}| || | || | || | || | || t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  t g dd	gdd}t g ddgdtd}t|}t|}| ||d d d
  | |d d d
 | | ||d d d
  | |d d d |d d d  d S )N@    'r   @hr/  )ry  rz  r{  r|  i  z<iz>h)ry  r{  r}  r!  r    r   rm  z!hz<lr.  zh  0cz>  h)r   rN   r<  ra  rx  r%  ru  rv  r   ro  r<   r<   r=    test_memoryview_compare_ndim_one  sl      $z3TestBufferProtocol.test_memoryview_compare_ndim_onec                 C   s   t ddgdgdd}t ddgdgdd}t|}t|}| || | || | || | || | || t ddgdgdd}t ddgdgd	d}t|}t|}| || | || | || | || | || d S )
N  r  r   r~  r/  i|i?z= h0cz@   ir   rN   r<  r  r<   r<   r=   "test_memoryview_compare_zero_shape%  s$   z5TestBufferProtocol.test_memoryview_compare_zero_shapec                 C   s   t g ddgdd}t dgdgdgdd}t|}t|}| || | || | || | || | || t d	gd dgd
d}t d	gdgdgdd}t|}t|}| || | || | || | || | || d S )N)r  r  r  r  r  rs  r/  r  r   r   r  )r  r  z@ Liz!L  hr  r  r<   r<   r=   $test_memoryview_compare_zero_strides?  s$   z7TestBufferProtocol.test_memoryview_compare_zero_stridesc           
      C   s   d}t d D ]7}t|dd|\}}}dtfD ]&}t||g||d}t|}| || |d d d }t|}| || qqd}tdD ]4}	t|\}}}dtfD ]&}t||g||d}t|}| || |d d d }t|}| || qQqDd S )	Nrg   rF   rN   r@   r   r.  r  r   )rR   rs   rx  r   rN   r<  rc   )
r%  r   rW   r9   rp   rb  r   r  rE   rn   r<   r<   r=   &test_memoryview_compare_random_formatsY  s.   
z9TestBufferProtocol.test_memoryview_compare_random_formatsc                 C   s  t ttddg ddd}t ttddg ddd}t|}t|}| || | || | || | || | || t dgd g dd	d}t d
gd g ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t dgd ddgdd}t dgd ddgdd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr3   r!  r    r   r~  r/  r   r  r   r+   r    z=f q xxL)g333333r+   r    z< f 2Qr  r      r!  r5   z! b B xLz
= Qx l xxLr   r   r   rc   rN   r<  ra  r  r<   r<   r=   "test_memoryview_compare_multidim_cu  sZ   z5TestBufferProtocol.test_memoryview_compare_multidim_cc                 C   s  t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t dgd	 d
dgdtd}t dgd	 d
dgdtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttddg ddtd}t ttddg ddtd}t|}t|}| || | || | || | || | || t ttdg ddtd}t ttdg ddtd}t|}t|}| || | || | || | || | || d S )Nr  r3   )r   r    r!  r~  r.  r   r  r   r   r  r    r!  =Qq)r   r   z=qQr  r   r  Z0llrH   )r   r   rc   rJ  rN   r<  ra  r  r<   r<   r=   (test_memoryview_compare_multidim_fortran  s   z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc           	      C   sP  t tdd}t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || dgd	 }d
|d< t|g d}t|g ddd}t|g ddtd}t|}t|}| || | || | || tt tdddgdd}|dddd d df }tt tdddgdd}|dddd d df }t|}t|}| || | || | || | || | || tdgd ddgdd}|dddd d df }tdgd ddgdd}|dddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tt td	g ddd}|ddd d df }tt td	g ddd}|ddd d df }t|}t|}| || | || | || | || | || tdgd	 g d d!d}|ddd d df }tdgd	 g d d"d}|ddd d df }t|}t|}| || | || | || | || | || d S )#Nr  r3   r  @lr/  r   r.  )gffffff
rY  r  )gr  rY  r   zd b czd h c(   r4   @Ir!  r+   r   rm  r   )r  r%      rl  r    z=iiz>iir  r   r   )r    r  r   r!  r    Zb3sZi3s)r   rc   r   r   rJ  rN   r<  ra  )	r%  lst1lst2ru  rv  r   ro  r|  r}  r<   r<   r=   &test_memoryview_compare_multidim_mixed  s   
z9TestBufferProtocol.test_memoryview_compare_multidim_mixedc                 C   s  t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || t ttdg ddd}t ttdg ddd}t|}t|}| || | || | || | || | || d S )Nr  )r   r!  r    r   r/  )r   r   r    z@ir  r  r<   r<   r=   +test_memoryview_compare_multidim_zero_shape\  s$   z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapec                 C   s  t dgd g ddd}t dgg dg ddd}t|}t|}| || | || | || | || | || | | |  t d	gd
 ddgdd}t d	gddgddgdd}t|}t|}| || | || | || | || | || d S )Nr  r   )r  r   r  rs  r/  rQ  r   r  r  rg   r    r   z=lQr   z<lQ)r   rN   r<  r  r  r<   r<   r=   -test_memoryview_compare_multidim_zero_stridesv  s&   z@TestBufferProtocol.test_memoryview_compare_multidim_zero_stridesc           	      C   s^  t ttdddgdd}|dddd d d	f }t ttdddgd
td}|dddd d d	f }t|}t|}| || | || | || | || | || t dgd ddgdtd}d|d d< |dddd d d	f }t dgd ddgdttB d}d|d d< |dddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}|ddd d d	f }t dgd g ddd}|ddd d d	f }t|}t|}| || | || | || | || | || t ttdg ddtd}|ddd d d	f }t ttdg ddtd}|ddd d d	f }t|}t|}| || | || | || | || | || t dgd g ddttB d}d|d d d< |ddd d d	f }t dgd g ddttB d}d|d d d< |ddd d d	f }t|}t|}| || | || | || | || | || ttdd }t|g d}t |g dd!td}t |g dd"t	tB d}t|}t|}| || | || | || d#gd }d$|d%< t|g d}t |g dd&td}t |g dd't	tB d}t|}t|}| || | || | || d S )(Nr  r   r4   r  r/  r!  r+   r   rm  r   r.  r  r  )r+   rm  r    r5   z>Qqr  r  r   r  )r  r   ZBbr  r   )s   hellor  r+      )r!  r!  r!  Z5s0sP)s   sushir  r+   r  r3   r  r   )s   sashimis   slicedg4@)s   ramens   spicygfffff"@rl  z
< 10p 9p dz
> 10p 9p d)
r   r   rc   rx  rN   r<  ra  r   r   rJ  )	r%  r|  ru  r}  rv  r   ro  r  r  r<   r<   r=   +test_memoryview_compare_multidim_suboffsets  s   
z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetsc                 C   sR  dD ]}t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || t dgd g d|d d}t dgd g d|d ttB d}d|d	 d
 d d d< t|}t|}| || | || | || | || | || qd S )N)rC   rA   rB   rD   r(   r  )r!  r   r    r    r    r   r/  r.  r+   r    r!  )r(   r&   r$   ZQLH)r+   r+   r+   )r   r   rJ  rN   r<  ra  )r%  	byteorderrY   rT  rG   r   r<   r<   r=   !test_memoryview_compare_not_equal  s8   z4TestBufferProtocol.test_memoryview_compare_not_equalc                 C   s  t  dg d}t|}|  | tt| | t|jd | tt| | t|j | t|j | tt	dt
  | t|jd | t|jdd dD ]
}| t|j| qPt  dg d}t|}t|}| || |  | || | || | || d S )Nr   )g?g@gffffff
@r   z1.0 in mr   r+   )rX   r   r=  r   r   r   r   r   r  r]  r_  ra  )rL   rN   r  rf  r   r(  r   r  r   r  r  r  r  __getattribute__r<  ra  )r%  rG   rE   rp  r   r~  r  r<   r<   r=   test_memoryview_check_released/  s*   z1TestBufferProtocol.test_memoryview_check_releasedc                    sR  d t  dgdd}t|}| || | | |  t  gdgdd}t|}| || | | |  t  fddtd	D g d
dd}t|}| || | | |  t  fddtdD g ddd}t|}| || | | |  trG dd dtj}|dd}t|}| | t| d S d S )Nrx  r   r~  r/  r+   z>hQiLlc                       g | ]} qS r<   r<   r  rw   r<   r=   r~   f  r*  z>TestBufferProtocol.test_memoryview_tobytes.<locals>.<listcomp>ri  r  z=hQiLlc                    r  r<   r<   r  r  r<   r=   r~   k  r*  r  )r   r    r    r!  r    z<hQiLlc                   @   rl  )z;TestBufferProtocol.test_memoryview_tobytes.<locals>.BEPointrY   rT  Nrm  r<   r<   r<   r=   r-  t  r.  r-  r   r  )r   rN   r<  r   rc   r)  r0  rS   )r%  r  rE   r-  r2  rG   r<   r  r=   test_memoryview_tobytesW  s2   $
z*TestBufferProtocol.test_memoryview_tobytesc                 C   s  |  tti td |  ttdtd tg ddgdgd}|  tt|td tdd	d
d}dD ]}t|t|}| || | |d	 d q/tdd	d
td}dD ]}t|t|}| || | |d	 d qPdD ].}d|d	< t|t|}| || | |d	 d d|d	< | |d	 d | |d	 d qitdgdgd
td}dD ]}t|t|}|  t	|j
d | || | | g  qtttdg dd
td}dD ]}t|t|}| t| g g g qtdgdgdtd}dD ]}t|t|}| || | | |  qtg ddgdtd}dD ]}t|t|}| || | | |  qtg ddgdgtd}dD ]2}t|t|}| || | | |  |  t|jdd | |d d | |d d qB|d d d }dD ]2}t|t|}| || | | |  |  t|jdd | |d d | |d d q~tttdddgtd}dD ]}t|t|}| t| |  q|  tt|td t|t|}| t| |  tttdddgttB d}dD ]}t|t|}| t| |  q|  tt|td t|t|}| t| |  tttdddgttB d}dD ]}|  tt|t| t|t|}| t| |  qDtg ddgdgd}t|td}| |j d S )Nr,  rY  r   r  r    r   r-  rk  r<   r   r/  r+  r.  rg   r+   r   r4   r  r   r!  r   r  r  r   ri  r  ro  )r   r-  )r,  r-  r  )rf  r  rK  rL  rF  ZPyBUF_WRITEr   r<  r   r  r  r  r   rc   r  rJ  rx  rM  r]  )r%  r  r:  rE   r<   r<   r=   test_memoryview_get_contiguousz  s   z1TestBufferProtocol.test_memoryview_get_contiguousc                 C   s   t d}tdg d}t|}t|}td| }|| | |	 | t d}t
ttdg ddd}t|}t|}td| }|| | |	 | d S )Nr   r  r   r   ri  r  r/  )r6   ry  rL   rN   ioBytesIOr   readintor<  r   r   r   rc   )r%  r1  rG   rE   r   r   r  r<   r<   r=   test_memoryview_serializing  s   





z.TestBufferProtocol.test_memoryview_serializingc           	      C   s  t ttd}t|}| t|t| |jdddgd}| t|t| |d d d }t ttdd d d }| t|t| tttdg dtd	}t|}| t|t| tttdg dd}|d d d
d d d d df }t|}| t|t| tttdg dt	d	}|d d d
d d d d df }t|}| t|t| tttddgdd}t|}tttddgdd}t|}| || | t|t| tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j tttdg ddd}t|}| 
t|j tttddg ddd}t|}| 
t|j d S )Nri  r   r!  r  r  rm  r  r  ro  r    r   )r    r   r!  r   r/  r   r  r   r  r  r   z= Lz< h)rS   r   rc   rN   r<  r  r(  r   rJ  rx  rf  r   __hash__)	r%  r   rE   ZmcZmxr  rY   rG   rT  r<   r<   r=   test_memoryview_hash  sJ   z'TestBufferProtocol.test_memoryview_hashc                 C   s  t g d}t|}t|}| t|j ~|  t g d}t|}t|ttd}t|ttd}| |j	| | t|j ~~|  t g d}t|}t|}t|}|  | t|j ~|  t g d}t|}t|}t|ttd}t|ttd}| |j	| |  | t|j ~~|  tg ddgt
d}t|}|jg ddgd t|}t|}| | |  t|}	| |	 |  | |	 |  |  |	  |  | | |  ~|  |  dd	 }
t d
}t|}|
| | |d td W d    n	1 s#w   Y  tttdg ddd}t|ttd}	t|	ttd}| |j	| t|}|
| | |dd  g dg dgg W d    n	1 sqw   Y  dtfD ]}t d
}t|p}~t|t|d}	t|	R}~	t|t|d}t|4}~|
| |
| |
| | |d td | |d td | |d td ~W d    n	1 sw   Y  ~W d    n	1 sw   Y  ~W d    n	1 sw   Y  t d
}t|p}~t|t|d}	t|	S}~	t|t|d}t|6}~|
| |
| |
| | |d td | |d td | |d td ~~~W d    n	1 sZw   Y  W d    n	1 sjw   Y  W d    n	1 szw   Y  qzt d
}| t2 t|}t|}|d tdk W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr  rZ  r   r!  ro  )r  r   r  r5   r4   r   r  c                 S   s.   t | 	}W d    d S 1 sw   Y  d S rt   )rN   )r   r  r<   r<   r=   catch22  s   
"z;TestBufferProtocol.test_memoryview_release.<locals>.catch22r  r   rV  ri  r  r   r/  r+   r  )r!  r  r   rW  r    rX  )r   rN   r   rf  rF  r  rP  ND_REDIRECTr;  rX   r  r  r<  r  r  ordr   rc   )r%  rG   rE   r  ru  rv  r~  r  rY   rT  r  r  r   r%  r@  r<   r<   r=   test_memoryview_releaseI  s   

(


	



 $z*TestBufferProtocol.test_memoryview_releasec                 C   s0  t dd tdD dgdd}tddd tdD }||fD ]u}t |ttd}t |ttd}t|}| |j| | |j| | |j| | || | || | || | |dd	 |dd	  | |dd	 |dd	  | |dd	 |dd	  ~~| |dd	 |dd	  q d S )
Nc                 S      g | ]}d | qS r>  r<   rh  r<   r<   r=   r~     r   z?TestBufferProtocol.test_memoryview_redirect.<locals>.<listcomp>ri  r   r/  c                 S   r  r  r<   rh  r<   r<   r=   r~     r   r  r+   r!  )	r   rc   rL   rP  r  rN   r;  rX   r<  )r%  r  rG   rY   rT  r  rE   r<   r<   r=   test_memoryview_redirect  s$   z+TestBufferProtocol.test_memoryview_redirectc                 C   s  d}g d}|  ttddd t }t|}| j||d|dddgdg|d	 tdD ]
}| || | q*~~t }t|}~~t }t|td	}t|td	}t|}| 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~t }t|ttd
}t|ttd
}t|}| 	|j
| | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|}| j|d d|dddgdg|d	 tdD ]
}| || | q~~tdd}t|}~~tdd}t|td	}t|td	}t|}| 	|j
d  | 	|j
| | j||d|dddgdg|d	 ~~~~tdd}t|ttd
}t|ttd
}t|}| 	|j
d  | 	|j
| | 	|j
| | j||d|dddgdg|d	 ~~~~d S )Nr   )r   r+   r    r!  r  r   r  r5   r4   rk  rg   rl  r+   r    r!  Tri  r  rY  r  )Zlegacy_mode)rf  r  ZstaticarrayrN   rU  rc   r<  r   rP  r;  rX   r  )r%  r9   rd   rY   rT  r   r  rE   r<   r<   r=   $test_memoryview_from_static_exporter  s   









z7TestBufferProtocol.test_memoryview_from_static_exporterc                 C   s*   t g ddgttB d}| tt| d S )Nr  r!  r  )r   rz  ZND_GETBUF_UNDEFINEDrf  rF  rN   r  r<   r<   r=   #test_memoryview_getbuffer_undefined?  s   z6TestBufferProtocol.test_memoryview_getbuffer_undefinedc                 C   s&   t g ddgtd}| tt| d S )Nr  r!  ro  )r   rz  rf  rF  rN   )r%  rY   r<   r<   r=   test_issue_7385E  s   z"TestBufferProtocol.test_issue_7385c                 C   s&   dD ]}|  t|t| qd S )N)r?   iiZ3s)r<  	_testcapiZPyBuffer_SizeFromFormatr6   ry  )r%  r   r<   r<   r=   test_pybuffer_size_from_formatI  s
   z1TestBufferProtocol.test_pybuffer_size_from_formatNF)Ur  r  r  r&  rU  rg  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r(  r3  r:  r=  rA  
contextlibcontextmanagerrF  rT  r^  r`  rf  rq  rw  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  Zcpython_onlyr  r<   r<   r<   r=   r"    s     
K0i 
-'	0.Y #6!)>		% qu8	s<T	
5m8>5?H>Ha (#u< ^r"  __main__)r   NN)r   r2   r<   )r   r2   r7  rt   r  r  )ir  Zunittesttestr   Ztest.supportr   	itertoolsr   r   randomr   r   r   warningsr  rL   r  osZdecimalr	   Z	fractionsr
   Z_testbufferImportErrorr   r6   r)  ZEnvironmentVarGuardenvironcatch_warningsrM   r   r  Z
SHORT_TESTZNATIVEr7   r8   ZSTANDARDr>   rR   r9   copyrz   ZARRAYr  ZBYTEFMTri   rk   rh   rZ   ra   re   rr   rs   ru   rx   ry   r{   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r
  r  r  r   r  r  r   r  r  r  Z
skipUnlessZTestCaser"  r  mainr<   r<   r<   r=   <module>   s    	
	



	



	
?
+

!





                            
o