Author: Armin Rigo <[email protected]>
Branch:
Changeset: r72921:27305d2b0ff9
Date: 2014-08-20 09:43 +0200
http://bitbucket.org/pypy/pypy/changeset/27305d2b0ff9/
Log: str.find(), str.rfind(), str.index(), str.rindex()
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
@@ -775,6 +775,34 @@
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)
+ _StringMethods_descr_find = descr_find
+ def descr_find(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_find(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_find(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_rfind = descr_rfind
+ def descr_rfind(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_rfind(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_rfind(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_index = descr_index
+ def descr_index(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_index(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_index(space, w_sub, w_start, w_end)
+
+ _StringMethods_descr_rindex = descr_rindex
+ def descr_rindex(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_rindex(space, w_sub, w_start, w_end)
+ return self._StringMethods_descr_rindex(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
@@ -675,6 +675,16 @@
def test_rfind_corner_case(self):
assert u'abc'.rfind('', 4) == -1
+ def test_find_index_str_unicode(self):
+ assert 'abcdefghiabc'.find(u'bc') == 1
+ assert 'abcdefghiabc'.rfind(u'abc') == 9
+ raises(UnicodeDecodeError, '\x80'.find, u'')
+ raises(UnicodeDecodeError, '\x80'.rfind, u'')
+ assert 'abcdefghiabc'.index(u'bc') == 1
+ assert 'abcdefghiabc'.rindex(u'abc') == 9
+ raises(UnicodeDecodeError, '\x80'.index, u'')
+ raises(UnicodeDecodeError, '\x80'.rindex, u'')
+
def test_count(self):
assert u"".count(u"x") ==0
assert u"".count(u"") ==1
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit