Author: Manuel Jacob
Branch: refactor-str-types
Changeset: r65712:e4c68588a0de
Date: 2013-07-27 14:41 +0200
http://bitbucket.org/pypy/pypy/changeset/e4c68588a0de/

Log:    Add more auto-conversion fun.

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
@@ -13,7 +13,7 @@
     decode_object, unicode_from_encoded_object, _get_encoding_and_errors)
 from rpython.rlib.jit import we_are_jitted
 from rpython.rlib.objectmodel import compute_hash, compute_unique_id
-from rpython.rlib.rstring import StringBuilder
+from rpython.rlib.rstring import StringBuilder, replace
 
 
 class W_AbstractBytesObject(W_Object):
@@ -199,6 +199,27 @@
             return self_as_unicode._endswith(space, self_as_unicode._value, 
w_suffix, start, end)
         return StringMethods._endswith(self, space, value, w_suffix, start, 
end)
 
+    @unwrap_spec(count=int)
+    def descr_replace(self, space, w_old, w_new, count=-1):
+        old_is_unicode = space.isinstance_w(w_old, space.w_unicode)
+        new_is_unicode = space.isinstance_w(w_new, space.w_unicode)
+        if old_is_unicode or new_is_unicode:
+            self_as_uni = unicode_from_encoded_object(space, self, None, None)
+            if not old_is_unicode:
+                w_old = unicode_from_encoded_object(space, w_old, None, None)
+            if not new_is_unicode:
+                w_new = unicode_from_encoded_object(space, w_new, None, None)
+            input = self_as_uni._val(space)
+            sub = self_as_uni._op_val(space, w_old)
+            by = self_as_uni._op_val(space, w_new)
+            try:
+                res = replace(input, sub, by, count)
+            except OverflowError:
+                raise OperationError(space.w_OverflowError,
+                                     space.wrap("replace string is too long"))
+            return self_as_uni._new(res)
+        return StringMethods.descr_replace(self, space, w_old, w_new, count)
+
     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
@@ -900,3 +900,8 @@
         assert not u''.isnumeric()
         assert not u'a'.isnumeric()
         assert u'\u2460'.isnumeric() # CIRCLED DIGIT ONE
+
+    def test_replace_autoconvert(self):
+        res = 'one!two!three!'.replace(u'!', u'@', 1)
+        assert res == u'one@two!three!'
+        assert type(res) == unicode
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to