diff options
author | Travis Oliphant <oliphant@enthought.com> | 2006-09-07 06:36:54 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2006-09-07 06:36:54 +0000 |
commit | 2b6af2c5e95497ba9f7ea23eb9aef9f3bfc22aa2 (patch) | |
tree | 2217d8ce5bae1b2f044528c8932e5176aeb8ee93 | |
parent | c4b98bdd9b0046494c1f972912137b4a7e5c6874 (diff) | |
download | numpy-2b6af2c5e95497ba9f7ea23eb9aef9f3bfc22aa2.tar.gz |
Add c_intp to ctypeslib. Add converttree to alter_code functions. Fix ctypeslib when ctypes is not available.
-rw-r--r-- | numpy/core/_internal.py | 9 | ||||
-rw-r--r-- | numpy/ctypeslib.py | 218 | ||||
-rw-r--r-- | numpy/numarray/alter_code1.py | 2 | ||||
-rw-r--r-- | numpy/numarray/alter_code2.py | 80 | ||||
-rw-r--r-- | numpy/oldnumeric/alter_code1.py | 12 | ||||
-rw-r--r-- | numpy/oldnumeric/alter_code2.py | 15 | ||||
-rw-r--r-- | numpy/oldnumeric/fix_default_axis.py | 3 |
7 files changed, 163 insertions, 176 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index e650be9a0..ff816faf5 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -192,10 +192,11 @@ def _commastring(astr): return result def _getintp_ctype(): - if _getintp_ctype.cache: - return _getintp_ctype.cache - import ctypes + val = _getintp_ctype.cache + if val is not None: + return val char = dtype('p').char + import ctypes if (char == 'i'): val = ctypes.c_int elif char == 'l': @@ -203,7 +204,7 @@ def _getintp_ctype(): elif char == 'q': val = ctypes.c_longlong else: - raise ValueError, "confused about intp->ctypes." + val = ctypes.c_long _getintp_ctype.cache = val return val _getintp_ctype.cache = None diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 1d4b7dc19..9ea64297d 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -1,116 +1,134 @@ -__all__ = ['load_library', 'ndpointer', 'test', 'ctypes_load_library'] +__all__ = ['load_library', 'ndpointer', 'test', 'ctypes_load_library', + 'c_intp'] import sys, os from numpy import integer, product, ndarray, dtype as _dtype, deprecate from numpy.core.multiarray import _flagdict, flagsobj -# Adapted from Albert Strasheim -def 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 +try: import ctypes - libpath = os.path.join(libdir, libname) - return ctypes.cdll[libpath] +except ImportError: + ctypes = None + +if ctypes is None: + def _dummy(*args, **kwds): + raise ImportError, "ctypes is not available." + load_library = _dummy + ndpointer = _dummy + ctypes_load_library = _dummy + + from numpy import intp as c_intp +else: + import numpy.core._internal as nic + c_intp = nic._getintp_ctype() + del nic + + # Adapted from Albert Strasheim + def 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 + 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 _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 + def _flags_fromnum(num): + res = [] + for key, value in _flagdict.items(): + if (num & value): + res.append(key) + return res -ctypes_load_library = deprecate(load_library, 'ctypes_load_library', 'load_library') + ctypes_load_library = deprecate(load_library, 'ctypes_load_library', 'load_library') -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) + 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: + # 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: - 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 + 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 def test(level=1, verbosity=1): from numpy.testing import NumpyTest diff --git a/numpy/numarray/alter_code1.py b/numpy/numarray/alter_code1.py index 897b019db..990b594ef 100644 --- a/numpy/numarray/alter_code1.py +++ b/numpy/numarray/alter_code1.py @@ -52,7 +52,7 @@ Makes the following changes: - .setimaginary() --> .imag """ -__all__ = ['convertfile', 'convertall'] +__all__ = ['convertfile', 'convertall', 'converttree'] import sys import os diff --git a/numpy/numarray/alter_code2.py b/numpy/numarray/alter_code2.py index 46be8043b..8505ca9cc 100644 --- a/numpy/numarray/alter_code2.py +++ b/numpy/numarray/alter_code2.py @@ -2,12 +2,14 @@ This module converts code written for numpy.numarray to work with numpy +FIXME: finish this. + """ -#__all__ = ['convertfile', 'convertall'] +#__all__ = ['convertfile', 'convertall', 'converttree'] __all__ = [] import warnings -warnings.warn("numpy.numarray.alter_code2 is not ready yet.") +warnings.warn("numpy.numarray.alter_code2 is not working yet.") import sys @@ -15,70 +17,6 @@ import os import re import glob -# To convert typecharacters we need to -# Not very safe. Disabled for now.. -def replacetypechars(astr): - astr = astr.replace("'s'","'h'") - astr = astr.replace("'b'","'B'") - astr = astr.replace("'1'","'b'") - astr = astr.replace("'w'","'H'") - astr = astr.replace("'u'","'I'") - return astr - -def changeimports(fstr, name, newname): - importstr = 'import %s' % name - importasstr = 'import %s as ' % name - fromstr = 'from %s import ' % name - fromall=0 - - fstr = fstr.replace(importasstr, 'import %s as ' % newname) - fstr = fstr.replace(importstr, 'import %s as %s' % (newname,name)) - - ind = 0 - Nlen = len(fromstr) - Nlen2 = len("from %s import " % newname) - while 1: - found = fstr.find(fromstr,ind) - if (found < 0): - break - ind = found + Nlen - if fstr[ind] == '*': - continue - fstr = "%sfrom %s import %s" % (fstr[:found], newname, fstr[ind:]) - ind += Nlen2 - Nlen - return fstr, fromall - -def replaceattr(astr): - astr = astr.replace("matrixmultiply","dot") - return astr - -def replaceother(astr): - astr = re.sub(r'typecode\s*=', 'dtype=', astr) - astr = astr.replace('ArrayType', 'ndarray') - astr = astr.replace('NewAxis', 'newaxis') - return astr - -import datetime -def fromstr(filestr): - #filestr = replacetypechars(filestr) - filestr, fromall1 = changeimports(filestr, 'numpy.oldnumeric', 'numpy') - filestr, fromall1 = changeimports(filestr, 'numpy.core.multiarray', 'numpy') - filestr, fromall1 = changeimports(filestr, 'numpy.core.umath', 'numpy') - filestr, fromall3 = changeimports(filestr, 'LinearAlgebra', - 'numpy.linalg.old') - filestr, fromall3 = changeimports(filestr, 'RNG', 'numpy.random.oldrng') - filestr, fromall3 = changeimports(filestr, 'RNG.Statistics', 'numpy.random.oldrngstats') - filestr, fromall3 = changeimports(filestr, 'RandomArray', 'numpy.random.oldrandomarray') - filestr, fromall3 = changeimports(filestr, 'FFT', 'numpy.fft.old') - filestr, fromall3 = changeimports(filestr, 'MA', 'numpy.core.ma') - fromall = fromall1 or fromall2 or fromall3 - filestr = replaceattr(filestr) - filestr = replaceother(filestr) - today = datetime.date.today().strftime('%b %d, %Y') - name = os.path.split(sys.argv[0])[-1] - filestr = '## Automatically adapted for '\ - 'numpy %s by %s\n\n%s' % (today, name, filestr) - return filestr def makenewfile(name, filestr): fid = file(name, 'w') @@ -118,5 +56,15 @@ def convertall(direc=os.path.curdir): for afile in files: convertfile(afile) +def _func(arg, dirname, fnames): + convertall(dirname) + +def converttree(direc=os.path.curdir): + """Convert all .py files in the tree given + + """ + os.path.walk(direc, _func, None) + + if __name__ == '__main__': fromargs(sys.argv) diff --git a/numpy/oldnumeric/alter_code1.py b/numpy/oldnumeric/alter_code1.py index 2da1e8582..0a27fa15b 100644 --- a/numpy/oldnumeric/alter_code1.py +++ b/numpy/oldnumeric/alter_code1.py @@ -28,7 +28,7 @@ Makes the following changes: * Converts uses of type(...) is <type> isinstance(..., <type>) """ -__all__ = ['convertfile', 'convertall'] +__all__ = ['convertfile', 'convertall', 'converttree'] import sys import os @@ -195,5 +195,15 @@ def convertall(direc=os.path.curdir): for afile in files: convertfile(afile) +def _func(arg, dirname, fnames): + convertall(dirname) + +def converttree(direc=os.path.curdir): + """Convert all .py files in the tree given + + """ + os.path.walk(direc, _func, None) + + if __name__ == '__main__': fromargs(sys.argv) diff --git a/numpy/oldnumeric/alter_code2.py b/numpy/oldnumeric/alter_code2.py index c8ecd114c..6db8fe3e2 100644 --- a/numpy/oldnumeric/alter_code2.py +++ b/numpy/oldnumeric/alter_code2.py @@ -2,6 +2,8 @@ This module converts code written for numpy.oldnumeric to work with numpy +FIXME: Flesh this out. + Makes the following changes: * Converts typecharacters '1swu' to 'bhHI' respectively when used as typecodes @@ -17,11 +19,11 @@ Makes the following changes: oldnumeric.random_array, and oldnumeric.fft """ -#__all__ = ['convertfile', 'convertall'] +#__all__ = ['convertfile', 'convertall', 'converttree'] __all__ = [] import warnings -warnings.warn("numpy.oldnumeric.alter_code2 is not ready yet.") +warnings.warn("numpy.oldnumeric.alter_code2 is not working yet.") import sys import os @@ -131,5 +133,14 @@ def convertall(direc=os.path.curdir): for afile in files: convertfile(afile) +def _func(arg, dirname, fnames): + convertall(dirname) + +def converttree(direc=os.path.curdir): + """Convert all .py files in the tree given + + """ + os.path.walk(direc, _func, None) + if __name__ == '__main__': fromargs(sys.argv) diff --git a/numpy/oldnumeric/fix_default_axis.py b/numpy/oldnumeric/fix_default_axis.py index d9e1c9473..15054b50c 100644 --- a/numpy/oldnumeric/fix_default_axis.py +++ b/numpy/oldnumeric/fix_default_axis.py @@ -33,8 +33,7 @@ prod std mean """ -__all__ = ['convertfile', 'convertall', 'converttree', - 'convertfile2','convertall2', 'converttree2'] +__all__ = ['convertfile', 'convertall', 'converttree'] import sys import os |