]> granicus.if.org Git - python/commitdiff
remove "fast-path" for (i)adding strings
authorBenjamin Peterson <benjamin@python.org>
Sat, 1 Oct 2011 01:31:21 +0000 (21:31 -0400)
committerBenjamin Peterson <benjamin@python.org>
Sat, 1 Oct 2011 01:31:21 +0000 (21:31 -0400)
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.

Lib/test/test_unicode.py
Python/ceval.c

index b903fbe944cf24d1d47051d483cceb0169aae28d..a527dff9df2e64e65d6499b7d7be5417c2792146 100644 (file)
@@ -1760,6 +1760,18 @@ class UnicodeTest(string_tests.CommonTest,
         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):
index f6f422ee32f88de2fa0510097d90b708df9e5b15..870d19d55ca27c0ca0cb4d4c28f90858ad676024 100644 (file)
@@ -1507,10 +1507,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
         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 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
         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);