[pypy-commit] pypy py3.3: bytes.count() accepts an int for a character.

amauryfa noreply at buildbot.pypy.org
Sun Jan 4 19:14:03 CET 2015


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.3
Changeset: r75223:1db171bd1fdb
Date: 2015-01-01 22:42 +0100
http://bitbucket.org/pypy/pypy/changeset/1db171bd1fdb/

Log:	bytes.count() accepts an int for a character.

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
@@ -70,7 +70,7 @@
                 raise
         if not 0 <= char < 256:
             raise oefmt(space.w_ValueError,
-                        "byte must be in range(256)")
+                        "byte must be in range(0, 256)")
         return chr(char)
 
     def descr_len(self, space):
@@ -178,21 +178,12 @@
     def descr_count(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):
-            return space.newint(value.count(self._op_val(space, w_sub), start,
-                                            end))
-
-        from pypy.objspace.std.bytearrayobject import W_BytearrayObject
-        from pypy.objspace.std.bytesobject import W_BytesObject
-        if isinstance(w_sub, W_BytearrayObject):
-            res = count(value, w_sub.data, start, end)
-        elif isinstance(w_sub, W_BytesObject):
-            res = count(value, w_sub._value, start, end)
+            return space.newint(value.count(sub, start, end))
         else:
-            buffer = _get_buffer(space, w_sub)
-            res = count(value, buffer, start, end)
-
-        return space.wrap(max(res, 0))
+            res = count(value, sub, start, end)
+            return space.wrap(max(res, 0))
 
     def descr_decode(self, space, w_encoding=None, w_errors=None):
         from pypy.objspace.std.unicodeobject import (
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
@@ -169,6 +169,7 @@
         assert bytearray(b'hello').count(b'l') == 2
         assert bytearray(b'hello').count(bytearray(b'l')) == 2
         assert bytearray(b'hello').count(memoryview(b'l')) == 2
+        assert bytearray(b'hello').count(ord('l')) == 2
 
         assert bytearray(b'hello').index(b'e') == 1
         assert bytearray(b'hello').rindex(b'l') == 3
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
@@ -307,6 +307,7 @@
         assert b'aaa'.count(b'a', 0, -1) == 2
         assert b'aaa'.count(b'a', 0, -10) == 0
         assert b'ababa'.count(b'aba') == 1
+        assert b'ababa'.count(ord('a')) == 3
 
     def test_startswith(self):
         assert b'ab'.startswith(b'ab') is True


More information about the pypy-commit mailing list