changeset: 82938:391e3a7db1a3 branch: 2.7 parent: 82929:87d266988905 user: Benjamin Peterson date: Sat Mar 23 22:32:00 2013 -0500 files: Lib/test/string_tests.py Misc/NEWS Objects/stringobject.c Objects/unicodeobject.c description: allow any type with __getitem__ to be a mapping for the purposes of % (#15801) diff -r 87d266988905 -r 391e3a7db1a3 Lib/test/string_tests.py --- a/Lib/test/string_tests.py Sat Mar 23 16:35:45 2013 -0500 +++ b/Lib/test/string_tests.py Sat Mar 23 22:32:00 2013 -0500 @@ -1130,6 +1130,10 @@ 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 diff -r 87d266988905 -r 391e3a7db1a3 Misc/NEWS --- a/Misc/NEWS Sat Mar 23 16:35:45 2013 -0500 +++ b/Misc/NEWS Sat Mar 23 22:32:00 2013 -0500 @@ -6,6 +6,13 @@ *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 ------- diff -r 87d266988905 -r 391e3a7db1a3 Objects/stringobject.c --- a/Objects/stringobject.c Sat Mar 23 16:35:45 2013 -0500 +++ b/Objects/stringobject.c Sat Mar 23 22:32:00 2013 -0500 @@ -4257,8 +4257,8 @@ 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 != '%') { diff -r 87d266988905 -r 391e3a7db1a3 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Mar 23 16:35:45 2013 -0500 +++ b/Objects/unicodeobject.c Sat Mar 23 22:32:00 2013 -0500 @@ -8287,8 +8287,8 @@ 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) {

AltStyle によって変換されたページ (->オリジナル) /