Author: Brian Kearns <[email protected]>
Branch:
Changeset: r68520:bf10cd3e2fa4
Date: 2013-12-20 18:01 -0500
http://bitbucket.org/pypy/pypy/changeset/bf10cd3e2fa4/
Log: fix some scalar setitem cases
diff --git a/pypy/module/micronumpy/arrayimpl/scalar.py
b/pypy/module/micronumpy/arrayimpl/scalar.py
--- a/pypy/module/micronumpy/arrayimpl/scalar.py
+++ b/pypy/module/micronumpy/arrayimpl/scalar.py
@@ -150,6 +150,12 @@
space.wrap("0-d arrays can't be indexed"))
def descr_setitem(self, space, _, w_idx, w_val):
+ if space.isinstance_w(w_idx, space.w_tuple):
+ if space.len_w(w_idx) == 0:
+ return self.set_scalar_value(self.dtype.coerce(space, w_val))
+ elif space.isinstance_w(w_idx, space.w_str):
+ if self.dtype.is_record_type():
+ return self.value.descr_setitem(space, w_idx, w_val)
raise OperationError(space.w_IndexError,
space.wrap("0-d arrays can't be indexed"))
@@ -181,7 +187,7 @@
s = self.dtype.itemtype.bool(self.value)
w_res = W_NDimArray.from_shape(space, [s], index_type)
if s == 1:
- w_res.implementation.setitem(0, index_type.itemtype.box(0))
+ w_res.implementation.setitem(0, index_type.itemtype.box(0))
return space.newtuple([w_res])
def fill(self, space, w_value):
diff --git a/pypy/module/micronumpy/interp_boxes.py
b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -458,13 +458,17 @@
return space.wrap(dtype.itemtype.to_str(read_val))
return read_val
- @unwrap_spec(item=str)
- def descr_setitem(self, space, item, w_value):
+ def descr_setitem(self, space, w_item, w_value):
+ if space.isinstance_w(w_item, space.w_basestring):
+ item = space.str_w(w_item)
+ else:
+ raise OperationError(space.w_IndexError, space.wrap(
+ "invalid index"))
try:
ofs, dtype = self.dtype.fields[item]
except KeyError:
- raise OperationError(space.w_IndexError,
- space.wrap("Field %s does not exist" % item))
+ raise OperationError(space.w_ValueError,
+ space.wrap("field named %s not found" % item))
dtype.itemtype.store(self.arr, self.ofs, ofs,
dtype.coerce(space, w_value))
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
@@ -748,6 +748,8 @@
b = a[()]
assert type(b) is int_
assert b == 3
+ a[()] = 4
+ assert a == 4
def test_len(self):
from numpypy import array
@@ -3104,6 +3106,12 @@
b = [('x', int), ('y', a)]
arr = zeros((), dtype=b)
assert arr['x'] == 0
+ arr['x'] = 2
+ assert arr['x'] == 2
+ exc = raises(IndexError, "arr[3L]")
+ assert exc.value.message == "0-d arrays can't be indexed"
+ exc = raises(ValueError, "arr['xx'] = 2")
+ assert exc.value.message == "field named xx not found"
arr['y']
#assert arr['y'].shape == ()
#assert arr['y'][()][0] == 0
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit