mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
allow any type with __getitem__ to be a mapping for the purposes of % (#15801)
This commit is contained in:
parent
7e2f197a2b
commit
da2c7ebd23
4 changed files with 15 additions and 4 deletions
|
@ -1130,6 +1130,10 @@ class MixinStrUnicodeUserStringTest:
|
||||||
|
|
||||||
class X(object): pass
|
class X(object): pass
|
||||||
self.checkraises(TypeError, 'abc', '__mod__', X())
|
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):
|
def test_floatformatting(self):
|
||||||
# float formatting
|
# float formatting
|
||||||
|
|
|
@ -6,6 +6,13 @@ What's New in Python 2.7.4?
|
||||||
|
|
||||||
*Release date: XXXX-XX-XX*
|
*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
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
@ -4257,8 +4257,8 @@ PyString_Format(PyObject *format, PyObject *args)
|
||||||
arglen = -1;
|
arglen = -1;
|
||||||
argidx = -2;
|
argidx = -2;
|
||||||
}
|
}
|
||||||
if (PyMapping_Check(args) && !PyTuple_Check(args) &&
|
if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
|
||||||
!PyObject_TypeCheck(args, &PyBaseString_Type))
|
!PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type))
|
||||||
dict = args;
|
dict = args;
|
||||||
while (--fmtcnt >= 0) {
|
while (--fmtcnt >= 0) {
|
||||||
if (*fmt != '%') {
|
if (*fmt != '%') {
|
||||||
|
|
|
@ -8287,8 +8287,8 @@ PyObject *PyUnicode_Format(PyObject *format,
|
||||||
arglen = -1;
|
arglen = -1;
|
||||||
argidx = -2;
|
argidx = -2;
|
||||||
}
|
}
|
||||||
if (PyMapping_Check(args) && !PyTuple_Check(args) &&
|
if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript &&
|
||||||
!PyObject_TypeCheck(args, &PyBaseString_Type))
|
!PyTuple_Check(args) && !PyObject_TypeCheck(args, &PyBaseString_Type))
|
||||||
dict = args;
|
dict = args;
|
||||||
|
|
||||||
while (--fmtcnt >= 0) {
|
while (--fmtcnt >= 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue