[pypy-svn] r37669 - in pypy/dist/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Jan 31 14:44:40 CET 2007


Author: cfbolz
Date: Wed Jan 31 14:44:37 2007
New Revision: 37669

Modified:
   pypy/dist/pypy/objspace/std/stringobject.py
   pypy/dist/pypy/objspace/std/strsliceobject.py
   pypy/dist/pypy/objspace/std/test/test_strsliceobject.py
Log:
some small fixes to string slice object: make str.split and str.strip produce
str slice objects. have count not force the string slice.


Modified: pypy/dist/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stringobject.py	(original)
+++ pypy/dist/pypy/objspace/std/stringobject.py	Wed Jan 31 14:44:37 2007
@@ -228,7 +228,7 @@
             maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
         # the word is value[i:j]
-        res_w.append(W_StringObject(value[i:j]))
+        res_w.append(sliced(space, value, i, j))
 
         # continue to look from the character following the space after the word
         i = j + 1
@@ -250,11 +250,11 @@
         next = value.find(by, start)
         if next < 0:
             break
-        res_w.append(W_StringObject(value[start:next]))
+        res_w.append(sliced(space, value, start, next))
         start = next + bylen
         maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
-    res_w.append(W_StringObject(value[start:]))
+    res_w.append(sliced(space, value, start, len(value)))
 
     return W_ListObject(res_w)
 
@@ -285,7 +285,7 @@
         # the word is value[j+1:i+1]
         j1 = j + 1
         assert j1 >= 0
-        res_w.append(W_StringObject(value[j1:i+1]))
+        res_w.append(sliced(space, value, j1, i+1))
 
         # continue to look from the character before the space before the word
         i = j - 1
@@ -307,11 +307,11 @@
         next = value.rfind(by, 0, end)
         if next < 0:
             break
-        res_w.append(W_StringObject(value[next+bylen:end]))
+        res_w.append(sliced(space, value, next+bylen, end))
         end = next
         maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
-    res_w.append(W_StringObject(value[:end]))
+    res_w.append(sliced(space, value, 0, end))
     res_w.reverse()
     return W_ListObject(res_w)
 
@@ -559,7 +559,7 @@
            rpos -= 1
        
     assert rpos >= lpos    # annotator hint, don't remove
-    return space.wrap(u_self[lpos:rpos])
+    return sliced(space, u_self, lpos, rpos)
 
 def _strip_none(space, w_self, left, right):
     "internal function called by str_xstrip methods"
@@ -578,7 +578,7 @@
            rpos -= 1
        
     assert rpos >= lpos    # annotator hint, don't remove
-    return space.wrap(u_self[lpos:rpos])
+    return sliced(space, u_self, lpos, rpos)
 
 def str_strip__String_String(space, w_self, w_chars):
     return _strip(space, w_self, w_chars, left=1, right=1)
@@ -703,7 +703,7 @@
     u_keepends  = space.int_w(w_keepends)  # truth value, but type checked
     selflen = len(data)
     
-    L = []
+    strs_w = []
     i = j = 0
     while i < selflen:
         # Find a line and append it
@@ -716,13 +716,13 @@
             i += 1
         if u_keepends:
             eol = i
-        L.append(W_StringObject(data[j:eol]))
+        strs_w.append(sliced(space, data, j, eol))
         j = i
 
     if j < selflen:
-        L.append(W_StringObject(data[j:]))
+        strs_w.append(sliced(space, data, j, len(data)))
 
-    return W_ListObject(L)
+    return space.newlist(strs_w)
 
 def str_zfill__String_ANY(space, w_self, w_width):
     input = w_self._value

Modified: pypy/dist/pypy/objspace/std/strsliceobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/strsliceobject.py	(original)
+++ pypy/dist/pypy/objspace/std/strsliceobject.py	Wed Jan 31 14:44:37 2007
@@ -3,6 +3,7 @@
 from pypy.objspace.std.unicodeobject import delegate_String2Unicode
 from pypy.objspace.std.sliceobject import W_SliceObject
 from pypy.objspace.std import slicetype
