[pypy-svn] pypy bytearray: (mfoord) bytearray.__contains__ works with buffers

mfoord commits-noreply at bitbucket.org
Fri Jan 21 12:46:59 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r41121:40baddfdcae9
Date: 2011-01-21 12:18 +0100
http://bitbucket.org/pypy/pypy/changeset/40baddfdcae9/

Log:	(mfoord) bytearray.__contains__ works with buffers

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -125,6 +125,12 @@
     w_str2 = str__Bytearray(space, w_bytearray)
     return stringobject.contains__String_String(space, w_str2, w_str)
 
+def contains__Bytearray_ANY(space, w_bytearray, w_sub):
+    # XXX slow - copies, needs rewriting
+    w_str = space.wrap(space.bufferstr_w(w_sub))
+    w_str2 = str__Bytearray(space, w_bytearray)
+    return stringobject.contains__String_String(space, w_str2, w_str)
+
 def add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
     data1 = w_bytearray1.data
     data2 = w_bytearray2.data
@@ -618,6 +624,8 @@
                                           'setitem_slice_helper')
 
 def _strip(space, w_bytearray, u_chars, left, right):
+    # note: mostly copied from stringobject._strip
+    # should really be shared
     u_self = w_bytearray.data
 
     lpos = 0

diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -76,6 +76,8 @@
     def test_contains(self):
         assert ord('l') in bytearray('hello')
         assert 'l' in bytearray('hello')
+        assert bytearray('ll') in bytearray('hello')
+        assert memoryview('ll') in bytearray('hello')
 
     def test_translate(self):
         b = 'hello'


More information about the Pypy-commit mailing list