]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-28029: Make "".replace("", s, n) returning s for any n != 0. (GH-16981)
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 30 Oct 2019 10:03:53 +0000 (12:03 +0200)
committerGitHub <noreply@github.com>
Wed, 30 Oct 2019 10:03:53 +0000 (12:03 +0200)
Doc/whatsnew/3.9.rst
Lib/test/string_tests.py
Misc/NEWS.d/next/Core and Builtins/2019-10-29-09-38-54.bpo-28029.AmRMEF.rst [new file with mode: 0644]
Objects/stringlib/transmogrify.h
Objects/unicodeobject.c

index bca222955668d5a1d26632e7430c0cdcdcc7e202..d41e70808d6e1c25b38685ccb80ab0881cf35696 100644 (file)
@@ -99,6 +99,11 @@ Other Language Changes
   ignored for empty strings.
   (Contributed by Victor Stinner in :issue:`37388`.)
 
+* ``"".replace("", s, n)`` now returns ``s`` instead of an empty string for
+  all non-zero ``n``.  It is now consistent with ``"".replace("", s)``.
+  There are similar changes for :class:`bytes` and :class:`bytearray` objects.
+  (Contributed by Serhiy Storchaka in :issue:`28029`.)
+
 
 New Modules
 ===========
index 38da941f7f50e19f4e2e33f83553a8a37d4d6f49..948e2a3c3c56b007f5c12e1b2102d5167253065d 100644 (file)
@@ -505,6 +505,7 @@ class BaseTest:
         EQ("", "", "replace", "A", "")
         EQ("", "", "replace", "A", "A")
         EQ("", "", "replace", "", "", 100)
+        EQ("A", "", "replace", "", "A", 100)
         EQ("", "", "replace", "", "", sys.maxsize)
 
         # interleave (from=="", 'to' gets inserted everywhere)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-10-29-09-38-54.bpo-28029.AmRMEF.rst b/Misc/NEWS.d/next/Core and Builtins/2019-10-29-09-38-54.bpo-28029.AmRMEF.rst
new file mode 100644 (file)
index 0000000..420e53a
--- /dev/null
@@ -0,0 +1,3 @@
+``"".replace("", s, n)`` now returns ``s`` instead of an empty string for
+all non-zero ``n``.  There are similar changes for :class:`bytes` and
+:class:`bytearray` objects.
index 9506019d5af2b8766b3f0cb052f7526651769319..e1165ea38e8256d3ef45a5d5c14355eb6cc68275 100644 (file)
@@ -680,9 +680,13 @@ stringlib_replace(PyObject *self,
                   const char *to_s, Py_ssize_t to_len,
                   Py_ssize_t maxcount)
 {
+    if (STRINGLIB_LEN(self) < from_len) {
+        /* nothing to do; return the original bytes */
+        return return_self(self);
+    }
     if (maxcount < 0) {
         maxcount = PY_SSIZE_T_MAX;
-    } else if (maxcount == 0 || STRINGLIB_LEN(self) == 0) {
+    } else if (maxcount == 0) {
         /* nothing to do; return the original bytes */
         return return_self(self);
     }
@@ -699,13 +703,6 @@ stringlib_replace(PyObject *self,
         return stringlib_replace_interleave(self, to_s, to_len, maxcount);
     }
 
-    /* Except for b"".replace(b"", b"A") == b"A" there is no way beyond this */
-    /* point for an empty self bytes to generate a non-empty bytes */
-    /* Special case so the remaining code always gets a non-empty bytes */
-    if (STRINGLIB_LEN(self) == 0) {
-        return return_self(self);
-    }
-
     if (to_len == 0) {
         /* delete all occurrences of 'from' bytes */
         if (from_len == 1) {
index 2d60627b19193bceb6949af895a58ad78f569f50..5ae0af8ea336a7d1cde6b9bb65b7e75bd3ee75fc 100644 (file)
@@ -10572,9 +10572,12 @@ replace(PyObject *self, PyObject *str1,
     int mayshrink;
     Py_UCS4 maxchar, maxchar_str1, maxchar_str2;
 
+    if (slen < len1)
+        goto nothing;
+
     if (maxcount < 0)
         maxcount = PY_SSIZE_T_MAX;
-    else if (maxcount == 0 || slen == 0)
+    else if (maxcount == 0)
         goto nothing;
 
     if (str1 == str2)