mirror of https://github.com/python/cpython
bpo-30978: str.format_map() now passes key lookup exceptions through. (#2790)
Previously any exception was replaced with a KeyError exception.
This commit is contained in:
parent
25e4f779d7
commit
5075416b8f
|
@ -468,7 +468,7 @@ class ReTests(unittest.TestCase):
|
||||||
m[(0,)]
|
m[(0,)]
|
||||||
with self.assertRaisesRegex(IndexError, 'no such group'):
|
with self.assertRaisesRegex(IndexError, 'no such group'):
|
||||||
m[(0, 1)]
|
m[(0, 1)]
|
||||||
with self.assertRaisesRegex(KeyError, 'a2'):
|
with self.assertRaisesRegex(IndexError, 'no such group'):
|
||||||
'a1={a2}'.format_map(m)
|
'a1={a2}'.format_map(m)
|
||||||
|
|
||||||
m = pat.match('ac')
|
m = pat.match('ac')
|
||||||
|
|
|
@ -1278,6 +1278,13 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertRaises(ValueError, '{}'.format_map, 'a')
|
self.assertRaises(ValueError, '{}'.format_map, 'a')
|
||||||
self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
|
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):
|
def test_format_huge_precision(self):
|
||||||
format_string = ".{}f".format(sys.maxsize + 1)
|
format_string = ".{}f".format(sys.maxsize + 1)
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
str.format_map() now passes key lookup exceptions through.
|
||||||
|
Previously any exception was replaced with a KeyError exception.
|
|
@ -410,18 +410,22 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
/* look up in kwargs */
|
/* look up in kwargs */
|
||||||
PyObject *key = SubString_new_object(&first);
|
PyObject *key = SubString_new_object(&first);
|
||||||
if (key == NULL)
|
if (key == NULL) {
|
||||||
goto error;
|
goto error;
|
||||||
|
}
|
||||||
/* Use PyObject_GetItem instead of PyDict_GetItem because this
|
if (kwargs == NULL) {
|
||||||
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) {
|
|
||||||
PyErr_SetObject(PyExc_KeyError, key);
|
PyErr_SetObject(PyExc_KeyError, key);
|
||||||
Py_DECREF(key);
|
Py_DECREF(key);
|
||||||
goto error;
|
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);
|
Py_DECREF(key);
|
||||||
|
if (obj == NULL) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* If args is NULL, we have a format string with a positional field
|
/* If args is NULL, we have a format string with a positional field
|
||||||
|
|
Loading…
Reference in New Issue