summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-08-28 17:55:35 -0400
committerBenjamin Peterson <benjamin@python.org>2012-08-28 17:55:35 -0400
commit23d49d3e7e386bb2b26d5b944fc123f0f21ce0a6 (patch)
tree2505d06c40de0283e39a5628108f72bf0549b7ff
parent25cf30faf92b8b77c69a0fd4b839ceefbbaccc4f (diff)
downloadcpython-git-23d49d3e7e386bb2b26d5b944fc123f0f21ce0a6.tar.gz
use the stricter PyMapping_Check (closes #15801)
-rw-r--r--Lib/test/string_tests.py3
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/stringobject.c2
-rw-r--r--Objects/unicodeobject.c2
4 files changed, 8 insertions, 2 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 5931f3d88b..a161512634 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -1113,6 +1113,9 @@ class MixinStrUnicodeUserStringTest:
self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
self.checkraises(ValueError, '%10', '__mod__', (42,))
+ class X(object): pass
+ self.checkraises(TypeError, 'abc', '__mod__', X())
+
def test_floatformatting(self):
# float formatting
for prec in xrange(100):
diff --git a/Misc/NEWS b/Misc/NEWS
index 71a858eb53..54c260c660 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.4
Core and Builtins
-----------------
+- Issue #15801: Make sure mappings passed to '%' formatting are actually
+ subscriptable.
+
- Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
errors correctly. Patch by Serhiy Storchaka.
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index fd2f63001d..7c4a86b5e6 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -4254,7 +4254,7 @@ PyString_Format(PyObject *format, PyObject *args)
arglen = -1;
argidx = -2;
}
- if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&
+ if (PyMapping_Check(args) && !PyTuple_Check(args) &&
!PyObject_TypeCheck(args, &PyBaseString_Type))
dict = args;
while (--fmtcnt >= 0) {
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 59b138a21f..d40f2e46b2 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8275,7 +8275,7 @@ PyObject *PyUnicode_Format(PyObject *format,
arglen = -1;
argidx = -2;
}
- if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) &&
+ if (PyMapping_Check(args) && !PyTuple_Check(args) &&
!PyObject_TypeCheck(args, &PyBaseString_Type))
dict = args;