Kaydet (Commit) 8725dce2 authored tarafından Collin Winter's avatar Collin Winter

Issue 5176: special-case string formatting in BINARY_MODULO implementation. This…

Issue 5176: special-case string formatting in BINARY_MODULO implementation. This shows a modest (1-3%) speed-up in templating systems, for example.
üst e9fb6863
...@@ -102,6 +102,12 @@ class OpcodeTest(unittest.TestCase): ...@@ -102,6 +102,12 @@ class OpcodeTest(unittest.TestCase):
g = eval('lambda a=1: None') g = eval('lambda a=1: None')
self.assertNotEquals(f, g) 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(): def test_main():
run_unittest(OpcodeTest) run_unittest(OpcodeTest)
......
...@@ -1283,7 +1283,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1283,7 +1283,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
case BINARY_MODULO: case BINARY_MODULO:
w = POP(); w = POP();
v = TOP(); 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(v);
Py_DECREF(w); Py_DECREF(w);
SET_TOP(x); SET_TOP(x);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment