Commits

Armin Rigo committed 4533e42

str.count()

Comments (0)

Files changed (2)

pypy/objspace/std/bytesobject.py

             return self_as_uni.descr_rstrip(space, w_chars)
         return self._StringMethods_descr_rstrip(space, w_chars)
 
+    _StringMethods_descr_count = descr_count
+    def descr_count(self, space, w_sub, w_start=None, w_end=None):
+        if space.isinstance_w(w_sub, space.w_unicode):
+            self_as_uni = unicode_from_encoded_object(space, self, None, None)
+            return self_as_uni.descr_count(space, w_sub, w_start, w_end)
+        return self._StringMethods_descr_count(space, w_sub, w_start, w_end)
+
     def _join_return_one(self, space, w_obj):
         return (space.is_w(space.type(w_obj), space.w_str) or
                 space.is_w(space.type(w_obj), space.w_unicode))

pypy/objspace/std/test/test_unicodeobject.py

         assert u"".count(u"") ==1
         assert u"Python".count(u"") ==7
         assert u"ab aaba".count(u"ab") ==2
-        assert 'aaa'.count('a') == 3
-        assert 'aaa'.count('b') == 0
-        assert 'aaa'.count('a', -1) == 1
-        assert 'aaa'.count('a', -10) == 3
-        assert 'aaa'.count('a', 0, -1) == 2
-        assert 'aaa'.count('a', 0, -10) == 0
-        assert 'ababa'.count('aba') == 1
+        assert u'aaa'.count(u'a') == 3
+        assert u'aaa'.count(u'b') == 0
+        assert u'aaa'.count(u'a', -1) == 1
+        assert u'aaa'.count(u'a', -10) == 3
+        assert u'aaa'.count(u'a', 0, -1) == 2
+        assert u'aaa'.count(u'a', 0, -10) == 0
+        assert u'ababa'.count(u'aba') == 1
+
+    def test_count_str_unicode(self):
+        assert 'aaa'.count(u'a') == 3
+        assert 'aaa'.count(u'b') == 0
+        assert 'aaa'.count(u'a', -1) == 1
+        assert 'aaa'.count(u'a', -10) == 3
+        assert 'aaa'.count(u'a', 0, -1) == 2
+        assert 'aaa'.count(u'a', 0, -10) == 0
+        assert 'ababa'.count(u'aba') == 1
+        raises(UnicodeDecodeError, '\x80'.count, u'')
 
     def test_swapcase(self):
         assert u'\xe4\xc4\xdf'.swapcase() == u'\xc4\xe4\xdf'