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) zReturn range of a native type.r 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) zWReturn random item for a type specified by a mode and a single format character.r rLr rrr)rfmtdictbytesboolr5r6unpack)modecharobjxr;r;r< randrange_fmts  rYcCsJ|d\}}g}|D]}|t|||qt|dkrB|dSt|S)zReturn single random item.#r*r)splitappendrYlentuple)r8rWrUcharsrXr r;r;r<gen_items r`cCs<|dkrt||Sdg|}t|D]}t||||<q$|S)z,Return a list of random items (or a scalar).r)r`range)rr8rWlstrr;r;r< gen_itemss    rcc Cstt|t}|d}|d}tdd}t|D]H}ttt|}tt|t}||t |rf|nd7}|||7}q4t |||} t ||} || | fS)NrZambr r*) rcapMODEstriprrar^rQMULTintrcr`) rrWrUZxfmtr8nmemb_rV multiplieritemsitemr;r;r< struct_itemss     rpr cCs|durtt|t}|dur0ttt|}tt|t}|d|t|rR|nd}t|||}t||}| d||}|||fS)z"Return random format, items, item.NrZr*rd) rrfrgr^rQrirjrcr`rh)rrWrUrVrmr8rnror;r;r< randitemss  rqccs4t|tD]"}t|D]}t||||Vqq dS)z1Iterate through supported mode/char combinations.N)rfrgrQrq)rrWrUrVr;r;r< iter_modes rrccs2t||D] }|Vq |dkr"dSt||VdS)zwYield (format, items, item) for all possible modes and format characters plus one random compound format string.r N)rrrp)nitemsZtestobjtr;r;r< iter_formats rucCsd|vpd|vpd|vS)Nr rrr;)r8r;r;r<is_byte_formatsrvcCs4t|}|dks$|dko2|ddko2||dtvS)zformat suitable for memoryviewr*rrr?)r] MEMORYVIEW)r8rXr;r;r<is_memoryview_formatsrxcCsg|]}t|s|qSr;)rv.0r r;r;r< r{cCs t|t S)z9Tuple items (representing structs) are regarded as atoms. isinstancelistrbr;r;r<atompsrcCs t|tSNr}rr;r;r<listpsrcCs6t|dkrdS|d}|ddD] }||9}q$|S)zProduct of list elements.rr*N)r])rbrXvr;r;r<prods   rcCs|dkr dS|dkrZt|dd|g}t|dddD]}||||d9<q:n>|gt|dd}td|D]}||||d9<qz|S)zOCalculate strides of a contiguous array. Layout is 'C' or 'F' (Fortran).rr;Cr*Nr)rra)ndimshapeitemsizeZlayoutstridesrr;r;r<strides_from_shapesrcCst|r |St|dkr |dSdg|d}|drFt||dnd}t|dD].}||}t|||||dd||<qV|S)zmConvert flat item list to the nested list representation of a multidimensional C array with shape 's'.rr*N)rr]ra_ca)rnsrbstriderstartr;r;r<rs $rcCslt|r |St|dkr |dSdg|d}|d}t|dD]$}t||d||dd||<qB|S)zsConvert flat item list to the nested list representation of a multidimensional Fortran array with shape 's'.rNr*)rr]ra_fa)rnrrbrrr;r;r<r$s "rcCs2t|r(d|vr(t|t|kr(tdt||SNrzprod(shape) != len(items))rrr] ValueErrorrrnrr;r;r<carray1s rcCs2t|r(d|vr(t|t|kr(tdt||Sr)rrr]rrrr;r;r<farray6s rcCsdd|D}t|S)z(Generate all possible tuples of indices.cSsg|] }t|qSr;rarzrr;r;r<r{=r|zindices..)r)r iterablesr;r;r<indices;srcCs*d}t|D]}|||||7}q |S)zAConvert multi-dimensional index to the position in the flat list.rr)rindrretrr;r;r<getindex@s rc Cs|s|St|}t||dd}t||ddddd}dgt|}t|D]2}t|||}t||ddd|}||||<qL|S)zTranspose flat item list that is regarded as a multi-dimensional matrix defined by shape: dest...[k][j][i] = src[i][j][k]... r*rNrr)r]rrr) srcrrZsstridesZdstridesdestrfrtor;r;r< transposeGs  rcCs6|gkr |St|r|gSt|dt|ddS)z flatten listrr*Nr_flattenrr;r;r<rVs rcCst|r |St|S)zflatten list or return scalarrrr;r;r<flatten^srcCs4t|r gSt||dgt|d|ddS)zMGet the shape of lst after slicing: slices is a list of slice objects.rr*N)rr] slice_shaperbslicesr;r;r<rdsrcs&t|r |Sfdd|dDS)z=Multi-dimensional slicing: slices is a list of slice objects.csg|]}t|ddqSr*N) multislice)rzZsublstrr;r<r{or|zmultislice..r)rrr;rr<rksrcsFt|r |Sfddt|d|dD}||d<|S)aMulti-dimensional slice assignment: llst and rlst are the operands, lslices and rslices are lists of slice objects. llst and rlst must have the same structure. For a two-dimensional example, this is not implemented in Python: llst[0:3:2, 0:3:2] = rlst[1:3:1, 1:3:1] Instead we write: lslices = [slice(0,3,2), slice(0,3,2)] rslices = [slice(1,3,1), slice(1,3,1)] multislice_assign(llst, rlst, lslices, rslices) c s.g|]&\}}t||ddddqSr)m_assign)rzrrlslicesrslicesr;r<r{szm_assign..r)rzipllstrlstrrr;rr<rqs  rcCsft||}t||}t|t|kr(dStt|D],}||||krNdS||dkr4dSq4dS)z9Compare the structure of llst[lslices] and rlst[rslices].rr)rr]ra)rrrrlshapershaperr;r;r< cmp_structures   rcCs(t||||dkrtdt||||S)z:Return llst after assigning: llst[lslices] = rlst[rslices]rz+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)zVerify that the parameters represent a valid array within the bounds of the allocated memory: char *mem: start of the physical memory block memlen: length of the physical memory block offset: (char *)buf - mem Frc3s|]}|VqdSrr;rrr;r< r|z#verify_structure..Tc3s.|]&}|dkr||dVqdSrr*Nr;rzjrrr;r<rs c3s.|]&}|dkr||dVqdSrr;rrr;r<rs )anysumra)memlenrrrroffsetiminimaxr;)rrrr<verify_structuresrcCs|D] }||}q|Srr;)rbrrr;r;r<get_items rc Cs:|\}}}}}}|}t|D]} ||| || 7}q|S)z-Location of an item in the underlying memory.r) rrtrrrrrrprr;r;r< memory_indexs  rc CsR|\}}}}}}d|>}t|D],}t||} d| >} || @rDdS|| O}q dS)zThe structure 't' is overlapping if at least one memory location is visited twice while iterating through all possible tuples of indices.r*TF)rr) rtrrrrrrZvisitedrrbitr;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)zReturn random structure: (memlen, itemsize, ndim, shape, strides, offset) If 'valid' is true, the returned structure is valid, otherwise invalid. If 'shape' is given, use that instead of creating a random shape. r*rr;rd_ZrTFrr*rc3s.|]&}|dkr||dVqdSrr;rrr;r<rs z!rand_structure..c3s.|]&}|dkr||dVqdSrr;rrr;r<rs )rrar]r)rZvalidmaxdimmaxshaperrrsrrminshaperrZ maxstrideZ zero_striderrr;rr<rand_structures\       "    rc Csp||}t|d}|r$|||nd}td|d}|||}t|||}t||\}}}} | |krlt|S)z=Create a random slice of len slicelen that fits into listlen.r*)rslice slice_indices RuntimeError) slicelenZlistlenZmaxstartrZmaxstepstepstoprrlZcontrolr;r;r<randslice_from_slicelen&s   rcCspdg|}dg|}t|D]B}||}|dkr>td|dnd}t||||<t||||<qt|t|fS)zxCreate two sets of slices for an array x with shape 'shape' such that shapeof(x[lslices]) == shapeof(x[rslices]).rr*)rarrr^)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) zCreate (lshape, rshape, tuple(lslices), tuple(rslices)) such that shapeof(x[lslices]) == shapeof(y[rslices]), where x is an array with shape 'lshape' and y is an array with shape 'rshape'.r*rrrrrPTFr)rrarrrr^)rrrrrZ all_randomrrrrZsmallbigrrrZs_smallrlrZs_bigr;r;r<rand_aligned_slices?s<    rcCs$|\}}}}}}t||d|dS)zMReturn a list of random items for structure 't' with format 'fmtchar'.rZrL)rc)r8rtrrrlr;r;r<randitems_from_structurejsrc Cs(|\}}}}}} t||||| t|BdS)z:Return ndarray from the tuple returned by rand_structure()rrformatrflags)r ND_WRITABLE) rnr8rtrrrrrrrr;r;r<ndarray_from_structureps rc CsP|\}}}}}}t|} t|D]\} } t|| | || q t| ||||dS)z>Return numpy_array from the tuple returned by rand_structure())bufferrrdtyper) bytearray enumerater5 pack_into numpy_array) rnr8rtrrrrrrbufrrr;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)aInterpret the raw memory of 'exporter' as a list of items with size 'itemsize'. If shape=None, the new structure is assumed to be 1-D with n * itemsize = bytelen. If shape is given, the usual constraint for contiguous arrays prod(shape) * itemsize = bytelen applies. On success, return (items, shape). If the constraints cannot be met, return (None, None). If a chunk of bytes is interpreted as NaN as a result of float conversion, return ('nan', None).Nrcsg|]}||qSr;r;)rzrrZmemr;r<r{r|zcast_items..nan) nbytesrrdivmodtobytesrar]r5rTr\) Zexporterr8rrZbytelenrrZ byteitemsrnrror;rr< cast_itemss(     rc#stdD] }|gVqtdd}tddkr0dndfdd t|DVtdd}tddkrfdndfd d t|DVd S) z Generate shapes to test casting.r/rrr*rcsg|]}tdqSrrrzrlrr;r<r{r|z!gencastshapes..csg|]}tdqSrrrrr;r<r{r|N)rar)rrr;rr< gencastshapess    rcCs0tt| |dt| |dt| |dS)z4Generate all possible slices for a single dimension.r*)rra)rr;r;r< genslicessrcsfddt|D}t|S)z/Generate all possible slice tuples for 'shape'.csg|]}t|qSr;)rrzrrr;r<r{r|z"genslices_ndim..)rar)rrrr;rr<genslices_ndimsrFcCs,|s |dkrdnd}t||d}t||S)zGenerate random slice for a single dimension of length n. If zero=True, the slices may be empty, otherwise they will be non-empty.rr*)rr)r allow_emptyZminlenrr;r;r<rslicesrccstdD]}t||VqdS)z.Generate random slices for a single dimension.rN)rar)rrrlr;r;r<rs rc#spt|D] }tfddt|DVqt|D] }tfddt|DVq2tddt|DVdS)z)Generate random slice tuples for 'shape'.c3s|]}t|VqdSrrrrr;r<rr|zrslices_ndim..c3s|]}t|ddVqdS)T)rNrrrr;r<rr|css|]}tdddVqdSr)rrr;r;r<rr|N)rar^)rrZ iterationsrlr;rr< rslices_ndims   rccs0t|}|durt|n|}tt||VdSr)r^r]r)iterablerpoolr;r;r< rpermutationsrc Csz |}Wnttfy*|}Yn0t|trD|j}|j}nd}d}td||j |j |j ||j |j |ftjdS)zPrint ndarray for debugging.unknownz_ndarray(%s, shape=%s, strides=%s, suboffsets=%s, offset=%s, format='%s', itemsize=%s, flags=%s)N)tolist TypeErrorNotImplementedErrorrr~r rrprintrr suboffsetsrrsysstdoutflush)ndrXrrr;r;r< ndarray_prints   rrrerz%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|_dSr)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;ryr;r;r<rTr|z,TestBufferProtocol.verify..rrFArrrrrrrrcsg|]}t|qSr;r5 unpack_fromrcontigr8rr;r<r{scSsg|] }|dqSrr;rr;r;r<r{r|rrrordercsg|]}t|qSr;rrr r;r<r{scSsg|] }|dqSr"r;rr;r;r<r{r|csg|]}t|qSr;rrr 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}||dS)Nrr*)assertIsrW assertEqualrrrreadonlyrrr^rr r]rr)rDexpected_readonlyrrep) ex expected_lenr8rrbrresultrrrrr r;r<check_memoryviews z3TestBufferProtocol.verify..check_memoryview)r)).rr]r Z assertGreaterrarrr&rWr'rrrr(rrr^rr~r rxrrrr get_pointerrr5r6extend BufferErrorrMjoinhexr is_contiguousr ND_FORTRANget_contiguous PyBUF_READ assertTrue cmp_contigrpy_buffer_to_contiguous PyBUF_FULL_RO toreadonly)%rr-rWrr8r(rrrrbrrZ suboffset0rZstride0r*rZbuf_errrZitem1Zitem2rXrDrZffZ flattenedr%expectedZtransrkroZinitlstyZ contig_bytesrr.Zmmr;) r!r+r,r8rrbrr-rrrrr r<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 ||@|kSrr;)reqflagr;r;r<matchsz/TestBufferProtocol.verify_getbuf..matchgetbufFr>r*r;) rWrr8r(rrrrbr)r(PyBUF_WRITABLEPyBUF_C_CONTIGUOUS c_contiguousPyBUF_F_CONTIGUOUS f_contiguousPyBUF_ANY_CONTIGUOUS contiguousPyBUF_INDIRECTr  PyBUF_STRIDESPyBUF_ND PyBUF_FORMAT assertRaisesr1r r~rxrrr;rrrrrr@)rZorig_exr+rArrCrbrr>r8rrrrr;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;rzrXr;r;r<r{ r|z:TestBufferProtocol.test_ndarray_getbuf.. r r*rrrrrr4r3 re rTrrcSs$g|]}|drd|nd|qS)rr%r$r;rSr;r;r<r{ r|rr)r*rr*rrWrrrrr*r3csg|] }|qSr;r;rrr;r<r{(r|)rrrrrT)rrrrVrD)r*rTr*)r4rr4rr)!rMrNrO PyBUF_SIMPLErGrIrK PyBUF_FULLr; PyBUF_RECORDSPyBUF_RECORDS_RO PyBUF_STRIDEDPyBUF_STRIDED_RO PyBUF_CONTIGPyBUF_CONTIG_ROrarr5ND_PILrFrPr5calcsizer rMrr'rRND_GETBUF_FAILrQr1r8r4)rrequestsZ items_fmtZ structureZndflagsZ real_flagsrnr8rrrrZ_itemsex1ex2m1m2rAbitsr+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)9NrVr*r>r*rr)r*rrJ)r*rrrr*rrrrrrrrrZ)rrErrrZXXXrirrrrZQLrrJr"r*rrrrrHr*rrrrrrr)rrrr>z@#$) r*rrrrrr4r3rVrerXr)rrrrr123rDrrT)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_VAREXPORTpushrQrr5rcrr5r7Z ND_MAX_NDIM OverflowErrorrr\r1rFpoprMreleaser/rrar[rmemoryview_from_bufferr6r7UnicodeEncodeErrorr9r4) rrZndmr rrnrDr+rgrhr;r;r<test_ndarray_exceptionsfs  z*TestBufferProtocol.test_ndarray_exceptionscCsttdD]}dgd}tgddgtd}t||d<tddD]$}|jgddgdt|||<qDtdD]}|||qr|t|j ~q dS)NrrrprrZr*r) rrar rrMrrrQr1r)rpermrDrrr;r;r<test_ndarray_linked_lists   z+TestBufferProtocol.test_ndarray_linked_listc CsLtdD]>\}}}t|}t|d|d}|j|d||dddd|d qdS)Nrr;rTrWrr8r(rrrrb)rur5rdr r@)rr8Zscalarrlrrr;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*rerrTr)rrur5rdrcr r@)rrsr8rnrlrrrr;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) rrur5rdrar]rcr r@) rrsr8rnrlrrrrrrrr;r;r<test_ndarray_format_strides*s$     z.TestBufferProtocol.test_ndarray_format_stridescCs@gd}t|ddd}t|ttBd}||t|ddS)NrUro)r*rrrD)r rIrPr'rr)rrnr+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)rrerrr;r;r<r{Gr|z.rrrTrrrDr;r)rarrrqr5rdrcr rrr@rxrbrPr8rrr5r)rrshape_trsrr8rnrlrrrrrbr+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*rr3rZr)r*rrrrrr4r3rrri,)r)r*rro)rrZLQ)s1r)r rQr __setitem__rMr'r __delitem__r __getitem__ IndexErrorr]r5r7r)rrrrnr;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;rZ.rr3rVr) r rrMr'rrQrrr)rrrrXr;r;r<test_ndarray_index_scalars" z,TestBufferProtocol.test_ndarray_index_scalarcCsRtttdddgtd}t|td}|t|jd|t|jtddddS) Nr3rrrZrDr*rr) r rrarrarQr1rr)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) Nrrrr#rDrtr)rur rar'rQrrrxrMrrarPrr&__eq__NotImplementedrbr[)rr8rnrlrrrr+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*) rur rrar'rrQrrrxrM)rr8rnZ 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) rrrqrcr rrar'rr5r) rrrsrr8rnrlrrrbrrkr;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 rQrevallocalsrMr'rurr8rx)rrrr8rnrlrrr;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)Nrr3rZrr*rDrrrrz@%$rrrr4ru)rrr4r3z ###########)r rrMrQrrrrOr1rrr r'rrrcadd_suboffsetsr[) rrnxlmlr+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) NrUrTrrrrVrrr*r)r rr'rrMr)rrnrXr?rrr;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]rrrqr5rdrcr rrr Exception __class__r&r'r)rrrrsrr8rnrlrrrrbrlisterrrenderrndslicedr;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]rrrqr5rdr rrMrrrrrr&r'r)rrrrsrr8rnrlrrr+rrbrrrrrrr;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)rurrcrr rMr]rrr&rr'rrxr@rrrr)rr8rnrllslicerrrrr+rZlsterrZdiff_structurerbZlvalZrvalrrZmverrr;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]rrrqrcra ITERATIONSrr rrrrrr&r'r)rrrrsrr8rnrlrrrrrbrr-rrr;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?TrrrrrnrFr)rarrQr5rdrMAXDIMMAXSHAPEr8rrrrrMrxr'rcrrr@rrrr)rrlr8rrtrnrXZxlistrZmvlistr?Zylistrzr;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)rarrQr5rdrrr assertFalserrrrrr8rr) rrlr8rrtrnrrXrZ numpy_errr?r;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?rTrrrrnFr)rarrQr5rdrrrrr8rrrrrMr'rcrrrr@rrrr)rrlr8rrrrrtltrZlitemsZritemsrrZxllistZxrlistrrZylrZyllistZyrlistZzlZzrr;r;r< test_ndarray_random_slice_assignsb               z3TestBufferProtocol.test_ndarray_random_slice_assigncCsvgd}t|ddgtd}t|}||jt@||j|||jd||j ||j ||j dS)NrUrrrZ)rr) r rcr8rr&rWr'r rrHrJrL)rrnrr+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)NrrprZrrrrrr)rrrrcr rMr'rrrrrr;r;r<test_ndarray_zero_shapes   z*TestBufferProtocol.test_ndarray_zero_shapecCsddtfD]V}tdgdgdg|d}t|}|||||gd||gdqdS)Nrr*rrr)r*r*r*r*r*rrr;r;r<test_ndarray_zero_stridess   z,TestBufferProtocol.test_ndarray_zero_stridescCs>tttddgdd}||jd||gddS)Nrr4rq)r4r3rV)r rrar'rrrrr;r;r<test_ndarray_offsetsz&TestBufferProtocol.test_ndarray_offsetcCs>dtfD]0}tttddg|d}|}|||qdSNrrrZ)rcr rrarr')rrrrDr;r;r<#test_ndarray_memoryview_from_buffers z6TestBufferProtocol.test_ndarray_memoryview_from_buffercCsPdtfD]B}tttddg|d}tdD]}|||t||gq,qdSr)rcr rrar'r/)rrrrr;r;r<test_ndarray_get_pointer s  z+TestBufferProtocol.test_ndarray_get_pointercCs\tttdgdd}t|ttBd}|||t|}|||dS)Nr)rrrrrD)r rrarOrPr'rrM)rr+rrDr;r;r< test_ndarray_tolist_null_stridess z3TestBufferProtocol.test_ndarray_tolist_null_stridescCs|tddtttdddgd}tttdddgd}|t||tdgdgdd }|t|d |td |dS) Nrys456rTrrrr*rrrP)rr9r rrar8)rrXr?r;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) NrrprTrrr)rrrrZrrrrrrr) rKr rQrhashrRrrar'rr5rcr)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)"NrrVr;rrrr*rr|r3rrr4rrprrrrrrrTrrZrr)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|)rMrNrOr[r\r;r]r^r_r`rarbrQrr:r rr'rrrar5r1rrrrMZtostringrc) rrfrr%ZrequestrrZndbytesrbZnarXr=r?r;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)NrVrprrrrr*rrTrrDr;r>rr*rZr9rXr"rrr)rrar rMr8rHrLr]rrr@rbrPr'rrr[rrrr5rJrrcrQrr1rr6r7) rZ items_shapernrr+rDrrrbrjrr;r;r<test_memoryview_construction: s(              z/TestBufferProtocol.test_memoryview_constructioncCsgd}gdgdgdfD]4}t||d}||jt|}|t|jdqtddD]<\}}}td}||}|| d|| gq^dS) Nrprrrr r*rMr|) r r8rHrMrQrrrur'rr)rrnrr+msrcr8rlrDr;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 calcvobjsizerrarMr )rcheckZvsizeZ base_structZper_dimrnrFr;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 ||_dSrvalrrr;r;r<__init__ szFTestBufferProtocol.test_memoryview_struct_module..INT.__init__cSs|jSrrrr;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 ||_dSrrrr;r;r<r szFTestBufferProtocol.test_memoryview_struct_module..IDX.__init__cSs|jSrrrr;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..frVy@@z-21.1gffffff(@rrrp>rrrr4r3r;rVTFasabcrFabccSs|Srr;)rXr;r;r< r|zBTestBufferProtocol.test_memoryview_struct_module..rerMrrrr*)objectrr Ellipsisrrur rrMr5rr'rdr7rrrZ assertIsNot)rrrrvaluesr8rnror+rrDrrZ struct_errZmv_errrr;r;r<test_memoryview_struct_module sH       z0TestBufferProtocol.test_memoryview_struct_modulecCs>tgddgdgd}||jt|}|t|jddS)Nrprrrr )r rrHrMrQrr)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/rr*rrrxrtrrXz@Xz@XYz=Bz!LzlZBIZxBIrroZIIrr3rr)r)rrrrrr4r)rrrrrr4r)rrrrrr4rXcSsg|]}dqSrr;rr;r;r<r{] r|zCTestBufferProtocol.test_memoryview_cast_invalid..i)rrr4rWr)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_FORMATrr5rdr rrarMrQrrrurxrrvr rrrrr maxsize)rZsfmtZsformatZssizeZdfmtZdformatZdsizer+rZsitemsrlZsize_hZsize_drDrrirjr;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|Srrrr+r;r;r<r r|z9TestBufferProtocol.test_memoryview_cast..rcSsddt|DS)NcSs g|]}|dkr|dn|qS)r,r!r;rSr;r;r<r{ r|MTestBufferProtocol.test_memoryview_cast....rrr;r;r<r r|r cSsddt|DS)NcSsg|]}tt|dqS)zlatin-1)rRchrrSr;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(rrrrbr)r5rdrr@r'r) r+rDrnr8srcsizerNZ to_bytelistrjrbm3Zbytespecrr;r<iter_roundtrip s"       z?TestBufferProtocol.test_memoryview_cast..iter_roundtriprrVrr*Trrrrr;r0rKrMr) r5rdr rrMrr@rurKr) rr rr+Z destitemsZ destshaperDrjZdestsizer8rnrlr;rr<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?rrMrNrrrrTrc@s eZdZdejfdejfgZdS)z>TestBufferProtocol.test_memoryview_cast_1D_ND..BEPointrXr?N)rrrctypesc_longc_double_fields_r;r;r;r<BEPoint srrg33333i@rr*Frr;333333?r )rrQrr5rdrrvrur rMrrQrrr]rrr@r BigEndianStructurer'rWrr&r(rrrr r )rZ_tshaperVZtfmtZtsizerrWr8rnrlsizerZtshaper+rDZtitemsrrjrrrbrZm4rpointrirXr;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 )rWrr4rrrr)rr)r4rWr912345r*r)rbcdef) rKrrarMr'rr rQr )rrFrDr+r;r;r<test_memoryview_tolist s(  z)TestBufferProtocol.test_memoryview_tolistcCsHttd}|}||d||}||ddS)NrVz.cmptestr*rrK) rarurrKrMr'rrr]rrr&r)rrErr8rnrCrrrFrrDZ 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)Nrrp)r*rr4)__lt____le____gt____ge__r,r*zl x d xrrrrurzc@s eZdZdejfdejfgZdS)zITestBufferProtocol.test_memoryview_compare_special_cases..BEPointrXr?Nrrrr r rr;r;r;r<r srrr)rKrMr&getattrrrr'rBr rr r rrQr 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@Lrrrir;rTrZrrV)rRrrZLf5sZhf5s)i?rr) r rrMr'r__ne__rBrrrarc)rnd1nd2rrOrr+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)rYrZr[r\izh)rYr[r]rrrrXz!hz h)r rMr'rBrcrrUrVrrOr;r;r< test_memoryview_compare_ndim_one sl             z3TestBufferProtocol.test_memoryview_compare_ndim_onecCstddgdgdd}tddgdgdd}t|}t|}|||||||||||||||tddgdgdd}tddgdgd d}t|}t|}|||||||||||||||dS) Nr{rr^ri|i?z= h0cz@ ir rMr'r_r;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)rararararrSrrarrrv)raraz@ Liz!L hrbr_r;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) NrerErMr?rrrwr)rQrqrcr rMr'ra) rrrVr8rnrCrrrDrlr;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)Nr2rrrr^rrrrr*rz=f q xxL)g333333r*rz< f 2Qrrrr4z! b B xLz = Qx l xxLrr rrarMr'rBr_r;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)Nrfr2)rrrr^rrrrrrrr=Qq)rrz=qQrrrgZ0llrG)r rrar5rMr'rBr_r;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)#Nrfr2rg@lrrr)gffffff r:r)grpr:rzd b czd h c(r3@Irr*rrXr)rr$rWrz=iiz>iirrr)rryrrrZb3sZi3s)rrarr r5rMr'rB) rlst1lst2rUrVrrOrgrhr;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@irjr_r;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)Nrar)rrrrSrr2rrvrrerrz=lQrzQqrrrrg)r}rZBbrtr)shellor|r*)rrrZ5s0sP)ssushir|r*rfr2ror)ssashimisslicedg 4@)sramensspicygfffff"@rWz < 10p 9p dz > 10p 9p d) r rrarcrMr'rBrrr5) rrgrUrhrVrrOrurvr;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)rrrrrrrrr*rr)r'r%r#ZQLH)r*r*r*)r rr5rMr'rB)r byteorderrXr?rFrr;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(rrrrrr rHrJrL)rKrMrrQrrr rrrrrr__getattribute__r'rB)rrFrDrPrrirjr;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)NrXrr^rr*z>hQiLlcsg|]}qSr;r;rrtr;r<r{er|z>TestBufferProtocol.test_memoryview_tobytes..rTrz=hQiLlcsg|]}qSr;r;rrr;r<r{jr|r)rrrrrz.BEPointrXr?NrMr;r;r;r<rssrrr)r rMr'rrar rrR)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:rrprrrrVr;rrrrrer*rr3rrrrrrrrrTrrZ)rr)rrrt)rQrr6r7r1Z PyBUF_WRITEr r'rrrrrrarr5rcr8rH)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)NrrtrrrTrr) r5rdrKrMioBytesIOrreadintor'rr rra)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)NrTr rrrrXrrgrZrr)rrrrrrrrrrrz= Lz< h) rRrrarMr'rrr r5rcrQr__hash__) rrrDZmcZmxrrXrFr?r;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)NrprErrrZ)rrrr4r3rrcSs,t|}Wdn1s0YdSr)rM)rrjr;r;r<catch22s z;TestBufferProtocol.test_memoryview_release..catch22ryrr7rTrrrr*rh)rrrr8rr9)rrMr rQr1rr; ND_REDIRECTr&rWrrr'rrordrra)rrFrDrrUrVrirjrXr?rrrrr+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|qSrr;rSr;r;r<r{r|z?TestBufferProtocol.test_memoryview_redirect..rTrrcSsg|] }d|qSrr;rSr;r;r<r{r|rr*r) r rarKr;rrMr&rWr')rrrFrXr?rrDr;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*rrrrrr4r3rVrerWr*rrTrTrrDr)Z legacy_mode) rQrZ staticarrayrMr@rar'r r;r&rWr)rr8rbrXr?rrrDr;r;r<$test_memoryview_from_static_exporters                z7TestBufferProtocol.test_memoryview_from_static_exportercCs*tgddgttBd}|tt|dS)Nrprrn)r reZND_GETBUF_UNDEFINEDrQr1rMrr;r;r<#test_memoryview_getbuffer_undefined>sz6TestBufferProtocol.test_memoryview_getbuffer_undefinedcCs&tgddgtd}|tt|dS)NrprrZ)r rerQr1rM)rrXr;r;r<test_issue_7385Dsz"TestBufferProtocol.test_issue_7385cCs&dD]}|t|t|qdS)N)r>iiZ3s)r' _testcapiZPyBuffer_SizeFromFormatr5rd)rrr;r;r<test_pybuffer_size_from_formatHs z1TestBufferProtocol.test_pybuffer_size_from_formatN)F)Urrrrr@rRrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrrr" contextlibcontextmanagerr'r5r?rArGrQrWr`rcrdrerkrnrwrxryr{r}rrrrrrrrrrZ 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 itertoolsrrrandomrrrwarningsr rKrosZdecimalrZ fractionsr Z _testbuffer ImportErrorr r5r ZEnvironmentVarGuardenvironcatch_warningsrLrrZ SHORT_TESTZNATIVEr6r7ZSTANDARDr=rQr8copyrwZARRAYrZBYTEFMTrgrirfrYr`rcrprqrrrurvrxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrZ skipUnlessZTestCaserrmainr;r;r;r<sf              P                        ?  +  !     o