[pypy-svn] r77484 - in pypy/branch/fast-forward/pypy/objspace/std: . test

afa at codespeak.net afa at codespeak.net
Thu Sep 30 11:03:43 CEST 2010


Author: afa
Date: Thu Sep 30 11:03:42 2010
New Revision: 77484

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
   pypy/branch/fast-forward/pypy/objspace/std/bytearraytype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
Log:
Add string-like methods to bytearray.
Not very efficient: we convert to a string, and then back to bytearray...


Modified: pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py	Thu Sep 30 11:03:42 2010
@@ -222,8 +222,90 @@
     raise OperationError(space.w_ValueError,
                          space.wrap("bytearray.index(x): x not in bytearray"))
 
+def str_join__Bytearray_ANY(space, w_self, w_list):
+    list_w = space.listview(w_list)
+    if not list_w:
+        return W_BytearrayObject([])
+    data = w_self.data
+    reslen = 0
+    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
+                space.is_true(space.isinstance(w_s, space.w_bytearray))):
+            raise operationerrfmt(
+                space.w_TypeError,
+                "sequence item %d: expected string, %s "
+                "found", i, space.type(w_s).getname(space, '?'))
+        reslen += len(space.str_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.str_w(list_w[i]))
+    return W_BytearrayObject(newdata)
+
 # These methods could just delegate to the string implementation,
 # but they have to return a bytearray.
+def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2, w_max):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "replace", w_str1, w_str2, w_max)
+    return String2Bytearray(space, w_res)
+
+def str_upper__Bytearray(space, w_bytearray):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "upper")
+    return String2Bytearray(space, w_res)
+
+def str_lower__Bytearray(space, w_bytearray):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "lower")
+    return String2Bytearray(space, w_res)
+
+def str_title__Bytearray(space, w_bytearray):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "title")
+    return String2Bytearray(space, w_res)
+
+def str_swapcase__Bytearray(space, w_bytearray):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "swapcase")
+    return String2Bytearray(space, w_res)
+
+def str_capitalize__Bytearray(space, w_bytearray):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "capitalize")
+    return String2Bytearray(space, w_res)
+
+def str_lstrip__Bytearray_ANY(space, w_bytearray, w_chars):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "lstrip", w_chars)
+    return String2Bytearray(space, w_res)
+
+def str_rstrip__Bytearray_ANY(space, w_bytearray, w_chars):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "rstrip", w_chars)
+    return String2Bytearray(space, w_res)
+
+def str_strip__Bytearray_ANY(space, w_bytearray, w_chars):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "strip", w_chars)
+    return String2Bytearray(space, w_res)
+
+def str_ljust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "ljust", w_width, w_fillchar)
+    return String2Bytearray(space, w_res)
+
+def str_rjust__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "rjust", w_width, w_fillchar)
+    return String2Bytearray(space, w_res)
+
+def str_center__Bytearray_ANY_ANY(space, w_bytearray, w_width, w_fillchar):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_res = space.call_method(w_str, "center", w_width, w_fillchar)
+    return String2Bytearray(space, w_res)
+
 def str_zfill__Bytearray_ANY(space, w_bytearray, w_width):
     w_str = delegate_Bytearray2String(space, w_bytearray)
     w_res = space.call_method(w_str, "zfill", w_width)
@@ -234,5 +316,39 @@
     w_res = space.call_method(w_str, "expandtabs", w_tabsize)
     return String2Bytearray(space, w_res)
 
+def str_split__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_list = space.call_method(w_str, "split", w_by, w_maxsplit)
+    list_w = space.listview(w_list)
+    for i in range(len(list_w)):
+        list_w[i] = String2Bytearray(space, list_w[i])
+    return w_list
+
+def str_rsplit__Bytearray_ANY_ANY(space, w_bytearray, w_by, w_maxsplit=-1):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_list = space.call_method(w_str, "rsplit", w_by, w_maxsplit)
+    list_w = space.listview(w_list)
+    for i in range(len(list_w)):
+        list_w[i] = String2Bytearray(space, list_w[i])
+    return w_list
+
+def str_partition__Bytearray_ANY(space, w_bytearray, w_sub):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_tuple = space.call_method(w_str, "partition", w_sub)
+    w_a, w_b, w_c = space.fixedview(w_tuple, 3)
+    return space.newtuple([
+        String2Bytearray(space, w_a),
+        String2Bytearray(space, w_b),
+        String2Bytearray(space, w_c)])
+
+def str_rpartition__Bytearray_ANY(space, w_bytearray, w_sub):
+    w_str = delegate_Bytearray2String(space, w_bytearray)
+    w_tuple = space.call_method(w_str, "rpartition", w_sub)
+    w_a, w_b, w_c = space.fixedview(w_tuple, 3)
+    return space.newtuple([
+        String2Bytearray(space, w_a),
+        String2Bytearray(space, w_b),
+        String2Bytearray(space, w_c)])
+
 from pypy.objspace.std import bytearraytype
 register_all(vars(), bytearraytype)

