summaryrefslogtreecommitdiff
path: root/numpy/lib/utils.py
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-08-13 09:04:39 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-08-13 09:04:39 +0000
commiteee00f8f7e15592a048c8b841aef9ea81faa0fda (patch)
tree74052bb733a01a84b83c3354593d2e7eb0243ef5 /numpy/lib/utils.py
parent8e24ef871ee8a58ae65d4d59d8ac916a48568c56 (diff)
downloadnumpy-eee00f8f7e15592a048c8b841aef9ea81faa0fda.tar.gz
Remove _as_parameter_ attribute from arrays and add it to the ctypes object. Create an ndpointer class factory to return classes that check for specific array types. These can be used in argtypes list to ctypes functions.
Diffstat (limited to 'numpy/lib/utils.py')
-rw-r--r--numpy/lib/utils.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index 8306b799c..14b0d8ea3 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -7,8 +7,8 @@ from numpy.core import product, ndarray
__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype',
'issubdtype', 'deprecate', 'get_numarray_include',
- 'get_include', 'ctypes_load_library', 'info',
- 'source', 'who']
+ 'get_include', 'ctypes_load_library', 'ndpointer',
+ 'info', 'source', 'who']
def issubclass_(arg1, arg2):
try:
@@ -76,6 +76,32 @@ def ctypes_load_library(libname, loader_path):
libpath = os.path.join(libdir, libname)
return ctypes.cdll[libpath]
+class _ndptr(object):
+ def from_param(cls, obj):
+ if not isinstance(obj, ndarray):
+ raise TypeError("argument must be an ndarray")
+ if obj.dtype != cls._dtype_:
+ raise TypeError("array must have data type", cls._dtype_)
+ return obj.ctypes
+ from_param = classmethod(from_param)
+
+# Factory for a type-checking object with from_param defined
+_pointer_type_cache = {}
+def ndpointer(datatype):
+ datatype = dtype(datatype)
+ try:
+ return _pointer_type_cache[datatype]
+ except KeyError:
+ pass
+ if datatype.names:
+ name = str(id(datatype))
+ else:
+ name = datatype.str
+ klass = type("ndpointer_%s"%name, (_ndptr,),
+ {"_dtype_": datatype})
+ _pointer_type_cache[datatype] = klass
+ return klass
+
if sys.version_info < (2, 4):
# Can't set __name__ in 2.3