]> granicus.if.org Git - python/commitdiff
str.replace(a, a) is now returning str unchanged if a is a
authorVictor Stinner <vstinner@wyplay.com>
Fri, 7 Oct 2011 08:01:28 +0000 (10:01 +0200)
committerVictor Stinner <vstinner@wyplay.com>
Fri, 7 Oct 2011 08:01:28 +0000 (10:01 +0200)
Lib/test/test_unicode.py
Objects/unicodeobject.c

index 870853e189426dda38f473d6f9cc9d1386b101ff..14d3fa6269a5d9b2141e96cb662a0cccb4d1ad4c 100644 (file)
@@ -275,6 +275,12 @@ class UnicodeTest(string_tests.CommonTest,
         self.checkequalnofix('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
         self.assertRaises(TypeError, 'replace'.replace, "r", 42)
 
+    @support.cpython_only
+    def test_replace_id(self):
+        a = 'a' # single ascii letters are singletons
+        text = 'abc'
+        self.assertIs(text.replace('a', 'a'), text)
+
     def test_bytes_comparison(self):
         with support.check_warnings():
             warnings.simplefilter('ignore', BytesWarning)
index caad3264bca23fad20a0e5eb6a4e1038516a8661..8608776b5ddad958e2b18600544992b2dab27f4a 100644 (file)
@@ -9604,6 +9604,8 @@ replace(PyObject *self, PyObject *str1,
     else if (maxcount == 0 || slen == 0)
         goto nothing;
 
+    if (str1 == str2)
+        goto nothing;
     if (skind < kind1)
         /* substring too wide to be present */
         goto nothing;