Commit 12ebefc9 authored by Eric V. Smith's avatar Eric V. Smith

Closes #12579. Positional fields with str.format_map() now raise a ValueError...

Closes #12579. Positional fields with str.format_map() now raise a ValueError instead of SystemError.
parent b8990072
...@@ -736,6 +736,11 @@ class UnicodeTest(string_tests.CommonTest, ...@@ -736,6 +736,11 @@ class UnicodeTest(string_tests.CommonTest,
self.assertRaises(TypeError, '{a'.format_map) self.assertRaises(TypeError, '{a'.format_map)
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): def test_format_auto_numbering(self):
class C: class C:
def __init__(self, x=100): def __init__(self, x=100):
......
...@@ -78,6 +78,7 @@ Eli Bendersky ...@@ -78,6 +78,7 @@ Eli Bendersky
Andrew Bennetts Andrew Bennetts
Andy Bensky Andy Bensky
Michel Van den Bergh Michel Van den Bergh
Julian Berman
Eric Beser Eric Beser
Steven Bethard Steven Bethard
Stephen Bevan Stephen Bevan
......
...@@ -10,6 +10,10 @@ What's New in Python 3.2.2? ...@@ -10,6 +10,10 @@ What's New in Python 3.2.2?
Core and Builtins 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 - Issue #11627: Fix segfault when __new__ on a exception returns a non-exception
class. class.
......
...@@ -511,6 +511,16 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, ...@@ -511,6 +511,16 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
Py_DECREF(key); Py_DECREF(key);
} }
else { 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 */ /* look up in args */
obj = PySequence_GetItem(args, index); obj = PySequence_GetItem(args, index);
if (obj == NULL) if (obj == NULL)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment