numpy.ctypeslib
index
/usr/lib/python2.6/dist-packages/numpy/ctypeslib.py

============================
``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: +ignore
 
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: +ignore
 
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: +ignore
>>> lib.foo.argtypes = [array_1d_double, c_int] #DOCTEST: +ignore
 
Then, we're ready to call ``foo_func``:
 
>>> out = np.empty(15, dtype=np.double)
>>> _lib.foo_func(out, len(out)) #DOCTEST: +ignore

 
Modules
       
ctypes
ctypes
os
sys

 
Classes
       
object
_ndptr

 
class _ndptr(object)
     Class methods defined here:
from_param(cls, obj) from type

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
Functions
       
_flags_fromnum(num)
_num_fromflags(flaglist)
as_array(obj)
Create a numpy array from a ctypes array.  The numpy array
shares the memory with the ctypes object.
as_ctypes(obj)
Create and return a ctypes object from a numpy array.  Actually
anything that exposes the __array_interface__ is accepted.
load_library(libname, loader_path)
# Adapted from Albert Strasheim
ndpointer(dtype=None, ndim=None, shape=None, flags=None)
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 : string or tuple of strings
    Array flags; may be one or more of:
 
      - C_CONTIGUOUS / C / CONTIGUOUS
      - F_CONTIGUOUS / F / FORTRAN
      - OWNDATA / O
      - WRITEABLE / W
      - ALIGNED / A
      - UPDATEIFCOPY / U
 
Examples
--------
>>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=float64,
...                                                  ndim=1,
...                                                  flags='C_CONTIGUOUS')]
>>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64))
prep_array(array_type)
Given a ctypes array type, construct and attach an
__array_interface__ property to it if it does not yet have one.
prep_simple(simple_type, typestr)
Given a ctypes simple type, construct and attach an
__array_interface__ property to it if it does not yet have one.

 
Data
        TYPESTR = '<%c%d'
__all__ = ['load_library', 'ndpointer', 'test', 'ctypes_load_library', 'c_intp', 'as_ctypes', 'as_array']
__file__ = '/usr/lib/python2.6/dist-packages/numpy/ctypeslib.pyc'
__name__ = 'numpy.ctypeslib'
__package__ = 'numpy'
_flagdict = {'A': 256, 'ALIGNED': 256, 'C': 1, 'CONTIGUOUS': 1, 'C_CONTIGUOUS': 1, 'F': 2, 'FORTRAN': 2, 'F_CONTIGUOUS': 2, 'O': 4, 'OWNDATA': 4, ...}
_flagnames = ['C_CONTIGUOUS', 'F_CONTIGUOUS', 'ALIGNED', 'WRITEABLE', 'OWNDATA', 'UPDATEIFCOPY']
_pointer_type_cache = {}
_typecodes = {'<f4': <class 'ctypes.c_float'>, '<f8': <class 'ctypes.c_double'>, '<i1': <class 'ctypes.c_byte'>, '<i2': <class 'ctypes.c_short'>, '<i4': <class 'ctypes.c_int'>, '<i8': <class 'ctypes.c_long'>, '<u1': <class 'ctypes.c_ubyte'>, '<u2': <class 'ctypes.c_ushort'>, '<u4': <class 'ctypes.c_uint'>, '<u8': <class 'ctypes.c_ulong'>}
code = 'f'
simple_types = [((<class 'ctypes.c_byte'>, <class 'ctypes.c_short'>, <class 'ctypes.c_int'>, <class 'ctypes.c_long'>, <class 'ctypes.c_long'>), 'i'), ((<class 'ctypes.c_ubyte'>, <class 'ctypes.c_ushort'>, <class 'ctypes.c_uint'>, <class 'ctypes.c_ulong'>, <class 'ctypes.c_ulong'>), 'u'), ((<class 'ctypes.c_float'>, <class 'ctypes.c_double'>), 'f')]
types = (<class 'ctypes.c_float'>, <class 'ctypes.c_double'>)