[pypy-svn] r61355 - in pypy/trunk/pypy/objspace/std: . test

antocuni at codespeak.net antocuni at codespeak.net
Mon Jan 26 16:15:00 CET 2009


Author: antocuni
Date: Mon Jan 26 16:14:58 2009
New Revision: 61355

Modified:
   pypy/trunk/pypy/objspace/std/stringobject.py
   pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
   pypy/trunk/pypy/objspace/std/unicodeobject.py
Log:
start sharing at least some code between unicodeobject and stringobject. One more unicode.rsplit test passes



Modified: pypy/trunk/pypy/objspace/std/stringobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/stringobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/stringobject.py	Mon Jan 26 16:14:58 2009
@@ -317,27 +317,35 @@
     res_w.reverse()
     return space.newlist(res_w)
 
-def str_rsplit__String_String_ANY(space, w_self, w_by, w_maxsplit=-1):
-    maxsplit = space.int_w(w_maxsplit)
-    res_w = []
-    value = w_self._value
-    end = len(value)
-    by = w_by._value
-    bylen = len(by)
-    if bylen == 0:
-        raise OperationError(space.w_ValueError, space.wrap("empty separator"))
-
-    while maxsplit != 0:
-        next = value.rfind(by, 0, end)
-        if next < 0:
-            break
-        res_w.append(sliced(space, value, next+bylen, end, w_self))
-        end = next
-        maxsplit -= 1   # NB. if it's already < 0, it stays < 0
+def make_rsplit_with_delim(funcname, sliced):
+    from pypy.tool.sourcetools import func_with_new_name
 
-    res_w.append(sliced(space, value, 0, end, w_self))
-    res_w.reverse()
-    return space.newlist(res_w)
+    def fn(space, w_self, w_by, w_maxsplit=-1):
+        maxsplit = space.int_w(w_maxsplit)
+        res_w = []
+        value = w_self._value
+        end = len(value)
+        by = w_by._value
+        bylen = len(by)
+        if bylen == 0:
+            raise OperationError(space.w_ValueError, space.wrap("empty separator"))
+
+        while maxsplit != 0:
+            next = value.rfind(by, 0, end)
+            if next < 0:
+                break
+            res_w.append(sliced(space, value, next+bylen, end, w_self))
+            end = next
+            maxsplit -= 1   # NB. if it's already < 0, it stays < 0
+
+        res_w.append(sliced(space, value, 0, end, w_self))
+        res_w.reverse()
+        return space.newlist(res_w)
+    
+    return func_with_new_name(fn, funcname)
+
+str_rsplit__String_String_ANY = make_rsplit_with_delim('str_rsplit__String_String_ANY',
+                                                       sliced)
 
 def str_join__String_ANY(space, w_self, w_list):
     list_w = space.unpackiterable(w_list)

Modified: pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	Mon Jan 26 16:14:58 2009
@@ -149,6 +149,7 @@
         raises(ValueError, u'abc'.rsplit, '')
         raises(ValueError, 'abc'.rsplit, u'')
         assert u'  a b c  '.rsplit(None, 0) == [u'  a b c']
+        assert u''.rsplit('aaa') == [u'']
 
     def test_center(self):
         s=u"a b"

Modified: pypy/trunk/pypy/objspace/std/unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodeobject.py	Mon Jan 26 16:14:58 2009
@@ -1,6 +1,6 @@
 from pypy.objspace.std.objspace import *
 from pypy.interpreter import gateway
-from pypy.objspace.std.stringobject import W_StringObject
+from pypy.objspace.std.stringobject import W_StringObject, make_rsplit_with_delim
 from pypy.objspace.std.ropeobject import W_RopeObject
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
@@ -720,29 +720,15 @@
     res_w.reverse()
     return space.newlist(res_w)
 
-def unicode_rsplit__Unicode_Unicode_ANY(space, w_self, w_delim, w_maxsplit):
-    self = w_self._value
-    delim = w_delim._value
-    maxsplit = space.int_w(w_maxsplit)
-    delim_len = len(delim)
-    if delim_len == 0:
-        raise OperationError(space.w_ValueError,
-                             space.wrap('empty separator'))
-    parts = []
-    if len(self) == 0:
-        return space.newlist([])
-    start = 0
-    end = len(self)
-    while maxsplit != 0:
-        index = self.rfind(delim, 0, end)
-        if index < 0:
-            break
-        parts.append(W_UnicodeObject(self[index+delim_len:end]))
-        end = index
-        maxsplit -= 1
-    parts.append(W_UnicodeObject(self[:end]))
-    parts.reverse()
-    return space.newlist(parts)
+def sliced(space, s, start, stop, orig_obj):
+    assert start >= 0
+    assert stop >= 0 
+    if start == 0 and stop == len(s) and space.is_w(space.type(orig_obj), space.w_unicode):
+        return orig_obj
+    return space.wrap( s[start:stop])
+
+unicode_rsplit__Unicode_Unicode_ANY = make_rsplit_with_delim('unicode_rsplit__Unicode_Unicode_ANY',
+                                                             sliced)
 
 def _split_into_chars(self, maxsplit):
     if maxsplit == 0:



More information about the Pypy-commit mailing list