[pypy-svn] pypy bytearray: (mfoord) bytearray contains/splitlines/ord

mfoord commits-noreply at bitbucket.org
Fri Jan 21 17:29:31 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r41159:851ac4d26e31
Date: 2011-01-21 17:29 +0100
http://bitbucket.org/pypy/pypy/changeset/851ac4d26e31/

Log:	(mfoord) bytearray contains/splitlines/ord

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
@@ -88,6 +88,13 @@
     result = len(w_bytearray.data)
     return wrapint(space, result)
 
+def ord__Bytearray(space, w_bytearray):
+    if len(w_bytearray.data) != 1:
+        raise OperationError(space.w_TypeError,
+                             space.wrap("expected a character, but string"
+                            "of length %s found" % len(w_bytearray.data)))
+    return space.wrap(ord(w_bytearray.data[0]))
+
 def getitem__Bytearray_ANY(space, w_bytearray, w_index):
     # getindex_w should get a second argument space.w_IndexError,
     # but that doesn't exist the first time this is called.
@@ -125,6 +132,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
@@ -357,7 +370,7 @@
     if not list_w:
         return W_BytearrayObject([])
     data = w_self.data
-    reslen = 0
+    newdata = []
     for i in range(len(list_w)):
         w_s = list_w[i]
         if not (space.is_true(space.isinstance(w_s, space.w_str)) or
@@ -366,12 +379,10 @@
                 space.w_TypeError,
                 "sequence item %d: expected string, %s "
                 "found", i, space.type(w_s).getname(space, '?'))
-        reslen += len(space.bufferstr_w(w_s))
-    newdata = []
-    for i in range(len(list_w)):
+
         if data and i != 0:
             newdata.extend(data)
-        newdata.extend([c for c in space.bufferstr_w(list_w[i])])
+        newdata.extend([c for c in space.bufferstr_w(w_s)])
     return W_BytearrayObject(newdata)
 
 def str_decode__Bytearray_ANY_ANY(space, w_bytearray, w_encoding, w_errors):
@@ -519,6 +530,12 @@
     w_res = stringobject.str_expandtabs__String_ANY(space, w_str, w_tabsize)
     return String2Bytearray(space, w_res)
 
+def str_splitlines__Bytearray_ANY(space, w_bytearray, w_keepends):
+    w_str = str__Bytearray(space, w_bytearray)
+    w_result = stringobject.str_splitlines__String_ANY(space, w_str, w_keepends)
+    return space.newlist([new_bytearray(space, space.w_bytearray, space.str_w(entry))
+                          for entry in space.unpackiterable(w_result)])
+
 def str_split__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
     w_str = str__Bytearray(space, w_bytearray)
     if not space.is_w(w_by, space.w_None):
@@ -619,6 +636,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,24 @@
     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_splitlines(self):
+        b = bytearray('1234')
+        assert b.splitlines()[0] == b
+        assert b.splitlines()[0] is not b
+
+        assert len(bytearray('foo\nbar').splitlines()) == 2
+        for item in bytearray('foo\nbar').splitlines():
+            assert isinstance(item, bytearray)
+
+    def test_ord(self):
+        b = bytearray('\0A\x7f\x80\xff')
+        assert ([ord(b[i:i+1]) for i in range(len(b))] ==
+                         [0, 65, 127, 128, 255])
+        raises(TypeError, ord, bytearray('ll'))
+        raises(TypeError, ord, bytearray())
 
     def test_translate(self):
         b = 'hello'


More information about the Pypy-commit mailing list