[pypy-commit] pypy py3.6: more closely follow cpython implementation for error modes

mattip pypy.commits at gmail.com
Fri Jun 21 02:46:16 EDT 2019


Author: Matti Picus <matti.picus at gmail.com>
Branch: py3.6
Changeset: r96834:bfa7f1269de9
Date: 2019-06-21 09:45 +0300
http://bitbucket.org/pypy/pypy/changeset/bfa7f1269de9/

Log:	more closely follow cpython implementation for error modes

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
@@ -18,6 +18,31 @@
 MEMORYVIEW_SCALAR   = 0x0008
 MEMORYVIEW_PIL      = 0x0010
 
+def is_multiindex(space, w_key):
+    if not space.isinstance_w(w_key, space.w_tuple):
+        return 0
+    length = space.len_w(w_key)
+    i = 0
+    while i < length:
+        w_obj = w_key.getitem(space, i)
+        if not space.lookup(w_obj, '__index__'):
+            return 0
+        i += 1
+    return 1
+
+def is_multislice(space, w_key):
+    if not space.isinstance_w(w_key, space.w_tuple):
+        return 0
+    length = space.len_w(w_key)
+    if length == 0:
+        return 0
+    i = 0
+    while i < length:
+        w_obj = w_key.getitem(space, i)
+        if not space.isinstance_w(w_obj, space.w_slice):
+            return 0
+        i += 1
+    return 1
 
 class W_MemoryView(W_Root):
     """Implement the built-in 'memoryview' type as a wrapper around
@@ -161,24 +186,28 @@
     def descr_getitem(self, space, w_index):
         self._check_released(space)
 
-        if space.isinstance_w(w_index, space.w_tuple):
+        is_slice = space.isinstance_w(w_index, space.w_slice)
+        if is_slice or space.lookup(w_index, '__index__'):
+            start, stop, step, slicelength = self._decode_index(space, w_index, is_slice)
+            # ^^^ for a non-slice index, this returns (index, 0, 0, 1)
+            if step == 0:  # index only
+                dim = self.getndim()
+                if dim == 0:
+                    raise oefmt(space.w_TypeError, "invalid indexing of 0-dim memory")
+                elif dim == 1:
+                    return self.view.w_getitem(space, start)
+                else:
+                    raise oefmt(space.w_NotImplementedError,
+                                "multi-dimensional sub-views are not implemented")
+            elif is_slice:
+                return self.view.new_slice(start, step, slicelength).wrap(space)
+        elif is_multiindex(space, w_index):
             return self._getitem_tuple_indexed(space, w_index)
-        is_slice = space.isinstance_w(w_index, space.w_slice)
-        start, stop, step, slicelength = self._decode_index(space, w_index, is_slice)
-        # ^^^ for a non-slice index, this returns (index, 0, 0, 1)
-        if step == 0:  # index only
-            dim = self.getndim()
-            if dim == 0:
-                raise oefmt(space.w_TypeError, "invalid indexing of 0-dim memory")
-            elif dim == 1:
-                return self.view.w_getitem(space, start)
-            else:
-                raise oefmt(space.w_NotImplementedError, "multi-dimensional sub-views are not implemented")
-        elif is_slice:
-            return self.view.new_slice(start, step, slicelength).wrap(space)
-        # multi index is handled at the top of this function
+        elif is_multislice(space, w_index):
+            raise oefmt(space.w_NotImplementedError,
+                        "multi-dimensional slicing is not implemented")
         else:
-            raise TypeError("memoryview: invalid slice key")
+            raise oefmt(space.w_TypeError, "memoryview: invalid slice key")
 
     def init_len(self):
         self.length = self.bytecount_from_shape()
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
@@ -299,6 +299,7 @@
         raises(TypeError, m.__setitem__, (2, 3), bytearray(b'12'))
         # slices in 2d memoryviews are not supported at all
         raises(TypeError, m.__getitem__, (slice(None), 3))
+        raises(NotImplementedError, m.__getitem__, (slice(None),))
 
 class AppTestCtypes(object):
     spaceconfig = dict(usemodules=['sys', '_rawffi'])


More information about the pypy-commit mailing list