]> granicus.if.org Git - python/commitdiff
bpo-30978: str.format_map() now passes key lookup exceptions through. (#2790)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 3 Aug 2017 08:45:23 +0000 (11:45 +0300)
committerGitHub <noreply@github.com>
Thu, 3 Aug 2017 08:45:23 +0000 (11:45 +0300)
Previously any exception was replaced with a KeyError exception.

Lib/test/test_re.py
Lib/test/test_unicode.py
Misc/NEWS.d/next/Core and Builtins/2017-07-21-07-39-05.bpo-30978.f0jODc.rst [new file with mode: 0644]
Objects/stringlib/unicode_format.h

index 0ea5a20469646b181025b3a39d0967c01a9ae168..e9c07a04f570140e8e8b033b982d4cbb224ae82c 100644 (file)
@@ -468,7 +468,7 @@ class ReTests(unittest.TestCase):
             m[(0,)]
         with self.assertRaisesRegex(IndexError, 'no such group'):
             m[(0, 1)]
-        with self.assertRaisesRegex(KeyError, 'a2'):
+        with self.assertRaisesRegex(IndexError, 'no such group'):
             'a1={a2}'.format_map(m)
 
         m = pat.match('ac')
index 506f0715c2eb325116b50401e67e4c1a69f6a42a..341007b6507d525f305eb0e67a295271f3b4d882 100644 (file)
@@ -1278,6 +1278,13 @@ class UnicodeTest(string_tests.CommonTest,
         self.assertRaises(ValueError, '{}'.format_map, 'a')
         self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
 
+        class BadMapping:
+            def __getitem__(self, key):
+                return 1/0
+        self.assertRaises(KeyError, '{a}'.format_map, {})
+        self.assertRaises(TypeError, '{a}'.format_map, [])
+        self.assertRaises(ZeroDivisionError, '{a}'.format_map, BadMapping())
+
     def test_format_huge_precision(self):
         format_string = ".{}f".format(sys.maxsize + 1)
         with self.assertRaises(ValueError):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-07-21-07-39-05.bpo-30978.f0jODc.rst b/Misc/NEWS.d/next/Core and Builtins/2017-07-21-07-39-05.bpo-30978.f0jODc.rst
new file mode 100644 (file)
index 0000000..bb67a9f
--- /dev/null
@@ -0,0 +1,2 @@
+str.format_map() now passes key lookup exceptions through.
+Previously any exception was replaced with a KeyError exception.
index 7ac0d75166bdf80b7f56d64b312b3062cca37f7d..baaac811a56ed07dd29a1092d772929bb8775d25 100644 (file)
@@ -410,18 +410,22 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
     if (index == -1) {
         /* look up in kwargs */
         PyObject *key = SubString_new_object(&first);
-        if (key == NULL)
+        if (key == NULL) {
             goto error;
-
-        /* Use PyObject_GetItem instead of PyDict_GetItem because this
-           code is no longer just used with kwargs. It might be passed
-           a non-dict when called through format_map. */
-        if ((kwargs == NULL) || (obj = PyObject_GetItem(kwargs, key)) == NULL) {
+        }
+        if (kwargs == NULL) {
             PyErr_SetObject(PyExc_KeyError, key);
             Py_DECREF(key);
             goto error;
         }
+        /* Use PyObject_GetItem instead of PyDict_GetItem because this
+           code is no longer just used with kwargs. It might be passed
+           a non-dict when called through format_map. */
+        obj = PyObject_GetItem(kwargs, key);
         Py_DECREF(key);
+        if (obj == NULL) {
+            goto error;
+        }
     }
     else {
         /* If args is NULL, we have a format string with a positional field