1. Pypy
  2. Untitled project
  3. pypy

Commits

Armin Rigo  committed 27305d2

str.find(), str.rfind(), str.index(), str.rindex()

  • Participants
  • Parent commits 4533e42
  • Branches default

Comments (0)

Files changed (2)

File pypy/objspace/std/bytesobject.py

View file
             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))

File pypy/objspace/std/test/test_unicodeobject.py

View file
     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