[pypy-commit] pypy default: test, fix for cpython2 optimization where "buffer('') + obj is obj"

mattip pypy.commits at gmail.com
Tue Mar 27 17:24:45 EDT 2018


Author: Matti Picus <matti.picus at gmail.com>
Branch: 
Changeset: r94151:d044ce2d7d53
Date: 2018-03-28 00:14 +0300
http://bitbucket.org/pypy/pypy/changeset/d044ce2d7d53/

Log:	test, fix for cpython2 optimization where "buffer('') + obj is obj"

diff --git a/pypy/module/cpyext/test/test_arraymodule.py b/pypy/module/cpyext/test/test_arraymodule.py
--- a/pypy/module/cpyext/test/test_arraymodule.py
+++ b/pypy/module/cpyext/test/test_arraymodule.py
@@ -76,7 +76,9 @@
         else:
             expected = '\x01\0\0\0' '\x02\0\0\0' '\x03\0\0\0' '\x04\0\0\0'
         assert str(buf) == expected
-        assert str(buffer('') + arr) == expected
+        assert str(buffer('a') + arr) == "a" + expected
+        # python2 special cases empty-buffer + obj
+        assert str(buffer('') + arr) == "array('i', [1, 2, 3, 4])"
 
     def test_releasebuffer(self):
         module = self.import_module(name='array')
diff --git a/pypy/objspace/std/bufferobject.py b/pypy/objspace/std/bufferobject.py
--- a/pypy/objspace/std/bufferobject.py
+++ b/pypy/objspace/std/bufferobject.py
@@ -89,9 +89,14 @@
     def descr_str(self, space):
         return space.newbytes(self.buf.as_str())
 
-    @unwrap_spec(other='bufferstr')
-    def descr_add(self, space, other):
-        return space.newbytes(self.buf.as_str() + other)
+    def descr_add(self, space, w_other):
+        try:
+            other = w_other.readbuf_w(space)
+        except BufferInterfaceNotFound:
+            raise oefmt(space.w_TypeError, "bad argument type for built-in operation")
+        if self.buf.getlength() < 1:
+            return w_other
+        return space.newbytes(self.buf.as_str() + other.as_str())
 
     def _make_descr__cmp(name):
         def descr__cmp(self, space, w_other):
diff --git a/pypy/objspace/std/test/test_bufferobject.py b/pypy/objspace/std/test/test_bufferobject.py
--- a/pypy/objspace/std/test/test_bufferobject.py
+++ b/pypy/objspace/std/test/test_bufferobject.py
@@ -29,9 +29,11 @@
 
     def test_array_buffer(self):
         import array
-        b = buffer(array.array("B", [1, 2, 3]))
+        arr = array.array("B", [1, 2, 3])
+        b = buffer(arr)
         assert len(b) == 3
         assert b[0:3] == "\x01\x02\x03"
+        assert buffer('') + arr is arr
 
     def test_nonzero(self):
         assert buffer('\x00')
@@ -51,6 +53,7 @@
         assert buffer('abc') + 'def' == 'abcdef'
         import array
         assert buffer('abc') + array.array('c', 'def') == 'abcdef'
+        raises(TypeError, buffer('abc').__add__, 3)
 
     def test_cmp(self):
         assert buffer('ab') != 'ab'
@@ -199,6 +202,9 @@
         raises(TypeError, "buf[MyInt(0):MyInt(5)]")
 
     def test_pypy_raw_address_base(self):
+        import sys
+        if '__pypy__' not in sys.builtin_module_names:
+            skip('PyPy only')
         a = buffer("foobar")._pypy_raw_address()
         assert a != 0
         b = buffer(u"foobar")._pypy_raw_address()


More information about the pypy-commit mailing list