Modified: pypy/branch/fast-forward/pypy/objspace/std/bytearraytype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/bytearraytype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/bytearraytype.py	Thu Sep 30 11:03:42 2010
@@ -4,10 +4,15 @@
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.stdtypedef import StdTypeDef, SMM, no_hash_descr
 
-from pypy.objspace.std.stringtype import str_islower, str_isupper
-from pypy.objspace.std.stringtype import str_count, str_index
-from pypy.objspace.std.stringtype import str_expandtabs, str_zfill
-from pypy.objspace.std.stringtype import str_splitlines
+from pypy.objspace.std.stringtype import (
+    str_count, str_index, str_rindex, str_find, str_rfind, str_replace,
+    str_startswith, str_endswith, str_islower, str_isupper, str_isalpha,
+    str_isalnum, str_isdigit, str_isspace, str_istitle,
+    str_upper, str_lower, str_title, str_swapcase, str_capitalize,
+    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)
 
 @gateway.unwrap_spec(ObjSpace, W_Root, W_Root, W_Root, W_Root)
 def descr__new__(space, w_bytearraytype,

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py	Thu Sep 30 11:03:42 2010
@@ -60,18 +60,64 @@
     def test_stringlike_operations(self):
         assert bytearray('hello').islower()
         assert bytearray('HELLO').isupper()
+        assert bytearray('hello').isalpha()
+        assert not bytearray('hello2').isalpha()
+        assert bytearray('hello2').isalnum()
+        assert bytearray('1234').isdigit()
+        assert bytearray('   ').isspace()
+        assert bytearray('Abc').istitle()
 
         assert bytearray('hello').count('l') == 2
         assert bytearray('hello').count(bytearray('l')) == 2
         assert bytearray('hello').count(ord('l')) == 2
 
         assert bytearray('hello').index('e') == 1
-        assert bytearray('hello').count(bytearray('e')) == 1
+        assert bytearray('hello').rindex('l') == 3
+        assert bytearray('hello').index(bytearray('e')) == 1
         assert bytearray('hello').index(ord('e')) == 1
+        assert bytearray('hello').find('l') == 2
+        assert bytearray('hello').rfind('l') == 3
 
-        r = bytearray('1').zfill(5)
-        assert type(r) is bytearray and r == '00001'
-        r = bytearray('1\t2').expandtabs(5)
-        assert type(r) is bytearray and r == '1    2'
+        assert bytearray('hello').startswith('he')
+        assert bytearray('hello').startswith(bytearray('he'))
+        assert bytearray('hello').endswith('lo')
+        assert bytearray('hello').endswith(bytearray('lo'))
 
+    def test_stringlike_conversions(self):
+        # methods that should return bytearray (and not str)
+        def check(result, expected):
+            assert result == expected
+            assert type(result) is bytearray
 
+        check(bytearray('abc').replace('b', bytearray('d')), 'adc')
+
+        check(bytearray('abc').upper(), 'ABC')
+        check(bytearray('ABC').lower(), 'abc')
+        check(bytearray('abc').title(), 'Abc')
+        check(bytearray('AbC').swapcase(), 'aBc')
+        check(bytearray('abC').capitalize(), 'Abc')
+
+        check(bytearray('abc').ljust(5),  'abc  ')
+        check(bytearray('abc').rjust(5),  '  abc')
+        check(bytearray('abc').center(5), ' abc ')
+        check(bytearray('1').zfill(5), '00001')
+        check(bytearray('1\t2').expandtabs(5), '1    2')
+
+        check(bytearray(',').join(['a', bytearray('b')]), 'a,b')
+        check(bytearray('abc').lstrip('a'), 'bc')
+        check(bytearray('abc').rstrip('c'), 'ab')
+        check(bytearray('aba').strip('a'), 'b')
+
+    def test_split(self):
+        # methods that should return a sequence of bytearrays
+        def check(result, expected):
+            assert result == expected
+            assert set(type(x) for x in result) == set([bytearray])
+
+        b = bytearray('mississippi')
+        check(b.split('i'), [b'm', b'ss', b'ss', b'pp', b''])
+        check(b.rsplit('i'), [b'm', b'ss', b'ss', b'pp', b''])
+        check(b.rsplit('i', 2), [b'mississ', b'pp', b''])
+
+        check(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
+        check(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))



More information about the Pypy-commit mailing list