• File: ctypeslib.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/proc/self/root/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyc
  • Date Modified: 12/19/2017 2:16 PM
  • File size: 13.91 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit

9Zc@`sdZddlmZmZmZddddddd	gZdd
lZdd
lZddlm	Z	m
Z
mZm
Z
mZddlmZmZydd
lZWnek
rd
ZnXed
krd
ZeZeZeZeZddlmZeZnEdd
ljj Z!e!j"Z[!ej#ZdZe
eddZdZ$ddddddgZ%dZ&defdYZ'iZ(d
d
d
d
dZ)ed
k	reZ*iZ+dZ,e*j-e*j.e*j/e*j0e*j1fdfe*j2e*j3e*j4e*j5e*j6fdfe*j7e*j8fdfgZ9xEe9D]=\Z:Z;x.e:D]&Z<e,e<de;e*j=e<fqGWq4We>e*j/d Z?d!Z@d"ZAd
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`stddS(s
        Dummy object that raises an ImportError if ctypes is not available.

        Raises
        ------
        ImportError
            If ctypes is not available.

        sctypes is not available.N(tImportError(targstkwds((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt_dummyCs
(tintpcC`s[tjdkr+ddl}|jdntjj|d}|sddlm}|}||g}|dt	}||ks|j
d||qn	|g}tjj|}tjj|stjj
|}n|}x^|D]V}	tjj||	}
tjj|
rytj|
SWqGtk
rCqGXqqWtddS(	sj
        It is possible to load a library using 
        >>> lib = ctypes.cdll[<full_path_name>]

        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.1i(tget_shared_lib_extensiont
is_python_extsno file with expected extension(tctypest__version__twarningstwarntostpathtsplitexttnumpy.distutils.misc_utilRtTruetinserttabspathtisdirtdirnametjointexiststcdlltOSError(tlibnametloader_pathRtextRtso_exttlibname_exttso_ext2tlibdirtlntlibpath((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyR[s0	
	

cC`s)d}x|D]}|t|7}q
W|S(Ni(R(tflaglisttnumtval((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt_num_fromflagss
tC_CONTIGUOUStF_CONTIGUOUStALIGNEDt	WRITEABLEtOWNDATAtUPDATEIFCOPYcC`s?g}x2tD]*}t|}||@r
|j|q
q
W|S(N(t
_flagnamesRtappend(R3trestkeytvalue((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt_flags_fromnums


t_ndptrcB`s/eZdZedZedZRS(cC`s
t|S(sThis method is called when this class is used as the .restype
        asttribute for a shared-library function.   It constructs a numpy
        array from a void pointer.(R(tself((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt_check_retval_scC`sUi|jjd6|d6dd6|jd6dd6|jjddd	6|jtfd
6S(Ntdescrt__reftstridestshapeitversioniittypestrtdata(t_dtype_REtNonet_shape_R@tFalse(RC((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt__array_interface__s
cC`st|tstdn|jdk	rU|j|jkrUtd|jn|jdk	r|j|jkrtd|jn|jdk	r|j	|jkrtdt
|jn|jdk	r|jj
|j@|jkrtdt|jn|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
isinstanceRt	TypeErrorRLRMRt_ndim_tndimRNRHtstrt_flags_tflagsR3RAR(tclstobj((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt
from_params (t__name__t
__module__RDtpropertyRPtclassmethodRZ(((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyRBs	c	C`sq|dk	rt|}nd}|dk	rt|trN|jd}nQt|ttfrx|}t|}n't|tr|j	}t|}n|dkry)g|D]}|j
j^q}WntdnXt
|}qnyt||||fSWntk
r'nX|dkr=d}n'|jr[tt|}n	|j}|dk	r|d|7}n|dk	ry#g|D]}t|^q}Wn)tk
rt|g}|f}nXt|}|ddj|7}n|dk	r(|ddj|7}ng}td|tfi|d6|d	6|d
6|d6}|t|<|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
          - 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_%sRLRNRSRVN(RMt_dtypeRQRUtsplittintR
RARR3tstriptupperRRR5t_pointer_type_cachetKeyErrortnamestidttupleR%ttypeRB(	RRTRHRWR3Rbtnametstrshapetklass((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyRs^4	)
			#




c`s^y|jWntk
rnXdSt|j|t<fd}t||_dS(sGiven a ctypes simple type, construct and attach an
        __array_interface__ property to it if it does not yet have one.
        Nc`sMidfgd6|d6dd6d
d6dd6d6tj|tfd	6S(NtRERFRGRHiRIRJRK((RMtctt	addressofRO(RC(RJ(sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyRPUs(RPtAttributeErrorRcRUt
_typecodesR](tsimple_typeRRP((RJsB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pytprep_simpleJs


titutfs%c%dic`sy|jWntk
rnXdSg|}x/t|tkr`j|j|j}q2Wt|j}|d|dfd}t||_dS(sGiven a ctypes array type, construct and attach an
        __array_interface__ property to it if it does not yet have one.
        NRERJc`sDid6|d6dd6d6dd6d6tj|tfd6S(	NRERFRGRHiRIRJRK(RMRrRsRO(RC(RERHRJ(sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyRPs
(	RPRtRmt_ARRAY_TYPER=t_length_t_type_RlR](t
array_typetobtaiRP((RERHRJsB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt
prep_arrayqs 




cC`sy|jWntk
rnXdS|j}tt|}idd6|jd6tj|tfd6|d6}||_dS(sGiven a ctypes pointer object, construct and
        attach an __array_interface__ property to it if it does not
        yet have one.
        NiRIRJRKRH(	RPRttcontentsRcRmRURrRsRO(tpointer_objRHRRtinter((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pytprep_pointers
	


cC`sdt|}y|jWn:tk
rSt|drFt||qTt|nXt|dtS(sCreate a numpy array from a ctypes array or a ctypes POINTER.
        The numpy array shares the memory with the ctypes object.

        The size parameter must be given if converting from a ctypes POINTER.
        The size parameter is ignored if converting from a ctypes array
        Rtcopy(RmRPRtthasattrRRRRO(RYRHttp((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyR	s
cC`s|j}|dr"tdn|ddkrAtdn|d\}}|rftdnt|d}x)|d	d
d
dD]}||}qW|j|}||_|S(sCreate and return a ctypes object from a numpy array.  Actually
        anything that exposes the __array_interface__ is accepted.RGsstrided arrays not supportedRIis,only __array_interface__ version 3 supportedRKsreadonly arrays unsupportedRJRHNi(RPRRRutfrom_addresst__keep(RYRtaddrtreadonlyRtdimtresult((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyRs	
	(Bt__doc__t
__future__RRRt__all__tsysRtnumpyR
RRRcR
Rtnumpy.core.multiarrayRRRRRMRRRRR	RRtobjectt_ndptr_basetnumpy.core._internaltcoret	_internaltnict_getintp_ctypetc_void_pR5R<RARBRhRRrRuRwtc_bytetc_shorttc_inttc_longt
c_longlongtc_ubytetc_ushorttc_uinttc_ulongtc_ulonglongtc_floattc_doubletsimple_typesttypestcodeRtsizeofRmR{RR(((sB/opt/alt/python27/lib64/python2.7/site-packages/numpy/ctypeslib.pyt<module>3s^(

				B					)f	''
(