Package mvpa :: Package clfs :: Package libsmlrc :: Module ctypes_helper

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.clfs.libsmlrc.ctypes_helper

     1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
     2  # vi: set ft=python sts=4 ts=4 sw=4 et:
     3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
     4  #
     5  #   See COPYING file distributed along with the PyMVPA package for the
     6  #   copyright and license terms.
     7  #
     8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
     9  """Helpers for wrapping C libraries with ctypes."""
    10
    11  import numpy as N
    12  import ctypes as C
    13
    14  # define an array type to help with wrapping
    15  c_darray = N.ctypeslib.ndpointer(dtype=N.float64, flags='aligned,contiguous')
    16  c_larray = N.ctypeslib.ndpointer(dtype=N.int64, flags='aligned,contiguous')
    17  c_farray = N.ctypeslib.ndpointer(dtype=N.float32, flags='aligned,contiguous')
    18  c_iarray = N.ctypeslib.ndpointer(dtype=N.int32, flags='aligned,contiguous')
    19
    20 -def extend_args(*args):
    21      """Turn ndarray arguments into dims and arrays."""
    22      arglist = []
    23      for arg in args:
    24          if isinstance(arg, N.ndarray):
    25              # add the dimensions
    26              arglist.extend(arg.shape)
    27
    28          # just append the arg
    29          arglist.append(arg)
    30
    31      return arglist
    32
    33  #############################################################
    34  # I'm not sure the rest is helpful, but I'll keep it for now.
    35  #############################################################
    36
    37  # incomplete type conversion
    38  typemap = {
    39      N.float64: C.c_double,
    40      N.float32: C.c_float,
    41      N.int64: C.c_int64,
    42      N.int32: C.c_int32}
    43
    44 -def process_args(*args):
    45      """Turn ndarray arguments into dims and array pointers for calling
    46      a ctypes-wrapped function."""
    47      arglist = []
    48      for arg in args:
    49          if isinstance(arg, N.ndarray):
    50              # add the dimensions
    51              arglist.extend(arg.shape)
    52
    53              # add the pointer to the ndarray
    54              arglist.append(arg.ctypes.data_as(
    55                  C.POINTER(typemap[arg.dtype.type])))
    56          else:
    57              # just append the arg
    58              arglist.append(arg)
    59
    60      return arglist
    61
    62 -def get_argtypes(*args):
    63      argtypes = []
    64      for arg in args:
    65          if isinstance(arg, N.ndarray):
    66              # add the dimensions
    67              argtypes.extend([C.c_int]*len(arg.shape))
    68
    69              # add the pointer to the ndarray
    70              argtypes.append(N.ctypeslib.ndpointer(dtype=arg.dtype))
    71          else:
    72              # try and figure out the type
    73              argtypes.append(arg)
    74      return argtypes
    75