aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'pypy/objspace/std/bytesobject.py')
-rw-r--r--pypy/objspace/std/bytesobject.py22
1 files changed, 20 insertions, 2 deletions
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
index 6315c5d6cf..2316f6e513 100644
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -690,15 +690,33 @@ class W_BytesObject(W_AbstractBytesObject):
self_as_unicode._utf8.find(w_sub._utf8) >= 0)
return self._StringMethods_descr_contains(space, w_sub)
- _StringMethods_descr_replace = descr_replace
@unwrap_spec(count=int)
def descr_replace(self, space, w_old, w_new, count=-1):
+ from rpython.rlib.rstring import replace
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)
return self_as_uni.descr_replace(space, w_old, w_new, count)
- return self._StringMethods_descr_replace(space, w_old, w_new, count)
+
+ # almost copy of StringMethods.descr_replace :-(
+ input = self._value
+
+ sub = self._op_val(space, w_old)
+ by = self._op_val(space, w_new)
+ # the following two lines are for being bug-to-bug compatible
+ # with CPython: see issue #2448
+ if count >= 0 and len(input) == 0:
+ return self._empty()
+ try:
+ res = replace(input, sub, by, count)
+ except OverflowError:
+ raise oefmt(space.w_OverflowError, "replace string is too long")
+ # difference: reuse self if no replacement was done
+ if type(self) is W_BytesObject and res is input:
+ return self
+
+ return self._new(res)
_StringMethods_descr_join = descr_join
def descr_join(self, space, w_list):