[pypy-commit] pypy default: test, fix __array_interface__ for non-compliant keys and values
mattip
noreply at buildbot.pypy.org
Mon Nov 2 10:38:47 EST 2015
Author: mattip
Branch:
Changeset: r80502:62330b3e9b7f
Date: 2015-11-02 16:39 +0200
http://bitbucket.org/pypy/pypy/changeset/62330b3e9b7f/
Log: test, fix __array_interface__ for non-compliant keys and values
diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -44,30 +44,43 @@
def try_interface_method(space, w_object):
try:
w_interface = space.getattr(w_object, space.wrap("__array_interface__"))
- except OperationError, e:
+ if w_interface is None:
+ return None
+ version_w = space.finditem(w_interface, space.wrap("version"))
+ if version_w is None:
+ raise oefmt(space.w_ValueError, "__array_interface__ found without"
+ " 'version' key")
+ if not space.isinstance_w(version_w, space.w_int):
+ raise oefmt(space.w_ValueError, "__array_interface__ found with"
+ " non-int 'version' key")
+ version = space.int_w(version_w)
+ if version < 3:
+ raise oefmt(space.w_ValueError,
+ "__array_interface__ version %d not supported", version)
+ # make a view into the data
+ w_shape = space.finditem(w_interface, space.wrap('shape'))
+ w_dtype = space.finditem(w_interface, space.wrap('typestr'))
+ w_descr = space.finditem(w_interface, space.wrap('descr'))
+ w_data = space.finditem(w_interface, space.wrap('data'))
+ w_strides = space.finditem(w_interface, space.wrap('strides'))
+ if w_shape is None or w_dtype is None:
+ raise oefmt(space.w_ValueError,
+ "__array_interface__ missing one or more required keys: shape, typestr"
+ )
+ raise oefmt(space.w_NotImplementedError,
+ "creating array from __array_interface__ not supported yet")
+ '''
+ data_w = space.listview()
+ shape = [space.int_w(i) for i in space.listview(w_shape)]
+ dtype = descriptor.decode_w_dtype(space, w_dtype)
+ rw = space.is_true(data_w[1])
+ '''
+ #print 'create view from shape',shape,'dtype',dtype,'descr',w_descr,'data',data_w[0],'rw',rw
+ return None
+ except OperationError as e:
if e.match(space, space.w_AttributeError):
return None
raise
- if w_interface is None:
- # happens from compile.py
- return None
- version = space.int_w(space.finditem(w_interface, space.wrap("version")))
- if version < 3:
- raise oefmt(space.w_NotImplementedError,
- "__array_interface__ version %d not supported", version)
- # make a view into the data
- w_shape = space.finditem(w_interface, space.wrap('shape'))
- w_dtype = space.finditem(w_interface, space.wrap('typestr'))
- w_descr = space.finditem(w_interface, space.wrap('descr'))
- data_w = space.listview(space.finditem(w_interface, space.wrap('data')))
- w_strides = space.finditem(w_interface, space.wrap('strides'))
- shape = [space.int_w(i) for i in space.listview(w_shape)]
- dtype = descriptor.decode_w_dtype(space, w_dtype)
- rw = space.is_true(data_w[1])
- #print 'create view from shape',shape,'dtype',dtype,'descr',w_descr,'data',data_w[0],'rw',rw
- raise oefmt(space.w_NotImplementedError,
- "creating array from __array_interface__ not supported yet")
- return
@unwrap_spec(ndmin=int, copy=bool, subok=bool)
diff --git a/pypy/module/micronumpy/test/test_ndarray.py b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -3073,6 +3073,18 @@
c_data = c.__array_interface__['data'][0]
assert b_data + 3 * b.dtype.itemsize == c_data
+ class Dummy(object):
+ def __init__(self, aif=None):
+ if aif:
+ self.__array_interface__ = aif
+
+ a = array(Dummy())
+ assert a.dtype == object
+ raises(ValueError, array, Dummy({'xxx': 0}))
+ raises(ValueError), array, Dummy({'version': 0}))
+ raises(ValueError, array, Dummy({'version': 'abc'}))
+ raises(ValueError, array, Dummy({'version': 3}))
+
def test_array_indexing_one_elem(self):
from numpy import array, arange
raises(IndexError, 'arange(3)[array([3.5])]')
More information about the pypy-commit
mailing list