[pypy-commit] pypy numpy-data-buffer: implement setting items on buffers of arrays and views.
timo_jbo
noreply at buildbot.pypy.org
Mon Oct 3 05:39:57 CEST 2011
Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpy-data-buffer
Changeset: r47776:d577d2a94297
Date: 2011-10-03 05:38 +0200
http://bitbucket.org/pypy/pypy/changeset/d577d2a94297/
Log: implement setting items on buffers of arrays and views.
diff --git a/pypy/module/_numpy/interp_buffer.py b/pypy/module/_numpy/interp_buffer.py
--- a/pypy/module/_numpy/interp_buffer.py
+++ b/pypy/module/_numpy/interp_buffer.py
@@ -18,6 +18,13 @@
char_data = rffi.cast(CHAR_TP, storage)
return char_data[self.calc_index(index)]
+ def setitem(self, index, value):
+ if index > self.getlength() - 1:
+ raise IndexError("Index out of bounds (0<=index<%d)" % self.getlength())
+ storage = self.array.get_concrete().get_root_storage()
+ char_ptr = rffi.cast(CHAR_TP, storage)
+ char_ptr[self.calc_index(index)] = value
+
def calc_index(self, index):
return index
diff --git a/pypy/module/_numpy/test/test_buffer.py b/pypy/module/_numpy/test/test_buffer.py
--- a/pypy/module/_numpy/test/test_buffer.py
+++ b/pypy/module/_numpy/test/test_buffer.py
@@ -38,3 +38,36 @@
assert arbuf[1] == '\5'
assert viewbuf[0] == '\5'
+
+ def test_buffer_set(self):
+ from _numpy import array
+ from _numpy import dtype
+ ar = array(range(5), dtype=dtype("int8"))
+ buf = ar.data
+
+ buf[0] = '\5'
+ buf[1] = '\0'
+
+ assert ar[0] == 5
+ assert ar[1] == 0
+
+ raises(IndexError, "buf[5] = '\\9'")
+
+ def test_slice_set(self):
+ from _numpy import array
+ from _numpy import dtype
+ ar = array(range(5), dtype=dtype("int8"))
+
+ view = ar[1:-1]
+
+ arbuf = ar.data
+ viewbuf = view.data
+
+ viewbuf[0] = '\5'
+
+ assert view[0] == 5
+
+ arbuf[1] = '\4'
+ assert view[0] == 4
+
+ raises(IndexError, "view[4] = '\\5'")
More information about the pypy-commit
mailing list