[pypy-svn] pypy bytearray: (lac, mfoord) fix bytearray stringlike operations that previously worked by automatic delegation

mfoord commits-noreply at bitbucket.org
Thu Jan 20 16:27:02 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r41045:53ac2c5c5aab
Date: 2011-01-20 16:27 +0100
http://bitbucket.org/pypy/pypy/changeset/53ac2c5c5aab/

Log:	(lac, mfoord) fix bytearray stringlike operations that previously
	worked by automatic delegation

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
@@ -14,9 +14,10 @@
     get_positive_index
 )
 from pypy.objspace.std.listtype import get_list_index
+from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std.stringobject import W_StringObject
+from pypy.objspace.std.tupleobject import W_TupleObject
 from pypy.objspace.std.unicodeobject import W_UnicodeObject
-from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway
 from pypy.interpreter.argument import Signature
@@ -274,15 +275,61 @@
             count += 1
     return space.wrap(count)
 
-def str_index__Bytearray_Int_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
-    char = w_char.intval
-    start, stop, length = _convert_idx_params(space, w_bytearray, w_start, w_stop)
-    for i in range(start, min(stop, length)):
-        c = w_bytearray.data[i]
-        if ord(c) == char:
-            return space.wrap(i)
-    raise OperationError(space.w_ValueError,
-                         space.wrap("bytearray.index(x): x not in bytearray"))
+def str_count__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
+    w_char = space.wrap(space.bufferstr_w(w_char))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_count__String_String_ANY_ANY(space, w_str, w_char,
+                                                         w_start, w_stop)
+
+def str_index__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
+    w_char = space.wrap(space.bufferstr_w(w_char))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_index__String_String_ANY_ANY(space, w_str, w_char,
+                                                         w_start, w_stop)
+
+def str_rindex__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
+    w_char = space.wrap(space.bufferstr_w(w_char))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_rindex__String_String_ANY_ANY(space, w_str, w_char,
+                                                         w_start, w_stop)
+
+def str_find__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
+    w_char = space.wrap(space.bufferstr_w(w_char))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_find__String_String_ANY_ANY(space, w_str, w_char,
+                                                         w_start, w_stop)
+
+def str_rfind__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_char, w_start, w_stop):
+    w_char = space.wrap(space.bufferstr_w(w_char))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_rfind__String_String_ANY_ANY(space, w_str, w_char,
+                                                         w_start, w_stop)
+
+def str_startswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_prefix, w_start, w_stop):
+    w_prefix = space.wrap(space.bufferstr_w(w_prefix))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_startswith__String_String_ANY_ANY(space, w_str, w_prefix,
+                                                              w_start, w_stop)
+
+def str_startswith__Bytearray_Tuple_ANY_ANY(space, w_bytearray, w_prefix, w_start, w_stop):
+    w_str = str__Bytearray(space, w_bytearray)
+    w_prefix = space.newtuple([space.wrap(space.bufferstr_w(w_entry)) for w_entry in
+                               space.unpackiterable(w_prefix)])
+    return stringobject.str_startswith__String_Tuple_ANY_ANY(space, w_str, w_prefix,
+                                                              w_start, w_stop)
+
+def str_endswith__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_suffix, w_start, w_stop):
+    w_suffix = space.wrap(space.bufferstr_w(w_suffix))
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_endswith__String_String_ANY_ANY(space, w_str, w_suffix,
+                                                              w_start, w_stop)
+
+def str_endswith__Bytearray_Tuple_ANY_ANY(space, w_bytearray, w_suffix, w_start, w_stop):
+    w_str = str__Bytearray(space, w_bytearray)
+    w_suffix = space.newtuple([space.wrap(space.bufferstr_w(w_entry)) for w_entry in
+                               space.unpackiterable(w_suffix)])
+    return stringobject.str_endswith__String_Tuple_ANY_ANY(space, w_str, w_suffix,
+                                                              w_start, w_stop)
 
 def str_join__Bytearray_ANY(space, w_self, w_list):
     list_w = space.listview(w_list)
@@ -310,6 +357,30 @@
     w_str = str__Bytearray(space, w_bytearray)
     return stringobject.str_islower__String(space, w_str)
 
+def str_isupper__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_isupper__String(space, w_str)
+
+def str_isalpha__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_isalpha__String(space, w_str)
+
+def str_isalnum__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_isalnum__String(space, w_str)
+
+def str_isdigit__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_isdigit__String(space, w_str)
+
+def str_istitle__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_istitle__String(space, w_str)
+
+def str_isspace__Bytearray(space, w_bytearray):
+    w_str = str__Bytearray(space, w_bytearray)
+    return stringobject.str_isspace__String(space, w_str)
+
 def bytearray_insert__Bytearray_Int_ANY(space, w_bytearray, w_idx, w_other):
     where = space.int_w(w_idx)
     length = len(w_bytearray.data)

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
@@ -136,19 +136,27 @@
 
         assert bytearray('hello').count('l') == 2
         assert bytearray('hello').count(bytearray('l')) == 2
+        assert bytearray('hello').count(memoryview('l')) == 2
         assert bytearray('hello').count(ord('l')) == 2
 
         assert bytearray('hello').index('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
 
+        # these checks used to not raise in pypy but they should
+        raises(TypeError, bytearray('hello').index, ord('e'))
+        raises(TypeError, bytearray('hello').rindex, ord('e'))
+        raises(TypeError, bytearray('hello').find, ord('e'))
+        raises(TypeError, bytearray('hello').rfind, ord('e'))
+
         assert bytearray('hello').startswith('he')
         assert bytearray('hello').startswith(bytearray('he'))
+        assert bytearray('hello').startswith(('lo', bytearray('he')))
         assert bytearray('hello').endswith('lo')
         assert bytearray('hello').endswith(bytearray('lo'))
+        assert bytearray('hello').endswith((bytearray('lo'), 'he'))
 
     def test_stringlike_conversions(self):
         # methods that should return bytearray (and not str)


More information about the Pypy-commit mailing list