[pypy-svn] pypy default: Implement comparison of memoryview with other buffer objects

amauryfa commits-noreply at bitbucket.org
Thu Jan 27 19:27:40 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41409:e3d9d170dd50
Date: 2011-01-27 19:26 +0100
http://bitbucket.org/pypy/pypy/changeset/e3d9d170dd50/

Log:	Implement comparison of memoryview with other buffer objects

diff --git a/pypy/module/__builtin__/test/test_buffer.py b/pypy/module/__builtin__/test/test_buffer.py
--- a/pypy/module/__builtin__/test/test_buffer.py
+++ b/pypy/module/__builtin__/test/test_buffer.py
@@ -209,3 +209,8 @@
         v = memoryview(buffer("a"*100, 2))
         assert v.shape == (98,)
         assert v.suboffsets == None
+
+    def test_compare(self):
+        assert memoryview("abc") == "abc"
+        assert memoryview("abc") == bytearray("abc")
+        assert memoryview("abc") != 3

diff --git a/pypy/module/__builtin__/interp_memoryview.py b/pypy/module/__builtin__/interp_memoryview.py
--- a/pypy/module/__builtin__/interp_memoryview.py
+++ b/pypy/module/__builtin__/interp_memoryview.py
@@ -23,12 +23,22 @@
     def _make_descr__cmp(name):
         def descr__cmp(self, space, w_other):
             other = space.interpclass_w(w_other)
-            if not isinstance(other, W_MemoryView):
+            if isinstance(other, W_MemoryView):
+                # xxx not the most efficient implementation
+                str1 = self.as_str()
+                str2 = other.as_str()
+                return space.wrap(getattr(operator, name)(str1, str2))
+
+            try:
+                w_buf = space.buffer(w_other)
+            except OperationError, e:
+                if not e.match(space, space.w_TypeError):
+                    raise
                 return space.w_NotImplemented
-            # xxx not the most efficient implementation
-            str1 = self.as_str()
-            str2 = other.as_str()
-            return space.wrap(getattr(operator, name)(str1, str2))
+            else:
+                str1 = self.as_str()
+                str2 = space.buffer_w(w_buf).as_str()
+                return space.wrap(getattr(operator, name)(str1, str2))
         descr__cmp.unwrap_spec = ['self', ObjSpace, W_Root]
         descr__cmp.func_name = name
         return descr__cmp


More information about the Pypy-commit mailing list