C-Types Foreign Function Interface (numpy.ctypeslib)

  • numpy.ctypeslib.as_array(obj, shape=None)[source]open in new window

    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

  • numpy.ctypeslib.as_ctypes(obj)[source]open in new window

    Create and return a ctypes object from a numpy array. Actually anything that exposes the __array_interface__ is accepted.

  • numpy.ctypeslib.as_ctypes_type(dtype)[source]open in new window

    Convert a dtype into a ctypes type.

    Parameters:

    typedescription
    dtype : dtypeThe dtype to convert

    Returns:

    typedescription
    ctypeA ctype scalar, union, array, or struct

    Raises:

    typedescription
    NotImplementedErrorIf the conversion is not possible

    Notes

    This function does not losslessly round-trip in either direction.

  • numpy.ctypeslib.ctypes_load_library(*args, **kwds)[source]open in new window

    ctypes_load_library is deprecated, use load_library instead!

    It is possible to load a library using >>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP

    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:

    typedescription
    libname : strName of the library, which can have ‘lib’ as a prefix, but without an extension.
    loader_path : strWhere the library can be found.

    Returns:

    typedescription
    ctypes.cdll[libpath] : library objectA ctypes library object

    Raises:

    typedescription
    OSErrorIf there is no library with the expected extension, or the library is defective and cannot be loaded.
  • numpy.ctypeslib.load_library(libname, loader_path)[source]open in new window

    It is possible to load a library using >>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP

    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:

    typedescription
    libname : strName of the library, which can have ‘lib’ as a prefix, but without an extension.
    loader_path : strWhere the library can be found.

    Returns:

    typedescription
    ctypes.cdll[libpath] : library objectA ctypes library object

    Raises:

    typedescription
    OSErrorIf there is no library with the expected extension, or the library is defective and cannot be loaded.
  • numpy.ctypeslib.ndpointer(dtype=None, ndim=None, shape=None, flags=None)[source]open in new window

    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:

    typedescription
    dtype : data-type, optionalArray data-type.
    ndim : int, optionalNumber of array dimensions.
    shape : tuple of ints, optionalArray shape.
    flags : str or tuple of strArray flags; may be one or more of:

    Possible items for flags:

    • C_CONTIGUOUS / C / CONTIGUOUS
    • F_CONTIGUOUS / F / FORTRAN
    • OWNDATA / O
    • WRITEABLE / W
    • ALIGNED / A
    • WRITEBACKIFCOPY / X
    • UPDATEIFCOPY / U

    Returns:

    typedescription
    klass : ndpointer type objectA type object, which is an _ndtpr instance containing dtype, ndim, shape and flags information.

    Raises:

    typedescription
    TypeErrorIf 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