a ze@sDddlZddlZddlmZddlmZmZddlmZm Z m Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlmZz ddlTWneydZYn0z ddlZWneydZYn0z ddlZWneydZYn0zfeHe_e ddlmZWdn1s&0YWdn1sF0YWneyjdZYn0z ddlZWneydZYn0d Zdddddddddddddddd Z ere d =e d =er z e!d dde d<de d <Wnej"yYn0ddddddddddddddd> dd>fdZ#ddZ$e e e#e#e#e#dZ%erze%dD]Z&e$e&e%de&<qbe 'Z(e 'Z)e D]Z*e*d vre)e*=qe 'Z+e D]Z*e*d!vre+e*=qe(e%d"<e(e%d#<e)e%d$<e+e%d%<e+e%d&<dZ,dZ-gdgd'fd$gd(gfd(gd(gfd#d"gd(gfd&d%gd(gfd)Z.d*d+Z/d,d-Z0d.d/Z1d0d1Z2dd3d4Z3dd5d6Z4dd7d8Z5d9d:Z6d;d<Z7d=d>e%dDZ8d?d@Z9dAdBZ:dCdDZ;dEdFZdKdLZ?dMdNZ@dOdPZAdQdRZBdSdTZCdUdVZDdWdXZEdYdZZFd[d\ZGd]d^ZHd_d`ZIdadbZJdcddZKdedfZLdgdhZMdidjZNddndoZOdpdqZPdrdsZQddtduZRdvdwZSddxdyZTdzd{ZUdd|d}ZVd~dZWddZXddZYdddZZdddZ[dddZ\dddZ]ddZ^dZ_dkZ`dZaerdZ_dZ`dZae[ZXe\ZYe]ZebedebedGdddejcZdeedkr@efdS)N)support) permutationsproduct) randrangesamplechoice)Decimal)Fraction)*)ndarrayT)?cbBhHiIlLnNfdPrrQqrr)i)i)ri))rllr)r r rrrrrrrrrrrrc Cs|dkrd}n|dkrd}n|dkr*d}n|dkrFdd > dd >f}ndd D]8}zt|d|>dWqWqJtjyYqJ0qJ|d@rd|> d|>fn d d|>f}|S) Nr r r rrr&rr*r+) r"@? r)structpackerror)fmtlhexpr;D/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_buffer.pynative_type_range^s  &r=)@<>=!r?Z bBhHiIlLfdZBbcm@mar@b)r>123r>)r arraynumpy memoryviewbytefmtcCsvtt||}|dkr6t|g}|dkr6|dkr6d}|dkrFt|}|dksV|dkrrt||}t||d}|S) Nr rLr rrr)rfmtdictbytesboolr5r6unpack)modecharobjxr;r;r< randrange_fmts  rYcCsJ|d\}}g}|D]}|t|||qt|dkrB|dSt|S)N#r*r)splitappendrYlentuple)r8rWrUcharsrXr r;r;r<gen_items r`cCs<|dkrt||Sdg|}t|D]}t||||<q$|SNr)r`range)rr8rWlstrr;r;r< gen_itemss    rdc Cstt|t}|d}|d}tdd}t|D]H}ttt|}tt|t}||t |rf|nd7}|||7}q4t |||} t ||} || | fS)NrZambr r*) rcapMODEstriprrbr^rQMULTintrdr`) rrWrUZxfmtr8nmemb_rV multiplieritemsitemr;r;r< struct_itemss     rqr cCs|durtt|t}|dur0ttt|}tt|t}|d|t|rR|nd}t|||}t||}| d||}|||fS)NrZr*re) rrgrhr^rQrjrkrdr`ri)rrWrUrVrnr8rorpr;r;r< randitemss  rrccs4t|tD]"}t|D]}t||||Vqq dSN)rgrhrQrr)rrWrUrVr;r;r< iter_modes rtccs2t||D] }|Vq |dkr"dSt||VdS)Nr )rtrq)nitemsZtestobjtr;r;r< iter_formats rwcCsd|vpd|vpd|vS)Nr rrr;)r8r;r;r<is_byte_formatsrxcCs4t|}|dks$|dko2|ddko2||dtvS)Nr*rrr?)r] MEMORYVIEW)r8rXr;r;r<is_memoryview_formatsrzcCsg|]}t|s|qSr;)rx.0r r;r;r< r}cCs t|t Srs isinstancelistrcr;r;r<atompsrcCs t|tSrsrrr;r;r<listpsrcCs6t|dkrdS|d}|ddD] }||9}q$|SNrr*)r])rcrXvr;r;r<prods   rcCs|dkr dS|dkrZt|dd|g}t|dddD]}||||d9<q:n>|gt|dd}td|D]}||||d9<qz|S)Nrr;Cr*r)rrb)ndimshapeitemsizeZlayoutstridesrr;r;r<strides_from_shapesrcCst|r |St|dkr |dSdg|d}|drFt||dnd}t|dD].}||}t|||||dd||<qV|Sr)rr]rb_ca)rosrcstriderstartr;r;r<rs $rcCslt|r |St|dkr |dSdg|d}|d}t|dD]$}t||d||dd||<qB|Sr)rr]rb_fa)rorrcrrr;r;r<r$s "rcCs2t|r(d|vr(t|t|kr(tdt||SNrzprod(shape) != len(items))rrr] ValueErrorrrorr;r;r<carray1s rcCs2t|r(d|vr(t|t|kr(tdt||Sr)rrr]rrrr;r;r<farray6s rcCsdd|D}t|S)NcSsg|] }t|qSr;rbr|rr;r;r<r}=r~zindices..)r)r iterablesr;r;r<indices;srcCs*d}t|D]}|||||7}q |Srar)rindrretrr;r;r<getindex@s rc Cs|s|St|}t||dd}t||ddddd}dgt|}t|D]2}t|||}t||ddd|}||||<qL|S)Nr*rrr)r]rrr) srcrrZsstridesZdstridesdestrfrtor;r;r< transposeGs  rcCs6|gkr |St|r|gSt|dt|ddSrr_flattenrr;r;r<rVs rcCst|r |St|Srsrrr;r;r<flatten^srcCs4t|r gSt||dgt|d|ddSr)rr] slice_shapercslicesr;r;r<rdsrcs&t|r |Sfdd|dDS)Ncsg|]}t|ddqSr*N) multislice)r|Zsublstrr;r<r}or~zmultislice..r)rrr;rr<rksrcsFt|r |Sfddt|d|dD}||d<|S)Nc s.g|]&\}}t||ddddqSr)m_assign)r|rrlslicesrslicesr;r<r}szm_assign..r)rzipllstrlstrrr;rr<rqs  rcCsft||}t||}t|t|kr(dStt|D],}||||krNdS||dkr4dSq4dS)Nrr)rr]rb)rrrrlshapershaperr;r;r< cmp_structures   rcCs(t||||dkrtdt||||S)Nrz+lvalue and rvalue have different structures)rrrrr;r;r<multislice_assignsrcs|r dS|dks ||kr$dStfddDr>dS|dkrZ|dkoX oX SdvrfdStfddt|D}tfddt|D}d||ko|||kS)NFrc3s|]}|VqdSrsr;rrr;r< r~z#verify_structure..Tc3s.|]&}|dkr||dVqdSrr*Nr;r|jrrr;r<rs c3s.|]&}|dkr||dVqdSrr;rrr;r<rs )anysumrb)memlenrrrroffsetiminimaxr;)rrrr<verify_structuresrcCs|D] }||}q|Srsr;)rcrrr;r;r<get_items rc Cs:|\}}}}}}|}t|D]} ||| || 7}q|Srsr) rrvrrrrrrprr;r;r< memory_indexs  rc CsR|\}}}}}}d|>}t|D],}t||} d| >} || @rDdS|| O}q dS)Nr*TF)rr) rvrrrrrrZvisitedrrbitr;r;r<is_overlappings   rr1r;csZst|d}|dkrd|r,|||dddfStdd}||}tddkrP| n|}|||dd|fSd} td} | dkr|rd} n | dkrd} dg|t|D]} t| |d| <qnt}d } td} | dkr| d@rd nd } dg||t| | d|d<| s0|ddkr0||d<t|dd d D]j} | | dr`| dnd9} | r|t| | d| <n"d td|td| d| <q@d}}dvrtfddt|D}tfddt|D}||}|r | |}||d|}n*| ||}tddkrF| |n|}||||fS)Nr*rr;rd_ZrTFrr*rc3s.|]&}|dkr||dVqdSrr;rrr;r<rs z!rand_structure..c3s.|]&}|dkr||dVqdSrr;rrr;r<rs )rrbr]r)rZvalidmaxdimmaxshaperrrurrminshaperrZ maxstrideZ zero_striderrr;rr<rand_structures\       "    rc Csp||}t|d}|r$|||nd}td|d}|||}t|||}t||\}}}} | |krlt|SNr*)rslice slice_indices RuntimeError) slicelenZlistlenZmaxstartrZmaxstepstepstoprrmZcontrolr;r;r<randslice_from_slicelen&s   rcCspdg|}dg|}t|D]B}||}|dkr>td|dnd}t||||<t||||<qt|t|fSr)rbrrr^)rrrrrrrr;r;r<randslice_from_shape3s   rcCstd|d}d}td}|dkr(d}n |dkr4d}tddkrDdnd }dg|}dg|}dg|}dg|} t|D]}t||d} t||d} | | kr| | } } |r t| | d} t| | d} d tdtd| d}t| | |}t|| \}}}}n&| dkr$td| dnd}t|| }t|| }tddkrp| | ||<||<||| |<||<qx| | ||<||<||| |<||<qx||t|t| fS) Nr*rrrrrPTFr)rrbrrrr^)rrrrrZ all_randomrrrrZsmallbigrrrZs_smallrmrZs_bigr;r;r<rand_aligned_slices?s<    rcCs$|\}}}}}}t||d|dS)NrZrL)rd)r8rvrrrmr;r;r<randitems_from_structurejsrc Cs(|\}}}}}} t||||| t|BdS)Nrrformatrflags)r ND_WRITABLE) ror8rvrrrrrrrr;r;r<ndarray_from_structureps rc CsP|\}}}}}}t|} t|D]\} } t|| | || q t| ||||dS)N)bufferrrdtyper) bytearray enumerater5 pack_into numpy_array) ror8rvrrrrrrbufrrr;r;r<numpy_array_from_structurevsrc s|j}|r$t||krld|fSnH|gkrH|jdks>|krld|fSn$t|\}}|g}|dkrld|fS|fddtdtD}g}|D]2} t|| d} | | krd|fS| | q|gkr||fS|d|fS)Nrcsg|]}||qSr;r;)r|rrZmemr;r<r}r~zcast_items..nan) nbytesrrdivmodtobytesrbr]r5rTr\) Zexporterr8rrZbytelenrrZ byteitemsrorrpr;rr< cast_itemss(     rc#stdD] }|gVqtdd}tddkr0dndfdd t|DVtdd}tddkrfdndfd d t|DVdS) Nr/rrr*rcsg|]}tdqSrrr|rmrr;r<r}r~z!gencastshapes..csg|]}tdqSrrrrr;r<r}r~)rbr)rrr;rr< gencastshapess    rcCs0tt| |dt| |dt| |dSr)rrb)rr;r;r< genslicessrcsfddt|D}t|S)Ncsg|]}t|qSr;)rr|rrr;r<r}r~z"genslices_ndim..)rbr)rrrr;rr<genslices_ndimsrFcCs,|s |dkrdnd}t||d}t||Sr)rr)r allow_emptyZminlenrr;r;r<rslicesrccstdD]}t||VqdS)Nr)rbr)rrrmr;r;r<rs rc#spt|D] }tfddt|DVqt|D] }tfddt|DVq2tddt|DVdS)Nc3s|]}t|VqdSrsrrrr;r<rr~zrslices_ndim..c3s|]}t|ddVqdS)T)rNrrrr;r<rr~css|]}tdddVqdSr)rrr;r;r<rr~)rbr^)rrZ iterationsrmr;rr< rslices_ndims   rccs0t|}|durt|n|}tt||VdSrs)r^r]r)iterablerpoolr;r;r< rpermutationsrc Csz |}Wnttfy*|}Yn0t|trD|j}|j}nd}d}td||j |j |j ||j |j |ftjdS)Nunknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s))tolist TypeErrorNotImplementedErrorrrr rrprintrr suboffsetsrrsysstdoutflush)ndrXrrr;r;r< ndarray_prints   rrrfrz%struct module required for this test.z%ndarray object required for this testc@seZdZddZdddddZdddZd d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Zd=d>Z d?d@Z!dAdBZ"dCdDZ#dEdFZ$dGdHZ%dIdJZ&dKdLZ'dMdNZ(dOdPZ)e*j+Z+dQdRZ,dSdTZ-dUdVZ.dWdXZ/dYdZZ0d[d\Z1d]d^Z2d_d`Z3dadbZ4e5j6dcddZ7dedfZ8dgdhZ9didjZ:dkdlZ;dmdnZdsdtZ?dudvZ@dwdxZAdydzZBd{d|ZCd}d~ZDddZEddZFddZGddZHddZIddZJddZKddZLddZMddZNddZOddZPddZQddZRe*jSddZTdS)TestBufferProtocolcCs t|_dSrs)Zget_sizeof_void_p sizeof_void_pselfr;r;r<setUpszTestBufferProtocol.setUpF)slicedcastc % s< rt ns tnd jrΈdd} tdD]:} | dkr\q | dkrH| |  | d7} qH| gddtdD j} ddkr| }|gt dd j| j  j  j j | j jt  r@ sR jt  jt ttsztrrn} |sdS| st}d}t D]}zNt|}t|}t|trtjg|R}n t|}||Wn ty0d}YqBYn0 ||q|s |t}d dd |D} |!|rnd }t"}d D]}}|d krt#d rt#drt$| }t| |t%d}n0t#d rt#d r|dkrt| |d}t&t'| |(t)|dkrPqt|}|rbdnt*}t+dt, t|fddt|D}t|ddkrdd|D}t| |d}  t| tt}! |!tjdd}! |!tjdd}! |!t+d t, t|fddt|D}t|ddkrdd|D}t| |t%Bd}  t| ttjd d}! |!t+d t, t|fddt|D}t|ddkr,dd|D}t#d r.Tr>css|]}d|VqdS)z%02xNr;r{r;r;r<rTr~z,TestBufferProtocol.verify..rrFArrrrrrrrcsg|]}t|qSr;r5 unpack_fromrcontigr8rr;r<r}scSsg|] }|dqSrr;rr;r;r<r}r~rrrordercsg|]}t|qSr;r!rr#r;r<r}scSsg|] }|dqSr%r;rr;r;r<r}r~csg|]}t|qSr;r!rr#r;r<r}scSsg|] }|dqSr%r;rr;r;r<r}r~cs|j|j|j|j|j||j|jt  rn s|j t |j t dkrdnt }t ||rˆ n}||dSr)assertIsrW assertEqualrrrreadonlyrrr^rrr]r r)rDexpected_readonlyrrep) ex expected_lenr8rrcrresultrrrrrr;r<check_memoryviews z3TestBufferProtocol.verify..check_memoryview)r,).rr]rZ assertGreaterrbrrr)rWr*rrrr+rrr^rrr rzr rrr get_pointerrr5r6extend BufferErrorrMjoinhexr is_contiguousr ND_FORTRANget_contiguous PyBUF_READ assertTrue cmp_contigrpy_buffer_to_contiguous PyBUF_FULL_RO toreadonly)%rr0rWrr8r+rrrrcrrZ suboffset0rZstride0r-rZbuf_errrZitem1Zitem2rXrDrZffZ flattenedr(expectedZtransrlroZinitlstyZ contig_bytesrr1Zmmr;) r$r.r/r8rrcrr0rrrrrr<verifys                              & ,zTestBufferProtocol.verifyc Csldd}|jr||ts|||tr(|jr|||tr8|jr|||trH|jr|||tsX|j s|||t sh|jr|||t s||t r|j tt||ddSt|tst|jr|}nt|td}|}||trdn|j}|j} |j} |j} ||t sd} |}||t sd} ||t r$|jnd} ||t r:|jnd} t||d}|j||| | || | | ||d dS) NcSs ||@|kSrsr;)reqflagr;r;r<matchsz/TestBufferProtocol.verify_getbuf..matchgetbufFr>r*r;) rWrr8r+rrrrcr)r+PyBUF_WRITABLEPyBUF_C_CONTIGUOUS c_contiguousPyBUF_F_CONTIGUOUS f_contiguousPyBUF_ANY_CONTIGUOUS contiguousPyBUF_INDIRECTr PyBUF_STRIDESPyBUF_ND PyBUF_FORMAT assertRaisesr4r rrzrr r>rrrrrrC)rZorig_exr.rDrrFrcrrAr8rrrrr;r;r< verify_getbufsh          z TestBufferProtocol.verify_getbufcstttttttttt t t t t tf}ddtdDdfgddfgddfddtdDd ff}ggd fgd gd fdggd fdgd gd fdgdgd fdgdgd fddggd fddgdd gd fddgddgdfddgdd gdff }d ttttBtttBf}d ttttBf}|D]\}}t||D]x\}} } fdd| D} | 9} |D]J} | rl| t@rlqR|s| t@rqR|r|n|d } t| || || | d} |r| dddnd}t| }|rt|}| jd ks| jdkr|r| r||| |r.|jdkr.|r.| r.||||D]h}|D]\}|| | ||B|| |||B|r:|j||||Bdd|j||||Bddq:q2qRq(qgd}t|dgtd}|tt|tdgdg}t|td}|jtt|td|jtt|td|jtt|td|jtt|td|jtt|td|jtt|tdt|td}gdgdfD]}d tfD]p}t|||tBd}|t |d|t |d |D]2} t|| d}|t |d|t |d qq|qpdS)!NcSsg|]}|drdndqS)rTFr;r|rXr;r;r<r} r~z:TestBufferProtocol.test_ndarray_getbuf.. r r*rrrrrr4r3 rf rWrrcSs$g|]}|drd|nd|qS)rr%r$r;rVr;r;r<r} r~rr)r*rr*rrZrrrrr*r3csg|] }|qSr;r;rrr;r<r}(r~)rrrrrT)rrrrYrG)r*rWr*)r4rr4rr)!rPrQrR PyBUF_SIMPLErJrLrN PyBUF_FULLr> PyBUF_RECORDSPyBUF_RECORDS_RO PyBUF_STRIDEDPyBUF_STRIDED_RO PyBUF_CONTIGPyBUF_CONTIG_ROrbrr8ND_PILrIrSr5calcsizer rMrr*rUND_GETBUF_FAILrTr4r;r7)rrequestsZ items_fmtZ structureZndflagsZ real_flagsror8rrrrZ_itemsex1ex2m1m2rDbitsr.baserr(r;rr<test_ndarray_getbufs            $      z&TestBufferProtocol.test_ndarray_getbufc Cstdgdg}tdgdgtd}t|j|jfD]H}|t|hd|t|gd|t|gd|jt|gddhd |jt|gddgdhd |jt|gddggd |jt|dgdgid |jt|dgdgid |jt|dgdgid|jt|dgdgdgtd|jt|dggtd |jt|gdgd |jt|dgdgdd |jtj |dgdgdd |jt|ddgdgdd |jt|gddgdd t d}|jt|dg|dg|d |jt|dgdgd |jt|gddgd |jt |dgddgd |jt|gdddgdd |jt|gddgdgd |jt |dgdgddgd |jt|ddgddgdgd |jt|gddgdgdd|jt|gddgdd |jt|gddgddd|jt|gddgd d |jtj |dgdgd!d gd"}|jt||ddgd#d$gd%d&|jt|t d'd(|jt|dgdgt d|t|dgq.|jttd)td*tdgdgtd}|jt|jdgdgtdtd)}|t|jdgdg|t|jtdgdg}|dgdgt|}|t|jdgdg|t|j|||t|j~|ttigd|ttd)itttd+dgd+d }|tt|d%gtttd,ddgd }|tt|gd-|tt|ddg|tt|d#dg|t t|d.dgtgddgdd }t|td*}|t|jtgddgdd }t|} t| }|t|jtd/gdgd0d }|t|jt }ttt|dg|d }|t|jtdgdgd }|ttddddd%|tt|d1d2|t t|d.d2|tt|td3|tt|td4|tt|td5|tt|d6d7tdgdgd }|ttddddd%|tti||tt|itdgdgd }|ttddddd%|ttid7|tt|d8dS)9NrYr*r>r*rr)r*rrJ)r*rrrr*rrrrrrrr r])rrHrrrZXXXrirrrrZQLrrJr"r*rrrrrHr*rrrrrrr)rrrr>z@#$) r*rrrrrr4r3rYrfr[r)rrrrr123rGrrW)rrrr))r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*r*ZLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLxyzru Zr)r ND_VAREXPORTpushrTr r8rfrr5r7Z ND_MAX_NDIM OverflowErrorrr_r4rIpoprMreleaser2rrbr^r memoryview_from_bufferr9r:UnicodeEncodeErrorr<r7) rrZndmr rrorDr.rjrkr;r;r<test_ndarray_exceptionsfs  z*TestBufferProtocol.test_ndarray_exceptionscCsttdD]}dgd}tgddgtd}t||d<tddD]$}|jgddgdt|||<qDtdD]}|||qr|t|j ~q dS)Nrrrsrr]r*r) rrbr rrMrrrTr4r)rpermrDrrr;r;r<test_ndarray_linked_lists   z+TestBufferProtocol.test_ndarray_linked_listc CsLtdD]>\}}}t|}t|d|d}|j|d||dddd|d qdS)Nrr;r TrWrr8r+rrrrc)rwr5rgr rC)rr8Zscalarrmrrr;r;r<test_ndarray_format_scalars z-TestBufferProtocol.test_ndarray_format_scalarc Csltdd}t|D]T\}}}t|}dtfD]6}t||g||d}|j|d||dd|f|f|d q.qdS)Nr*rfrrTr)rrwr5rgrfr rC)rrur8rormrrrr;r;r<test_ndarray_format_shapes    z,TestBufferProtocol.test_ndarray_format_shapec Cstdd}t|D]\}}}t|}tddD]}|dkr>q0t|dd|g}||g}|dkrp||dnd} dtfD]>} t||||| | d} |j| d||dd|||dd|d q|q0qdS) Nr*rrrTr) rrwr5rgrbr]rfr rC) rrur8rormrrrrrrrr;r;r<test_ndarray_format_strides*s$     z.TestBufferProtocol.test_ndarray_format_stridescCs@gd}t|ddd}t|ttBd}||t|ddS)NrXrr)r*rrrG)r rLrSr*r r)rror.rr;r;r<test_ndarray_fortran?sz'TestBufferProtocol.test_ndarray_fortrancCs^tdD]N}ddt|D}t|}t|D]$}t|\}}}t|}dtfD]} |dkrl| tkrlqVt|||| d} t|||d} t ||} |j | d||d||| | d t |r t|||d } t| t t Bd } || jd k| }|j |d||d||| | d t|||| tBd} t|||d } t||} |j | d||d||| | d qVq0qdS) NrcSsg|]}tddqS)rrfrrr;r;r<r}Gr~z.rrrTrr rGr;r)rbrrrrr5rgrfr rrrCrzrerSr;rrr8r)rrshape_trurr8rormrrrrrcr.mvr;r;r<test_ndarray_multidimEsJ     z(TestBufferProtocol.test_ndarray_multidimcCstdgdgd}|t|jddt|}||||t|jddtdgdgtd}|t|jdt|}||||t|jdtdgdgtd}|t|j d|t|jddt|}||||t |j d|t |jddgd}t|t |gdtd}|t j |jd d |t|jdd t|}||||t|jd d |t|jdd gd }t|t |gd td}|t|jd d |t j |jdddS)Nr*rr3r]r)r*rrrrrr4r3rrri,)r)r*rrr)rrZLQ)s1r)r rTr  __setitem__rMr*r __delitem__r __getitem__ IndexErrorr]r5r7r)rrrror;r;r<test_ndarray_index_invalidrs:    z-TestBufferProtocol.test_ndarray_index_invalidcCsJtddtd}t|}||||d}||d|d}||||d}||d|d}||||t|jd|t|jd|t|jdd|t|jdd||d||dd|d<||dd|d<||dd|d<||dd|d<||ddS) Nr*r;r].rr3rYr) r rrMr*r rTr rr)rrrrXr;r;r<test_ndarray_index_scalars" z,TestBufferProtocol.test_ndarray_index_scalarcCsRtttdddgtd}t|td}|t|jd|t|jtddddS) Nr3rrr]rGr*rr) r rrbrrdrTr4rr)rr.rr;r;r<test_ndarray_index_null_stridess z2TestBufferProtocol.test_ndarray_index_null_stridescCstdD]\}}}t|dg|d}tddD]}|||||q,|t|jd|t|jdt|rt|}|||tddD]}|||||q|t|jd|t|jdqtdD]\}}}t|dgt |d}t|t t Bd}tddD]}|||||qt|r| }| ||ttddD]}|||||qNqgd}t|dgd}t|td}tddD]}|||||qgd}t|dgd}t|td}tddD]}|||||qdS) Nrr rr&rGrwr)rwr rbr*rTrrrzrMrrdrSrr)__eq__NotImplementedrer^)rr8rormrrrr.r;r;r<!test_ndarray_index_getitem_singlesB   z4TestBufferProtocol.test_ndarray_index_getitem_singlecCstdD]\}}}t|dg|td}tdD]}|||<|||<q,||||t|jd||t|jd|t |sqt|dg|td}t |}|||tdD]}|||<|||<q||||t|jd||t|jd|qtdD]\}}}t|dg|td}tddD]&}||d||<||d||<q(|||t |snqt|dg|td}t |}|||tddD]&}||d||<||d||<q|||qdS)Nrrrrrr*) rwr rrbr*r rTrrrzrM)rr8roZ single_itemrrrr;r;r<!test_ndarray_index_setitem_singlesD       z4TestBufferProtocol.test_ndarray_index_setitem_singlec Csd}t|}t|D]}t|\}}}dtfD]}t||||d}t||} t|d |dD]} || | || t|d |dD]b} || | | || | t|d |dD](} || | | | || | | qqqdt||||t Bd}t ||} t|d |dD]} || | || t|d |dD]d} || | | || | t|d|dD]*} || | | | || | | qq\q,q0qdS)Nrrrrrr*r) rrrrrfr rrbr*r r8r) rrrurr8rormrrrcrrkr;r;r<#test_ndarray_index_getitem_multidims.  *  z6TestBufferProtocol.test_ndarray_index_getitem_multidimcCstddd}|ttdtt|}||||ttdttdD]\}}}t|dg|d}t|D]&\}}||||| ||vqnt |rLt|}t|D]&\}}||||| ||vqqLdS)Nr*r;rz1 in ndz1 in mvrr ) r rTr evallocalsrMr*rwrr;rz)rrrr8rormrrr;r;r<test_ndarray_sequence9s  z(TestBufferProtocol.test_ndarray_sequencec Csgd}t|dgtd}t|}|t|jtddd||t|jtddd|t|dgtd}t|dgtd}t|td}|t|jtddd|t|dgdtd}t|}|t |j tddd|t |j tdddt|d d gdtd}t|}|t |j tdddtdddf|t |j tdddtdddf|t|j d |t|j d tdddf|t|j tdddif|t |j tdddtdddf|t|j d t|dgd td}t|dgd d}t|}t|}|t |jtddd|dd| | ||t |jtddd|dd| | |t|dgd td}t|dgdd} t|}t|}|t |jtddd|dd| | ||t |jtddd|dd| | |t|d d gd td}t|dgd d}t|}t|}|t |jtddd|dd| | gdgdg|t |jtddd|ddt|dgd td}t|dgd d}t|}t|}|t |jtdd d|dd| | ||t |jtdd d|dd| | ||tttddd i|ttdd|t ttdddd t|dgd td} |t| jt|dgd d}t|td} |t| jdS)Nrr3r]rr*rGrrrrz@%$rrr r4rx)rrr4r3z ###########)r rrMrTr rrrRr4rrr r*r rrfadd_suboffsetsr^) rroxlmlr.xrrrmryrrXr;r;r<test_ndarray_slice_invalidLs    """"" "" z-TestBufferProtocol.test_ndarray_slice_invalidcCsgd}t|dgdtd}t|dgdd}|dd|dd<|||t|}t|}|||||||dd|dd<|||t|ddgdtd}t|ddgdd}|d d ddf|d d d d f<||t|ddgdS) NrXrWrrr rYrrr*r)r rr*r rMr)rrorXrBrrr;r;r<test_ndarray_slice_zero_shapes  $z0TestBufferProtocol.test_ndarray_slice_zero_shapec Csd}t|}t|}t|D]}t|\}}}t|}dtfD]} t|||| d} t||} t ||D]} d} zt | | }Wn*t y}z|j } WYd}~n d}~00d}z | | }Wn*t y}z|j }WYd}~n d}~00|s| r| || qh|||qhq@qdSNrrr)r]rrrrr5rgrfr rrr Exception __class__r)r*r )rrrrurr8rormrrrrcrlisterrrenderrndslicedr;r;r<test_ndarray_slice_multidims.     z.TestBufferProtocol.test_ndarray_slice_multidimc Cs2d}t|}t|}t|D]}t|\}}}t|}t|||d} | t|||d} | t| } t ||} t ||D]} d}zt | | }Wn*t y}z|j }WYd}~n d}~00d}z | | }Wn*t y}z|j }WYd}~n d}~00|s |r|||q|||qqdS)N)rrrrr )r]rrrrr5rgr rrMrrrrrr)r*r )rrrrurr8rormrrr.rrcrrrrrrr;r;r<'test_ndarray_slice_redundant_suboffsetss4    z:TestBufferProtocol.test_ndarray_slice_redundant_suboffsetscCstdD]\}}}tdD]}tdD]}dtfD]}|tB}t|dg||d}t|dg||d} t| } d} d} |dd} z0| |}| |}| || |<t|t|k} Wn*ty}z|j} WYd}~n d}~00d}z||||<Wn,ty"}z|j}WYd}~n d}~00| r8| |t n| | | | || t |s`q8d}z| || |<Wn,ty}z|j}WYd}~n d}~00| r| |t q8| | | | | || || |j| | |j|d|j|j|j| d q8q*qqdS)NrrrFr)rwrrfrr rMr]rrr)rr*r rzrCrrrr)rr8rormlslicerrrrr.rZlsterrZdiff_structurercZlvalZrvalrrZmverrr;r;r< test_ndarray_slice_assign_singlesX       z3TestBufferProtocol.test_ndarray_slice_assign_singlec Cs*d}t|}t|}t|D]}t|\}}}dtfD]}ttD]}t||\} } t||||t Bd} t ||} d} zt | | | | }Wn*t y}z|j } WYd}~n d}~00d}z| | | | <Wn*t y}z|j }WYd}~n d}~00|s| r||| qD|| |qDq8qdSr)r]rrrrrfrb ITERATIONSrr rrrrrr)r*r )rrrrurr8rormrrrrrcrr0rrr;r;r<"test_ndarray_slice_assign_multidim2s2    z5TestBufferProtocol.test_ndarray_slice_assign_multidimc Cs8ttD](}tdD]}t|}t|dttd}|t |t ||}t |||}| }t |}t|r| } || ||ddkrt |||td} | } ||| t | }t|r||| | } || | tr|d} d| vrqt|||} |j|d| j|d| j| j| j| d qqdS) Nr?TrrrrrqrFr)rbrrQr5rgrMAXDIMMAXSHAPEr;rrrr rMrzr*rfrrrCrrrr)rrmr8rrvrorXZxlistrZmvlistrBZylistrzr;r;r<test_ndarray_randomSsB          z&TestBufferProtocol.test_ndarray_randomc CsttD]}tdD]}t|}t|dttd}|t |t ||}d}zt |||}Wn*t y}z|j }WYd}~n d}~00||trd} zt|||} Wqt y}z|j } WYd}~qd}~00qqdS)Nr?Fr)rbrrQr5rgrrr assertFalserrrrrr;rr) rrmr8rrvrorrXrZ numpy_errrBr;r;r<test_ndarray_random_invalid|s*     z.TestBufferProtocol.test_ndarray_random_invalidc Cs2ttD]"}tdD]}t|}tttd\}}}}t|d|d}t|d|d} | t || t | t ||} t || } t | ||} t | || } | || |<| }| }t| }t| }|| ||| ||ddkr| ddkrt | ||td}t | || td}||||<| }| }||||||t|}t|}|| ||| |trd|vsd|vrqt| ||}t| || }||||<t|st| s|j| d|j|d|j|j|j| d |j| d|j|d|j|j|j| d qqdS) Nr?rTrrrrqFr)rbrrQr5rgrrrrr;rrrr rMr*rfrrrrCrrrr)rrmr8rrrrrtltrZlitemsZritemsrrZxllistZxrlistrrZylrZyllistZyrlistZzlZzrr;r;r< test_ndarray_random_slice_assignsb               z3TestBufferProtocol.test_ndarray_random_slice_assigncCsvgd}t|ddgtd}t|}||jt@||j|||jd||j ||j ||j dS)NrXrrr])rr) r rfr;rr)rWr*rrrKrMrO)rrorr.r;r;r<test_ndarray_re_exports  z)TestBufferProtocol.test_ndarray_re_exportc CsdtfD]}tgddg|d}t|}|||||g||gtgdgd|d}||gtgdgd|d}||ggggtgdgd|d}||gggggggggggggqdS)Nrrsr]rrrrrr)rrrrfr rMr*r rrrrr;r;r<test_ndarray_zero_shapes   z*TestBufferProtocol.test_ndarray_zero_shapecCsddtfD]V}tdgdgdg|d}t|}|||||gd||gdqdS)Nrr*rru)r*r*r*r*r*rrr;r;r<test_ndarray_zero_stridess   z,TestBufferProtocol.test_ndarray_zero_stridescCs>tttddgdd}||jd||gddS)Nrr4rt)r4r3rY)r rrbr*rr rrr;r;r<test_ndarray_offsetsz&TestBufferProtocol.test_ndarray_offsetcCs>dtfD]0}tttddg|d}|}|||qdSNrrr])rfr rrbrr*)rrrrDr;r;r<#test_ndarray_memoryview_from_buffers z6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsPdtfD]B}tttddg|d}tdD]}|||t||gq,qdSr)rfr rrbr*r2)rrrrr;r;r<test_ndarray_get_pointer s  z+TestBufferProtocol.test_ndarray_get_pointercCs\tttdgdd}t|ttBd}|||t|}|||dS)Nr)rrrrrG)r rrbrRrSr*r rM)rr.rrDr;r;r< test_ndarray_tolist_null_stridess z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs|tddtttdddgd}tttdddgd}|t||tdgdgdd }|t|d |td |dS) Nr|s456rWrrrr*rr rP)rr<r rrbr;)rrXrBr;r;r<test_ndarray_cmp_contigsz*TestBufferProtocol.test_ndarray_cmp_contigcCstdgd}t|}|tt|tttd}tttddgd}|t|t|tttdddgd}|t|t|tttdgdd}|t|t|tt ttdddgd}tttdddgt d}|t|t|tt ttdgd d}tttdgd t d}|t|t|tttd}tttdgd t d}|t|t|tttdgd dd }|t|t| dS) NrrsrWrrr)rrrr]rrrrrrr ) rKr rTrhashrRrrbr*rr8rfr)rrFrrr;r;r<test_ndarray_hash%s*z$TestBufferProtocol.test_ndarray_hashc CsTtttttttttt t t f }| t tidttdddtd}dD]*}|D] }t|||}|||qJqBtdgdgdtd}dD]&}|D]}t|||}||d qqtttd gd dtd}dD]&}|D]}t|||}||d qqdtfD]}tdgdgd |tBd}|}dD]*}|D]}t|||}|||q.q&tgd dgd|tBd}|}dD]*}|D]}t|||}|||qqxqtgd dgdgtd}|}dD].}ttfD]}t|||}|||q֐q|ddd}|}dD]J}|D]>}zt|||}WntyJYqYn0|||qqttd}dtfD]}t|ddg|tBd}trtt|ddgd|dkrdndd} |tkrtt|ddgddgtd} | } n|} |D]}zt|d|}Wnty&YqYn0||| tdd|Dddgtd} |t| t|tr||| jddq|dkrtt|ddgddgtd} nt|ddgtd} | } tttttfD]}zt|d|}WntyYqYn0||| tdd|DddgttBd} |t| t|tr||| jddq|tkrt|ddgtd} | } n|} tttttfD]}zt|d|}WntyYqYn0||| tdd|Dddg|tBd} |t| t|tr||| jddqqttttdddgttBd}t|dt}|||tdd|Dddgtd} |t| t|t|dt}tt|ddgddgtd} ||| td d|DddgttBd} |t| t|t|dt}|||td!d|Dddgtd} |t| t|dS)"NrrYr;rrrr*rr~r3rrr4rrsrrrrurrWrr]rr)rrrr(cSsg|]}|qSr;r;rr;r;r<r}r~zCTestBufferProtocol.test_py_buffer_to_contiguous..r'cSsg|]}|qSr;r;rr;r;r<r} r~rcSsg|]}|qSr;r;rr;r;r<r} r~cSsg|]}|qSr;r;rr;r;r<r}* r~cSsg|]}|qSr;r;rr;r;r<r}1 r~cSsg|]}|qSr;r;rr;r;r<r}7 r~)rPrQrRr^r_r>r`rarbrcrdrerTr r=r rr*rrrbr8r4rrrrMZtostringrf) rrirr(ZrequestrrZndbytesrcZnarXr@rBr;r;r<test_py_buffer_to_contiguousJs          B                  z/TestBufferProtocol.test_py_buffer_to_contiguousc Cs|dgfgddgfttdgdfg}|D]H\}}t||d}t|}||j||jt|}t||dd}t ||}|j ||dd d ||||d t|} |j | |dd d ||||d t|t t Bd } | | jd | }|j |ddd d ||||d t|td } | | jd| | jd | | jd | }|dkrR|gn|}|j |ddd d d|jgd|d q,|D]\}}t||td}t|}||j||jt|}t||dd}t||}|j ||dd d ||||d t|} |j | |dd d ||||d q||ddD]v\}}t||td}t|}t|}t ||}|j ||dd d |||j|d t|} |j | |dd d |||j|d q"|ttdd|ttitgddgd}t|td } |tt| t|t t Bd } |tt| tdgddgddd} |tt| |t| j|tt| td|tt| td|tt| dddtddS)NrYrsrrrrr*rrTrrGr;r>rr*r]r9rXr"rr r)rrbr rMr;rKrOr]rrrCrerSr*rrr^rrrr8rMrrfrTr r4rr9r:) rZ items_shaperorr.rDrrrcrmrr;r;r<test_memoryview_construction: s(              z/TestBufferProtocol.test_memoryview_constructioncCsgd}gdgdgdfD]4}t||d}||jt|}|t|jdqtddD]<\}}}td}||}|| d|| gq^dS) Nrsrrrr r*rMr~) r r;rKrMrTr rrwr*rr )rrorr.msrcr8rmrDr;r;r<test_memoryview_cast_zero_shape s   z2TestBufferProtocol.test_memoryview_cast_zero_shapecCs|j}tj}d}d}ttd}|td||d|t|ddgdd }|t|||d|t|gd dd }|t|||d |dS) NzPnin 2P2n2i5P PZ3nr3r~r*rrrr )rrrr) check_sizeofrZ calcvobjsizerrbrMr )rcheckZvsizeZ base_structZper_dimrorFr;r;r<test_memoryview_sizeof s z)TestBufferProtocol.test_memoryview_sizeofcCsGdddt}Gdddt}dd}|d|ddtd d td d gd hdddidddddtddtdtddddd|ddg}tddD]6\}}}t|dg|td}t|dg|td} t|} t || d||| d<| | d| dt |} d|vr q|D]} d} zt || | | Wnt j yHt j } Yn0d}z | | d <Wn0ttfy}z|j}WYd}~n d}~00| s|r|| d||dn| | d | d qqdS)!Nc@seZdZddZddZdS)z=TestBufferProtocol.test_memoryview_struct_module..INTcSs ||_dSrsvalrrr;r;r<__init__ szFTestBufferProtocol.test_memoryview_struct_module..INT.__init__cSs|jSrsrrr;r;r<__int__ szETestBufferProtocol.test_memoryview_struct_module..INT.__int__N)__name__ __module__ __qualname__rrr;r;r;r<INT src@seZdZddZddZdS)z=TestBufferProtocol.test_memoryview_struct_module..IDXcSs ||_dSrsrrr;r;r<r szFTestBufferProtocol.test_memoryview_struct_module..IDX.__init__cSs|jSrsrrr;r;r< __index__ szGTestBufferProtocol.test_memoryview_struct_module..IDX.__index__N)rrrrrr;r;r;r<IDX srcSsdS)Nr4r;r;r;r;r<r r~z;TestBufferProtocol.test_memoryview_struct_module..frYy@@z-21.1gffffff(@rrrs>rrrr4r3r;rYTFasabcrFabccSs|Srsr;)rXr;r;r< r~zBTestBufferProtocol.test_memoryview_struct_module..rfrMrrrr*)objectrr Ellipsisrrwr rrMr5rr*rgr7r rrZ assertIsNot)rrrrvaluesr8rorpr.rrDrrZ struct_errZmv_errrr;r;r<test_memoryview_struct_module sH       z0TestBufferProtocol.test_memoryview_struct_modulecCs>tgddgdgd}||jt|}|t|jddS)Nrsrrrr )r rrKrMrTr r)rr.rr;r;r<!test_memoryview_cast_zero_strides s z4TestBufferProtocol.test_memoryview_cast_zero_stridesc Cs0tD]}tdrd|n|}t|}tD]^}tdr>d|n|}t|}tttdd|g|d}t|}|t |j |d|gq*qt dD]\}} } t| dg|d}t|}t dD]T\}} } t |s|t |j |d|gqt|st|s|t |j |d|gqqtd} td} tttd| dd| gdd}t|}|jt |j dd| gddtttd gd d }t|} |t | j |t | j ddd |t | j i|t | j d |t | j d|t | j d|t | j d|t | j d|t | j d|t | j d|t | j d|t | j dtddgdgdd}t|} |t| jd|t| jdd|t| jtttd gd d }t|} |jt | j did tttd d gd }t|} |jt| j ddgd |jt | j ddgd |jt | j dgdd |jt | j dgdd |jt | j dgd d ttd!d"td#Dgd$d }t|} |jt | j d%gd&d tttd'd'gd%d}t|} |t | j d%dgd'ttd(d"td#Dd#gd }t|} |jt | j d%gd&d ttd)d"td#Dd#gd }t|} |jt | j dgd&d tttd'd'gd%d}t|}tttd'd'gdd}t|}tjd*kr|t |j dgd+|t |j dgd,|t |j d%gd-nB|t |j ddd.g|t |j dgd/|t |j d%gd0dS)1Nrr?r/r r*rrrxrwrrXz@Xz@XYz=Bz!LzlZBIZxBIrrrZIIrr3rr)r)rrrrrr4r)rrrrrr4r)rrrrrr4rXcSsg|]}dqSrr;rr;r;r<r}] r~zCTestBufferProtocol.test_memoryview_cast_invalid..i)rrr4rZr)rrrrr"cSsg|]}dqSrr;rr;r;r<r}g r~cSsg|]}dqSrr;rr;r;r<r}l r~l)r4r4Ir,iQiAji )rrr3)rrrrr)rrr/r/r)rrr/r3r)NON_BYTE_FORMATrr5rgr rrbrMrTr rrwrzrrxr rrr rrmaxsize)rZsfmtZsformatZssizeZdfmtZdformatZdsizer.rZsitemsrmZsize_hZsize_drDrrlrmr;r;r<test_memoryview_cast_invalid s       "         z/TestBufferProtocol.test_memoryview_cast_invalidc sldddfdddfdddfffdd }td }td gd d }t|dd \}}t|}|d}j||d ddd |d|dd td }td g||gdd }t|d |gd\}}t|}|jd gd}j|||d dddd|dd tddD],\} } } t| | }t|}|||| | qtddD]4\} } } t| dg| t d}t|}|||| | q2dS)NrcSs t|Srsrrr.r;r;r<r r~z9TestBufferProtocol.test_memoryview_cast..rcSsddt|DS)NcSs g|]}|dkr|dn|qS)r,r!r;rVr;r;r<r} r~MTestBufferProtocol.test_memoryview_cast....rrr;r;r<r r~r cSsddt|DS)NcSsg|]}tt|dqS)zlatin-1)rRchrrVr;r;r<r} r~rrrr;r;r<r r~c st|}D]~\}}||}||}j||d|ddd|gd|dd ||} | ||}j| |||dddg|f|dd qdS)Nr*Fr0rT rWrr8r+rrrrcr)r5rgrrCr*r ) r.rDror8srcsizerNZ to_bytelistrmrcm3Zbytespecrr;r<iter_roundtrip s"       z?TestBufferProtocol.test_memoryview_cast..iter_roundtriprrYr r*Trrrrr;r0rKrMr) r5rgr rrMrrCrwrKr) rr r r.Z destitemsZ destshaperDrmZdestsizer8rormr;r r<test_memoryview_cast sB       z'TestBufferProtocol.test_memoryview_castcCstD]}tdD]}|dkr$qdtd|}t|}t||}t|rVdnd}t||D]:\}}} t|} |dkr|gng} || g} t|| |d} t | }t | ||| d \}} |dur| t |j || qd|d krqdt|| |d}|j || d }t| }|j}|}|j|| ||d || ||d d | |}|j || d }t| }| j}| }|j|| | |d || ||d d |j|| | |d || ||d d qdqqtrGd ddtj}|dd}t |}| d}||j|||jd||jd||jd||j|jf||jd||jdtd}t |}| d}||j|||jd||jd||jd||j|jf||jd||jddS)Nr?r )r>r?rrMrNrr rrTrc@s eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND..BEPointrXrBN)rrrctypesc_longc_double_fields_r;r;r;r<BEPoint srrg33333i@rr*Frr;333333?r )rrQrr5rgrrxrwr rMrrTr rr]rr rCrBigEndianStructurer*rWrr)r+rrrrr)rZ_tshaperVZtfmtZtsizerrWr8rormsizerZtshaper.rDZtitemsrrmrrrcr Zm4rpointrlrXr;r;r<test_memoryview_cast_1D_ND s           z-TestBufferProtocol.test_memoryview_cast_1D_NDcCs2tdttdd}t|}|||||||ddd}|ddd}||||||tttdgddd }t|}|||td d gdgd d }t|}|t|jtd gdgdd }t|}|t|jtgdddgdd }t|}|t|jdS)Nrrrrri )rZrr4rrrr )rr)r4rZr912345r*r)rbcdef) rKrrbrMr*r r rTr )rrFrDr.r;r;r<test_memoryview_tolist s(  z)TestBufferProtocol.test_memoryview_tolistcCsHttd}|}||d||}||ddS)NrYz.cmptestr*rrK) rbrwrrKrMr*r rr]rrr)r)rrHrr8rorFrrrFrrDZ array_errZ have_resizeZalarrZm_errr;r;r<test_memoryview_array* sF      z(TestBufferProtocol.test_memoryview_arrayc Cstdgd}tdgd}t|}t|}dD]0}|t|||t|t|||tq4t|}||||||||||t|}t|}|||||||t|}||gdtdgdgdt d}d t d f|d <|t||td d }t|}||||||t rGdddt j }|dd}t|}t|}|||||||||| t|jdS)Nrrs)r*rr4)__lt____le____gt____ge__r/r*zl x d xrrrrur}c@s eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases..BEPointrXrBNrrrrrrr;r;r;r<r srrr)rKrMr)getattrrrr*rEr rr#rrrTr r ) rrFrrwattrrrrr;r;r<%test_memoryview_compare_special_casesh sF            z8TestBufferProtocol.test_memoryview_compare_special_casescCsntdgdd}tdgdtd}t|}t|}||||||||||||||||||||||||d|d<||||||||||||||||||||||||ttt ddgtt Bd }ttt ddgtt Bd }t|}|||d |d <|||td gd d}td gdtd}t|}t|}||||||||||||||||||td gd d}tdgdtd}t|}t|}||||||||||||||||||||||||dS)N@Lr rrir;rWr]rrY)rUrrZLf5sZhf5s)i?rr) r rrMr*r__ne__rErrrbrf)rnd1nd2rrRrr.rDr;r;r<!test_memoryview_compare_ndim_zero sj                           z4TestBufferProtocol.test_memoryview_compare_ndim_zerocCsXtgddgdd}tgddgdd}t|}t|}|||||||||||||||tgddgdd}tgddgdd}t|}t|}|||||||||||||||tgdd gdd}tgddgdd}t|}t|}|||ddd ||ddd ||||ddd ||ddd |ddd tgdd gd d}tgddgdd}t|}t|}|||ddd ||ddd ||||ddd ||ddd |ddd tgdd gdd}tgddgdtd}t|}t|}|||ddd ||ddd ||||ddd ||ddd |ddd tgdd gdd}tgddgdtd}t|}t|}|||ddd ||ddd ||||ddd ||ddd |ddd dS)N@'r@hr )r\r]r^r_izh)r\r^r`rrrr[z!hz h)r rMr*rErfrrXrYrrRr;r;r< test_memoryview_compare_ndim_one sl             z3TestBufferProtocol.test_memoryview_compare_ndim_onecCstddgdgdd}tddgdgdd}t|}t|}|||||||||||||||tddgdgdd}tddgdgd d}t|}t|}|||||||||||||||dS) Nr~rrar i|i?z= h0cz@ ir rMr*rbr;r;r<"test_memoryview_compare_zero_shape$ s$         z5TestBufferProtocol.test_memoryview_compare_zero_shapecCstgddgdd}tdgdgdgdd}t|}t|}|||||||||||||||td gddgd d}td gdgdgd d}t|}t|}|||||||||||||||dS) N)rdrdrdrdrrVr rdrrry)rdrdz@ Liz!L hrerbr;r;r<$test_memoryview_compare_zero_strides> s$         z7TestBufferProtocol.test_memoryview_compare_zero_stridesc Csd}tdD]n}t|dd|\}}}dtfD]L}t||g||d}t|}||||ddd}t|}|||q,q d}tdD]h} t|\}}}dtfD]L}t||g||d}t|}||||ddd}t|}|||qqdS) NrfrErMr?rrrzr)rQrrrfr rMr*rb) rrrVr8rorFrrrDrmr;r;r<&test_memoryview_compare_random_formatsX s(      z9TestBufferProtocol.test_memoryview_compare_random_formatscCstttddgddd}tttddgddd}t|}t|}|||||||||||||||tdgdgdd d}td gdgdd d}t|}t|}|||||||||||||||tttdgd d d}tttdgdd d}t|}t|}|||||||||||||||tdgdddgdd}tdgdddgdd}t|}t|}|||||||||||||||tttdgd d d}tttdgd dd}t|}t|}|||||||||||||||dS)Nr2rrrrar rrrr*rz=f q xxL)g333333r*rz< f 2Qrrrr4z! b B xLz = Qx l xxLrr rrbrMr*rErbr;r;r<"test_memoryview_compare_multidim_ct sZ                        z5TestBufferProtocol.test_memoryview_compare_multidim_ccCstttddgddtd}tttddgddtd}t|}t|}|||||||||||||||tdgd d d gd td}td gd d d gdtd}t|}t|}|||||||||||||||tttddgddtd}tttddgddtd}t|}t|}|||||||||||||||tttddgddtd}tttddgddtd}t|}t|}|||||||||||||||tttdgddtd}tttdgddtd}t|}t|}|||||||||||||||dS)Nrir2)rrrrarrrrrrrr=Qq)rrz=qQrrrjZ0llrG)r rrbr8rMr*rErbr;r;r<(test_memoryview_compare_multidim_fortran s                        z;TestBufferProtocol.test_memoryview_compare_multidim_fortranc CsPttdd}t|gd}t|gddd}t|gddtd}t|}t|}|||||||||dgd }d |d <t|gd}t|gdd d}t|gdd td}t|}t|}|||||||||tttdd dgdd}|ddddddf}tttdd dgdd}|ddddddf}t|}t|}|||||||||||||||tdgdddgdd}|ddddddf}tdgdddgdd}|ddddddf}t|}t|}|||||||||||||||tttd gddd}|dddddf}tttd gddd}|dddddf}t|}t|}|||||||||||||||tttd gddd}|dddddf}tttd gddd}|dddddf}t|}t|}|||||||||||||||tdgd gd d!d}|dddddf}tdgd gd d"d}|dddddf}t|}t|}|||||||||||||||dS)#Nrir2rj@lr rr)gffffff r=r)grsr=rzd b czd h c(r3@Irr*rr[r)rr$rZrz=iiz>iirrr)rr|rrrZb3sZi3s)rrbrr r8rMr*rE) rlst1lst2rXrYrrRrjrkr;r;r<&test_memoryview_compare_multidim_mixed s                               z9TestBufferProtocol.test_memoryview_compare_multidim_mixedcCstttdgddd}tttdgddd}t|}t|}|||||||||||||||tttdgddd}tttdgddd}t|}t|}|||||||||||||||dS)Nr)rrrrr )rrrz@irmrbr;r;r<+test_memoryview_compare_multidim_zero_shape[s$         z>TestBufferProtocol.test_memoryview_compare_multidim_zero_shapecCstdgdgddd}tdggdgddd}t|}t|}||||||||||||||||||td gd d d gd d}td gd d gddgdd}t|}t|}|||||||||||||||dS)Nrdr)rrrrVr r5rryrrfrrz=lQrzQqrrrrj)rrZBbrwr)shellor~r*)rrrZ5s0sP)ssushir~r*rir2rrr)ssashimisslicedg 4@)sramensspicygfffff"@rZz < 10p 9p dz > 10p 9p d) r rrbrfrMr*rErrr8) rrjrXrkrYrrRrxryr;r;r<+test_memoryview_compare_multidim_suboffsetss                                       z>TestBufferProtocol.test_memoryview_compare_multidim_suboffsetscCsTdD]H}tdgdgd|dd}tdgdgd|dttBd}d|d d ddd<t|}t|}|||||||||||||||td gdgd|d d}td gdgd|d ttBd}d |d d ddd<t|}t|}|||||||||||||||qdS)N)rBr@rArCr'r)rrrrrrr rr*rr)r'r%r#ZQLH)r*r*r*)r rr8rMr*rE)r byteorderrXrBrFrr;r;r<!test_memoryview_compare_not_equals6           z4TestBufferProtocol.test_memoryview_compare_not_equalcCstdgd}t|}||tt||t|jd|tt||t|j|t|j|tt dt |t|j d|t|j dddD]}|t|j |qtdgd}t|}t|}|||||||||||||dS)Nr)g?g@gffffff @r z1.0 in mrr*) rWrr+rrrrrrrKrMrO)rKrMrrTrrr r rrrrr__getattribute__r*rE)rrFrDrSrrlrmr;r;r<test_memoryview_check_released.s*   z1TestBufferProtocol.test_memoryview_check_releasedcsPdtdgdd}t|}||||||tgdgdd}t|}||||||tfddtd Dgd d d}t|}||||||tfd dtd Dgddd}t|}||||||trLGdddtj}|dd}t|}||t|dS)Nr[rrar r*z>hQiLlcsg|]}qSr;r;rrvr;r<r}er~z>TestBufferProtocol.test_memoryview_tobytes..rWrz=hQiLlcsg|]}qSr;r;rrr;r<r}jr~r)rrrrrz.BEPointrXrBNrPr;r;r;r<rssrrr)r rMr*rrbrrrR)rrrDrrrFr;rr<test_memoryview_tobytesVs0  $   z*TestBufferProtocol.test_memoryview_tobytescCs|ttitd|ttdtdtgddgdgd}|tt|tdtdd d d }d D],}t|t|}|||||d dq^tdd d td }d D],}t|t|}|||||d dqd D]\}d|d <t|t|}|||||d dd|d <||d d||d dqtdgdgd td }d D]>}t|t|}|t |j d||||| gqHtt t dgdd td }d D]*}t|t|}|t| gggqtdgdgdtd }d D]2}t|t|}||||| | qtgddgdtd }d D]2}t|t|}||||| | q:tgddgdgtd}d D]d}t|t|}||||| | |t|jdd||dd||ddq|ddd}d D]d}t|t|}||||| | |t|jdd||dd||ddqtt t dddgtd}dD]*}t|t|}|t| | q|tt|tdt|t|}|t| | tt t dddgttBd}dD]*}t|t|}|t| | q |tt|tdt|t|}|t| | tt t dddgttBd}d D]<}|tt|t|t|t|}|t| | qtgddgdgd}t|td}||jdS)Nrr=rrsrrrrYr;rr rrrfr*rr3rrrrrurrrWrr])rr)rrrw)rTr r9r:r4Z PyBUF_WRITEr r*rrrr rrbrr8rfr;rK)rrr(rDr;r;r<test_memoryview_get_contiguousys                        z1TestBufferProtocol.test_memoryview_get_contiguouscCstd}tdgd}t|}t|}td|}|||| |td}t t t dgddd}t|}t|}td|}|||| |dS)NrrwrrrWrr ) r5rgrKrMioBytesIOrreadintor*rr rrb)rrrFrDrrrr;r;r<test_memoryview_serializings        z.TestBufferProtocol.test_memoryview_serializingc Cstttd}t|}|t|t||jdddgd}|t|t||ddd}tttdddd}|t|t|tttdgdtd }t|}|t|t|tttdgdd}|ddd ddddd f}t|}|t|t|tttdgd t d }|ddd ddddd f}t|}|t|t|tttddgd d}t|}tttddgdd}t|}||||t|t|tttdgddd}t|}| t |j tttddgddd}t|}| t |j tttdgddd}t|}| t |j tttddgddd}t|}| t |j dS)NrWr rrrr[rrjr]rr)rrrrr rrrrrrz= Lz< h) rRrrbrMr*rrr r8rfrTr__hash__) rrrDZmcZmxrrXrFrBr;r;r<test_memoryview_hash sJ z'TestBufferProtocol.test_memoryview_hashc Cs|tgd}t|}t|}|t|j~|tgd}t|}t|ttd}t|ttd}||j ||t|j~~|tgd}t|}t|}t|}||t|j~|tgd}t|}t|}t|ttd}t|ttd}||j |||t|j~~|tgddgt d}t|}|j gddgdt|}t|}| | | t|} | | | | | | || || | | ~||dd } td }t|,}| || |d td Wdn1sF0Ytttd gddd}t|ttd} t| ttd} || j |t| >}| || |d d gdgdggWdn1s0Yd tfD] } td }t|}~t|t| d} t| }~ t|t| d} t| h} ~ | | | || || |d td | |dtd| | dtd~ Wdn1s0Y~Wdn1s0Y~Wdn1s0Ytd }t|}~t|t| d} t| }~ t|t| d} t| l} ~ | || || | | |d td | |dtd| | dtd~~~ Wdn1s0YWdn1s0YWdn1s0Yqtd }|tRt|(}t|}|d td kWdn1sN0YWdn1sn0YdS)NrsrHrrr])rrrr4r3rrcSs,t|}Wdn1s0YdSrs)rM)rrmr;r;r<catch22s z;TestBufferProtocol.test_memoryview_release..catch22r|rr:rWrrr r*rk)rrrr;rr<)rrMr rTr4rr> ND_REDIRECTr)rWrrr*r rordrrb)rrFrDrrXrYrlrmrXrBrrrr r.r;r;r<test_memoryview_releaseHs     4 F   """   j  z*TestBufferProtocol.test_memoryview_releasecCs0tddtdDdgdd}tdddtdD}||fD]}t|ttd}t|ttd}t|}||j|||j|||j||||||||||||dd |dd ||dd |dd ||dd |dd ~~||dd |dd q@dS) NcSsg|] }d|qSr"r;rVr;r;r<r}r~z?TestBufferProtocol.test_memoryview_redirect..rWrr cSsg|] }d|qSrr;rVr;r;r<r}r~rr*r) r rbrKr>rrMr)rWr*)rrrFrXrBrrDr;r;r<test_memoryview_redirects"    z+TestBufferProtocol.test_memoryview_redirectc Csd}gd}|ttdddt}t|}|j||d|dddgdg|d tdD]}||||qT~~t}t|}~~t}t|td }t|td }t|}| |j || |j ||j||d|dddgdg|d ~~~~t}t|tt d }t|tt d }t|}| |j || |j || |j ||j||d|dddgdg|d ~~~~tdd }t|}|j|dd|dddgdg|d tdD]}||||q~~tdd }t|}~~tdd }t|td }t|td }t|}| |j d| |j ||j||d|dddgdg|d ~~~~tdd }t|tt d }t|tt d }t|}| |j d| |j || |j ||j||d|dddgdg|d ~~~~dS) Nr) rr*rrrrrr4r3rYrfrZr*rrTrWrrGr)Z legacy_mode) rTr Z staticarrayrMrCrbr*r r>r)rWr)rr8rcrXrBrrrDr;r;r<$test_memoryview_from_static_exporters                z7TestBufferProtocol.test_memoryview_from_static_exportercCs*tgddgttBd}|tt|dS)Nrsrrq)r rhZND_GETBUF_UNDEFINEDrTr4rMrr;r;r<#test_memoryview_getbuffer_undefined>sz6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs&tgddgtd}|tt|dS)Nrsrr])r rhrTr4rM)rrXr;r;r<test_issue_7385Dsz"TestBufferProtocol.test_issue_7385cCs&dD]}|t|t|qdS)N)r>iiZ3s)r* _testcapiZPyBuffer_SizeFromFormatr5rg)rrr;r;r<test_pybuffer_size_from_formatHs z1TestBufferProtocol.test_pybuffer_size_from_formatN)F)UrrrrrCrUrprrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrr!r% contextlibcontextmanagerr*r8rBrDrJrTrZrcrfrgrhrnrqrzr{r|r~rrrrrrrrrrrZ cpython_onlyrr;r;r;r<rs K 0i  -' 0.Y #6!)>    %qu 8 s<T  5m8>5?H>Ha(#u<^r__main__)r NN)r )r )rr1r;)rr1)r)N)F)F)r)N)grZunittesttestr itertoolsrrrandomrrrwarningsrrKrosZdecimalrZ fractionsr Z _testbuffer ImportErrorr r5rZEnvironmentVarGuardenvironcatch_warningsrLrrZ SHORT_TESTZNATIVEr6r7ZSTANDARDr=rQr8copyryZARRAYrZBYTEFMTrhrjrgrYr`rdrqrrrtrwrxrzrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrZ skipUnlessZTestCaserrmainr;r;r;r<sf              P                        ?  +  !     o