diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-08-14 21:30:52 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-08-14 21:30:52 +0000 |
commit | 06acb9555bf64997423b124620fb469cab24cb2b (patch) | |
tree | 52825c87b3a2adfbc8377853828e6fdf165e68f8 | |
parent | c347f572c0d1dbd4f38c5cce861bccaa7aaa2ccd (diff) | |
download | numpy-06acb9555bf64997423b124620fb469cab24cb2b.tar.gz |
Move ctypes-related functions out of top-level
-rw-r--r-- | numpy/ctypeslib.py | 108 | ||||
-rw-r--r-- | numpy/lib/utils.py | 109 |
2 files changed, 109 insertions, 108 deletions
diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py new file mode 100644 index 000000000..739c6632e --- /dev/null +++ b/numpy/ctypeslib.py @@ -0,0 +1,108 @@ +__all__ = ['ctypes_load_library', 'ndpointer'] + + +# Adapted from Albert Strasheim +def ctypes_load_library(libname, loader_path): + if '.' not in libname: + if sys.platform == 'win32': + libname = '%s.dll' % libname + elif sys.platform == 'darwin': + libname = '%s.dylib' % libname + else: + libname = '%s.so' % libname + loader_path = os.path.abspath(loader_path) + if not os.path.isdir(loader_path): + libdir = os.path.dirname(loader_path) + else: + libdir = loader_path + import ctypes + libpath = os.path.join(libdir, libname) + return ctypes.cdll[libpath] + +def _num_fromflags(flaglist): + num = 0 + for val in flaglist: + num += _flagdict[val] + return num + +def _flags_fromnum(num): + res = [] + for key, value in _flagdict.items(): + if (num & value): + res.append(key) + return res + +class _ndptr(object): + def from_param(cls, obj): + if not isinstance(obj, ndarray): + raise TypeError, "argument must be an ndarray" + if cls._dtype_ is not None \ + and obj.dtype != cls._dtype_: + raise TypeError, "array must have data type %s" % cls._dtype_ + if cls._ndim_ is not None \ + and obj.ndim != cls._ndim_: + raise TypeError, "array must have %d dimension(s)" % cls._ndim_ + if cls._shape_ is not None \ + and obj.shape != cls._shape_: + raise TypeError, "array must have shape %s" % str(cls._shape_) + if cls._flags_ is not None \ + and ((obj.flags.num & cls._flags_) != cls._flags_): + raise TypeError, "array must have flags %s" % \ + _flags_fromnum(cls._flags_) + return obj.ctypes + from_param = classmethod(from_param) + + +# Factory for an array-checking class with from_param defined for +# use with ctypes argtypes mechanism +_pointer_type_cache = {} +def ndpointer(dtype=None, ndim=None, shape=None, flags=None): + if dtype is not None: + dtype = _dtype(dtype) + num = None + if flags is not None: + if isinstance(flags, str): + flags = flags.split(',') + elif isinstance(flags, (int, integer)): + num = flags + flags = _flags_fromnum(num) + elif isinstance(flags, flagsobj): + num = flags.num + flags = _flags_fromnum(num) + if num is None: + try: + flags = [x.strip().upper() for x in flags] + except: + raise TypeError, "invalid flags specification" + num = _num_fromflags(flags) + try: + return _pointer_type_cache[(dtype, ndim, shape, num)] + except KeyError: + pass + if dtype is None: + name = 'any' + elif dtype.names: + name = str(id(dtype)) + else: + name = dtype.str + if ndim is not None: + name += "_%dd" % ndim + if shape is not None: + try: + strshape = [str(x) for x in shape] + except TypeError: + strshape = [str(shape)] + shape = (shape,) + shape = tuple(shape) + name += "_"+"x".join(strshape) + if flags is not None: + name += "_"+"_".join(flags) + else: + flags = [] + klass = type("ndpointer_%s"%name, (_ndptr,), + {"_dtype_": dtype, + "_shape_" : shape, + "_ndim_" : ndim, + "_flags_" : num}) + _pointer_type_cache[dtype] = klass + return klass diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index eae0ab7b7..400f378a6 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -7,8 +7,7 @@ from numpy.core import product, ndarray __all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'issubdtype', 'deprecate', 'get_numarray_include', - 'get_include', 'ctypes_load_library', 'ndpointer', - 'info', 'source', 'who'] + 'get_include', 'info', 'source', 'who'] def issubclass_(arg1, arg2): try: @@ -58,112 +57,6 @@ def get_numarray_include(type=None): return include_dirs + get_numpy_include_dirs() -# Adapted from Albert Strasheim -def ctypes_load_library(libname, loader_path): - if '.' not in libname: - if sys.platform == 'win32': - libname = '%s.dll' % libname - elif sys.platform == 'darwin': - libname = '%s.dylib' % libname - else: - libname = '%s.so' % libname - loader_path = os.path.abspath(loader_path) - if not os.path.isdir(loader_path): - libdir = os.path.dirname(loader_path) - else: - libdir = loader_path - import ctypes - libpath = os.path.join(libdir, libname) - return ctypes.cdll[libpath] - -def _num_fromflags(flaglist): - num = 0 - for val in flaglist: - num += _flagdict[val] - return num - -def _flags_fromnum(num): - res = [] - for key, value in _flagdict.items(): - if (num & value): - res.append(key) - return res - -class _ndptr(object): - def from_param(cls, obj): - if not isinstance(obj, ndarray): - raise TypeError, "argument must be an ndarray" - if cls._dtype_ is not None \ - and obj.dtype != cls._dtype_: - raise TypeError, "array must have data type %s" % cls._dtype_ - if cls._ndim_ is not None \ - and obj.ndim != cls._ndim_: - raise TypeError, "array must have %d dimension(s)" % cls._ndim_ - if cls._shape_ is not None \ - and obj.shape != cls._shape_: - raise TypeError, "array must have shape %s" % str(cls._shape_) - if cls._flags_ is not None \ - and ((obj.flags.num & cls._flags_) != cls._flags_): - raise TypeError, "array must have flags %s" % \ - _flags_fromnum(cls._flags_) - return obj.ctypes - from_param = classmethod(from_param) - -# Factory for an array-checking class with from_param defined for -# use with ctypes argtypes mechanism -_pointer_type_cache = {} -def ndpointer(dtype=None, ndim=None, shape=None, flags=None): - if dtype is not None: - dtype = _dtype(dtype) - num = None - if flags is not None: - if isinstance(flags, str): - flags = flags.split(',') - elif isinstance(flags, (int, integer)): - num = flags - flags = _flags_fromnum(num) - elif isinstance(flags, flagsobj): - num = flags.num - flags = _flags_fromnum(num) - if num is None: - try: - flags = [x.strip().upper() for x in flags] - except: - raise TypeError, "invalid flags specification" - num = _num_fromflags(flags) - try: - return _pointer_type_cache[(dtype, ndim, shape, num)] - except KeyError: - pass - if dtype is None: - name = 'any' - elif dtype.names: - name = str(id(dtype)) - else: - name = dtype.str - if ndim is not None: - name += "_%dd" % ndim - if shape is not None: - try: - strshape = [str(x) for x in shape] - except TypeError: - strshape = [str(shape)] - shape = (shape,) - shape = tuple(shape) - name += "_"+"x".join(strshape) - if flags is not None: - name += "_"+"_".join(flags) - else: - flags = [] - klass = type("ndpointer_%s"%name, (_ndptr,), - {"_dtype_": dtype, - "_shape_" : shape, - "_ndim_" : ndim, - "_flags_" : num}) - _pointer_type_cache[dtype] = klass - return klass - - if sys.version_info < (2, 4): # Can't set __name__ in 2.3 import new |