[pypy-svn] r48485 - in pypy/branch/unicode-objspace/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Nov 10 01:26:00 CET 2007


Author: cfbolz
Date: Sat Nov 10 01:25:59 2007
New Revision: 48485

Modified:
   pypy/branch/unicode-objspace/pypy/objspace/std/test/test_index.py
   pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py
Log:
move more stuff to interplevel. also fix indexing behaviour + tests


Modified: pypy/branch/unicode-objspace/pypy/objspace/std/test/test_index.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/test/test_index.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/test/test_index.py	Sat Nov 10 01:25:59 2007
@@ -223,15 +223,50 @@
         SeqTestCase.setup_method(self, method)
         self.w_seq = self.space.newtuple([self.space.wrap(x) for x in (0,10,20,30,40,50)])
 
-class AppTest_StringTestCase(SeqTestCase):
+class StringTestCase(object):
+    def test_startswith(self):
+        self.o.ind = 1
+        assert self.const('abc').startswith(self.const('b'), self.o)
+        self.o.ind = 2
+        assert not self.const('abc').startswith(self.const('abc'), 0, self.o)
+
+    def test_endswith(self):
+        self.o.ind = 1
+        assert self.const('abc').endswith(self.const('a'), 0, self.o)
+        self.o.ind = 2
+        assert not self.const('abc').endswith(self.const('abc'), 0, self.o)
+
+    def test_index(self):
+        self.o.ind = 3
+        assert self.const('abcabc').index(self.const('abc'), 0, self.o) == 0
+        assert self.const('abcabc').index(self.const('abc'), self.o) == 3
+        assert self.const('abcabc').rindex(self.const('abc'), 0, self.o) == 0
+        assert self.const('abcabc').rindex(self.const('abc'), self.o) == 3
+
+    def test_find(self):
+        self.o.ind = 3
+        assert self.const('abcabc').find(self.const('abc'), 0, self.o) == 0
+        assert self.const('abcabc').find(self.const('abc'), self.o) == 3
+        assert self.const('abcabc').rfind(self.const('abc'), 0, self.o) == 0
+        assert self.const('abcabc').rfind(self.const('abc'), self.o) == 3
+
+    def test_count(self):
+        self.o.ind = 3
+        assert self.const('abcabc').count(self.const('abc'), 0, self.o) == 1
+        assert self.const('abcabc').count(self.const('abc'), self.o) == 1
+
+
+class AppTest_StringTestCase(SeqTestCase, StringTestCase):
     def setup_method(self, method):
         SeqTestCase.setup_method(self, method)
         self.w_seq = self.space.wrap("this is a test")
+        self.w_const = self.space.w_str
     
-class AppTest_UnicodeTestCase(SeqTestCase):
+class AppTest_UnicodeTestCase(SeqTestCase, StringTestCase):
     def setup_method(self, method):
         SeqTestCase.setup_method(self, method)
         self.w_seq = self.space.wrap(u"this is a test")
+        self.w_const = self.space.w_unicode
 
 
 class AppTest_XRangeTestCase:

Modified: pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/branch/unicode-objspace/pypy/objspace/std/unicodeobject.py	Sat Nov 10 01:25:59 2007
@@ -4,6 +4,7 @@
 from pypy.objspace.std.ropeobject import W_RopeObject
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.sliceobject import W_SliceObject
+from pypy.objspace.std import slicetype
 from pypy.objspace.std.tupleobject import W_TupleObject
 from pypy.rlib.rarithmetic import intmask, ovfcheck
 from pypy.module.unicodedata import unicodedb_3_2_0 as unicodedb
@@ -395,11 +396,18 @@
         index = length
     return index
 
-def unicode_endswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
+def _convert_idx_params(space, w_self, w_start, w_end):
     self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    start = slicetype.adapt_bound(space, len(self), w_start)
+    end = slicetype.adapt_bound(space, len(self), w_end)
+
+    assert start >= 0
+    assert end >= 0
 
+    return (self, start, end)
+
+def unicode_endswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     substr_len = len(substr)
     
@@ -412,9 +420,7 @@
     return space.w_True
 
 def unicode_startswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
 
     substr = w_substr._value
     substr_len = len(substr)
@@ -427,6 +433,23 @@
             return space.w_False
     return space.w_True
 
+
+def unicode_startswith__Unicode_Tuple_ANY_ANY(space, w_unistr, w_prefixes,
+                                              w_start, w_end):
+    unistr, start, end = _convert_idx_params(space, w_unistr, w_start, w_end)
+    for prefix in space.unpacktuple(w_prefixes):
+        if unistr.startswith(prefix, start, end):
+            return True
+    return False
+
+def unicode_endswith__Unicode_Tuple_ANY_ANY(space, w_unistr, w_suffixes,
+                                            w_start, w_end):
+    unistr, start, end = _convert_idx_params(space, w_unistr, w_start, w_end)
+    for suffix in space.unpacktuple(w_suffixes):
+        if unistr.endswith(suffix, start, end):
+            return True
+    return False
+
 def _to_unichar_w(space, w_char):
     try:
         unistr = space.unicode_w(w_char)
@@ -525,23 +548,17 @@
     return space.newlist(lines)
 
 def unicode_find__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     return space.wrap(self.find(substr, start, end))
 
 def unicode_rfind__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     return space.wrap(self.rfind(substr, start, end))
 
 def unicode_index__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     index = self.find(substr, start, end)
     if index < 0:
@@ -550,9 +567,7 @@
     return space.wrap(index)
 
 def unicode_rindex__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     index = self.rfind(substr, start, end)
     if index < 0:
@@ -561,9 +576,7 @@
     return space.wrap(index)
 
 def unicode_count__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
-    self = w_self._value
-    start = _normalize_index(len(self), space.int_w(w_start))
-    end = _normalize_index(len(self), space.int_w(w_end))
+    self, start, end = _convert_idx_params(space, w_self, w_start, w_end)
     substr = w_substr._value
     return space.wrap(self.count(substr, start, end))
 
@@ -770,17 +783,6 @@
     return retval
 
 
-def unicode_startswith__Unicode_Tuple_ANY_ANY(unistr, prefixes, start, end):
-    for prefix in prefixes:
-        if unistr.startswith(prefix, start, end):
-            return True
-    return False
-
-def unicode_endswith__Unicode_Tuple_ANY_ANY(unistr, suffixes, start, end):
-    for suffix in suffixes:
-        if unistr.endswith(suffix, start, end):
-            return True
-    return False
 
 ''')
 
@@ -789,8 +791,6 @@
 unicode_expandtabs__Unicode_ANY = app.interphook('unicode_expandtabs__Unicode_ANY')
 unicode_translate__Unicode_ANY = app.interphook('unicode_translate__Unicode_ANY')
 unicode_encode__Unicode_ANY_ANY = app.interphook('unicode_encode__Unicode_ANY_ANY')
-unicode_startswith__Unicode_Tuple_ANY_ANY = app.interphook('unicode_startswith__Unicode_Tuple_ANY_ANY')
-unicode_endswith__Unicode_Tuple_ANY_ANY = app.interphook('unicode_endswith__Unicode_Tuple_ANY_ANY')
 
 def unicode_partition__Unicode_Unicode(space, w_unistr, w_unisub):
     unistr = w_unistr._value



More information about the Pypy-commit mailing list