From: Eric V. Smith Date: Mon, 18 Jul 2011 18:03:41 +0000 (-0400) Subject: Closes #12579. Positional fields with str.format_map() now raise a ValueError instead... X-Git-Tag: v3.2.2rc1~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12ebefc9d3b7268d17d4a042767f712c46d03dfe;p=thirdparty%2FPython%2Fcpython.git Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError. --- diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 885e740b7663..55aaba6d3b48 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -736,6 +736,11 @@ class UnicodeTest(string_tests.CommonTest, self.assertRaises(TypeError, '{a'.format_map) self.assertRaises(TypeError, '}a'.format_map) + # issue #12579: can't supply positional params to format_map + self.assertRaises(ValueError, '{}'.format_map, {'a' : 2}) + self.assertRaises(ValueError, '{}'.format_map, 'a') + self.assertRaises(ValueError, '{a} {}'.format_map, {"a" : 2, "b" : 1}) + def test_format_auto_numbering(self): class C: def __init__(self, x=100): diff --git a/Misc/ACKS b/Misc/ACKS index ee9b3734aa1e..ccbe152f6f9a 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -78,6 +78,7 @@ Eli Bendersky Andrew Bennetts Andy Bensky Michel Van den Bergh +Julian Berman Eric Beser Steven Bethard Stephen Bevan diff --git a/Misc/NEWS b/Misc/NEWS index bf3374583753..f444709ab50a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,10 @@ What's New in Python 3.2.2? Core and Builtins ----------------- +- Issue #12579: str.format_map() now raises a ValueError if used on a + format string that contains positional fields. Initial patch by + Julian Berman. + - Issue #11627: Fix segfault when __new__ on a exception returns a non-exception class. diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/string_format.h index 6f10727e2459..6c7adcb01ed8 100644 --- a/Objects/stringlib/string_format.h +++ b/Objects/stringlib/string_format.h @@ -511,6 +511,16 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, Py_DECREF(key); } else { + /* If args is NULL, we have a format string with a positional field + with only kwargs to retrieve it from. This can only happen when + used with format_map(), where positional arguments are not + allowed. */ + if (args == NULL) { + PyErr_SetString(PyExc_ValueError, "Format string contains " + "positional fields"); + goto error; + } + /* look up in args */ obj = PySequence_GetItem(args, index); if (obj == NULL)