[Python-checkins] r69811 - in python/trunk: Lib/test/test_opcodes.py Python/ceval.c

collin.winter python-checkins at python.org
Fri Feb 20 20:30:42 CET 2009


Author: collin.winter
Date: Fri Feb 20 20:30:41 2009
New Revision: 69811

Log:
Issue 5176: special-case string formatting in BINARY_MODULO implementation. This shows a modest (1-3%) speed-up in templating systems, for example.


Modified:
   python/trunk/Lib/test/test_opcodes.py
   python/trunk/Python/ceval.c

Modified: python/trunk/Lib/test/test_opcodes.py
==============================================================================
--- python/trunk/Lib/test/test_opcodes.py	(original)
+++ python/trunk/Lib/test/test_opcodes.py	Fri Feb 20 20:30:41 2009
@@ -102,6 +102,12 @@
         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)

Modified: python/trunk/Python/ceval.c
==============================================================================
--- python/trunk/Python/ceval.c	(original)
+++ python/trunk/Python/ceval.c	Fri Feb 20 20:30:41 2009
@@ -1283,7 +1283,10 @@
 		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);


More information about the Python-checkins mailing list