From 8725dce2ae77aeac72cdfa024a1d0aff897a6f56 Mon Sep 17 00:00:00 2001 From: Collin Winter Date: Fri, 20 Feb 2009 19:30:41 +0000 Subject: [PATCH] Issue 5176: special-case string formatting in BINARY_MODULO implementation. This shows a modest (1-3%) speed-up in templating systems, for example. --- Lib/test/test_opcodes.py | 6 ++++++ Python/ceval.c | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_opcodes.py b/Lib/test/test_opcodes.py index d7d1673a879..7985bf61a4d 100644 --- a/Lib/test/test_opcodes.py +++ b/Lib/test/test_opcodes.py @@ -102,6 +102,12 @@ class OpcodeTest(unittest.TestCase): g = eval('lambda a=1: None') self.assertNotEquals(f, g) + def test_modulo_of_string_subclasses(self): + class MyString(str): + def __mod__(self, value): + return 42 + self.assertEqual(MyString() % 3, 42) + def test_main(): run_unittest(OpcodeTest) diff --git a/Python/ceval.c b/Python/ceval.c index d457fd54894..419ecfaaaaa 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1283,7 +1283,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) case BINARY_MODULO: w = POP(); v = TOP(); - x = PyNumber_Remainder(v, w); + if (PyString_CheckExact(v)) + x = PyString_Format(v, w); + else + x = PyNumber_Remainder(v, w); Py_DECREF(v); Py_DECREF(w); SET_TOP(x);