Commits

Timo Paulssen committed b2f6f9b

test str(NumpyBuffer), fix incorrect size constraints in views.

Comments (0)

Files changed (2)

pypy/module/_numpy/interp_buffer.py

         return self.array.get_concrete().find_size() * self.array.find_dtype().num_bytes
 
     def getitem(self, index):
-        index = self.calc_index(index)
         if index > self.getlength():
             raise IndexError("Index out of bounds (0<=index<%d)" % self.getlength())
         storage = self.array.get_concrete().get_root_storage()
         char_data = rffi.cast(CHAR_TP, storage)
+        index = self.calc_index(index)
         return char_data[index]
 
     def setitem(self, index, value):
-        index = self.calc_index(index)
         if index > self.getlength():
             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)
+        index = self.calc_index(index)
         char_ptr[index] = value
 
     def setslice(self, index, newstring):

pypy/module/_numpy/test/test_buffer.py

         assert arbuf[1] == '\5'
         assert viewbuf[0] == '\5'
 
-        assert len(view) == len(ar) - 2 == 3
+        assert len(viewbuf) == len(arbuf) - 2 == 3
 
         br = array(range(5,10), dtype=float)
         buf = br.data
         assert ar[4] == 1
 
         # tests for float dtype already done above
+
+    def test_convert_to_string(self):
+        from _numpy import array
+        from _numpy import dtype
+        ar = array(range(5,10), dtype=dtype("int8"))
+        buf = ar.data
+        as_str = str(buf)
+
+        for idx in range(len(buf)):
+            assert buf[idx] == as_str[idx]
+
+        br = array(range(5,10), dtype=float)
+        buf = br.data
+        bs_str = str(buf)
+
+        for idx in range(len(buf)):
+            assert buf[idx] == bs_str[idx]
+
+    def test_convert_view_to_string(self):
+        from _numpy import array
+        from _numpy import dtype
+        ar = array(range(5,10), dtype=dtype("int8"))
+        view = ar[1:-1]
+        buf = view.data
+        as_str = str(buf)
+
+        for idx in range(len(buf)):
+            assert buf[idx] == as_str[idx]
+
+        br = array(range(5,10), dtype=float)
+        view = br[1:-1]
+        buf = view.data
+        bs_str = str(buf)
+
+        for idx in range(len(buf)):
+            assert buf[idx] == bs_str[idx]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.