[pypy-svn] pypy fast-forward: (jacob, mfoord) Initial implementation of bytearray.translate
mfoord
commits-noreply at bitbucket.org
Sun Jan 16 19:26:55 CET 2011
Author: Michael Foord <michael at voidspace.org.uk>
Branch: fast-forward
Changeset: r40733:acab350c3cd4
Date: 2011-01-16 19:15 +0100
http://bitbucket.org/pypy/pypy/changeset/acab350c3cd4/
Log: (jacob, mfoord) Initial implementation of bytearray.translate
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
@@ -23,9 +23,6 @@
""" 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)
@@ -70,6 +67,11 @@
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
@@ -156,6 +158,12 @@
# 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):
@@ -447,4 +455,4 @@
return space.wrap(b)
from pypy.objspace.std import bytearraytype
-register_all(vars(), bytearraytype)
+register_all(vars(), bytearraytype)
\ No newline at end of file
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
@@ -48,7 +48,23 @@
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]
@@ -136,7 +152,7 @@
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')
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -14,7 +14,7 @@
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)
More information about the Pypy-commit
mailing list