+from pypy.objspace.std.inttype import wrapint
 
 
 class W_StringSliceObject(W_Object):
@@ -66,6 +67,11 @@
     else:
         return space.wrap(res)
 
+def str_count__StringSlice_String_ANY_ANY(space, w_self, w_arg, w_start, w_end): 
+    (s, arg, start, end) =  _convert_idx_params(
+            space, w_self, w_arg, w_start, w_end)
+    return wrapint(space, s.count(arg, start, end))
+
 def str_rfind__StringSlice_String_ANY_ANY(space, w_self, w_sub, w_start, w_end):
 
     (self, sub, start, end) =  _convert_idx_params(space, w_self, w_sub, w_start, w_end)

Modified: pypy/dist/pypy/objspace/std/test/test_strsliceobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_strsliceobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_strsliceobject.py	Wed Jan 31 14:44:37 2007
@@ -2,23 +2,32 @@
 
 from pypy.objspace.std.test import test_stringobject
 from pypy.conftest import gettestobjspace
+from pypy.interpreter import gateway
+from pypy.objspace.std.strsliceobject import W_StringSliceObject
 
 class AppTestStringObject(test_stringobject.AppTestStringObject):
 
     def setup_class(cls):
         cls.space = gettestobjspace(**{"objspace.std.withstrslice": True})
+        def not_forced(space, w_s):
+            return space.wrap(isinstance(w_s, W_StringSliceObject) and
+                              (w_s.start != 0 or w_s.stop != len(w_s.str)))
+        cls.w_not_forced = cls.space.wrap(gateway.interp2app(not_forced))
 
     def test_basic(self):
         import pypymagic
         def slice(s): return (s*3)[len(s):-len(s)]
         s = slice('0123456789' * 20)
         assert len(s) == 200
+        assert self.not_forced(s)
         assert s[5] == '5'
         assert s[-2] == '8'
         assert s[3:7] == '3456'
         assert 'W_StringSliceObject' in pypymagic.pypy_repr(s)
         # when the slice is too short, don't use the slice string object
         assert 'W_StringObject' in pypymagic.pypy_repr("abcdefgh"[3:7])
+        s2 = s.upper()
+        assert not self.not_forced(s)
 
     def test_find(self):
         import pypymagic
@@ -100,3 +109,27 @@
         s = slice('a' * 101)
         assert 'W_StringSliceObject' in pypymagic.pypy_repr(s)
         assert hash(s) & 0x7fffffff == 0x7e0bce58
+
+    def test_split_produces_strslices(self):
+        import pypymagic
+        l = ("X" * 100 + "," + "Y" * 100).split(",")
+        assert "W_StringSliceObject" in pypymagic.pypy_repr(l[0])
+        assert "W_StringSliceObject" in pypymagic.pypy_repr(l[1])
+
+    def test_strip_produces_strslices(self):
+        import pypymagic
+        s = ("abc" + "X" * 100 + "," + "Y" * 100 + "abc").strip("abc")
+        assert "W_StringSliceObject" in pypymagic.pypy_repr(s)
+
+    def test_splitlines_produces_strslices(self):
+        import pypymagic
+        l = ("X" * 100 + "\n" + "Y" * 100).splitlines()
+        assert "W_StringSliceObject" in pypymagic.pypy_repr(l[0])
+        assert "W_StringSliceObject" in pypymagic.pypy_repr(l[1])
+
+    def test_count_does_not_force(self):
+        def slice(s): return (s*3)[len(s):-len(s)]
+        s = slice("X" * 100 + "\n" + "Y" * 100)
+        assert s.count("X") == 100
+        assert s.count("Y") == 100
+        assert self.not_forced(s)



More information about the Pypy-commit mailing list