Author: Maciej Fijalkowski <[email protected]>
Branch: numpy-record-dtypes
Changeset: r52467:729aca0b90d6
Date: 2012-02-14 18:49 +0200
http://bitbucket.org/pypy/pypy/changeset/729aca0b90d6/
Log: start supporting array creation not working so far
diff --git a/pypy/module/micronumpy/interp_dtype.py
b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -112,6 +112,9 @@
def is_bool_type(self):
return self.kind == BOOLLTR
+ def is_record_type(self):
+ return self.fields is not None
+
def __repr__(self):
if self.fields is not None:
return '<DType %r>' % self.fields
diff --git a/pypy/module/micronumpy/interp_numarray.py
b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -1132,21 +1132,20 @@
if copy:
return w_item_or_iterable.copy(space)
return w_item_or_iterable
- shape, elems_w = find_shape_and_elems(space, w_item_or_iterable)
+ if w_dtype is None or space.is_w(w_dtype, space.w_None):
+ dtype = None
+ else:
+ dtype = space.interp_w(interp_dtype.W_Dtype,
+ space.call_function(space.gettypefor(interp_dtype.W_Dtype),
w_dtype))
+ shape, elems_w = find_shape_and_elems(space, w_item_or_iterable, dtype)
# they come back in C order
size = len(elems_w)
- if w_dtype is None or space.is_w(w_dtype, space.w_None):
- w_dtype = None
+ if dtype is None:
for w_elem in elems_w:
- w_dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
+ dtype = interp_ufuncs.find_dtype_for_scalar(space, w_elem,
w_dtype)
- if w_dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:
+ if dtype is interp_dtype.get_dtype_cache(space).w_float64dtype:
break
- if w_dtype is None:
- w_dtype = space.w_None
- dtype = space.interp_w(interp_dtype.W_Dtype,
- space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype)
- )
arr = W_NDimArray(size, shape[:], dtype=dtype, order=order)
shapelen = len(shape)
arr_iter = ArrayIterator(arr.size)
diff --git a/pypy/module/micronumpy/strides.py
b/pypy/module/micronumpy/strides.py
--- a/pypy/module/micronumpy/strides.py
+++ b/pypy/module/micronumpy/strides.py
@@ -38,22 +38,31 @@
rbackstrides = [0] * (len(res_shape) - len(orig_shape)) + rbackstrides
return rstrides, rbackstrides
-def find_shape_and_elems(space, w_iterable):
+def is_single_elem(space, w_elem, is_rec_type):
+ if (is_rec_type and space.isinstance_w(w_elem, space.w_tuple)):
+ return True
+ if space.issequence_w(w_elem):
+ return False
+ return True
+
+def find_shape_and_elems(space, w_iterable, dtype):
shape = [space.len_w(w_iterable)]
batch = space.listview(w_iterable)
+ is_rec_type = dtype.is_record_type()
while True:
new_batch = []
if not batch:
return shape, []
- if not space.issequence_w(batch[0]):
- for elem in batch:
- if space.issequence_w(elem):
+ if is_single_elem(space, batch[0], is_rec_type):
+ for w_elem in batch:
+ if is_single_elem(space, w_elem, is_rec_type):
raise OperationError(space.w_ValueError, space.wrap(
"setting an array element with a sequence"))
return shape, batch
size = space.len_w(batch[0])
for w_elem in batch:
- if not space.issequence_w(w_elem) or space.len_w(w_elem) != size:
+ if (not is_single_elem(space, w_elem, is_rec_type) or
+ space.len_w(w_elem) != size):
raise OperationError(space.w_ValueError, space.wrap(
"setting an array element with a sequence"))
new_batch += space.listview(w_elem)
diff --git a/pypy/module/micronumpy/test/test_numarray.py
b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1810,3 +1810,10 @@
b[1] = a[1]
assert a[1]['y'] == 2
+ def test_views(self):
+ skip("xx")
+
+ def test_creation(self):
+ from _numpypy import array
+ a = array([(1, 2), (3, 4)], dtype=[('x', int), ('y', float)])
+ assert repr(a[0]) == '(1, 2.0)'
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit