Skip to content

Commit f08b2be

Browse files
[3.6] bpo-30978: str.format_map() now passes key lookup exceptions through. (GH-2790) (#2992)
Previously any exception was replaced with a KeyError exception. (cherry picked from commit 5075416)
1 parent f142e85 commit f08b2be

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

Lib/test/test_re.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def test_match_getitem(self):
471471
m[(0,)]
472472
with self.assertRaisesRegex(IndexError, 'no such group'):
473473
m[(0, 1)]
474-
with self.assertRaisesRegex(KeyError, 'a2'):
474+
with self.assertRaisesRegex(IndexError, 'no such group'):
475475
'a1={a2}'.format_map(m)
476476

477477
m = pat.match('ac')

Lib/test/test_unicode.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,13 @@ def __format__(self, spec):
12791279
self.assertRaises(ValueError, '{}'.format_map, 'a')
12801280
self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1})
12811281

1282+
class BadMapping:
1283+
def __getitem__(self, key):
1284+
return 1/0
1285+
self.assertRaises(KeyError, '{a}'.format_map, {})
1286+
self.assertRaises(TypeError, '{a}'.format_map, [])
1287+
self.assertRaises(ZeroDivisionError, '{a}'.format_map, BadMapping())
1288+
12821289
def test_format_huge_precision(self):
12831290
format_string = ".{}f".format(sys.maxsize + 1)
12841291
with self.assertRaises(ValueError):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
str.format_map() now passes key lookup exceptions through.
2+
Previously any exception was replaced with a KeyError exception.

Objects/stringlib/unicode_format.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,22 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
412412
if (index == -1) {
413413
/* look up in kwargs */
414414
PyObject *key = SubString_new_object(&first);
415-
if (key == NULL)
415+
if (key == NULL) {
416416
goto error;
417-
418-
/* Use PyObject_GetItem instead of PyDict_GetItem because this
419-
code is no longer just used with kwargs. It might be passed
420-
a non-dict when called through format_map. */
421-
if ((kwargs == NULL) || (obj = PyObject_GetItem(kwargs, key)) == NULL) {
417+
}
418+
if (kwargs == NULL) {
422419
PyErr_SetObject(PyExc_KeyError, key);
423420
Py_DECREF(key);
424421
goto error;
425422
}
423+
/* Use PyObject_GetItem instead of PyDict_GetItem because this
424+
code is no longer just used with kwargs. It might be passed
425+
a non-dict when called through format_map. */
426+
obj = PyObject_GetItem(kwargs, key);
426427
Py_DECREF(key);
428+
if (obj == NULL) {
429+
goto error;
430+
}
427431
}
428432
else {
429433
/* If args is NULL, we have a format string with a positional field

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy