Author: mattip <[email protected]>
Branch: object-dtype2
Changeset: r76838:f8e96786ec62
Date: 2015-04-19 23:26 +0300
http://bitbucket.org/pypy/pypy/changeset/f8e96786ec62/
Log: start to add creation of array from __array_interface__, should
finish this on another branch
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
@@ -38,6 +38,31 @@
raise oefmt(space.w_ValueError,
"object __array__ method not producing an array")
+def try_interface_method(space, w_object):
+ try:
+ w_interface = space.getattr(w_object,
space.wrap("__array_interface__"))
+ except OperationError, e:
+ if e.match(space, space.w_AttributeError):
+ return None
+ raise
+ 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)
def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False,
@@ -63,7 +88,11 @@
# continue with w_array, but do further operations in place
w_object = w_array
copy = False
-
+ if not isinstance(w_object, W_NDimArray):
+ w_array = try_interface_method(space, w_object)
+ if w_array is not None:
+ w_object = w_array
+ copy = False
dtype = descriptor.decode_w_dtype(space, w_dtype)
if space.is_none(w_order):
diff --git a/pypy/module/micronumpy/ndarray.py
b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -551,6 +551,7 @@
else:
strides = self.descr_get_strides(space)
space.setitem_str(w_d, 'strides', strides)
+ space.setitem_str(w_d, 'version', space.wrap(3))
return w_d
w_pypy_data = None
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py
b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -107,4 +107,6 @@
interface['shape'] = tuple([3])
interface['strides'] = tuple([0])
c = np.array(DummyArray(interface, base=a))
+ c.dtype = a.dtype
+ #print c
assert (c == np.array([(1, 2, 3), (1, 2, 3), (1, 2, 3)],
dtype='u4,u4,u4') ).all()
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit