o 6aj@sdZddlZddlZddlZddlZddlZddlmZmZm Z zddl Z Wn e y1dZ Ynwe dkZ ejdkr@dZndZd d Zd d Zd dZddZedZedZedZdeiZddZGdddZddZde_GdddZGdddZdd Zd!d"Z d#d$Z!d%d&Z"id'd'd(d)d*d*d+d+d,d,d-d-d.d.d/d/d0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d9d:d;dd?d@Z#dA$e#%Z&id'd'd(d)d*d*d+d+d,dBd-dCd.dDd/dEd0dDd1dEd2dFd3dGd4dHd5d5d6d6d8d9dId:dd?dJZ'dA$e'%Z(dKdLdMdNdOZ)GdPdQdQZ*dRdSZ+dTdUZ,dVdWZ-dXdYZ.dZd[Z/d\d]Z0d^d_Z1d`daZ2dbdcZ3dddeZ4dfdgZ5GdhdidiZ6dS)jzI A place for internal code Some things are more easily handled Python. N)dtypearrayndarrayPyPylittle<>c Csg}|D]J\}}t|}t|tr|dvrtd|dkr&|d|kr&qt|d}|dkr4tdt|d|d}|dkrE|d}nd}|||||fq|jdd d d d |D} d d |D} dd |D} dd |D} | | | | fS)N)zentry not a 2- or 3- tupler rrzinvalid offset.)aligncSs|dS)Nr )xr r 6/usr/lib/python3/dist-packages/numpy/core/_internal.py-sz!_makenames_list..)keycSg|]}|dqSrr .0rr r r .z#_makenames_list..cSrrr rr r rr/rcSr)r r rr r rr0rcSr)r r rr r rr1r) itemslen isinstancetuple ValueErrorintrappendsort) adictr allfieldsfnameobjnnumformattitlenamesformatsoffsetstitlesr r r_makenames_lists*   r-cCsz|d}Wn tyd}Ynw|dur t||\}}}}n0g}g}g}|D]'}||}||d||dt|dkrJ||dq(|dq(t||||d|S)Nrrr )r)r*r+r,)KeyErrorr-rrr)r!r r)r*r+r,nameresr r r _usefields8s0    r2c sz|jdur/|j}|dur%|jdur|jS|j}|r"|j|fS|jSt|d|dfS|j}fdd|D}g}d}|D]e}|d|kr^|d|}|dd|f||7}n |d|krhtdt |dkrw|d |df} n|d } |djr| t|djd|djdf} n| t|df} ||dj 7}|| qA|j |kr|j |}|dd|f|S) Nrrcsg|] }||fqSr r rfieldsr rriz _array_descr..z|VzLdtype.descr is not defined for types with overlapping or out-of-order fieldsr r ) r4subdtypemetadatastrcopy _array_descrr)rrritemsize) descriptorr7newr)ordered_fieldsresultoffsetfieldr&r0tupr r3rr;XsL             r;cCst|||SN)r__new__)subtypeshaperr r r _reconstructsrHzw(?P[<>|=]?)(?P *[(]?[ ,0-9]*[)]? *)(?P[<>|=]?)(?P[A-Za-z0-9.?]*(?:\[[a-zA-Z0-9,.]+\])?)z\s*,\s*z\s+$=c Cshd}g}|t|krtj||d}z |\}}}}Wnttfy4tdt|dd|ddw|}|t|krdtj||drKt|}nt j||d}|s`tdt|d|f|}|dkrk|}n!|dkrr|}nt ||}t ||}||krtd ||f|}|d d t fvrd}||}|dkr|} n|t |f} || |t|ks |S) Nr)poszformat number rz of "z" is not recognizedz*format number %d of "%s" is not recognizedr6z/inconsistent byte-order specification %s and %s|rI)r format_rematchgroups TypeErrorAttributeErrorrendspace_resep_re _convorderget_nboast literal_evalr) astr startindexr@moorder1repeatsorder2rordernewitemr r r _commastrings^       +rac@s4eZdZddZddZddZddZd d Zd S) dummy_ctypecC ||_dSrD_cls)selfclsr r r__init__ zdummy_ctype.__init__cCs|SrDr rfotherr r r__mul__szdummy_ctype.__mul__cGs ||SrDrdrjr r r__call__rizdummy_ctype.__call__cCs |j|jkSrDrdrjr r r__eq__ zdummy_ctype.__eq__cCs |j|jkSrDrdrjr r r__ne__rozdummy_ctype.__ne__N)__name__ __module__ __qualname__rhrlrmrnrpr r r rrbs  rbcCsxtj}|dur |Stdurddl}t|j}n tdj}|dkr$tj}n|dkr,tj }n |dkr4tj }ntj }|t_|S)Nrpilq) _getintp_ctypecachectypesnumpyrbintprcharc_intc_long c_longlong)valnpr}r r rrxs   rxc@s"eZdZddZGdddZdS)_missing_ctypescCs|jSrDvalue)rfr&r$r r rcastsz_missing_ctypes.castc@seZdZddZdS)z_missing_ctypes.c_void_pcCrcrDr)rfptrr r rrhriz!_missing_ctypes.c_void_p.__init__N)rqrrrsrhr r r rc_void_ps rN)rqrrrsrrr r r rrsrc@s~eZdZdddZddZddZdd Zed d Zed d Z eddZ eddZ ddZ ddZ ddZddZdS)_ctypesNcCs^||_trt|_|j||_nt|_|j||_||j_|jjdkr*d|_dSd|_dS)NrTF) _arrrzrr_datar_objectsndim_zerod)rfrrr r rrhs   z_ctypes.__init__cCs|j|j|}|j|_|S)a Return the data pointer cast to a particular c-types object. For example, calling ``self._as_parameter_`` is equivalent to ``self.data_as(ctypes.c_void_p)``. Perhaps you want to use the data as a pointer to a ctypes array of floating-point data: ``self.data_as(ctypes.POINTER(ctypes.c_double))``. The returned pointer will keep a reference to the array. )rrrr)rfr$rr r rdata_as sz_ctypes.data_ascC|jrdS||jj|jjS)z Return the shape tuple as an array of some other c-types type. For example: ``self.shape_as(ctypes.c_short)``. N)rrrrGrfr$r r rshape_asz_ctypes.shape_ascCr)z Return the strides tuple as an array of some other c-types type. For example: ``self.strides_as(ctypes.c_longlong)``. N)rrrstridesrr r r strides_as'rz_ctypes.strides_ascCs|jjS)a A pointer to the memory area of the array as a Python integer. This memory area may contain data that is not aligned, or not in correct byte-order. The memory area may not even be writeable. The array flags and data-type of this array should be respected when passing this attribute to arbitrary C-code to avoid trouble that can include Python crashing. User Beware! The value of this attribute is exactly the same as ``self._array_interface_['data'][0]``. Note that unlike ``data_as``, a reference will not be kept to the array: code like ``ctypes.c_void_p((a + b).ctypes.data)`` will result in a pointer to a deallocated array, and should be spelt ``(a + b).ctypes.data_as(ctypes.c_void_p)`` )rrrfr r rdata0sz _ctypes.datacC |tS)a (c_intp*self.ndim): A ctypes array of length self.ndim where the basetype is the C-integer corresponding to ``dtype('p')`` on this platform. This base-type could be `ctypes.c_int`, `ctypes.c_long`, or `ctypes.c_longlong` depending on the platform. The c_intp type is defined accordingly in `numpy.ctypeslib`. The ctypes array contains the shape of the underlying array. )rrxrr r rrGBs z _ctypes.shapecCr)aj (c_intp*self.ndim): A ctypes array of length self.ndim where the basetype is the same as for the shape attribute. This ctypes array contains the strides information from the underlying array. This strides information is important for showing how many bytes must be jumped to get to the next element in the array. )rrxrr r rrNs z_ctypes.stridescCs |tjS)ze Overrides the ctypes semi-magic method Enables `c_func(some_array.ctypes)` )rrzrrr r r_as_parameter_Ys z_ctypes._as_parameter_cCtjdtdd|jS)zYDeprecated getter for the `_ctypes.data` property. .. deprecated:: 1.21 z,"get_data" is deprecated. Use "data" insteadr  stacklevel)warningswarnDeprecationWarningrrr r rget_datadz_ctypes.get_datacCr)zZDeprecated getter for the `_ctypes.shape` property. .. deprecated:: 1.21 z."get_shape" is deprecated. Use "shape" insteadr r)rrrrGrr r r get_shapemrz_ctypes.get_shapecCr)z\Deprecated getter for the `_ctypes.strides` property. .. deprecated:: 1.21 z2"get_strides" is deprecated. Use "strides" insteadr r)rrrrrr r r get_stridesvrz_ctypes.get_stridescCr)zcDeprecated getter for the `_ctypes._as_parameter_` property. .. deprecated:: 1.21 z>"get_as_parameter" is deprecated. Use "_as_parameter_" insteadr r)rrrrrr r rget_as_parameters z_ctypes.get_as_parameterrD)rqrrrsrhrrrpropertyrrGrrrrrrr r r rrs"         rc Cs|j}t|}t|tr|g}t}t|ttfrN|D]*}z||Wnty?||vr7td|dtd|dw||qtt||Std|)zf Given a datatype and an order object, return a new names tuple, with the order indicated zduplicate field name: Nzunknown field name: zunsupported order value: ) r)listrr9setrremoveradd)datatyper_oldnames nameslistseenr0r r r _newnamess"   rcs0|jjfddjDd}t||ddS)aReturn copy of structured array with padding between fields removed. Parameters ---------- ary : ndarray Structured array from which to remove padding bytes Returns ------- ary_copy : ndarray Copy of ary with padding bytes removed csg|] }j|dqSrr3rr0dtr rrr5z _copy_fields..)r)r*T)rr:)rr)r)ary copy_dtyper rr _copy_fieldss  rcCsl|js|jr4|dkr||krdS|jdur0|jD]}|j|d|kr/|j|d|kr/dSqtddS)a Checks safety of getfield for object arrays. As in _view_is_safe, we need to check that memory containing objects is not reinterpreted as a non-object datatype and vice versa. Parameters ---------- oldtype : data-type Data type of the original ndarray. newtype : data-type Data type of the field being accessed by ndarray.getfield offset : int Offset of the field being accessed by ndarray.getfield Raises ------ TypeError If the field access is invalid rNrz'Cannot get/set field of an object array) hasobjectr)r4rO)oldtypenewtyperAr0r r r_getfield_is_safes   rcCs$||krdS|js |jrtddS)az Checks safety of a view involving object arrays, for example when doing:: np.zeros(10, dtype=oldtype).view(newtype) Parameters ---------- oldtype : data-type Data type of original ndarray newtype : data-type Data type of the view Raises ------ TypeError If the new type is incompatible with the old type. Nz)Cannot change data-type for object array.)rrO)rrr r r _view_is_safes  r?cS1bBhHruIrvLrwQefdgZfFDGSUOV)ZdZgswrrr6i2u2i4u4i8u8f2r)rrrrz UCS-2 stringspointers bitfieldszfunction pointers)u&tXc@s@eZdZddZddZddZddZed d Zd d Z d S)_StreamcCs||_d|_dS)N@)r byteorder)rfrr r rrh0s z_Stream.__init__cCs"|jd|}|j|d|_|SrDr)rfr%r1r r radvance4sz_Stream.advancecCs,|jdt||kr|t|dSdS)NTF)rrr)rfrr r rconsume9sz_Stream.consumecCs~t|r+d}|t|jkr&||j|s&|d}|t|jkr&||j|r||S|j|}||}|t||S)Nrr)callablerrrindex)rfrrur1r r r consume_until?s   z_Stream.consume_untilcCs |jdS)Nrrrr r rnextKs z _Stream.nextcCs t|jSrD)boolrrr r r__bool__Oriz_Stream.__bool__N) rqrrrsrhrrrrrrr r r rr/s  rcCst|}t|dd\}}|S)NF is_subdtype)r__dtype_from_pep3118)specstreamrr r r r_dtype_from_pep3118SsrcCsZtgggdd}d}d}d}|rfd}|drnLd}|dr0|d}ttt|d}|jd vrC|d}|d kr@d }||_ |j d vrMt } t } nt } t } |d d} | r_t| } nd} d}|drqt|dd\}} nX|j| vr|jdkr|d}n|d}|dk}| |}|dvr|d| 7}d} ddd |j |j }t||}|j} n|jtvrt|j}td|j|td|jd}|j dkr| | }|j | }||7}|dkr| dks|durt|dkrt||}n||7}t| |}| dkr t|| ff}|durt||f}|dr$|d}nd}|r.|dusW|durB||dvrBtd|d|d||d||d |||j7}||7}||d!<|s|j dkrw|d!| |7<|ddgkr|d ddkr|d!|ddjkr|s|dd}||fSt|t|}||fS)"Nrr)r*r+r<rF}(),)rrIrr ^!rr )rrcSs | SrD)isdigit)rr r rrs z&__dtype_from_pep3118..zT{TrZr rUSVz%drIz,Unrepresentable PEP 3118 data type {!r} ({})z'Unknown PEP 3118 data type specifier %rr:r)zDuplicate field name 'z' in PEP3118 formatr*r+r<)dictrrrmaprsplitrrr_pep3118_native_map_pep3118_native_typechars_pep3118_standard_map_pep3118_standard_typecharsrrUr alignment_pep3118_unsupported_mapNotImplementedErrorr'rrr<_prod_add_trailing_padding_lcm RuntimeErrorr _fix_names)rr field_specrAcommon_alignment is_paddingrrGrtype_maptype_map_chars itemsize_strr<r typechar dtypecharnumpy_byteorderdesc extra_offset start_padding intra_paddingr0retr r rrXs                              t rcCsT|d}t|D]\}}|durqd} d|}||vrn|d}q|||<qdS)z< Replace names which are None with the next unused f%d name r)NrTrr) enumerate)r r)rur0jr r rr s  r csv|jdurtdg|gdg|jd}n|j|j}t|fdd|Dfdd|D|jd}|d|7<t|S) zBInject the specified number of padding bytes at the end of a dtypeNf0rrcg|]}|dqSrr rr3r rrz)_add_trailing_padding..crrr rr3r rrrr<)r4rr<r)r)rpaddingr r)r r3rr s" r cCsd}|D]}||9}q|S)Nrr )artrr r rr s rcCs|r |||}}|s|S)z0Calculate the greatest common divisor of a and br r!rr r r_gcdsr#cCs|t|||SrD)r#r"r r rr sr cOsZddd|Ddd|D}||dd}ddd|D}d ||||S) = Format the error message for when __array_ufunc__ gives up. , cSsg|]}d|qS)z{!r}r'rargr r rrsz0array_ufunc_errmsg_formatter..cSsg|] \}}d||qS)z{}={!r}r&)rkvr r rrsoutr css|] }tt|jVqdSrD)reprtyperqr'r r r "z/array_ufunc_errmsg_formatter..zToperand type(s) all returned NotImplemented from __array_ufunc__({!r}, {!r}, {}): {})joinrrUr')dummyufuncmethodinputskwargs args_stringargs types_stringr r rarray_ufunc_errmsg_formatters r9cCs d|j|j}d|t|S)r$z{}.{}zOno implementation found for '{}' on types that implement __array_function__: {})r'rrrqr) public_apitypes func_namer r rarray_function_errmsg_formatter(s r=cCs|jdkrd}n dddt|jD}|jdkrd}n|jdkr%d}nd jdd dt|jDtd |jd }d }|jdurJd|d}n|d7}dj|j|||dS)zz Builds a signature string which resembles PEP 457 This is used to construct the first line of the docstring rrr%css|] }d|dVqdS)rrNr rrur r rr.:r/z1_ufunc_doc_signature_formatter..rz , /, out=()z , /, out=Nonez%[, {positional}], / [, out={default}]css|] }d|dVqdS)zout{}rNr&r>r r rr.Cs rD) positionaldefaultz8, casting='same_kind', order='K', dtype=None, subok=TrueNz , where=Truez[, signature, extobj]z![, signature, extobj, axes, axis]z&{name}({in_args}{out_args}, *{kwargs}))r0in_argsout_argsr5)ninr0rangenoutr'r, signaturerq)r2rArBr5r r r_ufunc_doc_signature_formatter/s0      rGcCs<ztr |jd}n|jd}d|jvWStyYdSw)NrF)IS_PYPY__mro__rr Exception)rg ctype_baser r rnpy_ctypes_check_s    rNc@s eZdZdZddZddZdS) recursivea A decorator class for recursive nested functions. Naive recursive nested functions hold a reference to themselves: def outer(*args): def stringify_leaky(arg0, *arg1): if len(arg1) > 0: return stringify_leaky(*arg1) # <- HERE return str(arg0) stringify_leaky(*args) This design pattern creates a reference cycle that is difficult for a garbage collector to resolve. The decorator class prevents the cycle by passing the nested function in as an argument `self`: def outer(*args): @recursive def stringify(self, arg0, *arg1): if len(arg1) > 0: return self(*arg1) return str(arg0) stringify(*args) cCrcrDfunc)rfrQr r rrhrizrecursive.__init__cOs|j|g|Ri|SrDrP)rfr7r5r r rrmszrecursive.__call__N)rqrrrs__doc__rhrmr r r rrOqs rO)7rRrWresysplatformr multiarrayrrrrz ImportErrorpython_implementationrJrrVr-r2r;rHcompilerLrSrRrTrarbrxryrrrrrrrr0keysrrrrrrrr r rr#r r9r=rGrNrOr r r rs     4   0              $ 0