[pypy-commit] pypy default: micronumpy: added indexing by tuples to get/setitem
justinpeel
noreply at buildbot.pypy.org
Thu Sep 1 02:02:26 CEST 2011
Author: Justin Peel <notmuchtotell at gmail.com>
Branch:
Changeset: r46964:93adb3d59791
Date: 2011-08-31 18:01 -0600
http://bitbucket.org/pypy/pypy/changeset/93adb3d59791/
Log: micronumpy: added indexing by tuples to get/setitem
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
@@ -3,6 +3,7 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
from pypy.module.micronumpy import interp_ufuncs, interp_dtype, signature
+from pypy.objspace.std.sliceobject import W_SliceObject
from pypy.rlib import jit
from pypy.rpython.lltypesystem import lltype
from pypy.tool.sourcetools import func_with_new_name
@@ -217,7 +218,15 @@
return space.wrap("[" + " ".join(concrete._getnums(True)) + "]")
def descr_getitem(self, space, w_idx):
- # TODO: indexing by tuples
+ # TODO: indexing by arrays and lists
+ if space.isinstance_w(w_idx, space.w_tuple):
+ length = space.len_w(w_idx)
+ if length == 0:
+ return space.wrap(self)
+ if length > 1: # only one dimension for now.
+ raise OperationError(space.w_IndexError,
+ space.wrap("invalid index"))
+ w_idx = space.getitem(w_idx, space.wrap(0))
start, stop, step, slice_length = space.decode_index4(w_idx, self.find_size())
if step == 0:
# Single index
@@ -231,8 +240,19 @@
return space.wrap(res)
def descr_setitem(self, space, w_idx, w_value):
- # TODO: indexing by tuples and lists
+ # TODO: indexing by arrays and lists
self.invalidated()
+ if space.isinstance_w(w_idx, space.w_tuple):
+ length = space.len_w(w_idx)
+ if length > 1: # only one dimension for now.
+ raise OperationError(space.w_IndexError,
+ space.wrap("invalid index"))
+ if length == 0:
+ w_idx = W_SliceObject(space.wrap(0),
+ space.wrap(self.find_size()),
+ space.wrap(1))
+ else:
+ w_idx = space.getitem(w_idx, space.wrap(0))
start, stop, step, slice_length = space.decode_index4(w_idx,
self.find_size())
if step == 0:
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
@@ -102,6 +102,16 @@
assert a[-1] == 8
raises(IndexError, "a[-6]")
+ def test_getitem_tuple(self):
+ from numpy import array
+ a = array(range(5))
+ raises(IndexError, "a[(1,2)]")
+ for i in xrange(5):
+ assert a[(i,)] == i
+ b = a[()]
+ for i in xrange(5):
+ assert a[i] == b[i]
+
def test_setitem(self):
from numpy import array
a = array(range(5))
@@ -110,6 +120,17 @@
raises(IndexError, "a[5] = 0.0")
raises(IndexError, "a[-6] = 3.0")
+ def test_setitem_tuple(self):
+ from numpy import array
+ a = array(range(5))
+ raises(IndexError, "a[(1,2)] = [0,1]")
+ for i in xrange(5):
+ a[(i,)] = i+1
+ assert a[i] == i+1
+ a[()] = range(5)
+ for i in xrange(5):
+ assert a[i] == i
+
def test_setslice_array(self):
from numpy import array
a = array(range(5))
@@ -541,4 +562,4 @@
a = fromstring(self.data)
for i in range(4):
assert a[i] == i + 1
- raises(ValueError, fromstring, "abc")
\ No newline at end of file
+ raises(ValueError, fromstring, "abc")
More information about the pypy-commit
mailing list