Commits

Michael Foord committed acab350

(jacob, mfoord) Initial implementation of bytearray.translate

Comments (0)

Files changed (3)

pypy/objspace/std/bytearrayobject.py

         """ representation for debugging purposes """
         return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data))
 
-    def unwrap(w_bytearray, space):
-        return bytearray(w_self.data)
-
 registerimplementation(W_BytearrayObject)
 
 
             return space.w_True
     return space.w_False
 
+def contains__Bytearray_String(space, w_bytearray, w_str):
+    # XXX slow - copies, needs rewriting
+    w_str2 = delegate_Bytearray2String(space, w_bytearray)
+    return space.call_method(w_str2, "__contains__", w_str)
+
 def add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
     data1 = w_bytearray1.data
     data2 = w_bytearray2.data
     # No more items to compare -- compare sizes
     return space.newbool(len(data1) > len(data2))
 
+def str_translate__Bytearray_Bytearray_String(space, w_bytearray1, w_bytearray2, w_str):
+    # XXX slow, copies *twice* needs proper implementation
+    w_str = delegate_Bytearray2String(space, w_bytearray1)
+    w_res = space.call_method(w_str, 'translate', w_bytearray2)
+    return String2Bytearray(space, w_res)
+
 # Mostly copied from repr__String, but without the "smart quote"
 # functionality.
 def repr__Bytearray(space, w_bytearray):
     return space.wrap(b)
 
 from pypy.objspace.std import bytearraytype
-register_all(vars(), bytearraytype)
+register_all(vars(), bytearraytype)

pypy/objspace/std/bytearraytype.py

     str_expandtabs, str_lstrip, str_rstrip, str_strip,
     str_ljust, str_rjust, str_center, str_zfill,
     str_join, str_split, str_rsplit, str_partition, str_rpartition,
-    str_splitlines)
+    str_splitlines, str_translate)
 from pypy.objspace.std.listtype import (
     list_append, list_extend)
 

pypy/objspace/std/test/test_bytes.py

 
     def test_contains(self):
         assert ord('l') in bytearray('hello')
-
+        assert 'l' in bytearray('hello')
+        
+    def test_translate(self):
+        b = 'hello'
+        ba = bytearray(b)
+        rosetta = bytearray(range(0, 256))
+        rosetta[ord('o')] = ord('e')
+        
+        for table in rosetta, str(rosetta):
+            c = ba.translate(table)
+            assert ba == bytearray('hello')
+            assert c == bytearray('helle')
+            
+            c = ba.translate(rosetta, 'l')
+            assert c == bytearray('hee')
+            assert typeof(c) is bytearray
+        
     def test_iter(self):
         assert list(bytearray('hello')) == [104, 101, 108, 108, 111]
 
 
         check(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
         check(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
-
+        
     def test_append(self):
         b = bytearray('abc')
         b.append('d')