[Numpy-svn] r3128 - in trunk/numpy: . core numarray oldnumeric
numpy-svn at scipy.org
numpy-svn at scipy.org
Thu Sep 7 02:37:06 EDT 2006
Author: oliphant
Date: 2006-09-07 01:36:54 -0500 (Thu, 07 Sep 2006)
New Revision: 3128
Modified:
trunk/numpy/core/_internal.py
trunk/numpy/ctypeslib.py
trunk/numpy/numarray/alter_code1.py
trunk/numpy/numarray/alter_code2.py
trunk/numpy/oldnumeric/alter_code1.py
trunk/numpy/oldnumeric/alter_code2.py
trunk/numpy/oldnumeric/fix_default_axis.py
Log:
Add c_intp to ctypeslib. Add converttree to alter_code functions. Fix ctypeslib when ctypes is not available.
Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/core/_internal.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -192,10 +192,11 @@
return result
def _getintp_ctype():
- if _getintp_ctype.cache:
- return _getintp_ctype.cache
+ val = _getintp_ctype.cache
+ if val is not None:
+ return val
+ char = dtype('p').char
import ctypes
- char = dtype('p').char
if (char == 'i'):
val = ctypes.c_int
elif char == 'l':
@@ -203,7 +204,7 @@
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
Modified: trunk/numpy/ctypeslib.py
===================================================================
--- trunk/numpy/ctypeslib.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/ctypeslib.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -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
-def _num_fromflags(flaglist):
- num = 0
- for val in flaglist:
- num += _flagdict[val]
- return num
+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 _flags_fromnum(num):
- res = []
- for key, value in _flagdict.items():
- if (num & value):
- res.append(key)
- return res
+ def _num_fromflags(flaglist):
+ num = 0
+ for val in flaglist:
+ num += _flagdict[val]
+ return num
-ctypes_load_library = deprecate(load_library, 'ctypes_load_library', 'load_library')
+ 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)
+ 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)
-# 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:
+
+ # 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:
- 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
+ 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
Modified: trunk/numpy/numarray/alter_code1.py
===================================================================
--- trunk/numpy/numarray/alter_code1.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/numarray/alter_code1.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -52,7 +52,7 @@
- .setimaginary() --> .imag
"""
-__all__ = ['convertfile', 'convertall']
+__all__ = ['convertfile', 'convertall', 'converttree']
import sys
import os
Modified: trunk/numpy/numarray/alter_code2.py
===================================================================
--- trunk/numpy/numarray/alter_code2.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/numarray/alter_code2.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -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,71 +17,7 @@
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')
fid.write(filestr)
@@ -118,5 +56,15 @@
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)
Modified: trunk/numpy/oldnumeric/alter_code1.py
===================================================================
--- trunk/numpy/oldnumeric/alter_code1.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/oldnumeric/alter_code1.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -28,7 +28,7 @@
* Converts uses of type(...) is <type>
isinstance(..., <type>)
"""
-__all__ = ['convertfile', 'convertall']
+__all__ = ['convertfile', 'convertall', 'converttree']
import sys
import os
@@ -195,5 +195,15 @@
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)
Modified: trunk/numpy/oldnumeric/alter_code2.py
===================================================================
--- trunk/numpy/oldnumeric/alter_code2.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/oldnumeric/alter_code2.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -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 @@
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 @@
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)
Modified: trunk/numpy/oldnumeric/fix_default_axis.py
===================================================================
--- trunk/numpy/oldnumeric/fix_default_axis.py 2006-09-07 00:34:30 UTC (rev 3127)
+++ trunk/numpy/oldnumeric/fix_default_axis.py 2006-09-07 06:36:54 UTC (rev 3128)
@@ -33,8 +33,7 @@
std
mean
"""
-__all__ = ['convertfile', 'convertall', 'converttree',
- 'convertfile2','convertall2', 'converttree2']
+__all__ = ['convertfile', 'convertall', 'converttree']
import sys
import os
More information about the Numpy-svn
mailing list