Author: Armin Rigo <[email protected]>
Branch:
Changeset: r72920:4533e4236ff1
Date: 2014-08-20 09:39 +0200
http://bitbucket.org/pypy/pypy/changeset/4533e4236ff1/
Log: str.count()
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
@@ -768,6 +768,13 @@
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))
diff --git a/pypy/objspace/std/test/test_unicodeobject.py
b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -680,13 +680,23 @@
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'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit