Author: Amaury Forgeot d'Arc <amaur...@gmail.com>
Branch: py3.5
Changeset: r87297:0dd312748a34
Date: 2016-09-22 02:23 +0200
http://bitbucket.org/pypy/pypy/changeset/0dd312748a34/

Log:    Add memoryview.nbytes.

diff --git a/pypy/objspace/std/memoryobject.py 
b/pypy/objspace/std/memoryobject.py
--- a/pypy/objspace/std/memoryobject.py
+++ b/pypy/objspace/std/memoryobject.py
@@ -387,6 +387,10 @@
         self._check_released(space)
         return space.wrap(self.getlength())
 
+    def w_get_nbytes(self, space):
+        self._check_released(space)
+        return space.wrap(self.buf.getlength())
+
     def w_get_format(self, space):
         self._check_released(space)
         return space.wrap(self.getformat())
@@ -684,6 +688,7 @@
     format      = GetSetProperty(W_MemoryView.w_get_format),
     itemsize    = GetSetProperty(W_MemoryView.w_get_itemsize),
     ndim        = GetSetProperty(W_MemoryView.w_get_ndim),
+    nbytes        = GetSetProperty(W_MemoryView.w_get_nbytes),
     readonly    = GetSetProperty(W_MemoryView.w_is_readonly),
     shape       = GetSetProperty(W_MemoryView.w_get_shape),
     strides     = GetSetProperty(W_MemoryView.w_get_strides),
diff --git a/pypy/objspace/std/test/test_memoryobject.py 
b/pypy/objspace/std/test/test_memoryobject.py
--- a/pypy/objspace/std/test/test_memoryobject.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -55,6 +55,7 @@
         assert v.shape == (100,)
         assert v.ndim == 1
         assert v.strides == (1,)
+        assert v.nbytes == 100
 
     def test_suboffsets(self):
         v = memoryview(b"a"*100)
@@ -127,6 +128,7 @@
         raises(ValueError, "v[0]")
         raises(ValueError, "v[0] = b'a'")
         raises(ValueError, "v.format")
+        raises(ValueError, "v.nbytes")
         raises(ValueError, "v.itemsize")
         raises(ValueError, "v.ndim")
         raises(ValueError, "v.readonly")
@@ -155,6 +157,7 @@
         assert len(m) == 10
         assert m.shape == (10,)
         assert len(m.tobytes()) == 40
+        assert m.nbytes == 40
         assert m[0] == 0
         m[0] = 1
         assert m[0] == 1
@@ -167,6 +170,7 @@
         assert slice.itemsize == 4
         assert len(slice) == 6
         assert len(slice.tobytes()) == 24
+        assert slice.nbytes == 24
         assert slice[0] == 2
         slice[0] = 1
         assert slice[0] == 1
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to