Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.3
Changeset: r75199:5494a2d79290
Date: 2015-01-01 18:57 +0100
http://bitbucket.org/pypy/pypy/changeset/5494a2d79290/

Log:    Fix tests to pass with -A and cpython3.3. + Allow some bytes
        functions to search for integers instead of single chars.

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
@@ -66,7 +66,16 @@
         return False
 
     @staticmethod
-    def _op_val(space, w_other):
+    def _op_val(space, w_other, allow_char=False):
+        # Some functions (contains, find) allow a number to specify a
+        # single char.
+        if allow_char and space.isinstance_w(w_other, space.w_int):
+            return StringMethods._single_char(space, w_other)
+        try:
+            return space.bytes_w(w_other)
+        except OperationError, e:
+            if not e.match(space, space.w_TypeError):
+                raise
         return space.buffer_w(w_other, space.BUF_SIMPLE).as_str()
 
     def _chr(self, char):
@@ -394,14 +403,6 @@
         except ValueError:
             raise oefmt(space.w_ValueError, "value not found in bytearray")
 
-    _StringMethods_descr_contains = descr_contains
-    def descr_contains(self, space, w_sub):
-        if space.isinstance_w(w_sub, space.w_int):
-            char = space.int_w(w_sub)
-            return _descr_contains_bytearray(self.data, space, char)
-
-        return self._StringMethods_descr_contains(space, w_sub)
-
     def descr_add(self, space, w_other):
         if isinstance(w_other, W_BytearrayObject):
             return self._new(self.data + w_other.data)
@@ -432,15 +433,6 @@
 def _make_data(s):
     return [s[i] for i in range(len(s))]
 
-
-def _descr_contains_bytearray(data, space, char):
-    if not 0 <= char < 256:
-        raise oefmt(space.w_ValueError, "byte must be in range(0, 256)")
-    for c in data:
-        if ord(c) == char:
-            return space.w_True
-    return space.w_False
-
 # ____________________________________________________________
 
 
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -431,7 +431,11 @@
         return True
 
     @staticmethod
-    def _op_val(space, w_other):
+    def _op_val(space, w_other, allow_char=False):
+        # Some functions (contains, find) allow a number to specify a
+        # single char.
+        if allow_char and space.isinstance_w(w_other, space.w_int):
+            return StringMethods._single_char(space, w_other)
         try:
             return space.bytes_w(w_other)
         except OperationError, e:
@@ -588,11 +592,6 @@
 
     _StringMethods_descr_add = descr_add
     def descr_add(self, space, w_other):
-        if space.isinstance_w(w_other, space.w_bytearray):
-            # XXX: eliminate double-copy
-            from .bytearrayobject import W_BytearrayObject, _make_data
-            self_as_bytearray = W_BytearrayObject(_make_data(self._value))
-            return space.add(self_as_bytearray, w_other)
         if space.config.objspace.std.withstrbuf:
             from pypy.objspace.std.strbufobject import W_StringBufferObject
             try:
@@ -607,23 +606,6 @@
             return W_StringBufferObject(builder)
         return self._StringMethods_descr_add(space, w_other)
 
-    _StringMethods_descr_contains = descr_contains
-    def descr_contains(self, space, w_sub):
-        if space.isinstance_w(w_sub, space.w_int):
-            try:
-                char = space.int_w(w_sub)
-            except OperationError as e:
-                if e.match(space, space.w_OverflowError):
-                    char = 256 # arbitrary value which will trigger the 
ValueError
-                               # condition below
-                else:
-                    raise
-            if not 0 <= char < 256:
-                raise oefmt(space.w_ValueError,
-                            "character must be in range(256)")
-            return space.newbool(self._value.find(chr(char)) >= 0)
-        return self._StringMethods_descr_contains(space, w_sub)
-
     _StringMethods_descr_join = descr_join
     def descr_join(self, space, w_list):
         l = space.listview_bytes(w_list)
diff --git a/pypy/objspace/std/stringmethods.py 
b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -58,6 +58,21 @@
     def _multi_chr(self, c):
         return c
 
+    @staticmethod
+    def _single_char(space, w_sub):
+        try:
+            char = space.int_w(w_sub)
+        except OperationError as e:
+            if e.match(space, space.w_OverflowError):
+                char = 256 # arbitrary value which will trigger the ValueError
+                # condition below
+            else:
+                raise
+        if not 0 <= char < 256:
+            raise oefmt(space.w_ValueError,
+                        "byte must be in range(256)")
+        return chr(char)
+
     def descr_len(self, space):
         return space.wrap(self._len())
 
@@ -66,18 +81,11 @@
 
     def descr_contains(self, space, w_sub):
         value = self._val(space)
+        other = self._op_val(space, w_sub, allow_char=True)
         if self._use_rstr_ops(space, w_sub):
-            other = self._op_val(space, w_sub)
-            return space.newbool(value.find(other) >= 0)
-
-        from pypy.objspace.std.bytesobject import W_BytesObject
-        if isinstance(w_sub, W_BytesObject):
-            other = self._op_val(space, w_sub)
+            res = value.find(other)
+        else:
             res = find(value, other, 0, len(value))
-        else:
-            buffer = _get_buffer(space, w_sub)
-            res = find(value, buffer, 0, len(value))
-
         return space.newbool(res >= 0)
 
     def descr_add(self, space, w_other):
@@ -246,55 +254,31 @@
     def descr_find(self, space, w_sub, w_start=None, w_end=None):
         (value, start, end) = self._convert_idx_params(space, w_start, w_end)
 
+        sub = self._op_val(space, w_sub, allow_char=True)
         if self._use_rstr_ops(space, w_sub):
