[Python-checkins] cpython: str.replace(a, a) is now returning str unchanged if a is a

victor.stinner python-checkins at python.org
Fri Oct 7 10:02:04 CEST 2011


http://hg.python.org/cpython/rev/9c1b76936b79
changeset:   72784:9c1b76936b79
user:        Victor Stinner <vstinner at wyplay.com>
date:        Fri Oct 07 10:01:28 2011 +0200
summary:
  str.replace(a, a) is now returning str unchanged if a is a

files:
  Lib/test/test_unicode.py |  6 ++++++
  Objects/unicodeobject.c  |  2 ++
  2 files changed, 8 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -275,6 +275,12 @@
         self.checkequalnofix('one at 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)
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -9604,6 +9604,8 @@
     else if (maxcount == 0 || slen == 0)
         goto nothing;
 
+    if (str1 == str2)
+        goto nothing;
     if (skind < kind1)
         /* substring too wide to be present */
         goto nothing;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list