ó 2ÄÈ[c@`sédZddlmZmZmZddddddd gZdd lZdd lZdd lm Z m Z m Z m Z mZdd lmZmZydd lZWnek r¼d ZnXed krd „ZeZeZeZeZddlmZeZnEdd ljj Z!e!j"ƒZ[!ej#Zd„Ze eddƒZd„Z$dddddddgZ%d„Z&defd„ƒYZ'iZ(d d d d d„Z)d„Z*d„Z+ed k råe*ƒZ,d d„Zd„Znd S( s% ============================ ``ctypes`` Utility Functions ============================ See Also --------- load_library : Load a C library. ndpointer : Array restype/argtype with verification. as_ctypes : Create a ctypes array from an ndarray. as_array : Create an ndarray from a ctypes array. References ---------- .. [1] "SciPy Cookbook: ctypes", http://www.scipy.org/Cookbook/Ctypes Examples -------- Load the C library: >>> _lib = np.ctypeslib.load_library('libmystuff', '.') #doctest: +SKIP Our result type, an ndarray that must be of type double, be 1-dimensional and is C-contiguous in memory: >>> array_1d_double = np.ctypeslib.ndpointer( ... dtype=np.double, ... ndim=1, flags='CONTIGUOUS') #doctest: +SKIP Our C-function typically takes an array and updates its values in-place. For example:: void foo_func(double* x, int length) { int i; for (i = 0; i < length; i++) { x[i] = i*i; } } We wrap it using: >>> _lib.foo_func.restype = None #doctest: +SKIP >>> _lib.foo_func.argtypes = [array_1d_double, c_int] #doctest: +SKIP Then, we're ready to call ``foo_func``: >>> out = np.empty(15, dtype=np.double) >>> _lib.foo_func(out, len(out)) #doctest: +SKIP i(tdivisiontabsolute_importtprint_functiont load_libraryt ndpointerttesttctypes_load_librarytc_intpt as_ctypestas_arrayN(tintegertndarraytdtypet deprecatetarray(t _flagdicttflagsobjcO`stdƒ‚dS(s± Dummy object that raises an ImportError if ctypes is not available. Raises ------ ImportError If ctypes is not available. sctypes is not available.N(t ImportError(targstkwds((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_dummyCs (tintpc C`satjdkr1ddl}|jdddƒntjj|ƒd}|s¨ddlm}|ƒ}||g}|d t ƒ}||ks±|j d||ƒq±n |g}tjj |ƒ}tjj |ƒsêtjj |ƒ}n|}x^|D]V} tjj|| ƒ} tjj| ƒr÷ytj| SWqMtk rI‚qMXq÷q÷Wtd ƒ‚dS( sj It is possible to load a library using >>> lib = ctypes.cdll[] But there are cross-platform considerations, such as library file extensions, plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience. Parameters ---------- libname : str Name of the library, which can have 'lib' as a prefix, but without an extension. loader_path : str Where the library can be found. Returns ------- ctypes.cdll[libpath] : library object A ctypes library object Raises ------ OSError If there is no library with the expected extension, or the library is defective and cannot be loaded. s1.0.1iNsAAll features of ctypes interface may not work with ctypes < 1.0.1t stacklevelii(tget_shared_lib_extensiont is_python_extsno file with expected extension(tctypest __version__twarningstwarntostpathtsplitexttnumpy.distutils.misc_utilRtTruetinserttabspathtisdirtdirnametjointexiststcdlltOSError( tlibnamet loader_pathRtextRtso_extt libname_exttso_ext2tlibdirtlntlibpath((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyR[s2         cC`s)d}x|D]}|t|7}q W|S(Ni(R(tflaglisttnumtval((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_num_fromflags s t C_CONTIGUOUSt F_CONTIGUOUStALIGNEDt WRITEABLEtOWNDATAt UPDATEIFCOPYtWRITEBACKIFCOPYcC`s?g}x2tD]*}t|}||@r |j|ƒq q W|S(N(t _flagnamesRtappend(R4trestkeytvalue((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_flags_fromnum¨s    t_ndptrcB`s/eZd„Zed„ƒZed„ƒZRS(cC`s t|ƒS(s©This method is called when this class is used as the .restype attribute for a shared-library function. It constructs a numpy array from a void pointer.(R(tself((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_check_retval_³scC`sUi|jjd6|d6dd6|jd6dd6|jjddd 6|jtfd 6S( Ntdescrt__reftstridestshapeitversioniittypestrtdata(t_dtype_RGtNonet_shape_RBtFalse(RE((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt__array_interface__¹s cC`st|tƒstdƒ‚n|jdk rU|j|jkrUtd|jƒ‚n|jdk rŒ|j|jkrŒtd|jƒ‚n|jdk rÉ|j |jkrÉtdt |jƒƒ‚n|j dk r|j j |j @|j krtdt|j ƒƒ‚n|jS(Nsargument must be an ndarraysarray must have data type %ssarray must have %d dimension(s)sarray must have shape %ssarray must have flags %s(t isinstanceR t TypeErrorRNROR t_ndim_tndimRPRJtstrt_flags_tflagsR4RCR(tclstobj((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt from_paramÄs (t__name__t __module__RFtpropertyRRt classmethodR\(((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyRD±s  c C`s‡|d k rt|ƒ}nd }|d k rt|tƒrN|jdƒ}nQt|ttfƒrx|}t|ƒ}n't|tƒrŸ|j }t|ƒ}n|d kry)g|D]}|j ƒj ƒ^qµ}Wnt k rót dƒ‚nXt|ƒ}qnyt||||fSWntk r1nX|d krGd}n'|jrett|ƒƒ}n |j}|d k r‹|d|7}n|d k r y#g|D]}t|ƒ^q¡}Wn)t k råt|ƒg}|f}nXt|ƒ}|ddj|ƒ7}n|d k r2|ddj|ƒ7}ng}td|tfi|d6|d 6|d 6|d 6ƒ}|t||||f<|S( sc Array-checking restype/argtypes. An ndpointer instance is used to describe an ndarray in restypes and argtypes specifications. This approach is more flexible than using, for example, ``POINTER(c_double)``, since several restrictions can be specified, which are verified upon calling the ctypes function. These include data type, number of dimensions, shape and flags. If a given array does not satisfy the specified restrictions, a ``TypeError`` is raised. Parameters ---------- dtype : data-type, optional Array data-type. ndim : int, optional Number of array dimensions. shape : tuple of ints, optional Array shape. flags : str or tuple of str Array flags; may be one or more of: - C_CONTIGUOUS / C / CONTIGUOUS - F_CONTIGUOUS / F / FORTRAN - OWNDATA / O - WRITEABLE / W - ALIGNED / A - WRITEBACKIFCOPY / X - UPDATEIFCOPY / U Returns ------- klass : ndpointer type object A type object, which is an ``_ndtpr`` instance containing dtype, ndim, shape and flags information. Raises ------ TypeError If a given array does not satisfy the specified restrictions. Examples -------- >>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, ... ndim=1, ... flags='C_CONTIGUOUS')] ... #doctest: +SKIP >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64)) ... #doctest: +SKIP t,sinvalid flags specificationtanys_%ddt_txs ndpointer_%sRNRPRURXN(ROt_dtypeRSRWtsplittintR RCRR4tstriptuppert ExceptionRTR6t_pointer_type_cachetKeyErrortnamestidttupleR&ttypeRD( R RVRJRYR4Rdtnametstrshapetklass((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyRÛs^5    )        #      c C`sbt}|j|j|j|j|j|j|j|j|j |j |j |j g }d„|DƒS(sI Return a dictionary mapping __array_interface__ formats to ctypes types cS`s"i|]}|t|ƒj“qS((ReRW(t.0tctype((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pys Ls ( Rtc_bytetc_shorttc_inttc_longt c_longlongtc_ubytetc_ushorttc_uinttc_ulongt c_ulonglongtc_floattc_double(tctt simple_types((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_get_typecodesCs cC`s,x%|ddd…D]}||}qW|S(s7 Create an ndarray of the given element type and shape Niÿÿÿÿ((t element_typeRJtdim((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt_ctype_ndarrayOscC`spt|tjƒr`|dkr-tdƒ‚ntjt|j|ƒƒ}tj||ƒj }nt |dt ƒS(s" Create a numpy array from a ctypes array or POINTER. The numpy array shares the memory with the ctypes object. The shape parameter must be given if converting from a ctypes POINTER. The shape parameter is ignored if converting from a ctypes array s=as_array() requires a shape argument when called on a pointertcopyN( RSRt_PointerRORTtPOINTERR‡t_type_tcasttcontentsRRQ(R[RJt p_arr_type((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyR Ys   cC`s|j}|dr"tdƒ‚n|ddkrAtdƒ‚n|d\}}|rftdƒ‚ntt|d|d ƒ}|j|ƒ}||_|S( s‚Create and return a ctypes object from a numpy array. Actually anything that exposes the __array_interface__ is accepted.RIsstrided arrays not supportedRKis,only __array_interface__ version 3 supportedRMsreadonly arrays unsupportedRLRJ(RRRTR‡t _typecodest from_addresst__keep(R[taitaddrtreadonlyttptresult((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyRms   (-t__doc__t __future__RRRt__all__tsysRtnumpyR R R ReR Rtnumpy.core.multiarrayRRRRRORRRRR RRtobjectt _ndptr_basetnumpy.core._internaltcoret _internaltnict_getintp_ctypetc_void_pR6R>RCRDRkRR„R‡R(((s./tmp/pip-build-fiC0ax/numpy/numpy/ctypeslib.pyt3sL (        B     )h