[Python-checkins] cpython: remove "fast-path" for (i)adding strings

benjamin.peterson python-checkins at python.org
Sat Oct 1 03:31:30 CEST 2011


http://hg.python.org/cpython/rev/6da962d77eb3
changeset:   72558:6da962d77eb3
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri Sep 30 21:31:21 2011 -0400
summary:
  remove "fast-path" for (i)adding strings

These were just an artifact of the old unicode concatenation hack and likely
just penalized other kinds of adding. Also, this fixes __(i)add__ on string
subclasses.

files:
  Lib/test/test_unicode.py |  12 ++++++++++++
  Python/ceval.c           |  10 ++--------
  2 files changed, 14 insertions(+), 8 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
@@ -1760,6 +1760,18 @@
         self.assertEqual(size, nchar)
         self.assertEqual(wchar, nonbmp + '\0')
 
+    def test_subclass_add(self):
+        class S(str):
+            def __add__(self, o):
+                return "3"
+        self.assertEqual(S("4") + S("5"), "3")
+        class S(str):
+            def __iadd__(self, o):
+                return "3"
+        s = S("1")
+        s += "4"
+        self.assertEqual(s, "3")
+
 
 class StringModuleTest(unittest.TestCase):
     def test_formatter_parser(self):
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1507,10 +1507,7 @@
         TARGET(BINARY_ADD)
             w = POP();
             v = TOP();
-            if (PyUnicode_Check(v) && PyUnicode_Check(w))
-                x = PyUnicode_Concat(v, w);
-            else
-                x = PyNumber_Add(v, w);
+            x = PyNumber_Add(v, w);
             Py_DECREF(v);
             Py_DECREF(w);
             SET_TOP(x);
@@ -1662,10 +1659,7 @@
         TARGET(INPLACE_ADD)
             w = POP();
             v = TOP();
-            if (PyUnicode_Check(v) && PyUnicode_Check(w))
-                x = PyUnicode_Concat(v, w);
-            else
-                x = PyNumber_InPlaceAdd(v, w);
+            x = PyNumber_InPlaceAdd(v, w);
             Py_DECREF(v);
             Py_DECREF(w);
             SET_TOP(x);

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


More information about the Python-checkins mailing list