Commits

Benjamin Peterson committed 391e3a7

allow any type with __getitem__ to be a mapping for the purposes of % (#15801)

Comments (0)

Files changed (4)

Lib/test/string_tests.py

 
         class X(object): pass
         self.checkraises(TypeError, 'abc', '__mod__', X())
+        class X(Exception):
+            def __getitem__(self, k):
+                return k
+        self.checkequal('melon apple', '%(melon)s %(apple)s', '__mod__', X())
 
     def test_floatformatting(self):
         # float formatting
 
 *Release date: XXXX-XX-XX*
 
+Core and Builtins
+-----------------
+
+- Issue #15801 (again): With string % formatting, relax the type check for a
+  mapping such that any type with a __getitem__ can be used on the right hand
+  side.
+
 Library
 -------
 

Objects/stringobject.c

         arglen = -1;
         argidx = -2;
     }
-    if (PyMapping_Check(args) && !PyTuple_Check(args) &&
-        !PyObject_TypeCheck(args, &PyBaseString_Type))
+    if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
+        !PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type))
         dict = args;
     while (--fmtcnt >= 0) {
         if (*fmt != '%') {

Objects/unicodeobject.c

         arglen = -1;
         argidx = -2;
     }
-    if (PyMapping_Check(args) && !PyTuple_Check(args) &&
-        !PyObject_TypeCheck(args, &PyBaseString_Type))
+    if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
+        !PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type))
         dict = args;
 
     while (--fmtcnt >= 0) {