-            res = value.find(self._op_val(space, w_sub), start, end)
-            return space.wrap(res)
-
-        from pypy.objspace.std.bytearrayobject import W_BytearrayObject
-        from pypy.objspace.std.bytesobject import W_BytesObject
-        if isinstance(w_sub, W_BytearrayObject):
-            res = find(value, w_sub.data, start, end)
-        elif isinstance(w_sub, W_BytesObject):
-            res = find(value, w_sub._value, start, end)
+            res = value.find(sub, start, end)
         else:
-            buffer = _get_buffer(space, w_sub)
-            res = find(value, buffer, start, end)
-
+            res = find(value, sub, start, end)
         return space.wrap(res)
 
     def descr_rfind(self, space, w_sub, w_start=None, w_end=None):
         (value, start, end) = self._convert_idx_params(space, w_start, w_end)
 
+        sub = self._op_val(space, w_sub, allow_char=True)
         if self._use_rstr_ops(space, w_sub):
-            res = value.rfind(self._op_val(space, w_sub), start, end)
-            return space.wrap(res)
-
-        from pypy.objspace.std.bytearrayobject import W_BytearrayObject
-        from pypy.objspace.std.bytesobject import W_BytesObject
-        if isinstance(w_sub, W_BytearrayObject):
-            res = rfind(value, w_sub.data, start, end)
-        elif isinstance(w_sub, W_BytesObject):
-            res = rfind(value, w_sub._value, start, end)
+            res = value.rfind(sub, start, end)
         else:
-            buffer = _get_buffer(space, w_sub)
-            res = rfind(value, buffer, start, end)
-
+            res = rfind(value, sub, start, end)
         return space.wrap(res)
 
     def descr_index(self, space, w_sub, w_start=None, w_end=None):
         (value, start, end) = self._convert_idx_params(space, w_start, w_end)
 
-        from pypy.objspace.std.bytearrayobject import W_BytearrayObject
-        from pypy.objspace.std.bytesobject import W_BytesObject
+        sub = self._op_val(space, w_sub, allow_char=True)
         if self._use_rstr_ops(space, w_sub):
-            res = value.find(self._op_val(space, w_sub), start, end)
-        elif isinstance(w_sub, W_BytearrayObject):
-            res = find(value, w_sub.data, start, end)
-        elif isinstance(w_sub, W_BytesObject):
-            res = find(value, w_sub._value, start, end)
+            res = value.find(sub, start, end)
         else:
-            buffer = _get_buffer(space, w_sub)
-            res = find(value, buffer, start, end)
+            res = find(value, sub, start, end)
 
         if res < 0:
             raise oefmt(space.w_ValueError,
@@ -306,15 +290,11 @@
 
         from pypy.objspace.std.bytearrayobject import W_BytearrayObject
         from pypy.objspace.std.bytesobject import W_BytesObject
+        sub = self._op_val(space, w_sub, allow_char=True)
         if self._use_rstr_ops(space, w_sub):
-            res = value.rfind(self._op_val(space, w_sub), start, end)
-        elif isinstance(w_sub, W_BytearrayObject):
-            res = rfind(value, w_sub.data, start, end)
-        elif isinstance(w_sub, W_BytesObject):
-            res = rfind(value, w_sub._value, start, end)
+            res = value.rfind(sub, start, end)
         else:
-            buffer = _get_buffer(space, w_sub)
-            res = rfind(value, buffer, start, end)
+            res = rfind(value, sub, start, end)
 
         if res < 0:
             raise oefmt(space.w_ValueError,
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -177,12 +177,10 @@
         assert bytearray(b'hello').find(b'l', -2) == 3
         assert bytearray(b'hello').rfind(b'l') == 3
 
-
-        # these checks used to not raise in pypy but they should
-        raises(TypeError, bytearray(b'hello').index, ord('e'))
-        raises(TypeError, bytearray(b'hello').rindex, ord('e'))
-        raises(TypeError, bytearray(b'hello').find, ord('e'))
-        raises(TypeError, bytearray(b'hello').rfind, ord('e'))
+        assert bytearray(b'hello').index(ord('e')) == 1
+        assert bytearray(b'hello').rindex(ord('l')) == 3
+        assert bytearray(b'hello').find(ord('e')) == 1
+        assert bytearray(b'hello').rfind(ord('l')) == 3
 
         assert bytearray(b'hello').startswith(b'he')
         assert bytearray(b'hello').startswith(bytearray(b'he'))
@@ -356,7 +354,7 @@
         def check(a, b, expected):
             result = a + b
             assert result == expected
-            assert isinstance(result, bytearray)
+            assert isinstance(result, type(a))
 
         check(b1, b2, b"abcdef")
         check(b1, b"def", b"abcdef")
@@ -454,8 +452,8 @@
     def test_buffer(self):
         b = bytearray(b'abcdefghi')
         buf = memoryview(b)
-        assert buf[2] == b'c'
-        buf[3] = b'D'
+        assert buf[2] == ord('c')
+        buf[3] = ord('D')
         assert b == b'abcDefghi'
         buf[4:6] = b'EF'
         assert b == b'abcDEFghi'
diff --git a/pypy/objspace/std/test/test_bytesobject.py 
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -706,7 +706,7 @@
         x += b"llo"
         b = memoryview(x)
         assert len(b) == 5
-        assert b[-1] == b"o"
+        assert b[-1] == ord("o")
         assert b[:] == b"hello"
         assert b[1:0] == b""
         raises(TypeError, "b[3] = 'x'")
diff --git a/pypy/objspace/std/unicodeobject.py 
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -103,7 +103,7 @@
         return True
 
     @staticmethod
-    def _op_val(space, w_other):
+    def _op_val(space, w_other, allow_char=False):
         if isinstance(w_other, W_UnicodeObject):
             return w_other._value
         raise oefmt(space.w_TypeError,
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to